Skip to content

Commit

Permalink
refactor: Used events instead of singletons
Browse files Browse the repository at this point in the history
For reusability.
  • Loading branch information
Anson-Cheung1227 committed Feb 24, 2023
1 parent bb6059b commit f0bb548
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 226 deletions.
234 changes: 104 additions & 130 deletions Assets/Scenes/Core.unity

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Assets/src/DataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

public class DataManager : MonoBehaviour
{
public static DataManager Instance;
public DateTime GameDateTime = new DateTime(2019, 12, 1, 0, 0, 0);
public bool ActiveStateDetailsPanel;
public State HoveringState;
Expand All @@ -13,7 +12,7 @@ public class DataManager : MonoBehaviour
[SerializeField] private GlobalStatsSO _globalStatsSO;
private void Awake()
{
Instance = this;

}
void Start()
{
Expand Down
10 changes: 5 additions & 5 deletions Assets/src/DeathGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ void Update()
{

}
public void GenerateDeath()
public void GenerateDeath(DataManager dataManager)
{
Infection findResult;
foreach (StateController stateController in _allStates)
{
foreach (Infection infection in stateController.State.InHospital)
{
int daysSinceInHospital = (int)((DataManager.Instance.GameDateTime.Date - infection.Date.Date).TotalDays);
int daysSinceInHospital = (int)((dataManager.GameDateTime.Date - infection.Date.Date).TotalDays);
if (daysSinceInHospital < 14) continue;
int generatedAmount = (int)(infection.Amount * DataManager.Instance.DeathRate);
int generatedAmount = (int)(infection.Amount * dataManager.DeathRate);
if (generatedAmount < 1) continue;
findResult = Infection.FindExistingInfection(stateController.State, infection.Date, infection.InHospitalDate, infection.RecoveryDate, DataManager.Instance.GameDateTime, InfectionStatus.Deceased, infection.HasSpread);
findResult = Infection.FindExistingInfection(stateController.State, infection.Date, infection.InHospitalDate, infection.RecoveryDate, dataManager.GameDateTime, InfectionStatus.Deceased, infection.HasSpread);
if (findResult == null)
{
findResult = new Infection{Date = infection.Date, InHospitalDate = infection.InHospitalDate, DeceasedDate = DataManager.Instance.GameDateTime ,InfectionStatus = InfectionStatus.Deceased, Amount = generatedAmount, HasSpread = infection.HasSpread};
findResult = new Infection{Date = infection.Date, InHospitalDate = infection.InHospitalDate, DeceasedDate = dataManager.GameDateTime ,InfectionStatus = InfectionStatus.Deceased, Amount = generatedAmount, HasSpread = infection.HasSpread};
stateController.State.Deceased.Add(findResult);
stateController.State.Infections.Add(findResult);
}
Expand Down
30 changes: 15 additions & 15 deletions Assets/src/GameEventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ public class GameEventManager : MonoBehaviour
{
public static GameEventManager Instance;
public event Action OnDateChange;
public event Action OnGenerateInfection;
public event Action OnGenerateInHospital;
public event Action OnGenerateRecovery;
public event Action OnGenerateDeath;
public event Action<DataManager> OnGenerateInfection;
public event Action<DataManager> OnGenerateInHospital;
public event Action<DataManager> OnGenerateRecovery;
public event Action<DataManager> OnGenerateDeath;
public event Action OnUpdateMorale;
public event Action OnUpdateUI;
public event Action<DataManager> OnUpdateUI;
private void Awake()
{
Instance = this;
Expand All @@ -24,32 +24,32 @@ public void InvokeOnDateChange()
OnDateChange();
}
}
public void InvokeOnGenerateInfection()
public void InvokeOnGenerateInfection(DataManager dataManager)
{
if (OnGenerateInfection != null)
{
OnGenerateInfection();
OnGenerateInfection(dataManager);
}
}
public void InvokeOnGenerateInHospital()
public void InvokeOnGenerateInHospital(DataManager dataManager)
{
if (OnGenerateInHospital != null)
{
OnGenerateInHospital();
OnGenerateInHospital(dataManager);
}
}
public void InvokeOnGenerateRecovery()
public void InvokeOnGenerateRecovery(DataManager dataManager)
{
if (OnGenerateRecovery != null)
{
OnGenerateRecovery();
OnGenerateRecovery(dataManager);
}
}
public void InvokeOnGenerateDeath()
public void InvokeOnGenerateDeath(DataManager dataManager)
{
if (OnGenerateDeath != null)
{
OnGenerateDeath();
OnGenerateDeath(dataManager);
}
}
public void InvokeOnUpdateMorale()
Expand All @@ -59,11 +59,11 @@ public void InvokeOnUpdateMorale()
OnUpdateMorale();
}
}
public void InvokeOnUpdateUI()
public void InvokeOnUpdateUI(DataManager dataManager)
{
if (OnUpdateUI != null)
{
OnUpdateUI();
OnUpdateUI(dataManager);
}
}
}
8 changes: 4 additions & 4 deletions Assets/src/InHospitalGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private void Update()
{

}
public void GenerateInHospital()
public void GenerateInHospital(DataManager dataManager)
{
s_GenerateInHospitalMarker.Begin();
foreach (StateController stateController in _allStates)
Expand All @@ -26,15 +26,15 @@ public void GenerateInHospital()
if (stateController.State.Infections.Count == 0) continue;
foreach (Infection infection in stateController.State.ActiveInfections)
{
int daysSinceInfection = (int)((DataManager.Instance.GameDateTime.Date - infection.Date.Date).TotalDays);
int daysSinceInfection = (int)((dataManager.GameDateTime.Date - infection.Date.Date).TotalDays);
if (daysSinceInfection <= 1) continue;
int chance = 100 - 100 / daysSinceInfection;
long generateAmount = (int)(infection.Amount * chance/100);
if (generateAmount < 1) continue;
findResult = Infection.FindExistingInfection(stateController.State, infection.Date, DataManager.Instance.GameDateTime, null, null,InfectionStatus.InHospital, infection.HasSpread);
findResult = Infection.FindExistingInfection(stateController.State, infection.Date, dataManager.GameDateTime, null, null,InfectionStatus.InHospital, infection.HasSpread);
if (findResult == null)
{
findResult = new Infection { Date = infection.Date, InHospitalDate = DataManager.Instance.GameDateTime, InfectionStatus = InfectionStatus.InHospital, Amount = generateAmount, HasSpread = infection.HasSpread};
findResult = new Infection { Date = infection.Date, InHospitalDate = dataManager.GameDateTime, InfectionStatus = InfectionStatus.InHospital, Amount = generateAmount, HasSpread = infection.HasSpread};
stateController.State.InHospital.Add(findResult);
stateController.State.Infections.Add(findResult);
}
Expand Down
36 changes: 18 additions & 18 deletions Assets/src/InfectionGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ private void Update()
}
}
//UnityEvent for adding infections, called once per day
public void GenerateInfection()
public void GenerateInfection(DataManager dataManager)
{
GenerateInfectionsLocal();
GenerateInfectionsInterstate();
GenerateInfectionsGlobal();
GenerateInfectionsLocal(dataManager);
GenerateInfectionsInterstate(dataManager);
GenerateInfectionsGlobal(dataManager);
}
public void GenerateInfectionsLocal()
public void GenerateInfectionsLocal(DataManager dataManager)
{
long actualActiveInfections;
foreach (StateController stateController in _allState)
Expand All @@ -64,10 +64,10 @@ public void GenerateInfectionsLocal()
if (generateAmount == 0 || stateController.State.InfectionsLong >= stateController.State.Population || stateController.State.InfectionsLong + generateAmount == stateController.State.Population) continue;
if (stateController.State.InfectionsLong + generateAmount > stateController.State.Population)
generateAmount = stateController.State.Population - stateController.State.InfectionsLong;
AddInfection(stateController.State, InfectionType.Local, infections: generateAmount);
AddInfection(dataManager, stateController.State, InfectionType.Local, infections: generateAmount);
}
}
public void GenerateInfectionsInterstate()
public void GenerateInfectionsInterstate(DataManager dataManager)
{
/*
We do not want new generated infections to pass on immediately, else we'll be in an infinite loop
Expand Down Expand Up @@ -102,9 +102,9 @@ We will add all infections at once after the calculations
if (infection.Key[0].InfectionsLong >= infection.Key[0].Population || infection.Key[0].InfectionsLong + infection.Value == infection.Key[0].Population)
continue;
if (infection.Key[0].InfectionsLong + infection.Value > infection.Key[0].Population)
AddInfection(infection.Key[0], InfectionType.Interstate, infection.Key[1], infection.Key[0].Population - infection.Key[0].InfectionsLong);
AddInfection(dataManager, infection.Key[0], InfectionType.Interstate, infection.Key[1], infection.Key[0].Population - infection.Key[0].InfectionsLong);
else
AddInfection(infection.Key[0], InfectionType.Interstate, infection.Key[1], infection.Value);
AddInfection(dataManager, infection.Key[0], InfectionType.Interstate, infection.Key[1], infection.Value);
}
}
public State DetermineStateInfectionInterstate()
Expand All @@ -117,13 +117,13 @@ public State DetermineStateInfectionInterstate()
}
return eligibleInfectionState[Random.Range(0, eligibleInfectionState.Count)];
}
public void GenerateInfectionsGlobal()
public void GenerateInfectionsGlobal(DataManager dataManager)
{
Dictionary<List<State>, long> delayedInfection = new Dictionary<List<State>, long>();
//Adding one global infection
State determinedState = DetermineStateInfectionGlobal();
if (determinedState == null) return;
AddInfection(DetermineStateInfectionGlobal(), InfectionType.Global);
AddInfection(dataManager, DetermineStateInfectionGlobal(), InfectionType.Global);
}
public State DetermineStateInfectionGlobal()
{
Expand Down Expand Up @@ -158,14 +158,14 @@ public State DetermineStateInfectionGlobal()
}
return determinedState;
}
public void AddInfection(State state, InfectionType infectionType, State originState = null, long infections = 1)
public void AddInfection(DataManager dataManager, State state, InfectionType infectionType,State originState = null, long infections = 1)
{
if (infectionType == InfectionType.Local)
{
Infection infection = Infection.FindExistingInfection(state, DataManager.Instance.GameDateTime, null, null, null, InfectionStatus.Active, false);
Infection infection = Infection.FindExistingInfection(state, dataManager.GameDateTime, null, null, null, InfectionStatus.Active, false);
if (infection == null)
{
infection = new Infection { Date = DataManager.Instance.GameDateTime, Amount = infections, InfectionStatus = InfectionStatus.Active};
infection = new Infection { Date = dataManager.GameDateTime, Amount = infections, InfectionStatus = InfectionStatus.Active};
state.Infections.Add(infection);
state.ActiveInfections.Add(infection);
}
Expand All @@ -177,10 +177,10 @@ public void AddInfection(State state, InfectionType infectionType, State originS
}
if (infectionType == InfectionType.Interstate)
{
Infection infection = Infection.FindExistingInfection(state, DataManager.Instance.GameDateTime, null, null, null, InfectionStatus.Active, false);
Infection infection = Infection.FindExistingInfection(state, dataManager.GameDateTime, null, null, null, InfectionStatus.Active, false);
if (infection == null)
{
infection = new Infection { Date = DataManager.Instance.GameDateTime, Amount = infections, InfectionStatus = InfectionStatus.Active};
infection = new Infection { Date = dataManager.GameDateTime, Amount = infections, InfectionStatus = InfectionStatus.Active};
state.Infections.Add(infection);
state.ActiveInfections.Add(infection);
}
Expand All @@ -192,10 +192,10 @@ public void AddInfection(State state, InfectionType infectionType, State originS
}
if (infectionType == InfectionType.Global)
{
Infection infection = Infection.FindExistingInfection(state, DataManager.Instance.GameDateTime, null, null, null, InfectionStatus.Active, false);
Infection infection = Infection.FindExistingInfection(state, dataManager.GameDateTime, null, null, null, InfectionStatus.Active, false);
if (infection == null)
{
infection = new Infection { Date = DataManager.Instance.GameDateTime, Amount = infections, InfectionStatus = InfectionStatus.Active};
infection = new Infection { Date = dataManager.GameDateTime, Amount = infections, InfectionStatus = InfectionStatus.Active};
state.Infections.Add(infection);
state.ActiveInfections.Add(infection);
}
Expand Down
10 changes: 5 additions & 5 deletions Assets/src/RecoveryGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ void Update()
{

}
public void GenerateRecovery()
public void GenerateRecovery(DataManager dataManager)
{
Infection findResult;
foreach (StateController stateController in _allStates)
{
foreach (Infection infection in stateController.State.InHospital)
{
int daysSinceInHospital = (int)((DataManager.Instance.GameDateTime.Date - infection.Date.Date).TotalDays);
int daysSinceInHospital = (int)((dataManager.GameDateTime.Date - infection.Date.Date).TotalDays);
if (daysSinceInHospital < 14) continue;
int generatedAmount = (int)(infection.Amount * DataManager.Instance.RecoveryRate);
int generatedAmount = (int)(infection.Amount * dataManager.RecoveryRate);
if (generatedAmount < 1) continue;
findResult = Infection.FindExistingInfection(stateController.State, infection.Date, infection.InHospitalDate, DataManager.Instance.GameDateTime, null,InfectionStatus.Recovered, infection.HasSpread);
findResult = Infection.FindExistingInfection(stateController.State, infection.Date, infection.InHospitalDate, dataManager.GameDateTime, null,InfectionStatus.Recovered, infection.HasSpread);
if (findResult == null)
{
findResult = new Infection{Date = infection.Date, InHospitalDate = infection.InHospitalDate, RecoveryDate = DataManager.Instance.GameDateTime, InfectionStatus = InfectionStatus.Recovered, Amount = generatedAmount, HasSpread = infection.HasSpread};
findResult = new Infection{Date = infection.Date, InHospitalDate = infection.InHospitalDate, RecoveryDate = dataManager.GameDateTime, InfectionStatus = InfectionStatus.Recovered, Amount = generatedAmount, HasSpread = infection.HasSpread};
stateController.State.Recovered.Add(findResult);
stateController.State.Infections.Add(findResult);
}
Expand Down
15 changes: 8 additions & 7 deletions Assets/src/StatesMouseDetection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class StatesMouseDetection : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField] private DataManager _dataManager;
[SerializeField] private StateColorSO _stateColorSORef;
[SerializeField] private SpriteRenderer _spriteRenderer;
[SerializeField] private StateController _stateController;
Expand All @@ -17,28 +18,28 @@ private void Start()
// Update is called once per frame
private void Update()
{
if (DataManager.Instance.SelectedState == _stateController.State) _spriteRenderer.color = _stateColorSORef.SelectedColor;
if (_dataManager.SelectedState == _stateController.State) _spriteRenderer.color = _stateColorSORef.SelectedColor;
else
{
if (DataManager.Instance.HoveringState == _stateController.State) _spriteRenderer.color = _stateColorSORef.HoveringColor;
if (_dataManager.HoveringState == _stateController.State) _spriteRenderer.color = _stateColorSORef.HoveringColor;
else _spriteRenderer.color = _stateColorSORef.OriginalColor;
}
}
private void OnMouseOver()
{
if (_cameraController.IsPointerOverUI()) return;
DataManager.Instance.HoveringState = _stateController.State;
TooltipSystem.Show("", DataManager.Instance.HoveringState.Name);
_dataManager.HoveringState = _stateController.State;
TooltipSystem.Show("", _dataManager.HoveringState.Name);
}
private void OnMouseExit()
{
DataManager.Instance.HoveringState = null;
_dataManager.HoveringState = null;
TooltipSystem.Hide();
}
private void OnMouseDown()
{
if (_cameraController.IsPointerOverUI()) return;
DataManager.Instance.SelectedState = _stateController.State;
DataManager.Instance.ActiveStateDetailsPanel = true;
_dataManager.SelectedState = _stateController.State;
_dataManager.ActiveStateDetailsPanel = true;
}
}
19 changes: 10 additions & 9 deletions Assets/src/TimeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

public class TimeController : MonoBehaviour
{
[SerializeField] private DataManager _dataManager;
[SerializeField] private float[] _changeMinutes = new float[5];
private Nullable<DateTime> _lastUpdateDateTime;
//public Property to control the speed of time;
public int GameSpeed { get; private set; }
private void Start()
{
_lastUpdateDateTime = DataManager.Instance.GameDateTime;
_lastUpdateDateTime = _dataManager.GameDateTime;
}

private void Update()
Expand All @@ -22,17 +23,17 @@ private void Update()
if (Input.GetKeyDown(KeyCode.Alpha3)) GameSpeed = 3; //1 second (in real life) = 4 hour (in game time)
if (Input.GetKeyDown(KeyCode.Alpha4)) GameSpeed = 4; //1 second (in real life) = 8 hour (in game time)
//Change the Game Speed;
DataManager.Instance.GameDateTime = DataManager.Instance.GameDateTime.AddMinutes(_changeMinutes[GameSpeed] * Time.deltaTime);
if (DataManager.Instance.GameDateTime.Date != _lastUpdateDateTime.Value.Date)
_dataManager.GameDateTime = _dataManager.GameDateTime.AddMinutes(_changeMinutes[GameSpeed] * Time.deltaTime);
if (_dataManager.GameDateTime.Date != _lastUpdateDateTime.Value.Date)
{
GameEventManager.Instance.InvokeOnDateChange();
GameEventManager.Instance.InvokeOnGenerateInfection();
GameEventManager.Instance.InvokeOnGenerateInHospital();
GameEventManager.Instance.InvokeOnGenerateRecovery();
GameEventManager.Instance.InvokeOnGenerateDeath();
GameEventManager.Instance.InvokeOnGenerateInfection(_dataManager);
GameEventManager.Instance.InvokeOnGenerateInHospital(_dataManager);
GameEventManager.Instance.InvokeOnGenerateRecovery(_dataManager);
GameEventManager.Instance.InvokeOnGenerateDeath(_dataManager);
GameEventManager.Instance.InvokeOnUpdateMorale();
GameEventManager.Instance.InvokeOnUpdateUI();
_lastUpdateDateTime = DataManager.Instance.GameDateTime;
_lastUpdateDateTime = _dataManager.GameDateTime;
}
GameEventManager.Instance.InvokeOnUpdateUI(_dataManager);
}
}
Loading

0 comments on commit f0bb548

Please sign in to comment.