player movement

This commit is contained in:
reisenlol 2026-01-04 01:44:17 -08:00
parent 01a1278465
commit 46d2b7bc95
No known key found for this signature in database
18 changed files with 1415 additions and 34 deletions

33
Assets/Scripts/Enemy.cs Normal file
View file

@ -0,0 +1,33 @@
using UnityEngine;
public class Enemy : Entity
{
public int turnSpeed;
public int minimumAttackRange;
private PlayerEntity closestPlayer;
void Start()
{
turnSpeed = Random.Range(0, 100);
}
public void StartTurn()
{
//attack the nearest player?
TurnHandler.instance.UpdateTurns();
closestPlayer = FindClosestPlayer();
}
private PlayerEntity FindClosestPlayer()
{
PlayerEntity currentClosestPlayer = null;
foreach (PlayerEntity player in TurnHandler.instance.playerEntities)
{
if (!currentClosestPlayer || Vector3.Distance(player.transform.position, transform.position) < Vector3.Distance(currentClosestPlayer.transform.position, transform.position))
{
currentClosestPlayer = player;
}
}
return currentClosestPlayer;
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 441b69580fe55f47ead9c7266bb4d363

View file

@ -9,9 +9,10 @@ public class Entity : MonoBehaviour
[Header("Movement")]
public int maxMovement;
public TileObject currentTile;
[Header("Flags")]
public bool canMove;
public bool canMove = true;
public bool invincible;
public void TakeDamage(float damage)
@ -33,9 +34,4 @@ public class Entity : MonoBehaviour
{
}
private void OnMouseDown()
{
PlayerEntityMovement.instance.SelectEntity(this);
}
}

View file

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class GridManager : MonoBehaviour
{
#region Statication
public static GridManager instance;
private void Awake()
{
if (instance != null && instance != this)
{
Destroy(gameObject);
return;
}
instance = this;
}
#endregion
[SerializeField] private int width;
[SerializeField] private int height;
[SerializeField] private TileObject tilePrefab;
[SerializeField] Camera cam;
private Dictionary<Vector2, TileObject> allTiles = new();
private void Start()
{
GenerateGrid();
cam.transform.position = new Vector3((float)width / 2, (float)height / 2, 0);
foreach (PlayerEntity player in TurnHandler.instance.playerEntities)
{
player.currentTile = GetTile(player.transform.position);
}
foreach (Enemy enemy in TurnHandler.instance.enemyEntities)
{
enemy.currentTile = GetTile(enemy.transform.position);
}
}
private void GenerateGrid()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Vector2 pos = new Vector2(x - ((float)width / 2) + 0.5f, y - ((float)height / 2) + 0.5f);
TileObject newTile = Instantiate(tilePrefab, pos, Quaternion.identity);
newTile.name = $"Tile {x} {y}";
allTiles[pos] = newTile;
}
}
GetTileNeighbors();
}
private void GetTileNeighbors()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Vector2 pos = new Vector2(x - ((float)width / 2) + 0.5f, y - ((float)height / 2) + 0.5f);
TileObject currentTile = allTiles[pos];
if (GetTile(pos + Vector2.up)) { currentTile.neighbors.Add(GetTile(pos+Vector2.up));}
if (GetTile(pos + Vector2.down)) { currentTile.neighbors.Add(GetTile(pos+Vector2.down));}
if (GetTile(pos + Vector2.left)) { currentTile.neighbors.Add(GetTile(pos+Vector2.left));}
if (GetTile(pos + Vector2.right)) { currentTile.neighbors.Add(GetTile(pos+Vector2.right));}
}
}
}
public TileObject GetTile(Vector2 pos)
{
if (allTiles.TryGetValue(pos, out TileObject tile))
{
return tile;
}
return null;
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4edc7f2a8c49b96c98785f81b8172eaf

View file

@ -0,0 +1,34 @@
using System.Collections.Generic;
using UnityEngine;
public class PlayerEntity : Entity
{
public bool hasMoved = false;
public bool hasAttacked = false;
private List<TileObject> moveableTiles = new();
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void SkipTurn()
{
hasMoved = true;
hasAttacked = true;
TurnHandler.instance.UpdateTurns();
}
private void OnMouseDown()
{
if (!hasMoved && !PlayerEntityMovement.instance.isMoving)
{
PlayerEntityMovement.instance.SelectEntity(this);
SkipTurn();
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 87b2f3acfe362ceb498b8129365d99a9

View file

@ -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;
}
}

View file

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using UnityEngine;
public class TileObject : MonoBehaviour
{
[SerializeField] private GameObject highlight;
public List<TileObject> neighbors = new();
public SpriteRenderer sprite;
[Header("Status")]
public bool blocked = false;
public bool hasUnit = false;
private void OnMouseEnter()
{
//highlight.SetActive(true);
}
private void OnMouseExit()
{
//highlight.SetActive(false);
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a693893494730262783d7c528fec97bb

View file

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class TurnHandler : MonoBehaviour
{
#region Statication
public static TurnHandler instance;
private void Awake()
{
if (instance != null && instance != this)
{
Destroy(gameObject);
return;
}
instance = this;
}
#endregion
public List<PlayerEntity> playerEntities = new();
public List<Enemy> enemyEntities = new();
public enum GameState {PlayerTurn, EnemyTurn}
public GameState currentGameState = GameState.PlayerTurn;
private int currentEnemy;
private void SortEnemies()
{
enemyEntities = enemyEntities.OrderBy(o=>o.turnSpeed).ToList();
}
private void Start()
{
SortEnemies();
}
public void UpdateTurns()
{
if (currentGameState == GameState.PlayerTurn)
{
bool allDone = true;
foreach (PlayerEntity player in playerEntities)
{
if (!player.hasMoved || !player.hasAttacked)
{
allDone = false;
}
}
if (allDone)
{
foreach (PlayerEntity player in playerEntities)
{
player.hasMoved = false;
player.hasAttacked = false;
}
//currentGameState = GameState.EnemyTurn;
}
}
else if (currentGameState == GameState.EnemyTurn)
{
enemyEntities[currentEnemy].StartTurn();
currentEnemy++;
if (currentEnemy > enemyEntities.Count)
{
currentGameState = GameState.PlayerTurn;
foreach (PlayerEntity player in playerEntities)
{
player.hasMoved = false;
player.hasAttacked = false;
}
}
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b500f9191e72a9dfa887fcf64a93e315