Poprawka słownika w pathfinding i usunięcie dodatkowej Coroutine z klasy kelenera
This commit is contained in:
parent
0e8f068656
commit
111772e9e9
@ -8,64 +8,60 @@ namespace Logic.Agent
|
||||
|
||||
public class PathFinder : MonoBehaviour
|
||||
{
|
||||
|
||||
private readonly IDictionary<Node, Tuple<Node, Node, Action>> _nodeParents = new Dictionary<Node, Tuple<Node, Node, Action>>();
|
||||
// Słownik
|
||||
// Klucz - Node rodzica
|
||||
// Wartość - Tuple< Node Dziecka, Akcja przejścia z Noda rodzica do Noda dziecka >
|
||||
private readonly IDictionary<Node, Tuple<Node, Action>> _exploredPaths = new Dictionary<Node, Tuple<Node, Action>>();
|
||||
|
||||
public Stack<Tuple<Node, Action>> FindPath(Node startNode, Node endNode)
|
||||
{
|
||||
_nodeParents.Clear();
|
||||
List<Tuple<Node, Node, Action>> path = new List<Tuple<Node, Node, Action>>();
|
||||
Stack<Tuple<Node, Action>> pathStack = new Stack<Tuple<Node, Action>>();
|
||||
_exploredPaths.Clear();
|
||||
Stack<Tuple<Node, Action>> shortestPath = new Stack<Tuple<Node, Action>>();
|
||||
|
||||
var goal = Bfs(startNode, endNode);
|
||||
|
||||
if (goal.Item1 == startNode) {
|
||||
print("Not found");
|
||||
return pathStack;
|
||||
print("Path Not found");
|
||||
return shortestPath;
|
||||
}
|
||||
|
||||
Tuple<Node, Node, Action> curr = goal;
|
||||
Tuple<Node, Action> curr = goal;
|
||||
|
||||
while (curr.Item1 != startNode) {
|
||||
path.Add(curr);
|
||||
curr = _nodeParents[curr.Item1];
|
||||
curr.Item1.MarkAsPath();
|
||||
shortestPath.Push(new Tuple<Node, Action>(curr.Item1, curr.Item2));
|
||||
curr = _exploredPaths[curr.Item1];
|
||||
}
|
||||
path.Add(curr);
|
||||
shortestPath.Push(new Tuple<Node, Action>(curr.Item1, curr.Item2));
|
||||
|
||||
foreach (Tuple<Node, Node, Action> node in path)
|
||||
{
|
||||
pathStack.Push(new Tuple<Node, Action>(node.Item1, node.Item3));
|
||||
node.Item1.MarkAsPath();
|
||||
}
|
||||
|
||||
return pathStack;
|
||||
return shortestPath;
|
||||
}
|
||||
|
||||
private Tuple<Node, Node, Action> Bfs(Node startNode, Node endNode){
|
||||
private Tuple<Node, Action> Bfs(Node startNode, Node endNode){
|
||||
|
||||
Queue<Node> queue = new Queue<Node> ();
|
||||
HashSet<Node> exploredNodes = new HashSet<Node> ();
|
||||
queue.Enqueue (startNode);
|
||||
queue.Enqueue(startNode);
|
||||
|
||||
while (queue.Count != 0) {
|
||||
Node currentNode = queue.Dequeue ();
|
||||
Node currentNode = queue.Dequeue();
|
||||
|
||||
// Warunek spełnienia celu
|
||||
if (currentNode == endNode)
|
||||
{
|
||||
return new Tuple<Node, Node, Action>(currentNode, currentNode, null);
|
||||
return new Tuple<Node, Action>(currentNode, null);
|
||||
}
|
||||
|
||||
foreach(Tuple<Node, Action> node in Succ(currentNode)){
|
||||
if(!exploredNodes.Contains(node.Item1)) {
|
||||
exploredNodes.Add(node.Item1);
|
||||
_nodeParents.Add (node.Item1, new Tuple<Node, Node, Action>(currentNode, node.Item1, node.Item2));
|
||||
queue.Enqueue (node.Item1);
|
||||
exploredNodes.Add(node.Item1);
|
||||
_exploredPaths.Add(node.Item1, new Tuple<Node, Action>(currentNode, node.Item2));
|
||||
queue.Enqueue(node.Item1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Tuple<Node, Node, Action>(startNode, startNode, null);
|
||||
return new Tuple<Node, Action>(startNode, null);
|
||||
}
|
||||
|
||||
private List<Tuple<Node, Action>> Succ(Node currentNode)
|
||||
|
@ -9,8 +9,8 @@ namespace Logic.Agent
|
||||
public class Waitress : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Node currentNode;
|
||||
[SerializeField] private bool isFollowingPath = false;
|
||||
[SerializeField] private bool canMove = true; //temporary
|
||||
[SerializeField] private bool isFollowingPath;
|
||||
[SerializeField] private bool canMove = true;
|
||||
private Node _previousNode;
|
||||
private PathFinder _pathFinder;
|
||||
|
||||
@ -28,7 +28,7 @@ namespace Logic.Agent
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Space))
|
||||
{
|
||||
kitchenTable.HandleSpace();
|
||||
kitchenTable.HandleSpaceClick();
|
||||
}
|
||||
}
|
||||
if (table is CustomerTable customerTable)
|
||||
@ -41,20 +41,17 @@ namespace Logic.Agent
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator GoToNode(Table table)
|
||||
public void GoToNode(Table table)
|
||||
{
|
||||
if (isFollowingPath) yield break;
|
||||
// Jużeli w stoliku to wyjdź ze stolika i znajdź ścieżkę
|
||||
if (isFollowingPath) return;
|
||||
|
||||
isFollowingPath = true;
|
||||
|
||||
if (currentNode is Table && _previousNode != null)
|
||||
{
|
||||
yield return StartCoroutine(RunToAnotherNode(_previousNode));
|
||||
currentNode = _previousNode;
|
||||
}
|
||||
PrepareMove(table);
|
||||
}
|
||||
|
||||
private void PrepareMove(Table table)
|
||||
{
|
||||
isFollowingPath = true;
|
||||
|
||||
Stack<Tuple<Node, Action>> path = _pathFinder.FindPath(currentNode, table);
|
||||
if (path.Count > 0)
|
||||
{
|
||||
@ -71,7 +68,7 @@ namespace Logic.Agent
|
||||
{
|
||||
currentNode.ClearPathMark();
|
||||
var node = path.Pop();
|
||||
if (node.Item2 != null) node.Item2.Invoke();
|
||||
node.Item2?.Invoke();
|
||||
}
|
||||
|
||||
yield return new WaitForEndOfFrame();
|
||||
@ -106,7 +103,7 @@ namespace Logic.Agent
|
||||
i += Time.deltaTime;
|
||||
}
|
||||
|
||||
if (!(destination is Table table))
|
||||
if (!(destination is Table))
|
||||
{
|
||||
transform.position = new Vector3(location.x, transform.position.y, location.z);
|
||||
}
|
||||
|
@ -1,5 +1,3 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Logic.Agent;
|
||||
using Logic.Data;
|
||||
@ -21,7 +19,7 @@ namespace Logic
|
||||
|
||||
public void GoToNode(Table table)
|
||||
{
|
||||
StartCoroutine(waitress.GoToNode(table));
|
||||
waitress.GoToNode(table);
|
||||
}
|
||||
|
||||
public bool AddItem(Item item)
|
||||
|
@ -18,7 +18,7 @@ namespace Logic.Graph
|
||||
StartCoroutine(PrepareMeal());
|
||||
}
|
||||
|
||||
public void HandleSpace()
|
||||
public void HandleSpaceClick()
|
||||
{
|
||||
List<Recipe> recipes = AgentManager.Instance.GetRecipes();
|
||||
foreach (Recipe recipe in recipes)
|
||||
|
Loading…
Reference in New Issue
Block a user