2022-05-16 15:22:02 +02:00
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class PlayerClickSystem : MonoBehaviour
|
|
|
|
|
{
|
2022-05-20 11:27:04 +02:00
|
|
|
[SerializeField] TilemapManager tilemapManager;
|
2022-05-23 15:01:14 +02:00
|
|
|
|
|
|
|
|
private BetterInput bInput;
|
|
|
|
|
private bool leftMouseClicked = false;
|
|
|
|
|
private Vector2 mousePos = Vector2.zero;
|
|
|
|
|
|
2022-05-23 19:07:14 +02:00
|
|
|
[SerializeField] Squad playerSquad;
|
|
|
|
|
public void SetPlayerSquad(Squad newSquad) { playerSquad = newSquad; } // called by Squad Manager //DEPENDENCY_INJECTION
|
|
|
|
|
|
2022-05-16 15:22:02 +02:00
|
|
|
private void Awake()
|
|
|
|
|
{
|
2022-05-23 15:01:14 +02:00
|
|
|
bInput = new BetterInput();
|
|
|
|
|
|
|
|
|
|
bInput.Main.MouseLeftClick.started += (ctx) => { leftMouseClicked = true; };
|
|
|
|
|
bInput.Main.MouseLeftClick.canceled += (ctx) => { leftMouseClicked = false; };
|
|
|
|
|
|
|
|
|
|
bInput.Main.MousePosition.performed += (ctx) => { mousePos = ctx.ReadValue<Vector2>(); };
|
|
|
|
|
|
2022-05-16 15:22:02 +02:00
|
|
|
tilemapManager = FindObjectOfType<TilemapManager>();
|
|
|
|
|
if(tilemapManager == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("Tilemap Manager not found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
2022-05-23 15:01:14 +02:00
|
|
|
if(leftMouseClicked) // Change to new input system
|
2022-05-16 15:22:02 +02:00
|
|
|
{
|
2022-05-23 15:01:14 +02:00
|
|
|
leftMouseClicked = false;
|
2022-05-20 11:30:15 +02:00
|
|
|
//Debug.Log("CLICK");
|
2022-05-16 15:22:02 +02:00
|
|
|
Camera camera = Camera.main;
|
|
|
|
|
RaycastHit hit;
|
2022-05-23 15:01:14 +02:00
|
|
|
Ray ray = camera.ScreenPointToRay(mousePos);
|
2022-05-23 17:29:47 +02:00
|
|
|
RaycastHit2D hit2D = Physics2D.GetRayIntersection(ray);
|
|
|
|
|
|
|
|
|
|
Vector3 tileCoord = new Vector3(-1, -1, -1);
|
|
|
|
|
bool hitted = false;
|
2022-05-16 15:22:02 +02:00
|
|
|
|
|
|
|
|
if (Physics.Raycast(ray, out hit))
|
|
|
|
|
{
|
2022-05-23 17:29:47 +02:00
|
|
|
tileCoord = hit.point;
|
|
|
|
|
hitted = true;
|
|
|
|
|
}
|
|
|
|
|
else if (hit2D.collider != null)
|
|
|
|
|
{
|
|
|
|
|
tileCoord = hit2D.point;
|
|
|
|
|
hitted = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hitted)
|
|
|
|
|
{
|
|
|
|
|
Vector3 hitWorldPosition = tileCoord;
|
2022-05-16 15:22:02 +02:00
|
|
|
|
2022-05-17 13:37:02 +02:00
|
|
|
Debug.Log($"Hit at {hitWorldPosition}");
|
2022-05-16 15:22:02 +02:00
|
|
|
TilemapManager.Tile selectedTile;
|
|
|
|
|
int x, y;
|
2022-05-17 13:37:02 +02:00
|
|
|
TilemapManager.TileState tileState = tilemapManager.GetTileFromWorldCoords(hitWorldPosition, out selectedTile, out x, out y);
|
2022-05-16 15:22:02 +02:00
|
|
|
|
|
|
|
|
if(tileState == TilemapManager.TileState.outOfBounds)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"No tile was hit (hit \"coords\" {x},{y})");
|
|
|
|
|
return; // for now do nothing if a tile was not selected
|
|
|
|
|
}
|
|
|
|
|
// a tile was hit
|
|
|
|
|
Debug.Log($"Tile {x},{y} was selected - DO MOVEMENT ORDER");
|
2022-05-23 19:07:14 +02:00
|
|
|
playerSquad.EnqueueOrder(new Squad.MovementOrder(new Vector2Int(x,y)));
|
|
|
|
|
|
|
|
|
|
|
2022-05-16 15:22:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-23 15:01:14 +02:00
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
bInput.Main.Enable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
bInput.Main.Disable();
|
|
|
|
|
}
|
2022-05-16 15:22:02 +02:00
|
|
|
}
|