-
Notifications
You must be signed in to change notification settings - Fork 0
/
DayNightCycle.cs
203 lines (166 loc) · 4.06 KB
/
DayNightCycle.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class DayNightCycle : MonoBehaviour {
public enum DayPeriod {day, dawn, night, morning}
[Header("Time")]
public DayPeriod currentPeriod = DayPeriod.day;
private int _currentTime = 0;
public Material morning;
public Material sunny;
public Material cloudy;
public Material rainy;
public Material stormy;
public Material dawn;
public Material night;
public Light sunLight;
private Animator timeAnim;
public delegate void SetTimeD(float time);
public static event SetTimeD SetTimeE;
private static DayNightCycle instance;
public static DayNightCycle Instance {
get {
if(instance == null) {
instance = GameObject.FindObjectOfType<DayNightCycle>();
}
return instance;
}
}
#region 0.Basics
void Awake (){
_currentTime = StartGame.Instance.startTime;
timeAnim = GetComponent<Animator>();
}
void Start () {
// SetAnimBasedOnCurrentTime(_currentTime);
InvokeRepeating("IncrementTime", 1f, 60f);
if(SetTimeE != null)
SetTimeE(_currentTime);
}
void OnEnable(){
ClimateManager.OnTriggerClimate += UpdateClimateSkybox;
SceneManager.sceneLoaded += UpdateDayNight;
}
void OnDisable(){
ClimateManager.OnTriggerClimate -= UpdateClimateSkybox;
SceneManager.sceneLoaded -= UpdateDayNight;
}
#endregion
#region 1.Statics
public static Vector3 SunPosition{
get { return Instance.sunLight.transform.position; }
}
#endregion
#region 2. Publics
public int CurrentTime{
get{ return _currentTime; }
set{
_currentTime = value;
UIManager.UpdateTimeUI(_currentTime);
}
}
/// <summary>
/// Handle for animator
/// </summary>
public void RefreshSkybox(){
SetSkyboxFromTime(_currentTime);
}
public void SetSkybox(DayPeriod period){
currentPeriod = period;
switch(currentPeriod){
case DayPeriod.day:
RenderSettings.skybox = sunny;
break;
case DayPeriod.dawn:
RenderSettings.skybox = dawn;
break;
case DayPeriod.night:
RenderSettings.skybox = night;
break;
default:
RenderSettings.skybox = morning;
break;
}
}
public void SetSkybox(Climate.ClimateType climateType){
// if(currentPeriod == DayPeriod.day){
// switch(climateType){
// case Climate.ClimateType.cloudy:
// RenderSettings.skybox = cloudy;
// break;
// case Climate.ClimateType.rainy:
// RenderSettings.skybox = rainy;
// break;
// case Climate.ClimateType.storm:
// RenderSettings.skybox = stormy;
// break;
// case Climate.ClimateType.sunny:
// RenderSettings.skybox = sunny;
// break;
// case Climate.ClimateType.snowy:
// RenderSettings.skybox = cloudy;
// break;
// default:
// break;
// }
// }
}
#endregion
#region 3.Privates
void SetSunLight(Climate.ClimateType currentClimate){
sunLight.intensity = .3f;
if(GameManager.CurrentSceneName == "Montreal"){
if(currentPeriod != DayPeriod.night){
switch(currentClimate){
case Climate.ClimateType.sunny:
sunLight.intensity = 1f;
break;
default:
sunLight.intensity = .8f;
break;
}
} else {
sunLight.intensity = .6f;
}
}
}
void SetAnimBasedOnCurrentTime(int time){
Debug.Log(time / 24f);
timeAnim.Play("cycle", 0, time / 24f);
SetSkyboxFromTime(time);
}
void IncrementTime(){
if(_currentTime >= 23)
_currentTime = 0;
Globals.Instance.playerData.time = _currentTime++;
SetSunLight(ClimateManager.CurrenClimate);
RefreshSkybox();
UIManager.UpdateTimeUI(_currentTime);
if(SetTimeE != null)
SetTimeE(_currentTime);
}
void SetSkyboxFromTime(int time){
if(7 <= time && time < 9){
SetSkybox(DayPeriod.morning);
} else {
if(9 <= time && time < 16){
SetSkybox(DayPeriod.day);
} else {
if(16 <= time && time < 20){
SetSkybox(DayPeriod.dawn);
} else {
SetSkybox(DayPeriod.night);
}
}
}
}
void UpdateDayNight(Scene scene, LoadSceneMode loadMode){
SetAnimBasedOnCurrentTime(_currentTime);
RefreshSkybox();
}
void UpdateClimateSkybox(Climate climate){
SetSkybox(climate.climateType);
}
#endregion
}