1
Fork 0
mirror of https://github.com/Steffo99/keep-everything-alive.git synced 2024-11-30 21:14:18 +00:00
keep-everything-alive/Assets/Scripts/Core/GameController.cs

190 lines
6 KiB
C#
Raw Normal View History

2020-04-18 14:09:37 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2020-04-19 16:12:45 +00:00
using UnityEngine.SceneManagement;
2020-04-18 14:09:37 +00:00
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);
}
}
}
2020-04-19 12:42:38 +00:00
public bool GameOver {
get {
return Lives <= 0;
}
}
2020-04-18 14:09:37 +00:00
[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 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;
2020-04-18 20:45:36 +00:00
public delegate void OnMicrogameCreateHandler(MicrogameController newMicrogame);
public event OnMicrogameCreateHandler OnMicrogameCreate;
public delegate void OnMicrogameDestroyHandler(MicrogameController endedMicrogame);
public event OnMicrogameDestroyHandler OnMicrogameDestroy;
2020-04-18 14:09:37 +00:00
public MicrogameController CurrentMicrogame {
get {
return currentMicrogame;
}
2020-04-18 20:45:36 +00:00
set {
2020-04-18 17:02:22 +00:00
if(CurrentMicrogame != null) {
2020-04-18 20:45:36 +00:00
OnMicrogameDestroy?.Invoke(CurrentMicrogame);
2020-04-18 14:09:37 +00:00
CurrentMicrogame.OnTimeLeftChange -= PropagateTimeLeftChange;
2020-04-18 20:45:36 +00:00
CurrentMicrogame.OnMicrogameEnd -= OnMicrogameEnd;
2020-04-18 14:09:37 +00:00
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;
2020-04-18 20:45:36 +00:00
CurrentMicrogame.OnMicrogameEnd += OnMicrogameEnd;
OnMicrogameCreate?.Invoke(CurrentMicrogame);
2020-04-18 14:09:37 +00:00
}
2020-04-18 20:45:36 +00:00
OnMicrogameTimeLeftChange?.Invoke(CurrentMicrogame?.TimeLeft, value?.TimeLeft);
2020-04-18 14:09:37 +00:00
}
}
public delegate void OnMicrogameTimeLeftChangeHandler(float? previous, float? current);
public event OnMicrogameTimeLeftChangeHandler OnMicrogameTimeLeftChange;
private void PropagateTimeLeftChange(float previous, float current) {
OnMicrogameTimeLeftChange?.Invoke(previous, current);
}
2020-04-19 01:48:15 +00:00
private void OnMicrogameEnd(MicrogameController microgame, bool victory) {
2020-04-18 20:45:36 +00:00
Debug.Assert(microgame != null);
2020-04-19 12:42:38 +00:00
if(!victory) {
2020-04-18 20:45:36 +00:00
Lives -= 1;
}
2020-04-19 12:42:38 +00:00
Score += 1;
2020-04-18 20:45:36 +00:00
CurrentMicrogame = null;
2020-04-19 12:42:38 +00:00
if(!GameOver) {
if(score % increaseSpeedEvery == 0) {
Faster();
}
StartCoroutine("SpinTheWheel");
}
2020-04-18 20:45:36 +00:00
}
2020-04-19 01:48:15 +00:00
public float timescaleIncreaseFactor = 0.05f;
2020-04-19 12:42:38 +00:00
public float increaseSpeedEvery = 5;
2020-04-19 01:48:15 +00:00
private void Faster() {
Timescale += timescaleIncreaseFactor;
}
2020-04-18 20:45:36 +00:00
private MicrogameController displayedMicrogame;
public delegate void OnDisplayedMicrogameChangeHandler(MicrogameController previous, MicrogameController current);
public event OnDisplayedMicrogameChangeHandler OnDisplayedMicrogameChange;
public MicrogameController DisplayedMicrogame {
get {
return displayedMicrogame;
}
set {
OnDisplayedMicrogameChange?.Invoke(displayedMicrogame, value);
displayedMicrogame = value;
}
}
[Header("Wheel Settings")]
public float wheelSelectionDelay = 0.1f;
public float wheelSelectionTime = 2f;
public float wheelDisplayTime = 2f;
2020-04-19 01:48:15 +00:00
public AudioSource wheelClickAudioSource;
public AudioSource wheelBoopAudioSource;
2020-04-18 20:45:36 +00:00
IEnumerator SpinTheWheel() {
float timePassed = 0f;
while(timePassed < wheelSelectionTime) {
DisplayedMicrogame = GetRandomMicrogame();
2020-04-19 01:48:15 +00:00
wheelClickAudioSource.Play();
2020-04-18 20:45:36 +00:00
yield return new WaitForSeconds(wheelSelectionDelay);
timePassed += wheelSelectionDelay;
}
DisplayedMicrogame = GetRandomMicrogame();
2020-04-19 01:48:15 +00:00
wheelBoopAudioSource.Play();
2020-04-18 20:45:36 +00:00
yield return new WaitForSeconds(wheelDisplayTime);
CurrentMicrogame = DisplayedMicrogame;
}
public MicrogameController GetRandomMicrogame() {
Debug.Assert(microgames.Count > 0);
return microgames.PickRandom();
}
2020-04-18 14:09:37 +00:00
private void Awake() {
camera = Camera.main;
}
private void Start() {
2020-04-18 17:02:22 +00:00
Lives = startingLives;
Timescale = startingTimescale;
Score = startingScore;
CurrentMicrogame = null;
2020-04-20 20:56:00 +00:00
gameOverTimer = 0f;
2020-04-18 17:02:22 +00:00
// Notify the TimePanel of the starting status
OnMicrogameTimeLeftChange?.Invoke(null, null);
2020-04-18 14:09:37 +00:00
2020-04-18 20:45:36 +00:00
StartCoroutine("SpinTheWheel");
2020-04-18 14:09:37 +00:00
}
2020-04-19 16:12:45 +00:00
2020-04-20 20:56:00 +00:00
public float gameOverRequiredTime = 4f;
private float gameOverTimer = 0f;
2020-04-19 16:12:45 +00:00
private void Update() {
if(GameOver) {
2020-04-20 20:56:00 +00:00
gameOverTimer += Time.unscaledDeltaTime;
if(gameOverTimer >= gameOverRequiredTime && Input.anyKeyDown) {
2020-04-19 16:12:45 +00:00
SceneManager.LoadScene("Default");
}
}
}
2020-04-18 14:09:37 +00:00
}