152 lines
4.6 KiB
C#
152 lines
4.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PlayerEntityMovement : MonoBehaviour
|
|
{
|
|
//unsure if this should also move enemies but who knows!
|
|
#region Statication
|
|
|
|
public static PlayerEntityMovement instance;
|
|
|
|
private void Awake()
|
|
{
|
|
if (instance != null && instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
instance = this;
|
|
}
|
|
|
|
#endregion
|
|
[SerializeField] private Grid gameplayGrid;
|
|
|
|
public Entity selectedEntity;
|
|
public GameObject templateObject;
|
|
public bool isMoving;
|
|
|
|
private Vector3 mouseWorldPos;
|
|
private Vector3 mouseGridPos;
|
|
private Camera cam;
|
|
|
|
private float debounceTime;
|
|
[SerializeField] private float debounceDuration;
|
|
|
|
public LayerMask playerLayer;
|
|
|
|
private Queue<TileObject> currentMovableTiles = new();
|
|
private Dictionary<TileObject, TileObject> paths = new();
|
|
private List<TileObject> currentPath = new();
|
|
private TileObject requestedTile;
|
|
|
|
private void Start()
|
|
{
|
|
cam = Camera.main;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
mouseWorldPos = cam.ScreenToWorldPoint(Input.mousePosition);
|
|
mouseGridPos = gameplayGrid.GetCellCenterWorld(gameplayGrid.WorldToCell(mouseWorldPos));
|
|
if (debounceTime > 0)
|
|
{
|
|
debounceTime -= Time.deltaTime;
|
|
}
|
|
if (isMoving)
|
|
{
|
|
TileObject newTile = GridManager.instance.GetTile(mouseGridPos);
|
|
if (requestedTile != newTile)
|
|
{
|
|
requestedTile = newTile;
|
|
foreach (TileObject tile in currentPath)
|
|
{
|
|
tile.sprite.color = Color.green;
|
|
}
|
|
currentPath.Clear();
|
|
if (currentMovableTiles.Contains(requestedTile))
|
|
{
|
|
TileObject currentTile = requestedTile;
|
|
while (currentTile != selectedEntity.currentTile)
|
|
{
|
|
currentPath.Add(currentTile);
|
|
currentTile = paths[currentTile];
|
|
}
|
|
foreach (TileObject tile in currentPath)
|
|
{
|
|
tile.sprite.color = Color.blue;
|
|
}
|
|
}
|
|
}
|
|
templateObject.transform.position = mouseGridPos;
|
|
if (Input.GetMouseButtonDown(0) && debounceTime <= 0 && SelectLocation())
|
|
{
|
|
isMoving = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SelectEntity(Entity entitySelected)
|
|
{
|
|
selectedEntity = entitySelected;
|
|
isMoving = true;
|
|
debounceTime = debounceDuration; //alotta for loops here
|
|
UncolorGrid();
|
|
currentMovableTiles.Clear();
|
|
paths.Clear();
|
|
foreach (TileObject tileObject in selectedEntity.currentTile.neighbors)
|
|
{
|
|
if (!tileObject.blocked && !tileObject.hasUnit)
|
|
{
|
|
currentMovableTiles.Enqueue(tileObject);
|
|
paths.Add(tileObject, selectedEntity.currentTile);
|
|
tileObject.sprite.color = Color.green;
|
|
}
|
|
}
|
|
for (int i = 0; i < selectedEntity.maxMovement - 1; i++)
|
|
{
|
|
foreach (TileObject tileObject in currentMovableTiles.ToArray())
|
|
{
|
|
foreach (TileObject neighboringTiles in tileObject.neighbors)
|
|
{
|
|
if (!neighboringTiles.blocked && !neighboringTiles.hasUnit)
|
|
{
|
|
currentMovableTiles.Enqueue(neighboringTiles);
|
|
neighboringTiles.sprite.color = Color.green;
|
|
paths.TryAdd(neighboringTiles, tileObject);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
CreateTemplate();
|
|
}
|
|
|
|
private void UncolorGrid()
|
|
{
|
|
foreach (TileObject tileObject in currentMovableTiles)
|
|
{
|
|
tileObject.sprite.color = new Color(137,137,137);
|
|
}
|
|
}
|
|
|
|
private bool SelectLocation()
|
|
{
|
|
if (!requestedTile || requestedTile.blocked || requestedTile.hasUnit || !currentMovableTiles.Contains(requestedTile))
|
|
{
|
|
return false;
|
|
}
|
|
selectedEntity.currentTile.hasUnit = false;
|
|
requestedTile.hasUnit = true;
|
|
selectedEntity.currentTile = requestedTile;
|
|
selectedEntity.transform.position = mouseGridPos;
|
|
templateObject.SetActive(false);
|
|
UncolorGrid();
|
|
return true;
|
|
}
|
|
|
|
private void CreateTemplate()
|
|
{
|
|
templateObject.SetActive(true);
|
|
templateObject.transform.position = mouseGridPos;
|
|
}
|
|
}
|