PlacedObjectType.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. using KairoEngine.Core.GameActions;
  6. using KairoEngine.Stockpiles;
  7. using Sirenix.OdinInspector;
  8. using KairoEngine.GameTools.Selectables;
  9. using KairoEngine.SFX;
  10. namespace KairoEngine.Grids
  11. {
  12. [CreateAssetMenu(fileName = "PlaceObjectType", menuName = "KairoEngine/BuildingSystem/PlaceObjectType")]
  13. [HideMonoScript]
  14. public class PlacedObjectType : ScriptableObject
  15. {
  16. public static Dir GetNextDir(Dir dir)
  17. {
  18. switch (dir)
  19. {
  20. default:
  21. case Dir.Down: return Dir.Left;
  22. case Dir.Left: return Dir.Up;
  23. case Dir.Up: return Dir.Right;
  24. case Dir.Right: return Dir.Down;
  25. }
  26. }
  27. public enum Dir
  28. {
  29. Down,
  30. Up,
  31. Left,
  32. Right
  33. }
  34. [BoxGroup("Properties", showLabel: false)] public string title;
  35. [BoxGroup("Properties")] public string category;
  36. [BoxGroup("Properties")] public Transform prefab;
  37. [BoxGroup("Properties")] public Transform visual;
  38. [BoxGroup("Properties")] public Sprite image;
  39. [BoxGroup("Properties")] public Sprite icon;
  40. [BoxGroup("Properties")] public SFXClip createdSound;
  41. [BoxGroup("Properties")] public SFXClip removedSound;
  42. [BoxGroup("Properties")] public bool isDestructable = true;
  43. [BoxGroup("Properties"), HorizontalGroup("Properties/size"), LabelText("Width/Height")] public int width;
  44. [BoxGroup("Properties"), HorizontalGroup("Properties/size", 0.3f), HideLabel] public int height;
  45. [BoxGroup("Properties"), TextArea(2, 8), HideLabel, PropertySpace(4, 4)] public string description;
  46. [PropertySpace(1, 1)] public List<KairoEngine.Stockpiles.Stockpile> cost = new List<KairoEngine.Stockpiles.Stockpile>();
  47. [PropertySpace(1, 1)] public List<PlaceableObjectConnector> connectors = new List<PlaceableObjectConnector>();
  48. [PropertySpace(1, 1)] public List<BuildingEffectArea> effectAreas = new List<BuildingEffectArea>();
  49. [FoldoutGroup("Rules"), PropertySpace(2, 2)]
  50. [InfoBox("Rules for positioning the building in the game world", InfoMessageType.Info)]
  51. public ConditionType rulesetCondition;
  52. [ListDrawerSettings(HideAddButton = false, HideRemoveButton = false, DraggableItems = false, Expanded = true, ShowPaging = false, ShowItemCount = true)]
  53. [PropertySpace(2, 2)]
  54. [FoldoutGroup("Rules")]
  55. public List<PlaceableObjectRuleGroup> ruleset = new List<PlaceableObjectRuleGroup>();
  56. [FoldoutGroup("Functionality")]
  57. [HideLabel, InlineProperty, OnInspectorInit("SetupContext"), PropertySpace(2, 4)]
  58. public GameActionContext context = new GameActionContext();
  59. [FoldoutGroup("Functionality")]
  60. [TabGroup("Functionality/Triggers", "On Action"), PropertySpace(1, 2)]
  61. [InfoBox("Actions that can be triggered by the player or game events", InfoMessageType.Info)]
  62. [LabelText("Actions")]
  63. public List<SelectableObjectAction> objectActions;
  64. [FoldoutGroup("Functionality")]
  65. [TabGroup("Functionality/Triggers", "On Create"), InlineProperty, HideLabel, PropertySpace(1, 2)]
  66. [InfoBox("Execute actions when the building is placed", InfoMessageType.Info)]
  67. public GameActionsController onCreateController;
  68. [FoldoutGroup("Functionality")]
  69. [TabGroup("Functionality/Triggers", "On Update"), InlineProperty, HideLabel, PropertySpace(1, 2)]
  70. [InfoBox("Executes every frame while the building is active", InfoMessageType.Info)]
  71. public GameActionsController onUpdateController;
  72. [FoldoutGroup("Functionality")]
  73. [TabGroup("Functionality/Triggers", "On Remove"), InlineProperty, HideLabel, PropertySpace(1, 2)]
  74. [InfoBox("Executes when the building is removed", InfoMessageType.Info)]
  75. public GameActionsController onRemoveController;
  76. public int GetRotationAngle(Dir dir)
  77. {
  78. switch (dir)
  79. {
  80. default:
  81. case Dir.Down: return 0;
  82. case Dir.Left: return 90;
  83. case Dir.Up: return 180;
  84. case Dir.Right: return 270;
  85. }
  86. }
  87. public Vector2Int GetRotationOffset(Dir dir)
  88. {
  89. switch (dir)
  90. {
  91. default:
  92. case Dir.Down: return new Vector2Int(0, 0);
  93. case Dir.Left: return new Vector2Int(0, width);
  94. case Dir.Up: return new Vector2Int(width, height);
  95. case Dir.Right: return new Vector2Int(height, 0);
  96. }
  97. }
  98. public Vector2Int GetRotationOffset(Dir dir, int width, int height)
  99. {
  100. switch (dir)
  101. {
  102. default:
  103. case Dir.Down: return new Vector2Int(0, 0);
  104. case Dir.Left: return new Vector2Int(0, width);
  105. case Dir.Up: return new Vector2Int(width, height);
  106. case Dir.Right: return new Vector2Int(height, 0);
  107. }
  108. }
  109. public List<Vector2Int> GetGridPositionList(Vector2Int offset, Dir dir)
  110. {
  111. List<Vector2Int> gridPositionList = new List<Vector2Int>();
  112. switch (dir)
  113. {
  114. default:
  115. case Dir.Down:
  116. case Dir.Up:
  117. for(int x = 0; x < width; x++)
  118. {
  119. for (int y = 0; y < height; y++)
  120. {
  121. gridPositionList.Add(offset + new Vector2Int(x, y));
  122. }
  123. }
  124. break;
  125. case Dir.Left:
  126. case Dir.Right:
  127. for (int x = 0; x < height; x++)
  128. {
  129. for (int y = 0; y < width; y++)
  130. {
  131. gridPositionList.Add(offset + new Vector2Int(x, y));
  132. }
  133. }
  134. break;
  135. }
  136. return gridPositionList;
  137. }
  138. public bool CanPlaceObject(Grid<GridObject> grid, Vector3 worldPos, PlacedObjectType.Dir dir)
  139. {
  140. grid.GetGridPosition(worldPos, out int x, out int y);
  141. return CanPlaceObject(grid, x, y, dir);
  142. }
  143. public bool CanPlaceObject(Grid<GridObject> grid, int x, int y, PlacedObjectType.Dir dir)
  144. {
  145. return EvaluateRuleset(grid, x, y, dir);
  146. }
  147. public bool EvaluateRuleset(Grid<GridObject> grid, int x, int y, PlacedObjectType.Dir dir)
  148. {
  149. List<bool> results = new List<bool>();
  150. for (int i = 0; i < ruleset.Count; i++)
  151. {
  152. results.Add(ruleset[i].Evaluate(grid, this, x, y, dir));
  153. }
  154. if(rulesetCondition == ConditionType.AND)
  155. {
  156. foreach (var value in results)
  157. {
  158. if(value == false) return false;
  159. }
  160. return true;
  161. }
  162. else
  163. {
  164. foreach (var value in results)
  165. {
  166. if(value == true) return true;
  167. }
  168. return false;
  169. }
  170. }
  171. public List<PlaceableObjectRule> GetRulesetError(Grid<GridObject> grid, int x, int y, PlacedObjectType.Dir dir)
  172. {
  173. List<PlaceableObjectRule> brokenRules = new List<PlaceableObjectRule>();
  174. for (int i = 0; i < ruleset.Count; i++)
  175. {
  176. for (int a = 0; a < ruleset[i].rules.Count; a++)
  177. {
  178. bool result = ruleset[i].rules[a].Evaluate(grid, this, x, y, dir);
  179. if(result == false) brokenRules.Add(ruleset[i].rules[a]);
  180. }
  181. }
  182. return brokenRules;
  183. }
  184. public Vector2Int GetConnectorPositionOffset(int index, PlacedObjectType.Dir dir)
  185. {
  186. PlaceableObjectConnector connector = connectors[index];
  187. switch (dir)
  188. {
  189. default:
  190. case Dir.Down: return connector.position;
  191. case Dir.Left: return new Vector2Int(connector.position.y, (connector.position.x * -1) + width - 1);
  192. case Dir.Up: return new Vector2Int((connector.position.x * -1) + width - 1, (connector.position.y * -1) + height - 1);
  193. case Dir.Right: return new Vector2Int((connector.position.y * -1) + height - 1, connector.position.x);
  194. }
  195. }
  196. public Vector2Int GetConnectorTargetPositionOffset(int index, PlacedObjectType.Dir objectDir)
  197. {
  198. var targetDir = connectors[index].dir;
  199. return GetPositionOffsetFromDirection(objectDir, targetDir);
  200. }
  201. public Vector2Int GetPositionOffsetFromDirection(PlacedObjectType.Dir objectDir, PlacedObjectType.Dir targetDir)
  202. {
  203. switch (objectDir)
  204. {
  205. default:
  206. case Dir.Down:
  207. switch (targetDir)
  208. {
  209. default:
  210. case Dir.Down: return new Vector2Int(0, -1);
  211. case Dir.Left: return new Vector2Int(-1, 0);
  212. case Dir.Up: return new Vector2Int(0, 1);
  213. case Dir.Right: return new Vector2Int(1, 0);
  214. }
  215. case Dir.Left:
  216. switch (targetDir)
  217. {
  218. default:
  219. case Dir.Down: return new Vector2Int(-1, 0);
  220. case Dir.Left: return new Vector2Int(0, 1);
  221. case Dir.Up: return new Vector2Int(1, 0);
  222. case Dir.Right: return new Vector2Int(0, -1);
  223. }
  224. case Dir.Up:
  225. switch (targetDir)
  226. {
  227. default:
  228. case Dir.Down: return new Vector2Int(0, 1);
  229. case Dir.Left: return new Vector2Int(1, 0);
  230. case Dir.Up: return new Vector2Int(0, -1);
  231. case Dir.Right: return new Vector2Int(-1, 0);
  232. }
  233. case Dir.Right:
  234. switch (targetDir)
  235. {
  236. default:
  237. case Dir.Down: return new Vector2Int(1, 0);
  238. case Dir.Left: return new Vector2Int(0, -1);
  239. case Dir.Up: return new Vector2Int(-1, 0);
  240. case Dir.Right: return new Vector2Int(0, 1);
  241. }
  242. }
  243. }
  244. private void SetupContext()
  245. {
  246. if(context == null) return;
  247. onCreateController.context = context;
  248. onUpdateController.context = context;
  249. onRemoveController.context = context;
  250. for (int i = 0; i < objectActions.Count; i++)
  251. {
  252. if(objectActions[i] != null) objectActions[i].actions.context = context;
  253. }
  254. if(!context.HasVariable("Building GameObject"))
  255. {
  256. var variable = new GameActionContextGameObject();
  257. variable.name = "Building GameObject";
  258. variable.value = null;
  259. variable.canEdit = false;
  260. context.variables.Add(variable);
  261. }
  262. }
  263. public Vector2Int OffsetValue(Vector2Int offset, Dir dir)
  264. {
  265. switch (dir)
  266. {
  267. default:
  268. case Dir.Down: return new Vector2Int(offset.x, offset.y);
  269. case Dir.Left: return new Vector2Int(offset.y, offset.x * -1);
  270. case Dir.Up: return new Vector2Int(offset.x * -1, offset.y * -1);
  271. case Dir.Right: return new Vector2Int(offset.y * -1, offset.x);
  272. }
  273. }
  274. }
  275. }