|
- using System;
- using System.Threading;
- using UnityEngine;
-
- namespace SUISSEngine
- {
- public class GridTile : MonoBehaviour
- {
- public enum Status
- {
- None,
- Preview,
- Moving,
- Created,
- Destroyed
- }
-
- public int gridSizeU;
-
- public int gridSizeV;
-
- public bool canMirror = true;
-
- public bool canHide = true;
-
- public Vector3 offset = Vector3.zero;
-
- public int requiredGridType = 3;
-
- [SelfReference]
- public SpriteRenderer spriteRenderer;
-
- [ChildReference(true)]
- public SpriteRenderer bottomRenderer;
-
- public int orderOffset;
-
- public bool fixedOrder;
-
- private Status _status;
-
- private GridSize _size;
-
- private bool _mirrored;
-
- private GridIndex _index;
-
- private bool _hidden;
-
- private Renderer _renderer;
-
- private Collider2D _collider2D;
-
- public Status status
- {
- get
- {
- return _status;
- }
- set
- {
- if (value != _status)
- {
- Status status = _status;
- _status = value;
- SendMessage("OnGridTileStatusChanged", status, SendMessageOptions.DontRequireReceiver);
- }
- }
- }
-
- public GridSize size => _size;
-
- public GridSize currentSize
- {
- get
- {
- if (_mirrored)
- {
- return new GridSize(_size.v, _size.u);
- }
- return _size;
- }
- }
-
- public bool mirrored
- {
- get
- {
- return _mirrored;
- }
- set
- {
- _mirrored = value;
- }
- }
-
- public GridIndex index
- {
- get
- {
- return _index;
- }
- set
- {
- _index = value;
- }
- }
-
- public bool hidden
- {
- get
- {
- return _hidden;
- }
- set
- {
- if (!canHide)
- {
- value = false;
- }
- if (value != _hidden)
- {
- _hidden = value;
- if (_renderer != null)
- {
- _renderer.enabled = !_hidden;
- }
- if (_collider2D != null)
- {
- _collider2D.enabled = !_hidden;
- }
- SendMessage("OnGridTileHiddenChanged", SendMessageOptions.DontRequireReceiver);
- }
- }
- }
-
- public GridPoint middle
- {
- get
- {
- GridSize currentSize = this.currentSize;
- GridPoint result = default(GridPoint);
- result.u = (float)(_index.u + 1) - (float)currentSize.u * 0.5f;
- result.v = (float)(_index.v + 1) - (float)currentSize.v * 0.5f;
- return result;
- }
- }
-
- public IsometricGrid grid
- {
- get
- {
- IsometricGrid result = null;
- Transform transform = base.transform;
- while (transform != null && (result = transform.gameObject.GetComponent<IsometricGrid>()) == null)
- {
- transform = transform.parent;
- }
- return result;
- }
- }
-
- public GridElement element
- {
- get
- {
- IsometricGrid grid = this.grid;
- if (grid == null)
- {
- return null;
- }
- return grid[_index];
- }
- }
-
- public event Action<GridTile> DidUpdateTransformEvent;
-
- private void Awake()
- {
- _renderer = GetComponent<Renderer>();
- _collider2D = GetComponent<Collider2D>();
- if (gridSizeU <= 0 || gridSizeV <= 0)
- {
- throw new ArgumentException("A tile's grid sizes must be larger than 0.", (gridSizeU > 0) ? "gridSizeV" : "gridSizeU");
- }
- _status = Status.None;
- _size.u = gridSizeU;
- _size.v = gridSizeV;
- _mirrored = false;
- _index = GridIndex.invalid;
- _hidden = false;
- }
-
- public static int GetSortingOrder(GridIndex index, GridSize size)
- {
- return 4 * (index.u + index.v - size.u);
- }
-
- public void UpdateTransform()
- {
- IsometricGrid grid = this.grid;
- Vector3 localPosition = offset + (Vector3)grid[_index].Origin;
- localPosition.z = 0f;
- int num = ((!fixedOrder) ? GetSortingOrder(_index, _size) : 0) + orderOffset;
- spriteRenderer.sortingOrder = num + 1;
- if (bottomRenderer != null)
- {
- bottomRenderer.sortingOrder = 0;
- }
- Vector3 one = Vector3.one;
- if (_mirrored)
- {
- one.x = 0f - one.x;
- }
- base.transform.localPosition = localPosition;
- base.transform.localScale = one;
- if (this.DidUpdateTransformEvent != null)
- {
- this.DidUpdateTransformEvent(this);
- }
- }
- }
- }
|