PlaceableObjectRule.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. namespace KairoEngine.Grids
  6. {
  7. [System.Serializable]
  8. public class PlaceableObjectRule
  9. {
  10. public enum Rule
  11. {
  12. EmptyCell,
  13. Connection,
  14. CellOcupied,
  15. AdjacentCellOcupied,
  16. AdjacentEmptyCell,
  17. CellInRadius,
  18. EmptyArea
  19. }
  20. private string title
  21. {
  22. get
  23. {
  24. switch (rule)
  25. {
  26. case Rule.EmptyCell:
  27. return $"Empty cell";
  28. case Rule.Connection:
  29. return $"Has connector";
  30. case Rule.CellOcupied:
  31. if(targetObject != null) return $"Cell ocupied by \"{targetObject?.title}\"";
  32. else return $"Cell ocupied (ERROR)";
  33. case Rule.AdjacentCellOcupied:
  34. if(targetObject != null) return $"{objDir.ToString()} cell ocupied by \"{targetObject?.title}\"";
  35. else return $"{objDir.ToString()} cell ocupied (ERROR)";
  36. case Rule.AdjacentEmptyCell:
  37. return $"{objDir.ToString()} cell is empty";
  38. case Rule.CellInRadius:
  39. if(targetObject != null) return $"\"{targetObject?.title}\" is in a {radius} cell radius";
  40. else return $"Cell in radius (ERROR)";
  41. case Rule.EmptyArea:
  42. return $"Empty cells in a {radius} cell radius";
  43. default:
  44. return "?";
  45. }
  46. }
  47. }
  48. [SerializeField, FoldoutGroup("@title")] private Rule rule = Rule.EmptyCell;
  49. [ShowIf("@rule == Rule.CellOcupied || rule == Rule.AdjacentCellOcupied || rule == Rule.CellInRadius")]
  50. [SerializeField, FoldoutGroup("@title")] private PlacedObjectType targetObject;
  51. [ShowIf("@rule == Rule.AdjacentCellOcupied || rule == Rule.AdjacentEmptyCell")]
  52. [SerializeField, FoldoutGroup("@title")] private PlacedObjectType.Dir objDir;
  53. [ShowIf("@rule == Rule.CellInRadius || rule == Rule.EmptyArea")]
  54. [SerializeField, FoldoutGroup("@title")] private int radius = 3;
  55. [ShowIf("@rule == Rule.Connection")]
  56. [SerializeField, FoldoutGroup("@title")] private string connector = "connector name";
  57. public bool Evaluate(Grid<GridObject> grid, PlacedObjectType placedObjectType, int x, int y, PlacedObjectType.Dir dir)
  58. {
  59. switch (rule)
  60. {
  61. case Rule.EmptyCell:
  62. return IsEmptyCells(grid, placedObjectType, x, y, dir);
  63. case Rule.Connection:
  64. return HasConnection(grid, placedObjectType, x, y, dir);
  65. case Rule.CellOcupied:
  66. break;
  67. case Rule.AdjacentCellOcupied:
  68. break;
  69. case Rule.AdjacentEmptyCell:
  70. break;
  71. case Rule.CellInRadius:
  72. break;
  73. case Rule.EmptyArea:
  74. break;
  75. default:
  76. break;
  77. }
  78. return false;
  79. }
  80. public Rule GetRule() => rule;
  81. public PlacedObjectType GetTargetObject() => targetObject;
  82. public int GetRadius() => radius;
  83. public string GetConnector() => connector;
  84. public bool IsEmptyCells(Grid<GridObject> grid, PlacedObjectType placedObjectType, int x, int z, PlacedObjectType.Dir dir)
  85. {
  86. GridObject gridObject = grid.GetObject(x, z);
  87. if(gridObject == null) return false;
  88. List<Vector2Int> gridPositionList = placedObjectType.GetGridPositionList(new Vector2Int(x, z), dir);
  89. bool canBuild = true;
  90. foreach (var gridPosition in gridPositionList)
  91. {
  92. var obj = grid.GetObject(gridPosition.x, gridPosition.y);
  93. if(obj != null)
  94. {
  95. if(!grid.GetObject(gridPosition.x, gridPosition.y).CanBuild()) canBuild = false;
  96. }
  97. else canBuild = false;
  98. }
  99. return canBuild;
  100. }
  101. public bool HasConnection(Grid<GridObject> grid, PlacedObjectType placedObjectType, int x, int z, PlacedObjectType.Dir dir)
  102. {
  103. for (int i = 0; i < placedObjectType.connectors.Count; i++)
  104. {
  105. Vector2Int pos = new Vector2Int(x, z);
  106. pos = pos + placedObjectType.GetConnectorPositionOffset(i, dir);
  107. Vector2Int targetPos = pos + placedObjectType.GetConnectorTargetPositionOffset(i, dir);
  108. GridObject gridObject = grid.GetObject(targetPos.x, targetPos.y);
  109. if(gridObject == null) continue;
  110. if(gridObject.GetPlacedObject() == null) continue;
  111. if(gridObject.GetPlacedObject().HasConnection(placedObjectType.connectors[i].title, pos.x, pos.y))
  112. {
  113. //Debug.Log($"Connector found from {pos} to {targetPos}");
  114. return true;
  115. }
  116. else
  117. {
  118. //Debug.Log($"No connector found from {pos} to {targetPos}");
  119. }
  120. }
  121. return false;
  122. }
  123. }
  124. }