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 RuntimeManager RuntimeManager; private string cameraTargetKey = "Camera Target"; private string cameraMountsKey = "Camera Mounts"; private string cameraMountFirstPersonKey = "FirstPersonMount"; private string cameraMountThirdPersonLeftKey = "LeftMount"; private string cameraMountThirdPersonCenterKey = "CenterMount"; private string cameraMountThirdPersonRightKey = "RightMount"; private int LastGeneratedWorldObjectInstanceIndex; private int LastGeneratedActorInstanceIndex; private int MaxActorInstanceIDLength = 5; private int MaxWorldObjectInstanceIDLength = 6; void Start() { } void Update() { } public KeyValuePair InstantiateWorldObject(WorldObjectPlacement worldObject) { var worldObjectKey = worldObject.Key; GameObject definitionList = null; GameObject instance = null; var instanceID = GenerateWorldObjectInstanceID(); switch (worldObject.Type) { case WorldObjectType.Architecture: definitionList = ArchitectureDefinitionContainer; break; case WorldObjectType.Droppable: definitionList = DroppableDefinitionContainer; break; case WorldObjectType.Station: definitionList = StationDefinitionContainer; break; } for (var i = 0; i < definitionList.transform.childCount; i++) { var worldObjectDefinition = definitionList.transform.GetChild(i); if (worldObjectDefinition.gameObject.name == worldObjectKey) instance = 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.SetActive(true); instance.GetComponentInChildren().InstanceID = instanceID; instance.GetComponentInChildren().Type = worldObject.Type; instance.GetComponentInChildren().Key = worldObject.Key; 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 = Instantiate(species).gameObject; } if (instance.GetComponent() == null) instance.AddComponent(); var actor = instance.GetComponent(); var inventoryManager = actor.InventoryManager != null ? actor.InventoryManager : actor.gameObject.GetComponentInChildren(); if (actor.InventoryManager == null && inventoryManager != null) actor.InventoryManager = inventoryManager; var equipmentManager = inventoryManager.EquipmentManager != null ? inventoryManager.EquipmentManager : inventoryManager.gameObject.GetComponentInChildren(); var apparrelManager = inventoryManager.ApparrelManager != null ? inventoryManager.ApparrelManager : inventoryManager.gameObject.GetComponentInChildren(); if (actor.InventoryManager == null && inventoryManager != null) actor.InventoryManager = inventoryManager; if (actor.InventoryManager == null && inventoryManager != null) actor.InventoryManager = inventoryManager; if (actor.Stats == null) actor.Stats = new Statistics(); if (!instance.activeSelf) instance.SetActive(true); if (stats.IsPlayer) { SwitchPlayerInstance(instance, stats.ActorInstanceID); } 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++) { inventoryManager.Inventory.AddItem(stats.InventoryItemIDs[i], stats.InventoryItemQuantities[i]); } foreach (var itemId in stats.ActiveInventoryItemIDs) { if (inventoryManager.GetItemByID(itemId).GetComponent() != null) equipmentManager.AddEquipmentToSlots(actor.InventoryManager.GetItemByID(itemId)); } apparrelManager.CurrentlyActiveIDs = stats.ActiveInventoryItemIDs.ToArray(); 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 SwitchPlayerInstance(GameObject playerInstanceObject, string instanceKey) { playerInstanceObject.tag = "Player"; RuntimeManager.PlayerActorInstance = playerInstanceObject; CurrentPlayerActorInstanceID = instanceKey; RuntimeManager.LoadingCellManager.PlayerTracker = playerInstanceObject.transform; RuntimeManager.CameraManager.Motor = playerInstanceObject.GetComponent(); for (var i = 0; i < playerInstanceObject.transform.childCount; i++) { var child = playerInstanceObject.transform.GetChild(i); if (child.gameObject.name == cameraMountsKey) { for (var j = 0; j < child.childCount; j++) { var cameraMount = child.GetChild(j); if (cameraMount.gameObject.name == cameraMountFirstPersonKey) RuntimeManager.CameraManager.FirstPersonMount = cameraMount; else if (cameraMount.gameObject.name == cameraMountThirdPersonLeftKey) RuntimeManager.CameraManager.LeftMount = cameraMount; else if (cameraMount.gameObject.name == cameraMountThirdPersonCenterKey) RuntimeManager.CameraManager.CenterMount = cameraMount; else if (cameraMount.gameObject.name == cameraMountThirdPersonRightKey) RuntimeManager.CameraManager.RightMount = cameraMount; } } else if (child.gameObject.name == cameraTargetKey) { RuntimeManager.CameraManager.CameraTarget = child; } } } public void ClearAndUnload() { for (var i = 0; i < ActorInstances.Count; i++) { ActorInstances[i].SetActive(false); GameObject.Destroy(ActorInstances[i]); } for (var i = 0; i < WorldObjectInstances.Count; i++) { WorldObjectInstances[i].SetActive(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; } } }