public void ReturnToPatrol() { s_pathNode nod = s_ninjaloader.pathfind.FindPathNode(marchDirection[0]); if (nod != null) { Tuple <List <s_pathNode>, float> nodes = s_ninjaloader.pathfind.PathFind(transform.position, nod); path = nodes.Item1; } }
public override void WalkControl() { /* * if (marchDirection.Count > 0) * { * terminalspd = terminalSpeedOrigin; * s_pathNode nod = s_ninjaloader.pathfind.FindPathNode(marchDirection[marchPoint]); * path = s_ninjaloader.pathfind.PathFind(transform.position, nod.position); * if (path != null) * { * if (!CheckTargetDistance(nod.position, 10)) * { * direction = LookAtTarget(nod.position); * CHARACTER_STATE = CHARACTER_STATES.STATE_MOVING; * } * else * { * if (marchPoint == marchDirection.Count - 1) * marchPoint = 0; * else * marchPoint++; * } * } * } */ if (path == null) { path = new List <s_pathNode>(); for (int i = 0; i < marchDirection.Count; i++) { s_pathNode nod = s_ninjaloader.pathfind.FindPathNode(marchDirection[i]); path.Add(nod); } } else { if (path.Count == 0) { for (int i = 0; i < marchDirection.Count; i++) { s_pathNode nod = s_ninjaloader.pathfind.FindPathNode(marchDirection[i]); path.Add(nod); } } } }
public s_pathNode FindNodeAtPosition(Vector2 pos) { s_pathNode returnNode = null; float smallest = float.MaxValue; for (int i = 0; i < nodes.Count; i++) { s_pathNode p = nodes[i]; Vector2 nodepos = new Vector2(p.position.x, p.position.y); float dist = Vector2.Distance(nodepos, pos); if (dist < smallest) { returnNode = p; smallest = dist; } } return(returnNode); }
public override void SetTileMap(s_map mapdat) { pathNodes.Clear(); debugNodes.Clear(); s_map mp = mapdat; List <s_map.s_block> tile = mp.graphicTiles; List <s_map.s_block> tileMid = mp.graphicTilesMiddle; List <s_map.s_block> tileTop = mp.graphicTilesTop; List <s_map.s_tileobj> coll = mp.tilesdata; // base.SetTileMap(mapdat); /* */ foreach (s_map.s_block b in tile) { tm.SetTile(new Vector3Int((int)b.position.x / 20, (int)b.position.y / 20, 0), tilesNew.Find(ti => ti.name == b.sprite)); } foreach (s_map.s_block b in tileMid) { tm2.SetTile(new Vector3Int((int)b.position.x / 20, (int)b.position.y / 20, 0), tilesNew.Find(ti => ti.name == b.sprite)); } foreach (s_map.s_block b in tileTop) { tm3.SetTile(new Vector3Int((int)b.position.x / 20, (int)b.position.y / 20, 0), tilesNew.Find(ti => ti.name == b.sprite)); } int nodeID = 0; for (int i = 0; i < coll.Count; i++) { s_map.s_tileobj b = coll[i]; string tilename = ""; COLLISION_T tileType = (COLLISION_T)b.enumthing; Tile t = null; s_pathNode nod2 = new s_pathNode(); GameObject go = null; if (InEditor) { switch (b.TYPENAME) { case "teleport_object": case "keyObj": go = Instantiate( FindOBJ(b.TYPENAME), new Vector3(b.pos_x, b.pos_y), Quaternion.identity); if (go != null) { go.transform.SetParent(tilesObj.transform); go.name = b.name; } break; case "Node": go = Instantiate(FindOBJ(b.TYPENAME), new Vector3(b.pos_x, b.pos_y), Quaternion.identity); if (go != null) { go.transform.SetParent(tilesObj.transform); go.name = b.name; } o_nodeobj nod = go.GetComponent <o_nodeobj>(); nod.nodeID = nodeID; nod.name = "Node_" + nod.nodeID; nod.transform.position = new Vector3(b.pos_x, b.pos_y); nod.transform.SetParent(tilesObj.transform); debugNodes.Add(nod); /* * o_nodeobj nod = go.GetComponent<o_nodeobj>(); * nod.nodeID = nodeID; * nod.name = "Node_" + nod.nodeID; * debugNodes.Add(nod); */ nod2 = new s_pathNode(); nod2.position = new Vector2(b.pos_x, b.pos_y); if (b.CustomTypes.Find(x => x.name == "nodeID").name == "") { nod2.id = nodeID; } else { nod2.id = b.CustomTypes.Find(x => x.name == "nodeID").type; } pathNodes.Add(nod2); nodeID++; break; } /* * switch (tileType) * { * case COLLISION_T.WALL: * if (b.TYPENAME == "teleport_object") * continue; * colmp.SetTile(new Vector3Int(b.pos_x / 20, b.pos_y / 20, 0), collisionTile); * break; * } */ } else { switch (b.TYPENAME) { case "teleport_object": continue; case "keyObj": SpawnObject <o_generic>("keyObj", new Vector2(b.pos_x, b.pos_y), Quaternion.identity); continue; case "Node": nod2 = new s_pathNode(); nod2.position = new Vector2(b.pos_x, b.pos_y); nod2.id = nodeID; pathNodes.Add(nod2); nodeID++; break; } } tilename = b.name; if (tilename != "") { colmp.SetTile(new Vector3Int(b.pos_x / 20, b.pos_y / 20, 0), collisionList.Find(ti => ti.name == tilename)); } else { colmp.SetTile(new Vector3Int(b.pos_x / 20, b.pos_y / 20, 0), t); } } for (int i = 0; i < pathNodes.Count; i++) { s_pathNode p = pathNodes[i]; List <s_pathNode> nbrs = new List <s_pathNode>(); List <int> GetNeighbours = new List <int>(); s_map.s_tileobj t = coll[p.id]; //Gets all the neighbours foreach (s_map.s_customType no in t.CustomTypes) { //Find all the custom values with "node" which resemble the neighbours if (no.name == "node") { s_pathNode path = pathNodes.Find(x => x.id == no.type); nbrs.Add(path); } } if (InEditor) { o_nodeobj currentNode = debugNodes.Find(x => x.nodeID == p.id); foreach (s_pathNode n in nbrs) { o_nodeobj nod = debugNodes.Find(x => x.nodeID == n.id); if (nod != null) { currentNode.nieghbours.Add(nod); GetNeighbours.Add(n.id); //nod.nieghbours.Add(currentNode); if (!currentNode.nieghbours.Find(x => nod)) { } } } foreach (s_pathNode n in nbrs) { GetNeighbours.Add(n.id); } p = new s_pathNode(p.id, new Vector2(p.position.x, p.position.y), GetNeighbours); pathNodes[i] = p; } else { foreach (s_pathNode n in nbrs) { GetNeighbours.Add(n.id); } p = new s_pathNode(p.id, new Vector2(p.position.x, p.position.y), GetNeighbours); pathNodes[i] = p; } } pathfind = GetComponent <s_pathfind>(); pathfind.SetNodes(ref pathNodes); }
public Tuple <List <s_pathNode>, float> PathFind(Vector2 start, Vector2 end) { ResetNodes(); int round = 0; List <s_pathNode> openList = new List <s_pathNode>(); HashSet <s_pathNode> closedList = new HashSet <s_pathNode>(); s_pathNode startNode = FindNodeAtPosition(start); s_pathNode goal = FindNodeAtPosition(end); if (goal == null) { return(null); } goal.isgoal = true; openList.Add(startNode); while (openList.Count > 0) { if (round == 200) { return(null); } s_pathNode current = openList[0]; for (int i = 1; i < openList.Count; i++) { s_pathNode pa = openList[i]; if (pa.f_cost < current.f_cost) { //FindPathNode(pa.id) current = FindNodeAtPosition(pa.position); } } if (current.id == goal.id) { List <s_pathNode> pathList = new List <s_pathNode>(); s_pathNode path = goal; int l = 0; while (path != startNode) { path.list = l; pathList.Add(path); l++; path = path.parent; } pathList.Reverse(); return(new Tuple <List <s_pathNode>, float>(pathList, current.f_cost)); } openList.Remove(current); closedList.Add(current); /* * if (current.neighbours == null) * continue; */ foreach (int n in current.neighbours) { s_pathNode no = nodes.Find(x => x.id == n); float g_cost = current.g_cost + Vector2.Distance(no.position, current.position); float h_cost = current.h_cost + Vector2.Distance(no.position, goal.position); if (closedList.Contains(no)) { continue; } if (!openList.Contains(no) || g_cost < no.g_cost) { no.g_cost = g_cost; no.h_cost = h_cost; no.parent = current; if (!openList.Contains(no)) { openList.Add(no); } } } round++; } return(null); }