summaryrefslogtreecommitdiff
path: root/GameCode/LightManager.cs
blob: e38ac0fc29056282862dea43ddb13c6eab63da64 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Collections;
using UnityEngine;

public class LightManager : MonoBehaviour
{
	[SerializeField]
	private Color stage2Color;

	[SerializeField]
	private Color stage3Color;

	[SerializeField]
	private Color stage4Color;

	[SerializeField]
	private Light light;

	private void Start()
	{
	}

	public void ChangeColor(int stage)
	{
		switch (stage)
		{
		case 2:
			StartCoroutine(ShiftLight(stage2Color, 180f));
			break;
		case 3:
			StartCoroutine(ShiftLight(stage3Color, 180f));
			break;
		case 4:
			StartCoroutine(ShiftLight(stage4Color, 180f));
			break;
		}
	}

	private IEnumerator ShiftLight(Color newColor, float rotationInDeg)
	{
		Color startColor = light.color;
		Vector3 startEuler = light.transform.eulerAngles;
		Vector3 newEuler = startEuler + new Vector3(0f, rotationInDeg, 0f);
		float timer = 0f;
		float count = 0f;
		while (timer < 1f)
		{
			light.color = Color.Lerp(startColor, newColor, timer);
			light.transform.eulerAngles = Vector3.Lerp(startEuler, newEuler, timer);
			count += Time.deltaTime;
			if (count > 60f)
			{
				Debug.LogError("Possible infinite loop");
				break;
			}
			timer += Time.deltaTime / 30f;
			yield return new WaitForEndOfFrame();
		}
		light.color = newColor;
		light.transform.eulerAngles = newEuler;
		yield return null;
	}
}