123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 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<GameActionsController> 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>();
- placedObject.placedObjectType = placedObjectType;
- placedObject.origin = origin;
- placedObject.dir = dir;
- placedObject.actionsControllerList = new List<GameActionsController>();
- 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<TooltipTrigger>();
- if(tooltipTrigger == null) tooltipTrigger = gameObject.GetComponentInChildren<TooltipTrigger>();
- if(tooltipTrigger != null)
- {
- tooltipTrigger.header = placedObjectType.title;
- tooltipTrigger.content = placedObjectType.description;
- }
- var selectableTrigger = gameObject.GetComponentInChildren<SelectableObjectTrigger>();
- 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<Vector2Int> GetGridPositionList()
- {
- return placedObjectType.GetGridPositionList(origin, dir);
- }
- public List<SelectableObjectAction> 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;
- }
-
- }
- }
|