From 4099e2908fee42d9bf734fdb30e43672aabd5f41 Mon Sep 17 00:00:00 2001 From: Antoine H Date: Mon, 14 Dec 2020 16:23:54 +0100 Subject: [PATCH] =?UTF-8?q?GameSystem=20en=20Singleton=20+=20d=C3=A9but=20?= =?UTF-8?q?timer=20service=20+=20Slow-mode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/GameSystem.cs | 107 ++++++++++++++++++++++ Assets/Scripts/GameSystem.cs.meta | 11 +++ Assets/Scripts/Tavernkeeper_controller.cs | 3 +- 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 Assets/Scripts/GameSystem.cs create mode 100644 Assets/Scripts/GameSystem.cs.meta diff --git a/Assets/Scripts/GameSystem.cs b/Assets/Scripts/GameSystem.cs new file mode 100644 index 0000000..a3fe004 --- /dev/null +++ b/Assets/Scripts/GameSystem.cs @@ -0,0 +1,107 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System; //Exceptions + +//Define the global game system of the service. (Singleton) +//TODO : Split gamesystem into subsystem ? +public sealed class GameSystem : MonoBehaviour +{ + //Time + bool serviceOpen = false; + float serviceTime = 10.0f; + float serviceTimer = 0.0f; + float slowScale = 0.5f; //Default scale for slow mode + private float fixedDeltaTime; + + //Clients + // int nbMaxClients = 2; + // float freqNewClients = 3.0f; + + public void startService() + { + serviceTimer=serviceTime; + serviceOpen=true; + } + + //Change time scale + //Return wether game time follow real-time (not scaled) + public bool toggleSlowMode(float? newTimeScale=null) + { + if(newTimeScale is null) //Toggle between default values + { + if(Mathf.Approximately(Time.timeScale, 1.0f)) + Time.timeScale = slowScale; + else + Time.timeScale = 1.0f; + } + else //Set to specific scale + { + if(newTimeScale<0.0f) + throw new Exception("Trying to set time scale to negative value (rewinding time...) :"+newTimeScale); + + Time.timeScale = (float)newTimeScale; + } + + // Adjust fixed delta time according to timescale + // The fixed delta time will now be 0.02 frames per real-time second + Time.fixedDeltaTime = this.fixedDeltaTime * Time.timeScale; + + //Return wether game time follow real-time (not scaled) + if(Mathf.Approximately(Time.timeScale, 1.0f)) + return true; + else + return false; + } + + //Awake is called when the script instance is being loaded. + void Awake() + { + // Make a copy of the fixedDeltaTime, it defaults to 0.02f, but it can be changed in the editor + this.fixedDeltaTime = Time.fixedDeltaTime; + } + + // Start is called before the first frame update + void Start() + { + startService(); + } + + // Update is called once per frame + void Update() + { + if(serviceOpen) + { + serviceTimer-= Time.deltaTime; + if (serviceTimer < 0) + serviceOpen = false; + } + + //Temporary manual slowmode toggle + if (Input.GetButtonDown("Fire2")) + { + toggleSlowMode(); + Debug.Log("Time scale: "+Time.timeScale); + } + // Debug.Log("Service timer : "+(int)serviceTimer); + } + + //// Singleton Implementation (https://jlambert.developpez.com/tutoriels/dotnet/implementation-pattern-singleton-csharp/#LIII) //// + private GameSystem() + { + } + + public static GameSystem Instance { get { return Nested.instance; } } + + private class Nested + { + // Explicit static constructor to tell C# compiler + // not to mark type as beforefieldinit + static Nested() + { + } + + internal static readonly GameSystem instance = new GameObject("GameSystem").AddComponent(); + } + //// +} \ No newline at end of file diff --git a/Assets/Scripts/GameSystem.cs.meta b/Assets/Scripts/GameSystem.cs.meta new file mode 100644 index 0000000..12bfedc --- /dev/null +++ b/Assets/Scripts/GameSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5f18e7df2f912f644a6a6949b25117b6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Tavernkeeper_controller.cs b/Assets/Scripts/Tavernkeeper_controller.cs index d0f9f22..94a5363 100644 --- a/Assets/Scripts/Tavernkeeper_controller.cs +++ b/Assets/Scripts/Tavernkeeper_controller.cs @@ -66,6 +66,8 @@ public class Tavernkeeper_controller : MonoBehaviour {"left", null}, {"right", null} }; + + // GameSystem.Instance.startService(); } // Update is called once per frame @@ -89,7 +91,6 @@ public class Tavernkeeper_controller : MonoBehaviour animator.SetFloat("Speed", move.magnitude); //Actions delay - actionTimer -= Time.deltaTime; if(isInteracting) { actionTimer -= Time.deltaTime;