player movement
This commit is contained in:
parent
01a1278465
commit
46d2b7bc95
18 changed files with 1415 additions and 34 deletions
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerEntityMovement : MonoBehaviour
|
||||
|
|
@ -26,11 +27,16 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
public bool isMoving;
|
||||
|
||||
private Vector3 mouseWorldPos;
|
||||
private Vector3 mouseGridPos;
|
||||
private Camera cam;
|
||||
|
||||
private float debounceTime;
|
||||
[SerializeField] private float debounceDuration;
|
||||
|
||||
public LayerMask playerLayer;
|
||||
|
||||
private List<TileObject> currentMovableTiles = new();
|
||||
|
||||
private void Start()
|
||||
{
|
||||
cam = Camera.main;
|
||||
|
|
@ -39,16 +45,16 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
private void Update()
|
||||
{
|
||||
mouseWorldPos = cam.ScreenToWorldPoint(Input.mousePosition);
|
||||
mouseGridPos = gameplayGrid.GetCellCenterWorld(gameplayGrid.WorldToCell(mouseWorldPos));
|
||||
if (debounceTime > 0)
|
||||
{
|
||||
debounceTime -= Time.deltaTime;
|
||||
}
|
||||
if (isMoving)
|
||||
{
|
||||
templateObject.transform.position = gameplayGrid.GetCellCenterWorld(gameplayGrid.WorldToCell(mouseWorldPos));
|
||||
if (Input.GetMouseButtonDown(0) && debounceTime <= 0)
|
||||
templateObject.transform.position = mouseGridPos;
|
||||
if (Input.GetMouseButtonDown(0) && debounceTime <= 0 && SelectLocation())
|
||||
{
|
||||
SelectLocation();
|
||||
isMoving = false;
|
||||
}
|
||||
}
|
||||
|
|
@ -58,19 +64,61 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
{
|
||||
selectedEntity = entitySelected;
|
||||
isMoving = true;
|
||||
debounceTime = debounceDuration;
|
||||
debounceTime = debounceDuration; //alotta for loops here
|
||||
UncolorGrid();
|
||||
currentMovableTiles.Clear();
|
||||
foreach (TileObject tileObject in selectedEntity.currentTile.neighbors)
|
||||
{
|
||||
if (!tileObject.blocked && !tileObject.hasUnit)
|
||||
{
|
||||
currentMovableTiles.Add(tileObject);
|
||||
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.Add(neighboringTiles);
|
||||
neighboringTiles.sprite.color = Color.green;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
CreateTemplate();
|
||||
}
|
||||
|
||||
private void SelectLocation()
|
||||
private void UncolorGrid()
|
||||
{
|
||||
selectedEntity.transform.position = gameplayGrid.GetCellCenterWorld(gameplayGrid.WorldToCell(mouseWorldPos));
|
||||
foreach (TileObject tileObject in currentMovableTiles)
|
||||
{
|
||||
tileObject.sprite.color = new Color(137,137,137);
|
||||
}
|
||||
}
|
||||
|
||||
private bool SelectLocation()
|
||||
{
|
||||
TileObject selectedTile = GridManager.instance.GetTile(mouseGridPos);
|
||||
if (!selectedTile || selectedTile.blocked || selectedTile.hasUnit || !currentMovableTiles.Contains(selectedTile))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
selectedEntity.currentTile.hasUnit = false;
|
||||
selectedTile.hasUnit = true;
|
||||
selectedEntity.currentTile = selectedTile;
|
||||
selectedEntity.transform.position = mouseGridPos;
|
||||
templateObject.SetActive(false);
|
||||
UncolorGrid();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void CreateTemplate()
|
||||
{
|
||||
templateObject.SetActive(true);
|
||||
templateObject.transform.position = gameplayGrid.GetCellCenterWorld(gameplayGrid.WorldToCell(mouseWorldPos));
|
||||
templateObject.transform.position = mouseGridPos;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue