Decoupled IDs from the spawning system.

This commit is contained in:
excitedneon 2017-05-08 06:20:29 +03:00
parent 3bd2e17ab8
commit 1f49f6a6f0
5 changed files with 94 additions and 18 deletions

View File

@ -844,6 +844,7 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 605817615} - component: {fileID: 605817615}
- component: {fileID: 605817614} - component: {fileID: 605817614}
- component: {fileID: 605817616}
m_Layer: 0 m_Layer: 0
m_Name: World Root m_Name: World Root
m_TagString: Untagged m_TagString: Untagged
@ -862,6 +863,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: fb83f65c40e344c3b9bc2d83195fe69d, type: 3} m_Script: {fileID: 11500000, guid: fb83f65c40e344c3b9bc2d83195fe69d, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
SyncDB: {fileID: 605817616}
PCEntityPrefab: {fileID: 1306609719649024, guid: e8761b9491a974d1780e60dafbd372a2, PCEntityPrefab: {fileID: 1306609719649024, guid: e8761b9491a974d1780e60dafbd372a2,
type: 2} type: 2}
NPCEntityPrefab: {fileID: 1225554598977308, guid: 88dbba5a284f041afbfbb6e6f52935fa, NPCEntityPrefab: {fileID: 1225554598977308, guid: 88dbba5a284f041afbfbb6e6f52935fa,
@ -879,6 +881,17 @@ Transform:
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 3 m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} 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 --- !u!1 &679442845
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -3,18 +3,16 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class Spawner : MonoBehaviour { public class Spawner : MonoBehaviour {
public SyncDB SyncDB;
public GameObject PCEntityPrefab; public GameObject PCEntityPrefab;
public GameObject NPCEntityPrefab; public GameObject NPCEntityPrefab;
private int LastID = 0;
private Dictionary<int, GameObject> SpawnedEntities = new Dictionary<int, GameObject>();
/// <summary> /// <summary>
/// Spawns an entity and returns that entity. /// Spawns an entity and returns that entity.
/// </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) { public GameObject Spawn(EntityType type, Vector3 position, uint[] ids) {
GameObject Spawned = null; GameObject Spawned = null;
switch (type) { switch (type) {
case EntityType.PC: case EntityType.PC:
@ -25,28 +23,18 @@ public class Spawner : MonoBehaviour {
break; break;
} }
if (Spawned != null) { if (Spawned != null) {
int ID = CreateID(); SyncDB.AddEntity(Spawned, ids);
Spawned.GetComponent<SyncBase>().ID = ID;
SpawnedEntities[ID] = Spawned;
} }
return Spawned; return Spawned;
} }
public GameObject Get(int id) {
return SpawnedEntities[id];
}
private void Start() { private void Start() {
Spawn(EntityType.PC, new Vector3()); Spawn(EntityType.PC, new Vector3(), new uint[]{ SyncDB.CreateID() });
} }
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))); Spawn(EntityType.NPC, new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-2f, 2f)), new uint[]{ SyncDB.CreateID() });
} }
} }
private int CreateID() {
return LastID++;
}
} }

View File

@ -3,5 +3,5 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class SyncBase : MonoBehaviour { public class SyncBase : MonoBehaviour {
public int ID; public uint ID;
} }

63
Assets/Scripts/SyncDB.cs Normal file
View File

@ -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<uint, SyncBase> Database = new Dictionary<uint, SyncBase>();
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<uint> IDs = new List<uint>();
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;
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9d715320baa1f401ab0f46b603399dc3
timeCreated: 1494210838
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: