-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameTime.cs
122 lines (98 loc) · 3.74 KB
/
GameTime.cs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameTime : MonoBehaviour
{
//The sun rotation logic is largely taken from a tutorial : https://www.youtube.com/watch?v=zxIyZzieFqE
//The logic of turning off the sun light as well as activating/ deactivating street lights is mine.
public Transform[] sun;
public float dayCycleInMinutes = 1;
private const float second = 1;
private const float minute = 60 * second;
private const float hour = 60 * minute;
private const float day = 24 * hour;
private const float degreesPerSecond = 360 / day;
private float _degreeRotation;
private float _timeOfDay;
private float _tod;
GameObject mainController;
GameObject[] nightLights;
BuildCity _city = null;
List<BuildCity.Pixel> cityPix;
private void Awake()
{
mainController = GameObject.Find("MainController");
cityPix = new List<BuildCity.Pixel>();
}
void Start()
{
_timeOfDay = 0.0f;
_tod = 0.0f;
_degreeRotation = degreesPerSecond * day / (dayCycleInMinutes * minute);
StartCoroutine(startNightLights());
}
void Update()
{
sun[0].Rotate(new Vector3(_degreeRotation, 0, 0) * Time.deltaTime);
_timeOfDay += Time.deltaTime;
if (sun[0].transform.rotation.eulerAngles.x >= 180.0f || sun[0].transform.rotation.eulerAngles.x <= 0.0f)
{
sun[0].GetComponent<LensFlare>().enabled = false;
sun[0].GetComponent<Light>().enabled = false;
if (nightLights != null)
LightsOn();
}
else
{
sun[0].GetComponent<LensFlare>().enabled = true;
sun[0].GetComponent<Light>().enabled = true;
if(nightLights != null)
LightsOff();
}
var remapped = Mathf.Cos(2 * Mathf.PI * _degreeRotation/360.0f * _timeOfDay);
RenderSettings.ambientLight = new Color(remapped, remapped, remapped);
}
public IEnumerator startNightLights() //takes care of turning lights on/off with the sun cycle
{
yield return new WaitForSeconds(1.0f);
_city = mainController.GetComponent<UIControl>().city;
cityPix.AddRange(_city.GetIntersectionPixels());
InstantiateLights(cityPix);
}
public void LightsOn() //turns lights on
{
foreach (var n in nightLights)
n.GetComponent<Light>().enabled = true;
}
public void LightsOff() //turns lights off
{
foreach (var n in nightLights)
n.GetComponent<Light>().enabled = false;
}
public GameObject[] InstantiateLights(List<BuildCity.Pixel> streets) //Instantiates lights over street and intersection 'pixels'
{
var intCount = streets.Count;
nightLights = new GameObject[intCount];
for (int i = 0; i < nightLights.Length; i++)
{
nightLights[i] = new GameObject();
nightLights[i].name = "nightLight";
var lightComp = nightLights[i].AddComponent<Light>();
lightComp.type = LightType.Spot;
lightComp.spotAngle = 52;
lightComp.intensity = 3.5f;
lightComp.range = 16.0f;
lightComp.color = Color.white;
lightComp.enabled = false;
var pos = streets[i].WorldPosition;
pos += new Vector3(0, 15, 0);
nightLights[i].transform.position = pos;
nightLights[i].transform.rotation = Quaternion.Euler(90.0f, 0.0f, 0.0f);
}
return nightLights;
}
public float getTime()
{
return _timeOfDay;
}
}