1
Fork 0
mirror of https://github.com/Steffo99/keep-everything-alive.git synced 2024-11-23 01:44:18 +00:00
keep-everything-alive/Assets/Scripts/Main/GameController.cs

136 lines
4.1 KiB
C#
Raw Normal View History

2020-04-18 14:09:37 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
[HideInInspector]
public new Camera camera;
[BeforeStart]
public int startingLives = 4;
[BeforeStart]
public int maxLives = 4;
private int lives;
public delegate void OnLivesChangeHandler(int previous, int current);
public event OnLivesChangeHandler OnLivesChange;
public delegate void OnGameOverHandler(GameController sender);
public event OnGameOverHandler OnGameOver;
public int Lives {
get {
return lives;
}
set {
OnLivesChange?.Invoke(Lives, value);
lives = value;
if(lives <= 0) {
OnGameOver?.Invoke(this);
}
}
}
[BeforeStart]
public float startingTimescale = 1.0f;
public delegate void OnSpeedChangeHandler(float previous, float current);
public event OnSpeedChangeHandler OnSpeedChange;
public float Timescale {
get {
return Time.timeScale;
}
set {
OnSpeedChange?.Invoke(Timescale, value);
Time.timeScale = value;
}
}
[BeforeStart]
public int startingDifficulty = 1;
private int difficulty;
public delegate void OnDifficultyChangeHandler(int previous, int current);
public event OnDifficultyChangeHandler OnDifficultyChange;
public int Difficulty {
get {
return difficulty;
}
set {
OnDifficultyChange?.Invoke(Difficulty, value);
difficulty = value;
}
}
[BeforeStart]
public int startingScore = 0;
private int score;
public delegate void OnScoreChangeHandler(int previous, int current);
public event OnScoreChangeHandler OnScoreChange;
public int Score {
get {
return score;
}
set {
OnScoreChange?.Invoke(Score, value);
score = value;
}
}
public List<MicrogameController> microgames;
private MicrogameController currentMicrogame;
public delegate void OnMicrogameStartHandler(MicrogameController newMicrogame);
public event OnMicrogameStartHandler OnMicrogameStart;
public delegate void OnMicrogameEndHandler(MicrogameController endedMicrogame);
public event OnMicrogameEndHandler OnMicrogameEnd;
public MicrogameController CurrentMicrogame {
get {
return currentMicrogame;
}
set {
2020-04-18 17:02:22 +00:00
OnMicrogameTimeLeftChange?.Invoke(CurrentMicrogame?.TimeLeft, value?.TimeLeft);
if(CurrentMicrogame != null) {
2020-04-18 14:09:37 +00:00
OnMicrogameEnd?.Invoke(CurrentMicrogame);
CurrentMicrogame.OnTimeLeftChange -= PropagateTimeLeftChange;
Destroy(CurrentMicrogame.gameObject);
}
2020-04-18 17:02:22 +00:00
if(value != null) {
2020-04-18 14:09:37 +00:00
currentMicrogame = Instantiate(value.gameObject, transform).GetComponent<MicrogameController>();
CurrentMicrogame.OnTimeLeftChange += PropagateTimeLeftChange;
OnMicrogameStart?.Invoke(CurrentMicrogame);
}
}
}
public delegate void OnMicrogameTimeLeftChangeHandler(float? previous, float? current);
public event OnMicrogameTimeLeftChangeHandler OnMicrogameTimeLeftChange;
private void PropagateTimeLeftChange(float previous, float current) {
OnMicrogameTimeLeftChange?.Invoke(previous, current);
}
private void Awake() {
camera = Camera.main;
}
private void Start() {
2020-04-18 17:02:22 +00:00
Lives = startingLives;
Timescale = startingTimescale;
Difficulty = startingDifficulty;
Score = startingScore;
CurrentMicrogame = null;
// Notify the TimePanel of the starting status
OnMicrogameTimeLeftChange?.Invoke(null, null);
2020-04-18 14:09:37 +00:00
}
private void CheckMicrogameResults(MicrogameController microgame) {
Debug.Assert(microgame != null);
if(microgame.MicrogameResults()) {
Score += 1;
}
else {
Lives -= 1;
}
}
private MicrogameController GetRandomMicrogame() {
return microgames.PickRandom();
}
}