PlacedObject.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. using KairoEngine.Core.GameActions;
  6. using KairoEngine.UI.Tooltips;
  7. using KairoEngine.GameTools.Selectables;
  8. using KairoEngine.SFX;
  9. namespace KairoEngine.Grids
  10. {
  11. public class PlacedObject : MonoBehaviour, ISelectableObject
  12. {
  13. public PlacedObjectType placedObjectType { get; private set;}
  14. public Vector2Int origin { get; private set;}
  15. public PlacedObjectType.Dir dir { get; private set;}
  16. public float destroyDelay = 3000f;
  17. public bool destructable = true;
  18. [SerializeField] private List<GameActionsController> actionsControllerList;
  19. [SerializeField] private GameActionContext context;
  20. public static PlacedObject Create(Vector3 worldPosition, Vector2Int origin, PlacedObjectType.Dir dir,
  21. PlacedObjectType placedObjectType, bool playCreatedSound = true)
  22. {
  23. Quaternion quaternion = Quaternion.Euler(0, placedObjectType.GetRotationAngle(dir), 0);
  24. Transform placedObjectTransform = Instantiate(placedObjectType.prefab, worldPosition, quaternion);
  25. PlacedObject placedObject = placedObjectTransform.GetComponent<PlacedObject>();
  26. placedObject.placedObjectType = placedObjectType;
  27. placedObject.origin = origin;
  28. placedObject.dir = dir;
  29. placedObject.actionsControllerList = new List<GameActionsController>();
  30. placedObject.actionsControllerList.Add(placedObjectType.onCreateController.Duplicate());
  31. placedObject.actionsControllerList.Add(placedObjectType.onUpdateController.Duplicate());
  32. placedObject.actionsControllerList.Add(placedObjectType.onRemoveController.Duplicate());
  33. for (int i = 0; i < placedObjectType.objectActions.Count; i++)
  34. {
  35. placedObject.actionsControllerList.Add( placedObjectType.objectActions[i].actions.Duplicate());
  36. }
  37. placedObject.context = placedObjectType.context.Duplicate();
  38. foreach (var ctrl in placedObject.actionsControllerList) ctrl.context = placedObject.context;
  39. // Set Game Action Variables
  40. var targetVar = placedObject.context.GetVariable("Building GameObject");
  41. if(targetVar == null)
  42. {
  43. targetVar = new GameActionContextGameObject();
  44. targetVar.name = "Building GameObject";
  45. targetVar.canEdit = false;
  46. placedObject.context.variables.Add(targetVar);
  47. }
  48. ((GameActionContextGameObject)targetVar).value = placedObject.gameObject;
  49. placedObject.actionsControllerList[0].Start(); // Run the OnAdd actions
  50. if(playCreatedSound)
  51. {
  52. SoundController.EmmitSound(placedObjectType.createdSound, placedObject.transform.position);
  53. }
  54. return placedObject;
  55. }
  56. private void Start()
  57. {
  58. TooltipTrigger tooltipTrigger = gameObject.GetComponent<TooltipTrigger>();
  59. if(tooltipTrigger == null) tooltipTrigger = gameObject.GetComponentInChildren<TooltipTrigger>();
  60. if(tooltipTrigger != null)
  61. {
  62. tooltipTrigger.header = placedObjectType.title;
  63. tooltipTrigger.content = placedObjectType.description;
  64. }
  65. var selectableTrigger = gameObject.GetComponentInChildren<SelectableObjectTrigger>();
  66. if(selectableTrigger != null) selectableTrigger.selectableObject = this;
  67. else Debug.LogError("No SelectableObjectTrigger found on placed object hierarchy", gameObject);
  68. }
  69. private void Update()
  70. {
  71. actionsControllerList[1].Start();
  72. }
  73. private void OnDestroy()
  74. {
  75. }
  76. public List<Vector2Int> GetGridPositionList()
  77. {
  78. return placedObjectType.GetGridPositionList(origin, dir);
  79. }
  80. public List<SelectableObjectAction> actions => placedObjectType.objectActions;
  81. public void ExecuteAction(SelectableObjectAction action)
  82. {
  83. for (int i = 0; i < placedObjectType.objectActions.Count; i++)
  84. {
  85. if(action == placedObjectType.objectActions[i])
  86. {
  87. if(actionsControllerList[3 + i] != null)
  88. {
  89. if(actionsControllerList[3 + i].started) actionsControllerList[3 + i].Restart();
  90. actionsControllerList[3 + i].Start();
  91. }
  92. else Debug.LogError($"Could not find GameActionController for \"{action.title} in {gameObject.name} PlacedObject", this.gameObject);
  93. }
  94. }
  95. }
  96. public void DestroySelf()
  97. {
  98. actionsControllerList[2].Start();
  99. Timer.Execute(destroyDelay, () => {
  100. Destroy(gameObject);
  101. });
  102. }
  103. public bool HasConnection(string connector, int x, int y)
  104. {
  105. for (int i = 0; i < placedObjectType.connectors.Count; i++)
  106. {
  107. Vector2Int pos = origin;
  108. pos = pos + placedObjectType.GetConnectorPositionOffset(i, dir);
  109. //Debug.Log($"{placedObjectType.connectors[i].position} - {placedObjectType.GetConnectorPositionOffset(i, dir)}");
  110. Vector2Int targetPos = pos + placedObjectType.GetConnectorTargetPositionOffset(i, dir);
  111. //Debug.Log($"Checking connector {pos} -->{targetPos} | {placedObjectType.connectors[i].dir.ToString()}");
  112. if(targetPos.x == x && targetPos.y == y && connector == placedObjectType.connectors[i].title) return true;
  113. }
  114. return false;
  115. }
  116. }
  117. }