using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
namespace Cyber.Entities {
///
/// A database of the game's all syncable components. Syncable components are
/// the instances of the subclasses of .
///
public class SyncDB : MonoBehaviour {
private static readonly Type[] SyncableClasses = new Type[] {
typeof(Character)
};
private uint IDCounter = 0;
private Dictionary Database = new Dictionary();
///
/// Add an entity to the database with the given IDs.
///
/// Game object.
/// The IDs. Should be from or
/// , since the order is important.
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;
}
}
}
///
/// Makes an ordered list of the given gameobject's syncable components'
/// IDs.
///
/// The IDs.
/// Game object.
/// Whether or not new IDs are created.
/// is a shorthand for this function with
/// this parameter set to true.
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);
}
}
uint[] IDArray = new uint[IDs.Count];
for (int i = 0; i < IDs.Count; i++) {
IDArray[i] = IDs[i];
}
return IDArray;
}
///
/// Creates an ordered list of the given gameobject's syncable components'
/// IDs. See for more information.
///
/// The new IDs.
/// Game object.
public uint[] GetNewEntityIDs(GameObject gameObject) {
return GetEntityIDs(gameObject, true);
}
///
/// Get a synced component by its ID.
///
/// The ID.
public SyncBase Get(uint id) {
return Database[id];
}
///
/// Creates a new ID which isn't in use yet.
///
/// A new, free ID.
public uint CreateID() {
uint ID;
try {
ID = IDCounter++;
} catch (OverflowException) {
ID = 0;
IDCounter = 1;
}
while (Database.ContainsKey(ID) && 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;
}
}
}