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.
256 lines
11 KiB
C#
256 lines
11 KiB
C#
|
3 years ago
|
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<GameObject> ActorInstances;
|
||
|
|
public List<string> ActorInstanceIDs;
|
||
|
|
|
||
|
|
public GameObject DroppableDefinitionContainer;
|
||
|
|
public GameObject ArchitectureDefinitionContainer;
|
||
|
|
public GameObject StationDefinitionContainer;
|
||
|
|
public GameObject WorldObjectInstanceContainer;
|
||
|
|
public List<GameObject> WorldObjectInstances;
|
||
|
|
public List<string> 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<string, GameObject> 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<WorldObject>().InstanceID = instanceID;
|
||
|
|
instance.GetComponentInChildren<WorldObject>().Type = worldObject.Type;
|
||
|
|
instance.GetComponentInChildren<WorldObject>().Key = worldObject.Key;
|
||
|
|
|
||
|
|
|
||
|
|
WorldObjectInstances.Add(instance);
|
||
|
|
WorldObjectInstanceIDs.Add(instanceID);
|
||
|
|
|
||
|
|
return new KeyValuePair<string, GameObject>(instanceID, instance);
|
||
|
|
}
|
||
|
|
|
||
|
|
public KeyValuePair<string, GameObject> 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<StatisticsManager>() == null)
|
||
|
|
instance.AddComponent<StatisticsManager>();
|
||
|
|
|
||
|
|
var actor = instance.GetComponent<StatisticsManager>();
|
||
|
|
var inventoryManager = actor.InventoryManager != null ? actor.InventoryManager : actor.gameObject.GetComponentInChildren<InventoryManager>();
|
||
|
|
if (actor.InventoryManager == null && inventoryManager != null)
|
||
|
|
actor.InventoryManager = inventoryManager;
|
||
|
|
var equipmentManager = inventoryManager.EquipmentManager != null ? inventoryManager.EquipmentManager : inventoryManager.gameObject.GetComponentInChildren<EquipmentManager>();
|
||
|
|
var apparrelManager = inventoryManager.ApparrelManager != null ? inventoryManager.ApparrelManager : inventoryManager.gameObject.GetComponentInChildren<ApparrelManager>();
|
||
|
|
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<Equipable>() != 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<string, GameObject>(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<Motor>();
|
||
|
|
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<GameObject>();
|
||
|
|
ActorInstanceIDs = new List<string>();
|
||
|
|
}
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|