You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

224 lines
5.7 KiB

  1. using System;
  2. using System.Threading;
  3. using UnityEngine;
  4. namespace SUISSEngine
  5. {
  6. public class GridTile : MonoBehaviour
  7. {
  8. public enum Status
  9. {
  10. None,
  11. Preview,
  12. Moving,
  13. Created,
  14. Destroyed
  15. }
  16. public int gridSizeU;
  17. public int gridSizeV;
  18. public bool canMirror = true;
  19. public bool canHide = true;
  20. public Vector3 offset = Vector3.zero;
  21. public int requiredGridType = 3;
  22. [SelfReference]
  23. public SpriteRenderer spriteRenderer;
  24. [ChildReference(true)]
  25. public SpriteRenderer bottomRenderer;
  26. public int orderOffset;
  27. public bool fixedOrder;
  28. private Status _status;
  29. private GridSize _size;
  30. private bool _mirrored;
  31. private GridIndex _index;
  32. private bool _hidden;
  33. private Renderer _renderer;
  34. private Collider2D _collider2D;
  35. public Status status
  36. {
  37. get
  38. {
  39. return _status;
  40. }
  41. set
  42. {
  43. if (value != _status)
  44. {
  45. Status status = _status;
  46. _status = value;
  47. SendMessage("OnGridTileStatusChanged", status, SendMessageOptions.DontRequireReceiver);
  48. }
  49. }
  50. }
  51. public GridSize size => _size;
  52. public GridSize currentSize
  53. {
  54. get
  55. {
  56. if (_mirrored)
  57. {
  58. return new GridSize(_size.v, _size.u);
  59. }
  60. return _size;
  61. }
  62. }
  63. public bool mirrored
  64. {
  65. get
  66. {
  67. return _mirrored;
  68. }
  69. set
  70. {
  71. _mirrored = value;
  72. }
  73. }
  74. public GridIndex index
  75. {
  76. get
  77. {
  78. return _index;
  79. }
  80. set
  81. {
  82. _index = value;
  83. }
  84. }
  85. public bool hidden
  86. {
  87. get
  88. {
  89. return _hidden;
  90. }
  91. set
  92. {
  93. if (!canHide)
  94. {
  95. value = false;
  96. }
  97. if (value != _hidden)
  98. {
  99. _hidden = value;
  100. if (_renderer != null)
  101. {
  102. _renderer.enabled = !_hidden;
  103. }
  104. if (_collider2D != null)
  105. {
  106. _collider2D.enabled = !_hidden;
  107. }
  108. SendMessage("OnGridTileHiddenChanged", SendMessageOptions.DontRequireReceiver);
  109. }
  110. }
  111. }
  112. public GridPoint middle
  113. {
  114. get
  115. {
  116. GridSize currentSize = this.currentSize;
  117. GridPoint result = default(GridPoint);
  118. result.u = (float)(_index.u + 1) - (float)currentSize.u * 0.5f;
  119. result.v = (float)(_index.v + 1) - (float)currentSize.v * 0.5f;
  120. return result;
  121. }
  122. }
  123. public IsometricGrid grid
  124. {
  125. get
  126. {
  127. IsometricGrid result = null;
  128. Transform transform = base.transform;
  129. while (transform != null && (result = transform.gameObject.GetComponent<IsometricGrid>()) == null)
  130. {
  131. transform = transform.parent;
  132. }
  133. return result;
  134. }
  135. }
  136. public GridElement element
  137. {
  138. get
  139. {
  140. IsometricGrid grid = this.grid;
  141. if (grid == null)
  142. {
  143. return null;
  144. }
  145. return grid[_index];
  146. }
  147. }
  148. public event Action<GridTile> DidUpdateTransformEvent;
  149. private void Awake()
  150. {
  151. _renderer = GetComponent<Renderer>();
  152. _collider2D = GetComponent<Collider2D>();
  153. if (gridSizeU <= 0 || gridSizeV <= 0)
  154. {
  155. throw new ArgumentException("A tile's grid sizes must be larger than 0.", (gridSizeU > 0) ? "gridSizeV" : "gridSizeU");
  156. }
  157. _status = Status.None;
  158. _size.u = gridSizeU;
  159. _size.v = gridSizeV;
  160. _mirrored = false;
  161. _index = GridIndex.invalid;
  162. _hidden = false;
  163. }
  164. public static int GetSortingOrder(GridIndex index, GridSize size)
  165. {
  166. return 4 * (index.u + index.v - size.u);
  167. }
  168. public void UpdateTransform()
  169. {
  170. IsometricGrid grid = this.grid;
  171. Vector3 localPosition = offset + (Vector3)grid[_index].Origin;
  172. localPosition.z = 0f;
  173. int num = ((!fixedOrder) ? GetSortingOrder(_index, _size) : 0) + orderOffset;
  174. spriteRenderer.sortingOrder = num + 1;
  175. if (bottomRenderer != null)
  176. {
  177. bottomRenderer.sortingOrder = 0;
  178. }
  179. Vector3 one = Vector3.one;
  180. if (_mirrored)
  181. {
  182. one.x = 0f - one.x;
  183. }
  184. base.transform.localPosition = localPosition;
  185. base.transform.localScale = one;
  186. if (this.DidUpdateTransformEvent != null)
  187. {
  188. this.DidUpdateTransformEvent(this);
  189. }
  190. }
  191. }
  192. }