finally get pathfinding done?
This commit is contained in:
parent
7b151c1a53
commit
7bd61df481
23 changed files with 1408 additions and 785 deletions
|
|
@ -1,4 +1,5 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ActionUIHandler : MonoBehaviour
|
||||
{
|
||||
|
|
@ -17,14 +18,28 @@ public class ActionUIHandler : MonoBehaviour
|
|||
}
|
||||
|
||||
#endregion
|
||||
private PlayerEntity selectedEntity;
|
||||
private RangedWeapon possibleRanged;
|
||||
[Header("UI")]
|
||||
[SerializeField] private Vector3 offset;
|
||||
[SerializeField] private GameObject actionUI;
|
||||
private Entity selectedEntity;
|
||||
public void ShowUI(Entity target)
|
||||
[SerializeField] private Button attackButton;
|
||||
[SerializeField] private Button reloadButton;
|
||||
|
||||
public void ShowUI(PlayerEntity target)
|
||||
{
|
||||
possibleRanged = null;
|
||||
selectedEntity = target;
|
||||
transform.position = Input.mousePosition + offset;
|
||||
if (target.currentWeapon.TryGetComponent(out RangedWeapon isRanged))
|
||||
{
|
||||
possibleRanged = isRanged;
|
||||
reloadButton.gameObject.SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
reloadButton.gameObject.SetActive(false);
|
||||
}
|
||||
actionUI.SetActive(true);
|
||||
}
|
||||
public void HideUI()
|
||||
|
|
@ -32,6 +47,20 @@ public class ActionUIHandler : MonoBehaviour
|
|||
actionUI.SetActive(false);
|
||||
}
|
||||
|
||||
public void ReloadGun()
|
||||
{
|
||||
possibleRanged.Reload();
|
||||
}
|
||||
|
||||
public void Attack()
|
||||
{
|
||||
selectedEntity.Attack();
|
||||
}
|
||||
|
||||
public void SkipTurn()
|
||||
{
|
||||
selectedEntity.SkipTurn();
|
||||
}
|
||||
public void MoveEntity()
|
||||
{
|
||||
HideUI();
|
||||
|
|
|
|||
10
Assets/Scripts/MeleeWeapon.cs
Normal file
10
Assets/Scripts/MeleeWeapon.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class MeleeWeapon : MonoBehaviour
|
||||
{
|
||||
|
||||
public virtual void AttackEffects()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/MeleeWeapon.cs.meta
Normal file
2
Assets/Scripts/MeleeWeapon.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6f675ec09a8210538b466e213a2c2bba
|
||||
|
|
@ -6,9 +6,19 @@ public class PlayerEntity : Entity
|
|||
public bool hasMoved = false;
|
||||
public bool hasAttacked = false;
|
||||
private List<TileObject> moveableTiles = new();
|
||||
[SerializeField] private Weapon[] weapons;
|
||||
private List<Weapon> weaponInstances = new();
|
||||
public Weapon currentWeapon;
|
||||
void Start()
|
||||
{
|
||||
|
||||
foreach (Weapon weapon in weapons)
|
||||
{
|
||||
Weapon newWeapon = Instantiate(weapon, transform);
|
||||
newWeapon.thisEntity = this;
|
||||
newWeapon.tag = tag;
|
||||
weaponInstances.Add(newWeapon);
|
||||
}
|
||||
currentWeapon = weaponInstances[0];
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
|
|
@ -22,13 +32,21 @@ public class PlayerEntity : Entity
|
|||
hasMoved = true;
|
||||
hasAttacked = true;
|
||||
TurnHandler.instance.UpdateTurns();
|
||||
}
|
||||
public void Attack()
|
||||
{
|
||||
currentWeapon.TryAttack();
|
||||
}
|
||||
|
||||
public void SwitchWeapon()
|
||||
{
|
||||
|
||||
}
|
||||
private void OnMouseDown()
|
||||
{
|
||||
if ((!hasMoved) && !PlayerEntityMovement.instance.isMoving)
|
||||
{
|
||||
ActionUIHandler.instance.ShowUI(this);
|
||||
SkipTurn();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
#endregion
|
||||
[SerializeField] private Grid gameplayGrid;
|
||||
|
||||
public Entity selectedEntity;
|
||||
public PlayerEntity selectedEntity;
|
||||
public GameObject templateObject;
|
||||
public bool isMoving;
|
||||
|
||||
|
|
@ -37,6 +37,7 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
private Queue<TileObject> frontier = new();
|
||||
private HashSet<TileObject> visited = new();
|
||||
private Dictionary<TileObject, TileObject> currentPaths = new();
|
||||
private Dictionary<TileObject, int> movementCosts = new();
|
||||
public List<TileObject> pathRequested;
|
||||
private TileObject requestedTile;
|
||||
|
||||
|
|
@ -77,7 +78,7 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
}
|
||||
}
|
||||
|
||||
public void SelectEntity(Entity entitySelected)
|
||||
public void SelectEntity(PlayerEntity entitySelected)
|
||||
{
|
||||
selectedEntity = entitySelected;
|
||||
isMoving = true;
|
||||
|
|
@ -87,8 +88,9 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
visited.Clear();
|
||||
currentPaths.Clear();
|
||||
pathRequested.Clear();
|
||||
currentMovableTiles.Clear();
|
||||
movementCosts.Clear();
|
||||
GetAllTiles();
|
||||
GetTilesInRange();
|
||||
CreateTemplate();
|
||||
}
|
||||
|
||||
|
|
@ -103,19 +105,31 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
private void GetAllTiles()
|
||||
{
|
||||
frontier.Enqueue(selectedEntity.currentTile);
|
||||
movementCosts.TryAdd(selectedEntity.currentTile, 0);
|
||||
while (frontier.Count > 0)
|
||||
{
|
||||
TileObject currentTile = frontier.Dequeue();
|
||||
if (movementCosts[currentTile] <= selectedEntity.maxMovement)
|
||||
{
|
||||
currentMovableTiles.Add(currentTile);
|
||||
}
|
||||
foreach (TileObject neighboringTiles in currentTile.neighbors)
|
||||
{
|
||||
int newCost = movementCosts[currentTile] + 1;
|
||||
if (!visited.Contains(neighboringTiles))
|
||||
{
|
||||
visited.Add(neighboringTiles);
|
||||
frontier.Enqueue(neighboringTiles);
|
||||
currentPaths.TryAdd(neighboringTiles, currentTile);
|
||||
movementCosts.TryAdd(neighboringTiles, newCost);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (TileObject tileObject in currentMovableTiles)
|
||||
{
|
||||
tileObject.sprite.color = Color.green;
|
||||
}
|
||||
}
|
||||
|
||||
private void PathfindToTarget()
|
||||
|
|
@ -134,32 +148,6 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
}
|
||||
}
|
||||
|
||||
private void GetTilesInRange()
|
||||
{
|
||||
foreach (TileObject neighboringTiles in selectedEntity.currentTile.neighbors)
|
||||
{
|
||||
if (!neighboringTiles.blocked && !neighboringTiles.hasUnit)
|
||||
{
|
||||
currentMovableTiles.Add(neighboringTiles);
|
||||
neighboringTiles.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool SelectLocation()
|
||||
{
|
||||
if (!requestedTile || requestedTile.blocked || requestedTile.hasUnit || !currentMovableTiles.Contains(requestedTile))
|
||||
|
|
|
|||
35
Assets/Scripts/Projectile.cs
Normal file
35
Assets/Scripts/Projectile.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
using System;
|
||||
using Core.Extensions;
|
||||
using UnityEngine;
|
||||
|
||||
public class Projectile : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Rigidbody2D rb;
|
||||
[Header("Stats")]
|
||||
public float damage;
|
||||
public float speed;
|
||||
public int pierceAmount;
|
||||
private int currentPierce;
|
||||
private void FixedUpdate()
|
||||
{
|
||||
rb.linearVelocity = transform.right * speed;
|
||||
}
|
||||
|
||||
public void RotateToTarget(Vector3 target)
|
||||
{
|
||||
transform.Lookat2D(target);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collision)
|
||||
{
|
||||
if (!collision.CompareTag(tag) && collision.TryGetComponent(out Entity isEntity))
|
||||
{
|
||||
isEntity.TakeDamage(damage);
|
||||
currentPierce++;
|
||||
if (currentPierce > pierceAmount)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Projectile.cs.meta
Normal file
2
Assets/Scripts/Projectile.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f78ca53861065e860854d753e8509af8
|
||||
53
Assets/Scripts/RangedWeapon.cs
Normal file
53
Assets/Scripts/RangedWeapon.cs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class RangedWeapon : Weapon
|
||||
{
|
||||
private Camera cam;
|
||||
private Vector3 mousePos;
|
||||
public bool fired;
|
||||
public bool isAiming;
|
||||
[SerializeField] private Projectile projectile;
|
||||
private void Start()
|
||||
{
|
||||
cam = Camera.main;
|
||||
}
|
||||
|
||||
public override void TryAttack()
|
||||
{
|
||||
if (!fired)
|
||||
{
|
||||
fired = true;
|
||||
AttackEffects();
|
||||
}
|
||||
}
|
||||
|
||||
public void Reload()
|
||||
{
|
||||
fired = false;
|
||||
}
|
||||
protected override void AttackEffects()
|
||||
{
|
||||
isAiming = true;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (isAiming)
|
||||
{
|
||||
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
CreateProjectile(mousePos);
|
||||
isAiming = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateProjectile(Vector3 target)
|
||||
{
|
||||
Projectile newProjectile = Instantiate(projectile, transform.position, Quaternion.identity);
|
||||
newProjectile.RotateToTarget(target);
|
||||
newProjectile.tag = tag;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/RangedWeapon.cs.meta
Normal file
2
Assets/Scripts/RangedWeapon.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a1c9e746311daba0092c2ff511ab3031
|
||||
15
Assets/Scripts/Weapon.cs
Normal file
15
Assets/Scripts/Weapon.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class Weapon : MonoBehaviour
|
||||
{
|
||||
public PlayerEntity thisEntity;
|
||||
public virtual void TryAttack()
|
||||
{
|
||||
AttackEffects();
|
||||
}
|
||||
|
||||
protected virtual void AttackEffects()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Weapon.cs.meta
Normal file
2
Assets/Scripts/Weapon.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 02f7281b91434f3b79ab427e45d8d855
|
||||
Loading…
Add table
Add a link
Reference in a new issue