-
Notifications
You must be signed in to change notification settings - Fork 404
Expand file tree
/
Copy pathWeatherManager.cs
More file actions
578 lines (483 loc) · 20.5 KB
/
Copy pathWeatherManager.cs
File metadata and controls
578 lines (483 loc) · 20.5 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2023 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Gavin Clayton (interkarma@dfworkshop.net)
// Contributors: Michael Rauter (Nystul)
//
// Notes:
//
using DaggerfallWorkshop.Game.Serialization;
using DaggerfallWorkshop.Game.Weather;
using DaggerfallWorkshop.Utility;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using DaggerfallConnect;
using DaggerfallConnect.Arena2;
namespace DaggerfallWorkshop.Game
{
/// <summary>
/// Helper component to assign weather settings across multiple components.
///
/// The logic for weather changes are based on the climate and weather table described in the Daggerfall Chronicles,
/// pg. 47. Plus some additional logic to handle more natural weather transitions.
///
/// TODO add smooth weather transitions
/// </summary>
public class WeatherManager : MonoBehaviour
{
public PlayerWeather PlayerWeather;
public DaggerfallSky DaggerfallSky;
public SunlightManager SunlightManager;
public AmbientEffectsPlayer WeatherEffects;
[Range(0, 1)]
public float OvercastSunlightScale = 0.65f;
[Range(0, 1)]
public float RainSunlightScale = 0.45f;
[Range(0, 1)]
public float StormSunlightScale = 0.25f;
[Range(0, 1)]
public float SnowSunlightScale = 0.45f;
[Range(0, 1)]
public float WinterSunlightScale = 0.65f;
[Range(0, 1)]
public float OvercastShadowStrength = 0.6f;
[Range(0, 1)]
public float RainShadowStrength = 0.4f;
[Range(0, 1)]
public float StormShadowStrength = 0.4f;
[Range(0, 1)]
public float SnowShadowStrength = 0.25f;
[Range(0, 1)]
public float WinterShadowStrength = 0.8f;
[System.Serializable]
public struct FogSettings
{
public FogMode fogMode;
[Range(0, 1)]
public float density;
public float startDistance;
public float endDistance;
public bool excludeSkybox;
}
public FogSettings SunnyFogSettings = new FogSettings { fogMode = FogMode.Linear, density = 0.0f, startDistance = 0, endDistance = 2400, excludeSkybox = true };
public FogSettings OvercastFogSettings = new FogSettings { fogMode = FogMode.Linear, density = 0.0f, startDistance = 0, endDistance = 2400, excludeSkybox = true };
public FogSettings RainyFogSettings = new FogSettings { fogMode = FogMode.Exponential, density = 0.003f, startDistance = 0, endDistance = 0, excludeSkybox = true };
public FogSettings SnowyFogSettings = new FogSettings { fogMode = FogMode.Exponential, density = 0.005f, startDistance = 0, endDistance = 0, excludeSkybox = true };
public FogSettings HeavyFogSettings = new FogSettings { fogMode = FogMode.Exponential, density = 0.05f, startDistance = 0, endDistance = 0, excludeSkybox = false };
public FogSettings InteriorFogSettings = new FogSettings { fogMode = FogMode.Exponential, density = 0.001f, startDistance = 0, endDistance = 0, excludeSkybox = false };
public FogSettings DungeonFogSettings = new FogSettings { fogMode = FogMode.Exponential, density = 0.005f, startDistance = 0, endDistance = 0, excludeSkybox = false };
public FogSettings currentOutdoorFogSettings;
Color previousOutdoorFogColor;
// this is needed so weather from savegame load is not overwritten by code in StreamingWorld_OnInitWorld()
// e.g. weather fog, going to interior, saving, restarting, loading save, going outdoors -> fog should still be present (without this workaround it is not)
bool startedFromLoadedSaveGame; // needed to correctly restore fog after loading a savegame since StreamingWorld_OnInitWorld() happens after load event overwritting the values set there
DaggerfallUnity _dfUnity;
float _pollTimer;
private WeatherTable _weatherTable;
private float _pollWeatherInSeconds = 30f;
DFLocation.ClimateBaseType lastRespawnClimate = DFLocation.ClimateBaseType.None;
// used to set post processing fog settings (excludeSkybox setting)
private PostProcessLayer postProcessLayer;
public bool IsRaining { get; private set; }
public bool IsStorming { get; private set; }
public bool IsSnowing { get; private set; }
public bool IsOvercast { get; private set; }
bool updateWeatherFromClimateArray = false;
public bool UpdateWeatherFromClimateArray { get { return updateWeatherFromClimateArray; } set { updateWeatherFromClimateArray = value; } }
void Awake()
{
StreamingWorld.OnInitWorld += StreamingWorld_OnInitWorld;
SaveLoadManager.OnLoad += SaveLoadManager_OnLoad;
PlayerEnterExit.OnRespawnerComplete += PlayerEnterExit_OnRespawnerComplete;
PlayerEnterExit.OnTransitionInterior += OnTransitionToInterior;
PlayerEnterExit.OnTransitionExterior += OnTransitionToExterior;
PlayerEnterExit.OnTransitionDungeonInterior += OnTransitionToDungeon;
PlayerEnterExit.OnTransitionDungeonExterior += OnTransitionToExterior;
}
void OnDestroy()
{
StreamingWorld.OnInitWorld -= StreamingWorld_OnInitWorld;
SaveLoadManager.OnLoad -= SaveLoadManager_OnLoad;
PlayerEnterExit.OnTransitionInterior -= OnTransitionToInterior;
PlayerEnterExit.OnTransitionExterior -= OnTransitionToExterior;
PlayerEnterExit.OnTransitionDungeonInterior -= OnTransitionToDungeon;
PlayerEnterExit.OnTransitionDungeonExterior -= OnTransitionToExterior;
}
void Start()
{
_dfUnity = DaggerfallUnity.Instance;
_weatherTable = WeatherTable.ParseJsonTable();
postProcessLayer = Camera.main.GetComponent<PostProcessLayer>();
if (postProcessLayer != null)
{
postProcessLayer.fog.excludeSkybox = true;
}
if (DaggerfallUnity.Settings.AssetInjection)
AddWindZone();
// initialize with clear overcast sky (so that initial fog settings like exponential fog mode are set)
ClearOvercast();
}
void Update()
{
// Do nothing if player inside
if (GameManager.Instance.PlayerEnterExit.IsPlayerInside)
return;
SetAmbientEffects();
//PollWeatherChanges();
SetSunlightScale();
UpdateFromClimateArrayCheck();
}
public void ClearOvercast()
{
if (DaggerfallSky)
DaggerfallSky.WeatherStyle = WeatherStyle.Normal;
IsOvercast = false;
SetFog(SunnyFogSettings);
}
public void ClearAllWeather()
{
StopRaining();
StopSnowing();
ClearOvercast();
}
#region Fog
public void SetFog(FogSettings fogSettings, bool isInteriorOrDungeonFog = false)
{
// if fog is a outdoor weather fog set currentOutdoorFogSettings so fog settings can be restored when player goes into an interior/dungeon and then back to exterior
if (isInteriorOrDungeonFog == false)
{
currentOutdoorFogSettings = fogSettings;
}
else // if in interior or dungeon
{
// set fog color to black
RenderSettings.fogColor = Color.black;
}
// set fog mode first
RenderSettings.fogMode = fogSettings.fogMode;
RenderSettings.fogDensity = fogSettings.density;
RenderSettings.fogStartDistance = fogSettings.startDistance;
RenderSettings.fogEndDistance = fogSettings.endDistance;
// edit by Nystul: don't disable RenderSettings fog! Rendering Fog != Weather Fog
//RenderSettings.fog = isFoggy;
if (fogSettings.excludeSkybox == false)
{
// RenderSettings.fogColor = Color.gray;
if (postProcessLayer != null)
{
postProcessLayer.fog.excludeSkybox = false;
}
}
else
{
// TODO set this based on ... something.
// ex. time of day/angle of sun so we can get nice sunset/rise atmospheric effects
// also blend with climate so climates end up having faint 'tints' to them
// RenderSettings.fogColor = Color.clear;
if (postProcessLayer != null)
{
postProcessLayer.fog.excludeSkybox = true;
}
}
}
#endregion
#region Rain
public void SetOvercast()
{
SetFog(OvercastFogSettings);
IsOvercast = true;
}
public void SetRainOvercast()
{
if (DaggerfallSky)
{
if (Random.Range(0f, 1f) > 0.5f)
DaggerfallSky.WeatherStyle = WeatherStyle.Rain1;
else
DaggerfallSky.WeatherStyle = WeatherStyle.Rain2;
}
SetFog(RainyFogSettings);
IsOvercast = true;
}
public void StartRaining()
{
SetRainOvercast();
IsRaining = true;
}
public void StartStorming()
{
StartRaining();
IsStorming = true;
}
public void StopRaining()
{
IsRaining = false;
IsStorming = false;
}
public static bool IsSnowFreeClimate(int climateIndex)
{
var climate = (MapsFile.Climates)climateIndex;
switch (climate)
{
case MapsFile.Climates.Desert:
case MapsFile.Climates.Desert2:
case MapsFile.Climates.Rainforest:
case MapsFile.Climates.Subtropical:
return true;
default:
return false;
}
}
#endregion
#region Snow
public void SetSnowOvercast()
{
if (DaggerfallSky)
{
if (Random.Range(0f, 1f) > 0.5f)
DaggerfallSky.WeatherStyle = WeatherStyle.Snow1;
else
DaggerfallSky.WeatherStyle = WeatherStyle.Snow2;
}
SetFog(SnowyFogSettings);
IsOvercast = true;
}
public void StartSnowing()
{
SetSnowOvercast();
IsSnowing = true;
}
public void StopSnowing()
{
IsSnowing = false;
}
#endregion
#region Private Methods
void SetSunlightScale()
{
// Start with default scale
float scale = SunlightManager.defaultScaleFactor;
float shadowStrength = SunlightManager.defaultShadowStrength;
// Apply winter
if (_dfUnity.WorldTime.Now.SeasonValue == DaggerfallDateTime.Seasons.Winter)
{
scale = WinterSunlightScale;
shadowStrength = WinterShadowStrength;
}
// Apply rain, storm, snow light scale
if (IsRaining && !IsStorming)
{
scale = RainSunlightScale;
shadowStrength = RainShadowStrength;
}
else if (IsRaining && IsStorming)
{
scale = StormSunlightScale;
shadowStrength = StormShadowStrength;
}
else if (IsSnowing)
{
scale = SnowSunlightScale;
shadowStrength = SnowShadowStrength;
}
else if (IsOvercast)
{
scale = OvercastSunlightScale;
shadowStrength = OvercastShadowStrength;
}
shadowStrength *= Mathf.Exp(-50f * currentOutdoorFogSettings.density);
// Apply scale to sunlight manager
if (SunlightManager)
{
SunlightManager.ScaleFactor = scale;
SunlightManager.ShadowStrength = shadowStrength;
}
}
void SetAmbientEffects()
{
if (!WeatherEffects)
return;
// Set presets based on weather type
if (IsRaining && !IsStorming)
{
WeatherEffects.Presets = AmbientEffectsPlayer.AmbientSoundPresets.Rain;
return;
}
else if (IsRaining && IsStorming)
{
WeatherEffects.Presets = AmbientEffectsPlayer.AmbientSoundPresets.Storm;
return;
}
// Set presets based on time of day
if (DaggerfallUnity.Instance.WorldTime.DaggerfallDateTime.IsDay)
{
WeatherEffects.Presets = AmbientEffectsPlayer.AmbientSoundPresets.SunnyDay;
return;
}
else if (DaggerfallUnity.Instance.WorldTime.DaggerfallDateTime.IsNight)
{
WeatherEffects.Presets = AmbientEffectsPlayer.AmbientSoundPresets.ClearNight;
return;
}
WeatherEffects.Presets = AmbientEffectsPlayer.AmbientSoundPresets.None;
}
void PollWeatherChanges(bool forceUpdate = false)
{
// Increment poll timer
_pollTimer += Time.deltaTime;
if (_pollTimer < _pollWeatherInSeconds && !forceUpdate)
return;
// Reset timer
_pollTimer = 0;
var climate = PlayerWeather.PlayerGps.CurrentClimateIndex;
var next = _weatherTable.GetWeather(climate, _dfUnity.WorldTime.Now.SeasonValue);
if (next == PlayerWeather.WeatherType)
return;
// TODO smooth weather transitions. eg: storming to sunny is a jarring transition
SetWeather(next);
}
void UpdateFromClimateArrayCheck()
{
if (updateWeatherFromClimateArray)
{
if (PlayerWeather.PlayerGps.ReadyCheck())
{
SetWeatherFromWeatherClimateArray();
updateWeatherFromClimateArray = false;
}
}
}
// Sets weathers for each of the climate zones, like classic
public void SetClimateWeathers()
{
PlayerWeather.ClimateWeathers[0] = (byte)_weatherTable.GetWeather((int)MapsFile.Climates.Desert, _dfUnity.WorldTime.Now.SeasonValue);
PlayerWeather.ClimateWeathers[1] = (byte)_weatherTable.GetWeather((int)MapsFile.Climates.Mountain, _dfUnity.WorldTime.Now.SeasonValue);
PlayerWeather.ClimateWeathers[2] = (byte)_weatherTable.GetWeather((int)MapsFile.Climates.Rainforest, _dfUnity.WorldTime.Now.SeasonValue);
PlayerWeather.ClimateWeathers[3] = (byte)_weatherTable.GetWeather((int)MapsFile.Climates.Swamp, _dfUnity.WorldTime.Now.SeasonValue);
PlayerWeather.ClimateWeathers[4] = (byte)_weatherTable.GetWeather((int)MapsFile.Climates.Subtropical, _dfUnity.WorldTime.Now.SeasonValue);
PlayerWeather.ClimateWeathers[5] = (byte)_weatherTable.GetWeather((int)MapsFile.Climates.Woodlands, _dfUnity.WorldTime.Now.SeasonValue);
}
public void SetWeatherFromWeatherClimateArray()
{
int climate = PlayerWeather.PlayerGps.CurrentClimateIndex;
int index = Utility.TravelTimeCalculator.climateIndices[climate - (int)MapsFile.Climates.Ocean];
WeatherType weather = (WeatherType)PlayerWeather.ClimateWeathers[index];
if (weather == PlayerWeather.WeatherType)
return;
SetWeather(weather);
}
public void SetWeather(WeatherType next)
{
Debug.Log("Next weather change: " + next);
ClearAllWeather();
switch (next)
{
case WeatherType.Cloudy:
// TODO make skybox cloudy
SetFog(SunnyFogSettings);
break;
case WeatherType.Overcast:
SetOvercast();
break;
case WeatherType.Fog:
SetRainOvercast();
SetFog(HeavyFogSettings);
break;
case WeatherType.Rain:
StartRaining();
break;
case WeatherType.Thunder:
StartStorming();
break;
case WeatherType.Snow:
StartSnowing();
break;
}
RaiseOnWeatherChangeEvent(next);
PlayerWeather.WeatherType = next;
}
void AddWindZone()
{
// Add a wind zone for trees and particles.
var windZone = gameObject.AddComponent<WindZone>();
windZone.mode = WindZoneMode.Directional;
windZone.windMain = 0.2f;
windZone.windTurbulence = 0.1f;
windZone.windPulseMagnitude = 1;
windZone.windPulseFrequency = 0.25f;
}
#endregion
#region Events Handlers
void SaveLoadManager_OnLoad(SaveData_v1 saveData)
{
// first restore general outdoor weather (which sets fog)
SetWeather(saveData.playerData.playerPosition.weather);
DaggerfallSky.SetSkyFogColor(DaggerfallSky.skyColors);
previousOutdoorFogColor = RenderSettings.fogColor;
// then check if player is inside and set fog accordingly
if (GameManager.Instance.IsPlayerInsideBuilding)
{
SetFog(InteriorFogSettings, true);
}
else if (GameManager.Instance.IsPlayerInsideDungeon || GameManager.Instance.IsPlayerInsideCastle)
{
SetFog(DungeonFogSettings, true);
}
startedFromLoadedSaveGame = true; // needed so StreamingWorld_OnInitWorld() does not break stuff again (see details in comment at variable definition of startedFromLoadedSaveGame)
}
private void PlayerEnterExit_OnRespawnerComplete()
{
DFLocation.ClimateBaseType currentClimate = GameManager.Instance.PlayerGPS.ClimateSettings.ClimateType;
if (currentClimate != lastRespawnClimate)
{
PollWeatherChanges(true);
lastRespawnClimate = currentClimate;
}
}
void StreamingWorld_OnInitWorld()
{
// check if we did not just load a savegame (see details in comment at variable definition of startedFromLoadedSaveGame)
if (startedFromLoadedSaveGame == false)
{
// Clear weather when starting up world
ClearAllWeather();
// Set weather in case we have loaded from a classic save.
// Currently loading a Unity save will replace this with its own weather value after this.
updateWeatherFromClimateArray = true;
startedFromLoadedSaveGame = false;
}
else
{
// important so no weather update from climate array happens in case of loaded savegame
updateWeatherFromClimateArray = false;
}
}
void OnTransitionToInterior(PlayerEnterExit.TransitionEventArgs args)
{
previousOutdoorFogColor = RenderSettings.fogColor;
SetFog(InteriorFogSettings, true);
}
void OnTransitionToDungeon(PlayerEnterExit.TransitionEventArgs args)
{
previousOutdoorFogColor = RenderSettings.fogColor;
SetFog(DungeonFogSettings, true);
}
void OnTransitionToExterior(PlayerEnterExit.TransitionEventArgs args)
{
SetFog(currentOutdoorFogSettings, false);
RenderSettings.fogColor = previousOutdoorFogColor;
}
#endregion
#region Events
public delegate void OnWeatherChangeHandler(WeatherType weather);
public static event OnWeatherChangeHandler OnWeatherChange;
static void RaiseOnWeatherChangeEvent(WeatherType weather)
{
if (OnWeatherChange != null)
OnWeatherChange(weather);
}
#endregion
}
}