From 1f49f6a6f07d659f2f6c14baeccda266187fd209 Mon Sep 17 00:00:00 2001 From: excitedneon Date: Mon, 8 May 2017 06:20:29 +0300 Subject: [PATCH] Decoupled IDs from the spawning system. --- Assets/Scenes/TestMap.unity | 13 ++++++++ Assets/Scripts/Spawner.cs | 22 +++--------- Assets/Scripts/SyncBase.cs | 2 +- Assets/Scripts/SyncDB.cs | 63 +++++++++++++++++++++++++++++++++++ Assets/Scripts/SyncDB.cs.meta | 12 +++++++ 5 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 Assets/Scripts/SyncDB.cs create mode 100644 Assets/Scripts/SyncDB.cs.meta diff --git a/Assets/Scenes/TestMap.unity b/Assets/Scenes/TestMap.unity index a278fe2..aa276fe 100644 --- a/Assets/Scenes/TestMap.unity +++ b/Assets/Scenes/TestMap.unity @@ -844,6 +844,7 @@ GameObject: m_Component: - component: {fileID: 605817615} - component: {fileID: 605817614} + - component: {fileID: 605817616} m_Layer: 0 m_Name: World Root m_TagString: Untagged @@ -862,6 +863,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: fb83f65c40e344c3b9bc2d83195fe69d, type: 3} m_Name: m_EditorClassIdentifier: + SyncDB: {fileID: 605817616} PCEntityPrefab: {fileID: 1306609719649024, guid: e8761b9491a974d1780e60dafbd372a2, type: 2} NPCEntityPrefab: {fileID: 1225554598977308, guid: 88dbba5a284f041afbfbb6e6f52935fa, @@ -879,6 +881,17 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &605817616 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 605817613} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9d715320baa1f401ab0f46b603399dc3, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &679442845 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Spawner.cs b/Assets/Scripts/Spawner.cs index c88578e..1aab139 100644 --- a/Assets/Scripts/Spawner.cs +++ b/Assets/Scripts/Spawner.cs @@ -3,18 +3,16 @@ using System.Collections.Generic; using UnityEngine; public class Spawner : MonoBehaviour { + public SyncDB SyncDB; public GameObject PCEntityPrefab; public GameObject NPCEntityPrefab; - private int LastID = 0; - private Dictionary SpawnedEntities = new Dictionary(); - /// /// Spawns an entity and returns that entity. /// /// Type. /// Position. - public GameObject Spawn(EntityType type, Vector3 position) { + public GameObject Spawn(EntityType type, Vector3 position, uint[] ids) { GameObject Spawned = null; switch (type) { case EntityType.PC: @@ -25,28 +23,18 @@ public class Spawner : MonoBehaviour { break; } if (Spawned != null) { - int ID = CreateID(); - Spawned.GetComponent().ID = ID; - SpawnedEntities[ID] = Spawned; + SyncDB.AddEntity(Spawned, ids); } return Spawned; } - public GameObject Get(int id) { - return SpawnedEntities[id]; - } - private void Start() { - Spawn(EntityType.PC, new Vector3()); + Spawn(EntityType.PC, new Vector3(), new uint[]{ SyncDB.CreateID() }); } private void Update() { if (Input.GetButtonDown("Jump") && !Term.IsVisible()) { - Spawn(EntityType.NPC, new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-2f, 2f))); + Spawn(EntityType.NPC, new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-2f, 2f)), new uint[]{ SyncDB.CreateID() }); } } - - private int CreateID() { - return LastID++; - } } diff --git a/Assets/Scripts/SyncBase.cs b/Assets/Scripts/SyncBase.cs index 8949ba1..b0e424d 100644 --- a/Assets/Scripts/SyncBase.cs +++ b/Assets/Scripts/SyncBase.cs @@ -3,5 +3,5 @@ using System.Collections.Generic; using UnityEngine; public class SyncBase : MonoBehaviour { - public int ID; + public uint ID; } diff --git a/Assets/Scripts/SyncDB.cs b/Assets/Scripts/SyncDB.cs new file mode 100644 index 0000000..8a9f558 --- /dev/null +++ b/Assets/Scripts/SyncDB.cs @@ -0,0 +1,63 @@ +using System.Collections; +using System.Collections.Generic; +using System; +using UnityEngine; + +public class SyncDB : MonoBehaviour { + private static readonly Type[] SyncableClasses = new Type[]{ + typeof(Character) + }; + + private uint IDCounter = 0; + private Dictionary Database = new Dictionary(); + + public void AddEntity(GameObject gameObject, uint[] ids) { + int Index = 0; + for (int i = 0; i < SyncableClasses.Length; i++) { + SyncBase Syncable = (SyncBase) gameObject.GetComponent(SyncableClasses[i]); + if (Syncable != null) { + Syncable.ID = ids[Index]; + Database[ids[Index++]] = Syncable; + } + } + } + + public uint[] GetEntityIDs(GameObject gameObject) { + List IDs = new List(); + for (int i = 0; i < SyncableClasses.Length; i++) { + SyncBase Syncable = (SyncBase) gameObject.GetComponent(SyncableClasses[i]); + if (Syncable != null) { + IDs.Add(Syncable.ID); + } + } + uint[] IDArray = new uint[IDs.Count]; + for (int i = 0; i < IDs.Count; i++) { + IDArray[i] = IDs[i]; + } + return IDArray; + } + + public SyncBase Get(uint id) { + return Database[id]; + } + + public uint CreateID() { + uint ID; + try { + ID = IDCounter++; + } catch (OverflowException Ex) { + ID = 0; + IDCounter = 1; + } + while (Database.ContainsKey(ID) && Database.Keys.Count < uint.MaxValue && ID < uint.MaxValue) { + ID++; + if (ID < uint.MaxValue - 1) IDCounter = ID + 1; + } + if (Database.ContainsKey(ID)) { + // Somehow we've managed to fill up the whole database. + // I can't even imagine why or how. + Debug.LogError("!!!WARNING!!! The SyncDB is full. Update the game to use longs instead of uints. !!!WARNING!!!"); + } + return ID; + } +} diff --git a/Assets/Scripts/SyncDB.cs.meta b/Assets/Scripts/SyncDB.cs.meta new file mode 100644 index 0000000..a28871c --- /dev/null +++ b/Assets/Scripts/SyncDB.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 9d715320baa1f401ab0f46b603399dc3 +timeCreated: 1494210838 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: