From b9b335be016093f6de65eceab7a80fdbf059393c Mon Sep 17 00:00:00 2001 From: Gabriel Ksawery Skowron-Rodriguez Date: Fri, 3 Jun 2022 12:59:45 +0200 Subject: [PATCH] preliminary final movement + path created - path costs not working correctly - only "whole map" path is generated --- .../Assets/Scripts/Algorithms/Pathfinding.cs | 15 +++++++-------- theProject/Assets/Scripts/Entities/Soldier.cs | 11 ++--------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/theProject/Assets/Scripts/Algorithms/Pathfinding.cs b/theProject/Assets/Scripts/Algorithms/Pathfinding.cs index c119b690..3afb0b02 100644 --- a/theProject/Assets/Scripts/Algorithms/Pathfinding.cs +++ b/theProject/Assets/Scripts/Algorithms/Pathfinding.cs @@ -52,7 +52,6 @@ public class Pathfinding : MonoBehaviour if (nodesToExplore.Contains(targetCoord)) // do not add a tile to be explored twice {// check if found cheaper path PathNode neighbor = (PathNode)nodesToExplore[targetCoord]; - //int newCost = GetTargetCost(currentNode.Cost, targetCoord); if (neighbor.Cost > newCost && newCost > 0) { nodesToExplore.Remove(targetCoord); @@ -63,7 +62,6 @@ public class Pathfinding : MonoBehaviour if (resolvedNodes.Contains(targetCoord)) {// check if found cheaper path PathNode neighbor = (PathNode)resolvedNodes[targetCoord]; - //int newCost = GetTargetCost(currentNode.Cost, targetCoord); if (neighbor.Cost > newCost && newCost > 0) nodesToExplore.Add(targetCoord, new PathNode(currentNode, targetCoord, newCost)); continue; @@ -80,6 +78,8 @@ public class Pathfinding : MonoBehaviour } path = ConstructPath(finalNode); + if (path.Count < 1) + return false; return true; } @@ -113,10 +113,10 @@ public class Pathfinding : MonoBehaviour { List final = new List(); List neighbors = new List(); - neighbors.Add(coords += Vector2Int.up); - neighbors.Add(coords += Vector2Int.down); - neighbors.Add(coords += Vector2Int.left); - neighbors.Add(coords += Vector2Int.right); + neighbors.Add(coords + Vector2Int.up); + neighbors.Add(coords + Vector2Int.down); + neighbors.Add(coords + Vector2Int.left); + neighbors.Add(coords + Vector2Int.right); foreach(Vector2Int neighbor in neighbors) { if(TilemapManager.ins.GetTileState(neighbor.x, neighbor.y) != TilemapManager.TileState.outOfBounds) @@ -134,10 +134,9 @@ public class Pathfinding : MonoBehaviour while(currentNode.Previous != null) {//TEMP just give next step path.Add(currentNode.Coords); - path.Reverse(); - path.RemoveAt(0); // remove start node currentNode = currentNode.Previous; } + path.Reverse(); //throw new System.NotImplementedException(); return path; } diff --git a/theProject/Assets/Scripts/Entities/Soldier.cs b/theProject/Assets/Scripts/Entities/Soldier.cs index 0b0fb0bc..158b6235 100644 --- a/theProject/Assets/Scripts/Entities/Soldier.cs +++ b/theProject/Assets/Scripts/Entities/Soldier.cs @@ -25,16 +25,9 @@ public class Soldier : Entity } List path; - if(!Pathfinding - .Instance - .FindPath - (soldier - .tileCoord, - soldier - .movementDestination - , out path)) + if(!Pathfinding.Instance.FindPath(soldier.tileCoord, soldier.movementDestination, out path)) { - return; // cannot find path: do nothing + return; // cannot find path: do nothing (for now) } Vector2Int movementStepDestination = path[0];