public bool IsWalkable(MapPos mapPos) { return(mapNodes[mapPos.x][mapPos.y].weight != WALL_WEIGHT); }
public bool FindPath(int startX, int startY, List <MapPos> finalPositions, OnPathCallback callback) { int width = widthMax; int height = heightMax; MapPos start = GetClosestValidPos(startX, startY); startX = start.x; startY = start.y; List <PathRoute> paths = new List <PathRoute>(); foreach (MapPos finalPos in finalPositions) { if (startX < 0 || startY < 0 || finalPos.x < 0 || finalPos.y < 0 || startX >= width || finalPos.x >= width || startY >= height || finalPos.y >= height) { continue; // Out of bounds! } if (mapNodes[finalPos.x][finalPos.y].weight == WALL_WEIGHT || mapNodes[startX][startY].weight == WALL_WEIGHT) { continue; // Wall } List <PathNode> currentPath = findPath(startX, startY, finalPos.x, finalPos.y); if (currentPath.Count <= 0 && (startX != finalPos.x || startY != finalPos.y)) { // Don't add path if there's no path } else { if (!finalPos.straightToOffset) { // Don't go straight to offset, add dummy currentPath.Add(currentPath[currentPath.Count - 1]); } paths.Add(new PathRoute(currentPath, worldOrigin, nodeSize, finalPos)); } } int smallest = 0; for (int i = 1; i < paths.Count; i++) { if (paths[i].pathNodeList.Count < paths[smallest].pathNodeList.Count) { smallest = i; } } if (paths.Count <= 0 || (paths.Count > 0 && paths[smallest].pathNodeList.Count <= 0)) { // No path return(false); } else { callback(paths[smallest].pathNodeList, paths[smallest].finalPos); } return(true); }
public void RefreshHitbox(MapPos mapPos) { mapNodes[mapPos.x][mapPos.y].TestHitbox(); }
public static int Distance(MapPos p1, MapPos p2) { return(System.Math.Abs(p1.x - p2.x) + System.Math.Abs(p1.y - p2.y)); }
// Update is called once per frame public void Update(float deltaTime) { if (pathList == null || (pathList != null && pathList.Count <= 0)) { return; } Vector3 pos = getPosition(); isMoving = true; PathNode currPath = pathList[0]; Vector3 currPos; float distanceCheck = 2f; if (pathList.Count == 1) // Last Pos { distanceCheck = 1f; currPos = new Vector3(currPath.xPos * 10 + finalPos.offsetX, currPath.yPos * 10 + finalPos.offsetY); } else { currPos = new Vector3(currPath.xPos * 10, currPath.yPos * 10); } // Move to path Vector3 dir = (currPos - pos).normalized; lastDir = dir; float moveAmount = Mathf.Min(deltaTime * speed, Vector3.Distance(pos, currPos)); Vector3 dirAmt = (dir * moveAmount); setPosition(new Vector3(pos.x + dirAmt.x, pos.y + dirAmt.y)); pos = getPosition(); if (pathList.Count > 1) //Not last pos { MapPos prevPos = new MapPos(mapPos.x, mapPos.y); if (mapPos.offsetX > 0 || mapPos.offsetY > 0) { // Last target had an offset if (Mathf.RoundToInt(pos.x / 10f) == mapPos.x && Mathf.RoundToInt(pos.y / 10f) == mapPos.y) { // Rounded position equals base MapPos without Offset mapPos.x = Mathf.RoundToInt(pos.x / 10f); mapPos.y = Mathf.RoundToInt(pos.y / 10f); mapPos.offsetX = 0f; mapPos.offsetY = 0f; } } else { mapPos.x = Mathf.RoundToInt(pos.x / 10f); mapPos.y = Mathf.RoundToInt(pos.y / 10f); } if (prevPos.x != mapPos.x || prevPos.y != mapPos.y) { //Event_Speaker.Broadcast(Event_Trigger.Unit_Moved, mapPos); //unit.OnUnitMoved(prevPos, mapPos); } } if (Vector3.Distance(pos, currPos) < distanceCheck) { // Next path pathList.RemoveAt(0); if (pathList.Count == 0) { // Final destination reached setPosition(currPos); pathList = null; mapPos.offsetX = finalPos.offsetX; mapPos.offsetY = finalPos.offsetY; mapPos.straightToOffset = finalPos.straightToOffset; if (finalPos.straightToOffset) { mapPos.x = finalPos.x; mapPos.y = finalPos.y; } PathComplete(); } } }
public bool EqualsDeep(MapPos p2) { // Check if this one equals that one return(x == p2.x && y == p2.y && offsetX == p2.offsetX && offsetY == p2.offsetY && straightToOffset == p2.straightToOffset); }
public bool Equals(MapPos p2) { // Check if this one equals that one return(x == p2.x && y == p2.y); }
public void RefreshHitbox(MapPos mapPos) { mapNodes[mapPos.x][mapPos.y].TestHitbox(); //Event_Speaker.Broadcast(Event_Trigger.Pathfinding_Refresh); }
public PathRoute(List <PathNode> pathNodeList, Vector3 worldOrigin, float nodeSize, MapPos finalPos) { this.pathNodeList = pathNodeList; pathVectorList = new List <Vector3>(); foreach (PathNode pathNode in pathNodeList) { pathVectorList.Add(pathNode.GetWorldVector(worldOrigin, nodeSize)); } this.finalPos = finalPos; }
public PathRoute(List <PathNode> pathNodeList, List <Vector3> pathVectorList, MapPos finalPos) { this.pathNodeList = pathNodeList; this.pathVectorList = pathVectorList; this.finalPos = finalPos; }