Add ClientManager

This commit is contained in:
Antoine H 2020-12-18 12:12:45 +01:00
parent 4099e2908f
commit 641ce0abcb
5 changed files with 109 additions and 5 deletions

View file

@ -0,0 +1,86 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
//Define the system managing the clients. (Singleton)
public sealed class ClientManager : MonoBehaviour
{
int currentNbClient = 0;
int nbMaxClients = 1;
bool clientSpawnReady = false;
float clientSpawnTimer = 2.0f; //Intial time before first spawn (pseudo-random after that)
float maxTimeNewClients = 10.0f;
string ClientRessourceFolder = "Clients";
Vector3 spawnPosition = new Vector3(0, 0, 0);
private Object[] clients;
//Request new client
//Return wether a new client was created
public bool clientRequest()
{
if(clientSpawnReady && currentNbClient<nbMaxClients)
{
GameObject newClient = (GameObject)clients[Random.Range(0, clients.Length)];
// Debug.Log("Spawning "+clientPrefab.name+" at "+spawnPosition);
Instantiate(newClient, spawnPosition, Quaternion.identity);
currentNbClient+=1;
clientSpawnTimer=Random.Range(1.0f, maxTimeNewClients); //Need more random ?
clientSpawnReady=false;
return true; //New client instantiated
}
return false; //No new client
}
// Start is called before the first frame update
void Start()
{
// Find all assets labelled with 'usable' :
// string[] guids = AssetDatabase.FindAssets("", new string[] {"Assets/Prefabs/Characters/Clients"});
// foreach (string guid in guids)
// {
// Debug.Log(AssetDatabase.GUIDToAssetPath(guid));
// Instantiate(guid, spawnPosition, Quaternion.identity);
// }
clients = Resources.LoadAll(ClientRessourceFolder);
foreach (var c in clients)
{
Debug.Log(gameObject.name+" : "+c.name + " loaded");
}
}
// Update is called once per frame
void Update()
{
if(!clientSpawnReady)
{
clientSpawnTimer-= Time.deltaTime;
if(clientSpawnTimer<=0)
clientSpawnReady=true;
}
}
//// Singleton Implementation (https://jlambert.developpez.com/tutoriels/dotnet/implementation-pattern-singleton-csharp/#LIII) ////
private ClientManager()
{
}
public static ClientManager 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 ClientManager instance = new GameObject("ClientManager").AddComponent<ClientManager>();
}
////
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 2bc1593400bcb054db0179d45fa332e9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences:
- clientPrefab: {fileID: 5304524770463492230, guid: 2dde00c8c5857d2438fd92435d8c3abb,
type: 3}
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,104 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System; //Exceptions
//Define the global game system of the service. (Singleton)
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;
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;
ClientManager.Instance.clientRequest();
}
//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<GameSystem>();
}
////
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5f18e7df2f912f644a6a6949b25117b6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: