From 4926530a492776007158e4806a57a27636c8358a Mon Sep 17 00:00:00 2001 From: excitedneon Date: Mon, 8 May 2017 21:21:03 +0300 Subject: [PATCH] Add convenience function for making entities with new IDs. --- Assets/Scripts/Spawner.cs | 12 ++++++++---- Assets/Scripts/SyncDB.cs | 9 ++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/Spawner.cs b/Assets/Scripts/Spawner.cs index 1aab139..09441c0 100644 --- a/Assets/Scripts/Spawner.cs +++ b/Assets/Scripts/Spawner.cs @@ -12,7 +12,7 @@ public class Spawner : MonoBehaviour { /// /// Type. /// Position. - public GameObject Spawn(EntityType type, Vector3 position, uint[] ids) { + public GameObject Spawn(EntityType type, Vector3 position, uint[] ids = null) { GameObject Spawned = null; switch (type) { case EntityType.PC: @@ -23,18 +23,22 @@ public class Spawner : MonoBehaviour { break; } if (Spawned != null) { - SyncDB.AddEntity(Spawned, ids); + if (ids != null) { + SyncDB.AddEntity(Spawned, ids); + } else { + SyncDB.AddEntity(Spawned, SyncDB.GetNewEntityIDs(Spawned)); + } } return Spawned; } private void Start() { - Spawn(EntityType.PC, new Vector3(), new uint[]{ SyncDB.CreateID() }); + Spawn(EntityType.PC, new Vector3()); } private void Update() { if (Input.GetButtonDown("Jump") && !Term.IsVisible()) { - Spawn(EntityType.NPC, new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-2f, 2f)), new uint[]{ SyncDB.CreateID() }); + Spawn(EntityType.NPC, new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-2f, 2f))); } } } diff --git a/Assets/Scripts/SyncDB.cs b/Assets/Scripts/SyncDB.cs index 8c76716..152f008 100644 --- a/Assets/Scripts/SyncDB.cs +++ b/Assets/Scripts/SyncDB.cs @@ -22,11 +22,14 @@ public class SyncDB : MonoBehaviour { } } - public uint[] GetEntityIDs(GameObject gameObject) { + public uint[] GetEntityIDs(GameObject gameObject, bool newIDs = false) { List IDs = new List(); for (int i = 0; i < SyncableClasses.Length; i++) { SyncBase Syncable = (SyncBase) gameObject.GetComponent(SyncableClasses[i]); if (Syncable != null) { + if (newIDs) { + Syncable.ID = CreateID(); + } IDs.Add(Syncable.ID); } } @@ -37,6 +40,10 @@ public class SyncDB : MonoBehaviour { return IDArray; } + public uint[] GetNewEntityIDs(GameObject gameObject) { + return GetEntityIDs(gameObject, true); + } + public SyncBase Get(uint id) { return Database[id]; }