using System.Collections; using System.Collections.Generic; using UnityEngine; using KairoEngine.Core; using KairoEngine.Core.GameActions; using KairoEngine.UI.Tooltips; using KairoEngine.GameTools.Selectables; using KairoEngine.SFX; namespace KairoEngine.Grids { public class PlacedObject : MonoBehaviour, ISelectableObject { public PlacedObjectType placedObjectType { get; private set;} public Vector2Int origin { get; private set;} public PlacedObjectType.Dir dir { get; private set;} public float destroyDelay = 3000f; public bool destructable = true; [SerializeField] private List actionsControllerList; [SerializeField] private GameActionContext context; public static PlacedObject Create(Vector3 worldPosition, Vector2Int origin, PlacedObjectType.Dir dir, PlacedObjectType placedObjectType, bool playCreatedSound = true) { Quaternion quaternion = Quaternion.Euler(0, placedObjectType.GetRotationAngle(dir), 0); Transform placedObjectTransform = Instantiate(placedObjectType.prefab, worldPosition, quaternion); PlacedObject placedObject = placedObjectTransform.GetComponent(); placedObject.placedObjectType = placedObjectType; placedObject.origin = origin; placedObject.dir = dir; placedObject.actionsControllerList = new List(); placedObject.actionsControllerList.Add(placedObjectType.onCreateController.Duplicate()); placedObject.actionsControllerList.Add(placedObjectType.onUpdateController.Duplicate()); placedObject.actionsControllerList.Add(placedObjectType.onRemoveController.Duplicate()); for (int i = 0; i < placedObjectType.objectActions.Count; i++) { placedObject.actionsControllerList.Add( placedObjectType.objectActions[i].actions.Duplicate()); } placedObject.context = placedObjectType.context.Duplicate(); foreach (var ctrl in placedObject.actionsControllerList) ctrl.context = placedObject.context; // Set Game Action Variables var targetVar = placedObject.context.GetVariable("Building GameObject"); if(targetVar == null) { targetVar = new GameActionContextGameObject(); targetVar.name = "Building GameObject"; targetVar.canEdit = false; placedObject.context.variables.Add(targetVar); } ((GameActionContextGameObject)targetVar).value = placedObject.gameObject; placedObject.actionsControllerList[0].Start(); // Run the OnAdd actions if(playCreatedSound) { SoundController.EmmitSound(placedObjectType.createdSound, placedObject.transform.position); } return placedObject; } private void Start() { TooltipTrigger tooltipTrigger = gameObject.GetComponent(); if(tooltipTrigger == null) tooltipTrigger = gameObject.GetComponentInChildren(); if(tooltipTrigger != null) { tooltipTrigger.header = placedObjectType.title; tooltipTrigger.content = placedObjectType.description; } var selectableTrigger = gameObject.GetComponentInChildren(); if(selectableTrigger != null) selectableTrigger.selectableObject = this; else Debug.LogError("No SelectableObjectTrigger found on placed object hierarchy", gameObject); } private void Update() { actionsControllerList[1].Start(); } private void OnDestroy() { } public List GetGridPositionList() { return placedObjectType.GetGridPositionList(origin, dir); } public List actions => placedObjectType.objectActions; public void ExecuteAction(SelectableObjectAction action) { for (int i = 0; i < placedObjectType.objectActions.Count; i++) { if(action == placedObjectType.objectActions[i]) { if(actionsControllerList[3 + i] != null) { if(actionsControllerList[3 + i].started) actionsControllerList[3 + i].Restart(); actionsControllerList[3 + i].Start(); } else Debug.LogError($"Could not find GameActionController for \"{action.title} in {gameObject.name} PlacedObject", this.gameObject); } } } public void DestroySelf() { actionsControllerList[2].Start(); Timer.Execute(destroyDelay, () => { Destroy(gameObject); }); } public bool HasConnection(string connector, int x, int y) { for (int i = 0; i < placedObjectType.connectors.Count; i++) { Vector2Int pos = origin; pos = pos + placedObjectType.GetConnectorPositionOffset(i, dir); //Debug.Log($"{placedObjectType.connectors[i].position} - {placedObjectType.GetConnectorPositionOffset(i, dir)}"); Vector2Int targetPos = pos + placedObjectType.GetConnectorTargetPositionOffset(i, dir); //Debug.Log($"Checking connector {pos} -->{targetPos} | {placedObjectType.connectors[i].dir.ToString()}"); if(targetPos.x == x && targetPos.y == y && connector == placedObjectType.connectors[i].title) return true; } return false; } } }