You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.6 KiB
C#

4 years ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SiegeSong;
namespace SiegeSong
{
public class InstanceManager : MonoBehaviour
{
public GameObject SpeciesDefinitionsContainer;
4 years ago
public string CurrentPlayerInstanceID;
public GameObject[] Instances;
public string[] InstanceIDs;
public int[] ActorIDs;
public string HumanSpeciesKey = "_Human";
4 years ago
public int LastGeneratedInstanceIndex;
public int MaxInstanceIDLength = 5;
void Start()
{
}
void Update()
{
}
public void ClearAndUnload()
{
for (var i = 0; i < Instances.Length; i++)
{
Instances[i].active = false;
GameObject.Destroy(Instances[i]);
}
LastGeneratedInstanceIndex = 0;
CurrentPlayerInstanceID = "";
Instances = new GameObject[0];
InstanceIDs = new string[0];
ActorIDs = new int[0];
}
4 years ago
public string GenerateInstanceID()
{
LastGeneratedInstanceIndex++;
var instanceIndex = LastGeneratedInstanceIndex.ToString();
var newInstanceID = "";
for (var i = 0; i < MaxInstanceIDLength; i++)
{
if (i >= instanceIndex.Length)
{
newInstanceID += "A";
}
else
{
newInstanceID += "BCDEFGHIJK"[int.Parse(instanceIndex[i].ToString())];
}
}
return newInstanceID;
}
public GameObject GetInstanceByID(string instanceID)
{
for(var i = 0; i < Instances.Length; i++)
{
if (InstanceIDs[i].ToLower() == instanceID.ToLower())
{
return Instances[i];
}
}
return new GameObject();
}
public void AddInstance(string instanceID, GameObject newInstance)
{
var newInstanceArray = new GameObject[Instances.Length + 1];
var newInstanceIDArray = new string[InstanceIDs.Length + 1];
Instances.CopyTo(newInstanceArray, 0);
InstanceIDs.CopyTo(newInstanceIDArray, 0);
newInstanceArray[newInstanceArray.Length - 1] = newInstance;
newInstanceIDArray[newInstanceIDArray.Length - 1] = instanceID;
Instances = newInstanceArray;
InstanceIDs = newInstanceIDArray;
}
public void RemoveInstance(string instanceID)
{
var newInstanceArray = new GameObject[Instances.Length - 1];
var newInstanceIDArray = new string[InstanceIDs.Length - 1];
Instances.CopyTo(newInstanceArray, 0);
InstanceIDs.CopyTo(newInstanceIDArray, 0);
var iterator = 0;
var index = 0;
foreach (var instance in Instances)
{
iterator++;
if (InstanceIDs[iterator].ToLower() == instanceID.ToLower())
{
var instanceObject = Instances[iterator];
4 years ago
GameObject.Destroy(instanceObject);
}
else
{
index++;
newInstanceArray[index] = instance;
newInstanceIDArray[index] = InstanceIDs[iterator];
}
}
Instances = newInstanceArray;
InstanceIDs = newInstanceIDArray;
4 years ago
}
}
}