public Node(TilesBehavior tileNew, Node[][] graph) { tile = tileNew; graph[tile.posX][tile.posY] = this; usable = tileNew.GetComponent <RoadBehavior>().activated; if (tile.top != null) { voisins[1] = new Node(tile.top.GetComponent <TilesBehavior>(), graph); } if (tile.gameEntrance) { if (tile.right != null) { new Node(tile.right.GetComponent <TilesBehavior>(), graph); } } if (tile.left != null) { voisins[0] = graph[tile.left.GetComponent <TilesBehavior>().posX][tile.left.GetComponent <TilesBehavior>().posY]; voisins[0].voisins[2] = this; } if (tile.down != null) { voisins[3] = graph[tile.down.GetComponent <TilesBehavior>().posX][tile.down.GetComponent <TilesBehavior>().posY]; } }
void CalculateWay(TilesBehavior beggin, TilesBehavior destination) { Node[][] graph = new Node[8][]; for (int i = 0; i < graph.Length; i++) { graph[i] = new Node[8]; } new Node(initTile, graph); Node end = graph[destination.posX][destination.posY]; Node start = graph[beggin.posX][beggin.posY]; List <Node> closedSet = new List <Node>(); List <Node> openSet = new List <Node>(); openSet.Add(start); openSet[0].g_score = 0; openSet[0].f_score = 0 /*HeuristicEstimation(start, destination)*/; Node current = null; while (openSet.Count != 0) { /*have to list openSet by f_score*/ current = openSet[0]; if (current == end) { way = end.Reconstruct(); return; } openSet.Remove(current); closedSet.Add(current); foreach (Node voisin in current.voisins) { if (voisin != null && voisin.usable) { if (closedSet.Contains(voisin)) { continue; } int new_g_score = current.g_score + 1; if (!openSet.Contains(voisin)) { openSet.Add(voisin); } else if (new_g_score >= voisin.g_score) { continue; } voisin.cameFrom = current; voisin.g_score = new_g_score; voisin.f_score = 0;//Heuristic truc chouette(voisin, end) } } } way = null; }
public int HeuristicEstimation(TilesBehavior start, TilesBehavior end) { return(0); }
void wayBehavior() { if (way[0] == container) { //ATTAIN OBJECTIF if (way.Count == 1) { if (objectif0 != null) { if (quest == null) { //ATTAIN OBJECTIF QUEST quest = new QuestLaunched(objectif0); way.Clear(); objectif0 = null; } else { //RETURNING QUEST quest.quest.questGiver.GetComponent <QuestGiver>().Return(this); questFinished++; classOf.questFinished++; quest = null; objectif0 = null; way.Clear(); } } else if (objectif1 != null) { //ATTAIN OBJECTIF DUNGEON //ENTERING DUNGEON if (objectif1.Entering(this)) { objectif1 = null; inDungeon = true; way.Clear(); } } else if (objectif2 != null) { //ATTAIN OBJECTIF FACILITY if (objectif2.GetComponent <RestoreLife>() != null) { //IS RESTORE LIFE if (objectif2.GetComponent <RestoreLife>().Use(this)) { objectif2 = null; way.Clear(); } } else { //NORMAL SHOP objectif2.Use(this); way.Clear(); } } else if (objectif3 != null) { //ATTAIN EXIT objectif3 = null; way.Clear(); outTime = outMax; quest = null; GetComponent <SpriteRenderer>().enabled = false; } } else { way.Remove(container); } } if (way.Count > 0) { //MOVING container = way[0]; gameObject.transform.position = container.gameObject.transform.position; gameObject.transform.parent = container.gameObject.transform; } }