using System.Collections; using System.Collections.Generic; using UnityEngine; using SiegeSong; namespace SiegeSong { public class InstanceManager : MonoBehaviour { public GameObject SpeciesDefinitionsContainer; public GameObject ActorInstanceContainer; public string CurrentPlayerActorInstanceID; public List ActorInstances; public List ActorInstanceIDs; public GameObject DroppableDefinitionContainer; public GameObject ArchitectureDefinitionContainer; public GameObject StationDefinitionContainer; public GameObject WorldObjectInstanceContainer; public List WorldObjectInstances; public List WorldObjectInstanceIDs; public string PlayerAgentKey = "PlayerAgent"; private int LastGeneratedWorldObjectInstanceIndex; private int LastGeneratedActorInstanceIndex; private int MaxActorInstanceIDLength = 5; private int MaxWorldObjectInstanceIDLength = 6; void Start() { } void Update() { } public KeyValuePair InstantiateWorldObject(WorldObject worldObject) { 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().InstanceID = instanceID; if (!string.IsNullOrEmpty(worldObject.StationKey)) instance.GetComponentInChildren().StationKey = worldObject.StationKey; else if (!string.IsNullOrEmpty(worldObject.ArchitectureKey)) instance.GetComponentInChildren().ArchitectureKey = worldObject.ArchitectureKey; else instance.GetComponentInChildren().DroppableKey = worldObject.DroppableKey; WorldObjectInstances.Add(instance); WorldObjectInstanceIDs.Add(instanceID); return new KeyValuePair(instanceID, instance); } public KeyValuePair InstantiateActor(Statistics stats) { 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() == null) instance.AddComponent(); var actor = instance.GetComponent(); 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() != 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(actor.Stats.ActorInstanceID, instance); } 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(); ActorInstanceIDs = new List(); } public string GenerateWorldObjectInstanceID() { LastGeneratedWorldObjectInstanceIndex++; var instanceIndex = LastGeneratedWorldObjectInstanceIndex.ToString(); var newInstanceID = ""; for (var i = 0; i < MaxWorldObjectInstanceIDLength; i++) { if (i >= instanceIndex.Length) { newInstanceID += "A"; } else { newInstanceID += "BCDEFGHIJK"[int.Parse(instanceIndex[i].ToString())]; } } return newInstanceID; } public string GenerateActorInstanceID() { LastGeneratedActorInstanceIndex++; var instanceIndex = LastGeneratedActorInstanceIndex.ToString(); var newInstanceID = ""; for (var i = 0; i < MaxActorInstanceIDLength; i++) { if (i >= instanceIndex.Length) { newInstanceID += "A"; } else { newInstanceID += "BCDEFGHIJK"[int.Parse(instanceIndex[i].ToString())]; } } return newInstanceID; } public GameObject GetWorldObjectInstanceByID(string instanceID) { for (var i = 0; i < WorldObjectInstances.Count; i++) { if (WorldObjectInstanceIDs[i].ToLower() == instanceID.ToLower()) { return WorldObjectInstances[i]; } } return null; } public GameObject GetActorInstanceByID(string instanceID) { for (var i = 0; i < ActorInstances.Count; i++) { if (ActorInstanceIDs[i].ToLower() == instanceID.ToLower()) { return ActorInstances[i]; } } return null; } } }