summaryrefslogtreecommitdiff
path: root/GameCode/BackgroundHandler.cs
blob: 90823de84f56f61435bbfecb8be45a734dfe829b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Collections;
using UnityEngine;

public class BackgroundHandler : MonoBehaviour
{
	private Background[] backgrounds;

	private float untilSwitch;

	private void Start()
	{
		backgrounds = GetComponentsInChildren<Background>(includeInactive: true);
	}

	private void Update()
	{
		untilSwitch -= TimeHandler.deltaTime;
		if (untilSwitch < 0f)
		{
			SwitchBackground();
		}
	}

	private void SwitchBackground()
	{
		untilSwitch = Random.Range(30, 60);
		for (int i = 0; i < backgrounds.Length; i++)
		{
			backgrounds[i].ToggleBackground(on: false);
		}
		StartCoroutine(StartBackGroundSoon());
	}

	private IEnumerator StartBackGroundSoon()
	{
		yield return new WaitForSeconds(5f);
		backgrounds[Random.Range(0, backgrounds.Length)].ToggleBackground(on: true);
	}
}