Add convenience function for making entities with new IDs.

This commit is contained in:
excitedneon 2017-05-08 21:21:03 +03:00
parent 78dfe42106
commit 4926530a49
2 changed files with 16 additions and 5 deletions

View File

@ -12,7 +12,7 @@ public class Spawner : MonoBehaviour {
/// </summary> /// </summary>
/// <param name="type">Type.</param> /// <param name="type">Type.</param>
/// <param name="position">Position.</param> /// <param name="position">Position.</param>
public GameObject Spawn(EntityType type, Vector3 position, uint[] ids) { public GameObject Spawn(EntityType type, Vector3 position, uint[] ids = null) {
GameObject Spawned = null; GameObject Spawned = null;
switch (type) { switch (type) {
case EntityType.PC: case EntityType.PC:
@ -23,18 +23,22 @@ public class Spawner : MonoBehaviour {
break; break;
} }
if (Spawned != null) { if (Spawned != null) {
SyncDB.AddEntity(Spawned, ids); if (ids != null) {
SyncDB.AddEntity(Spawned, ids);
} else {
SyncDB.AddEntity(Spawned, SyncDB.GetNewEntityIDs(Spawned));
}
} }
return Spawned; return Spawned;
} }
private void Start() { private void Start() {
Spawn(EntityType.PC, new Vector3(), new uint[]{ SyncDB.CreateID() }); Spawn(EntityType.PC, new Vector3());
} }
private void Update() { private void Update() {
if (Input.GetButtonDown("Jump") && !Term.IsVisible()) { 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)));
} }
} }
} }

View File

@ -22,11 +22,14 @@ public class SyncDB : MonoBehaviour {
} }
} }
public uint[] GetEntityIDs(GameObject gameObject) { public uint[] GetEntityIDs(GameObject gameObject, bool newIDs = false) {
List<uint> IDs = new List<uint>(); List<uint> IDs = new List<uint>();
for (int i = 0; i < SyncableClasses.Length; i++) { for (int i = 0; i < SyncableClasses.Length; i++) {
SyncBase Syncable = (SyncBase) gameObject.GetComponent(SyncableClasses[i]); SyncBase Syncable = (SyncBase) gameObject.GetComponent(SyncableClasses[i]);
if (Syncable != null) { if (Syncable != null) {
if (newIDs) {
Syncable.ID = CreateID();
}
IDs.Add(Syncable.ID); IDs.Add(Syncable.ID);
} }
} }
@ -37,6 +40,10 @@ public class SyncDB : MonoBehaviour {
return IDArray; return IDArray;
} }
public uint[] GetNewEntityIDs(GameObject gameObject) {
return GetEntityIDs(gameObject, true);
}
public SyncBase Get(uint id) { public SyncBase Get(uint id) {
return Database[id]; return Database[id];
} }