GridController.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Tilemaps;
  5. public class GridController : MonoBehaviour {
  6. [SerializeField] Tilemap highlightMap = null;
  7. [SerializeField] TileBase hilightTile = null;
  8. [SerializeField] TileBase hilightMovementAreaTile = null;
  9. private IAction hilightAction;
  10. public IAction HilightAction { get => hilightAction; set => hilightAction = value; }
  11. // Update is called once per frame
  12. void Update() {
  13. if (Input.GetMouseButtonDown(0) && hilightAction != null && hilightAction.HilightEnabled) {
  14. PaintMovementArea(RoundManager.GetInstance().activeCreature);
  15. Vector3Int clickedPos = GetMousePosition();
  16. highlightMap.SetTile(clickedPos, hilightTile);
  17. hilightAction.HilightEnabled = false;
  18. // TODO needs better handling.. Should be in MoveAction???
  19. YesNoDilougueUI.Instance.ShowDialog("Are you sure you want to move here?",
  20. () => { Debug.Log("YES PRESSED"); /* Confirm the move and paint the path to take. */},
  21. () => {
  22. Debug.Log("NO PRESSED");
  23. highlightMap.SetTile(clickedPos, null);
  24. hilightAction.HilightEnabled = true;
  25. });
  26. }
  27. }
  28. Vector3Int GetMousePosition() {
  29. Vector3 mousePos = Input.mousePosition;
  30. mousePos.z = -Camera.main.transform.position.z;
  31. Vector3 worldPoint = Camera.main.ScreenToWorldPoint(mousePos);
  32. return GameManagerScript.GetInstance().TileMap.WorldToCell(worldPoint);
  33. }
  34. public void PaintMovementArea(Creature toMove) {
  35. Vector3 startPos = toMove.transform.position;
  36. int movementInSquares = Mathf.FloorToInt(toMove.GetComponent<Creature>().movementRate / 5);
  37. //Physics2D.OverlapCircle(startPos); Maybe this one.. don't know
  38. Collider2D[] collider2Ds = Physics2D.OverlapCircleAll(new Vector2(startPos.x, startPos.y), movementInSquares, LayerMask.NameToLayer("Ground"));
  39. Debug.Log("Hitting " + collider2Ds.Length);
  40. }
  41. }