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.

228 lines
8.9 KiB
C#

4 years ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SiegeSong;
namespace SiegeSong
{
4 years ago
public class InstanceManager : MonoBehaviour
{
public GameObject SpeciesDefinitionsContainer;
public GameObject ActorInstanceContainer;
public string CurrentPlayerActorInstanceID;
public List<GameObject> ActorInstances;
public List<string> ActorInstanceIDs;
4 years ago
public GameObject DroppableDefinitionContainer;
public GameObject ArchitectureDefinitionContainer;
public GameObject StationDefinitionContainer;
public GameObject WorldObjectInstanceContainer;
public List<GameObject> WorldObjectInstances;
public List<string> WorldObjectInstanceIDs;
4 years ago
public string PlayerAgentKey = "PlayerAgent";
private int LastGeneratedWorldObjectInstanceIndex;
private int LastGeneratedActorInstanceIndex;
private int MaxActorInstanceIDLength = 5;
private int MaxWorldObjectInstanceIDLength = 6;
4 years ago
void Start() { }
4 years ago
void Update() { }
public KeyValuePair<string, GameObject> InstantiateWorldObject(WorldObject worldObject)
4 years ago
{
var worldObjectKey = "";
GameObject definitionList;
GameObject instance = null;
var instanceID = GenerateWorldObjectInstanceID();
if (!string.IsNullOrEmpty(worldObject.StationKey))
{
definitionList = StationDefinitionContainer;
worldObjectKey = worldObject.StationKey;
}
else if (!string.IsNullOrEmpty(worldObject.ArchitectureKey))
{
definitionList = ArchitectureDefinitionContainer;
worldObjectKey = worldObject.ArchitectureKey;
}
else
{
definitionList = DroppableDefinitionContainer;
worldObjectKey = worldObject.DroppableKey;
}
for (var i = 0; i < definitionList.transform.childCount; i++)
{
var worldObjectDefinition = definitionList.transform.GetChild(i);
if (worldObjectDefinition.gameObject.name == worldObjectKey)
instance = GameObject.Instantiate(worldObjectDefinition).gameObject;
}
instance.transform.eulerAngles = new Vector3(worldObject.RotationX, worldObject.RotationY, worldObject.RotationZ);
instance.transform.position = new Vector3(worldObject.PositionX, worldObject.PositionY, worldObject.PositionZ);
instance.gameObject.name = $"{(string.IsNullOrEmpty(worldObject.FriendlyName) ? worldObjectKey : worldObject.FriendlyName)}_{instanceID}";
instance.transform.parent = WorldObjectInstanceContainer.transform;
instance.active = true;
instance.GetComponentInChildren<Selectable>().InstanceID = instanceID;
if (!string.IsNullOrEmpty(worldObject.StationKey))
instance.GetComponentInChildren<Selectable>().StationKey = worldObject.StationKey;
else if (!string.IsNullOrEmpty(worldObject.ArchitectureKey))
instance.GetComponentInChildren<Selectable>().ArchitectureKey = worldObject.ArchitectureKey;
else
instance.GetComponentInChildren<Selectable>().DroppableKey = worldObject.DroppableKey;
WorldObjectInstances.Add(instance);
WorldObjectInstanceIDs.Add(instanceID);
return new KeyValuePair<string, GameObject>(instanceID, instance);
4 years ago
}
public KeyValuePair<string, GameObject> InstantiateActor(Statistics stats)
4 years ago
{
GameObject instance = null;
for (var i = 0; i < SpeciesDefinitionsContainer.transform.childCount; i++)
{
var species = SpeciesDefinitionsContainer.transform.GetChild(i);
if (species.gameObject.name == stats.SpeciesKey)
instance = GameObject.Instantiate(species).gameObject;
}
if (instance.GetComponent<StatisticsManager>() == null)
instance.AddComponent<StatisticsManager>();
var actor = instance.GetComponent<StatisticsManager>();
if (actor.Stats == null)
actor.Stats = new Statistics();
if (!instance.active)
instance.active = true;
if (stats.IsPlayer)
{
for (var j = 0; j < instance.transform.childCount; j++)
{
if (instance.transform.GetChild(j).name == "PlayerAgent")
{
instance.transform.GetChild(j).gameObject.active = true;
}
}
}
actor.Stats = stats;
instance.transform.eulerAngles = new Vector3(stats.RotationX, stats.RotationY, stats.RotationZ);
instance.transform.position = new Vector3(stats.LocationX, stats.LocationY, stats.LocationZ);
instance.transform.parent = ActorInstanceContainer.transform;
for (var i = 0; i < stats.InventoryItemIDs.Count; i++)
{
actor.Inventory.SelectedItemID = stats.InventoryItemIDs[i];
actor.Inventory.SelectedAmount = stats.InventoryItemQuantities[i];
actor.Inventory.AddItem();
actor.Inventory.SelectedItemID = 0;
actor.Inventory.SelectedAmount = 0;
actor.Inventory.SelectedItemIndex = 0;
}
foreach (var itemId in stats.ActiveInventoryItemIDs)
{
if (actor.Inventory.GetItemByID(itemId).GetComponent<Equipable>() != null)
actor.Inventory.EquipmentManager.AddEquipmentToSlots(actor.Inventory.GetItemByID(itemId));
}
actor.Inventory.ApparrelManager.CurrentlyActiveIDs = stats.ActiveInventoryItemIDs.ToArray();
actor.Inventory.ApparrelManager.UpdateApparrel();
actor.Stats.ActorInstanceID = GenerateActorInstanceID();
instance.gameObject.name = $"{actor.Stats.FirstName} {actor.Stats.LastName}";
ActorInstances.Add(instance);
ActorInstanceIDs.Add(actor.Stats.ActorInstanceID);
return new KeyValuePair<string, GameObject>(actor.Stats.ActorInstanceID, instance);
4 years ago
}
public void ClearAndUnload()
{
for (var i = 0; i < ActorInstances.Count; i++)
{
ActorInstances[i].active = false;
GameObject.Destroy(ActorInstances[i]);
}
for (var i = 0; i < WorldObjectInstances.Count; i++)
{
WorldObjectInstances[i].active = false;
GameObject.Destroy(ActorInstances[i]);
}
LastGeneratedActorInstanceIndex = 0;
CurrentPlayerActorInstanceID = "";
ActorInstances = new List<GameObject>();
ActorInstanceIDs = new List<string>();
}
public string GenerateWorldObjectInstanceID()
4 years ago
{
LastGeneratedWorldObjectInstanceIndex++;
var instanceIndex = LastGeneratedWorldObjectInstanceIndex.ToString();
4 years ago
var newInstanceID = "";
for (var i = 0; i < MaxWorldObjectInstanceIDLength; i++)
4 years ago
{
if (i >= instanceIndex.Length)
{
newInstanceID += "A";
}
4 years ago
else
{
newInstanceID += "BCDEFGHIJK"[int.Parse(instanceIndex[i].ToString())];
}
}
return newInstanceID;
}
public string GenerateActorInstanceID()
4 years ago
{
LastGeneratedActorInstanceIndex++;
var instanceIndex = LastGeneratedActorInstanceIndex.ToString();
var newInstanceID = "";
for (var i = 0; i < MaxActorInstanceIDLength; i++)
4 years ago
{
if (i >= instanceIndex.Length)
4 years ago
{
newInstanceID += "A";
}
else
{
newInstanceID += "BCDEFGHIJK"[int.Parse(instanceIndex[i].ToString())];
4 years ago
}
}
return newInstanceID;
4 years ago
}
public GameObject GetWorldObjectInstanceByID(string instanceID)
4 years ago
{
for (var i = 0; i < WorldObjectInstances.Count; i++)
{
if (WorldObjectInstanceIDs[i].ToLower() == instanceID.ToLower())
{
return WorldObjectInstances[i];
}
}
return null;
4 years ago
}
public GameObject GetActorInstanceByID(string instanceID)
4 years ago
{
for (var i = 0; i < ActorInstances.Count; i++)
{
if (ActorInstanceIDs[i].ToLower() == instanceID.ToLower())
{
return ActorInstances[i];
}
}
return null;
4 years ago
}
}
4 years ago
}