// This should be called whenever a change to the world // means that our old pathfinding info is invalid. public void InvalidateTileGraph() { tileGraph = null; }
public void ResizeWorld(int width, int height, int depth) { if (width < Width || height < Height || depth < Depth) { if (width < Width) { UnityDebugger.Debugger.LogWarning("World", "Width too small: " + Width + " " + width); } if (height < Height) { UnityDebugger.Debugger.LogWarning("World", "Height too small: " + Height + " " + height); } if (depth < Depth) { UnityDebugger.Debugger.LogWarning("World", "Depth too small: " + Depth + " " + depth); } UnityDebugger.Debugger.LogError("World", "Shrinking the world is not presently supported"); return; } if (width == Width && height == Height && depth == Depth) { // No change, just bail return; } int offsetX = (width - Width) / 2; int offsetY = ((height - Height) / 2) + 1; Tile[,,] oldTiles = (Tile[, , ])tiles.Clone(); tiles = new Tile[width, height, depth]; int oldWidth = Width; int oldHeight = Height; int oldDepth = Depth; Width = width; Height = height; Depth = depth; FillTilesArray(); tileGraph = null; roomGraph = null; // Reset temperature, so it properly sizes arrays to the new world size temperature.Resize(); for (int x = 0; x < oldWidth; x++) { for (int y = 0; y < oldHeight; y++) { for (int z = 0; z < oldDepth; z++) { tiles[x + offsetX, y + offsetY, z] = oldTiles[x, y, z]; oldTiles[x, y, z].MoveTile(x + offsetX, y + offsetY, z); } } } }
private void DoMovement(float deltaTime, bool useWorldTG, Path_TileGraph tg = null) { // We're already were we want to be. if (currTile == DestTile) { mPathAStar = null; return; } // currTile = The tile I am currently in (and may be in the process of leaving) // nextTile = The tile I am currently entering // destTile = Our final destination -- we never walk here directly, but instead use it for the pathfinding if (nextTile == null || nextTile == currTile) { // Get the next tile from the pathfinder. if (mPathAStar == null || mPathAStar.Length() == 0) { // Generate a path to our destination if (useWorldTG) { mPathAStar = new Path_AStar(true, currTile, DestTile); } else { mPathAStar = new Path_AStar(false, currTile, DestTile, tg); } // No valid if (mPathAStar.Length() == 0) { minionState = MinionState.InvalidTile; Debug.LogError("Path_AStar returned no path to destination!"); return; } // Dump the first tile, because nextTile == currTile nextTile = mPathAStar.Dequeue(); } // Grab the next waypoint from the pathing system! nextTile = mPathAStar.Dequeue(); if (nextTile == currTile) { Debug.LogError("Update_DoMovement - nextTile is currTile?"); } } /* if(pathAStar.Length() == 1) { * return; * } */ // At this point we should have a valid nextTile to move to. // What's the total distance from point A to point B? // We are going to use Euclidean distance FOR NOW... // But when we do the pathfinding system, we'll likely // switch to something like Manhattan or Chebyshev distance float distToTravel = Mathf.Sqrt( Mathf.Pow(currTile.X - nextTile.X, 2) + Mathf.Pow(currTile.Z - nextTile.Z, 2) ); /*if(nextTile.IsEnterable() == ENTERABILITY.Never) { * // Most likely a wall got built, so we just need to reset our pathfinding information. * // FIXME: Ideally, when a wall gets spawned, we should invalidate our path immediately, * // so that we don't waste a bunch of time walking towards a dead end. * // To save CPU, maybe we can only check every so often? * // Or maybe we should register a callback to the OnTileChanged event? * Debug.LogError("FIXME: A character was trying to enter an unwalkable tile."); * nextTile = null; // our next tile is a no-go * pathAStar = null; // clearly our pathfinding info is out of date. * return; * } * else if ( nextTile.IsEnterable() == ENTERABILITY.Soon ) { * // We can't enter the NOW, but we should be able to in the * // future. This is likely a DOOR. * // So we DON'T bail on our movement/path, but we do return * // now and don't actually process the movement. * return; * } */ // How much distance can be travel this Update? float distThisFrame = speed / nextTile.movementCost * deltaTime; // How much is that in terms of percentage to our destination? float percThisFrame = distThisFrame / distToTravel; // Add that to overall percentage travelled. movementPercentage += percThisFrame; if (movementPercentage >= 1) { // We have reached our destination // TODO: Get the next tile from the pathfinding system. // If there are no more tiles, then we have TRULY // reached our destination. currTile = nextTile; movementPercentage = 0; // FIXME? Do we actually want to retain any overshot movement? } }
public void SetPlayMode(World world) { canTakeDMG = true; // Set Patrol Point in 4 Quadrant minionState = MinionState.Patrol; //minionState2 = MinionState2.Normal; patrolPoints = new List <Tile>(); if (patrolRange == 0) { return; } // Set tile graph width and height int startW = charStartTile.X - patrolRange <= 0 ? 0 : charStartTile.X - patrolRange; int endW = charStartTile.X + patrolRange >= world.Width - 1 ? world.Width - 1 : charStartTile.X + patrolRange; //Debug.Log("StartWidth : " + startW + "EndWidth : " + endW); int startH = charStartTile.Z - patrolRange <= 0 ? 0 : charStartTile.Z - patrolRange; int endH = charStartTile.Z + patrolRange >= world.Height - 1 ? world.Height - 1 : charStartTile.Z + patrolRange; //Debug.Log("StartHeight : " + startH + "EndHeight : " + endH); // Create Tile Graph within Patrol range mTileGraph = new Path_TileGraph(startW, endW, startH, endH); //// Check 4 Quadrant have at least 1 patrol tile? Tile LUtile = null; Tile RUtile = null; Tile LLtile = null; Tile RLtile = null; // ======Loop Outer in 4 Quadrant============ // Left-Lower Q ,X(min > max), Z(min > max) // Loop the X axis farest tile for (int x = startW; x < charStartTile.X; x++) { int z = startH; Tile checkT = world.GetTileAt(x, z); if (LLtile == null && checkT != charStartTile) { mPathAStar = new Path_AStar(false, charStartTile, checkT, mTileGraph, startW, endW, startH, endH); // There is valid pathfinder to this tile if (mPathAStar.Length() != 0) { Debug.Log("Add LLtile to patrol point : " + checkT.X + "," + checkT.Z); LLtile = checkT; } } } // Loop the Z axis farest tile for (int z = startH; z < charStartTile.Z; z++) { int x = startW; Tile checkT = world.GetTileAt(x, z); //Debug.Log("Check tile : " + checkT.X + "," + checkT.Z); // Check the farest tile first and Check it not CharTile if (LLtile == null && checkT != charStartTile) { mPathAStar = new Path_AStar(false, charStartTile, checkT, mTileGraph, startW, endW, startH, endH); // There is valid pathfinder to this tile if (mPathAStar.Length() != 0) { Debug.Log("Add LLtile to patrol point : " + checkT.X + "," + checkT.Z); LLtile = checkT; } } } // Left-Upper Q ,X(min > max),Z(max > min) // Loop the X axis farest tile for (int x = startW; x <= charStartTile.X; x++) { int z = endH; Tile checkT = world.GetTileAt(x, z); if (LUtile == null && checkT != charStartTile) { mPathAStar = new Path_AStar(false, charStartTile, checkT, mTileGraph, startW, endW, startH, endH); // There is valid pathfinder to this tile if (mPathAStar.Length() != 0) { Debug.Log("Add LUtile to patrol point : " + checkT.X + "," + checkT.Z); LUtile = checkT; } } } // Loop the Z axis farest tile for (int z = endH; z >= charStartTile.Z; z--) { int x = startW; Tile checkT = world.GetTileAt(x, z); // Check the farest tile first and Check it not CharTile if (LUtile == null && checkT != charStartTile) { mPathAStar = new Path_AStar(false, charStartTile, checkT, mTileGraph, startW, endW, startH, endH); // There is valid pathfinder to this tile if (mPathAStar.Length() != 0) { Debug.Log("Add LUtile to patrol point : " + checkT.X + "," + checkT.Z); LUtile = checkT; } } } // Right-Upper Q ,X(max > min),Z(max > min) // Loop the X axis farest tile for (int x = endW; x >= charStartTile.X; x--) { int z = endH; Tile checkT = world.GetTileAt(x, z); // Check the farest tile first and Check it not CharTile if (RUtile == null && checkT != charStartTile) { mPathAStar = new Path_AStar(false, charStartTile, checkT, mTileGraph, startW, endW, startH, endH); // There is valid pathfinder to this tile if (mPathAStar.Length() != 0) { Debug.Log("Add RUtile to patrol point : " + checkT.X + "," + checkT.Z); RUtile = checkT; } } } // Loop the Z axis farest tile for (int z = endH; z >= charStartTile.Z; z--) { int x = endW; Tile checkT = world.GetTileAt(x, z); // Check the farest tile first and Check it not CharTile if (RUtile == null && checkT != charStartTile) { mPathAStar = new Path_AStar(false, charStartTile, checkT, mTileGraph, startW, endW, startH, endH); // There is valid pathfinder to this tile if (mPathAStar.Length() != 0) { Debug.Log("Add RUtile to patrol point : " + checkT.X + "," + checkT.Z); RUtile = checkT; } } } // Right-Lower Q ,X(max > min),Z(min > max) // Loop the X axis farest tile for (int x = endW; x >= charStartTile.X; x--) { int z = startH; Tile checkT = world.GetTileAt(x, z); // Check the farest tile first and Check it not CharTile if (RLtile == null && checkT != charStartTile) { mPathAStar = new Path_AStar(false, charStartTile, checkT, mTileGraph, startW, endW, startH, endH); // There is valid pathfinder to this tile if (mPathAStar.Length() != 0) { Debug.Log("Add RLtile to patrol point : " + checkT.X + "," + checkT.Z); RLtile = checkT; } } } // Loop the Z axis farest tile for (int z = startH; z > charStartTile.Z; z++) { int x = startW; Tile checkT = world.GetTileAt(x, z); // Check the farest tile first and Check it not CharTile if (RLtile == null && checkT != charStartTile) { mPathAStar = new Path_AStar(false, charStartTile, checkT, mTileGraph, startW, endW, startH, endH); // There is valid pathfinder to this tile if (mPathAStar.Length() != 0) { Debug.Log("Add RLtile to patrol point : " + checkT.X + "," + checkT.Z); RLtile = checkT; } } } // ======== Loop Inner in 4 Quadrant ========== // Left-Lower Q ,X(min > max), Z(min > max) // Loop the inner tile if (LLtile == null) { for (int x = startW + 1; x < charStartTile.X; x++) { for (int z = startH + 1; z < charStartTile.Z; z++) { Tile checkT = world.GetTileAt(x, z); if (LLtile == null && checkT != charStartTile) { mPathAStar = new Path_AStar(false, charStartTile, checkT, mTileGraph, startW, endW, startH, endH); // There is valid pathfinder to this tile if (mPathAStar.Length() != 0) { Debug.Log("Add LLtile to patrol point : " + checkT.X + "," + checkT.Z); LLtile = checkT; } } } } } // Left-Upper Q ,X(min > max),Z(max > min) // Loop the X axis farest tile if (LUtile == null) { for (int x = startW + 1; x <= charStartTile.X; x++) { for (int z = endH - 1; z >= charStartTile.Z; z--) { Tile checkT = world.GetTileAt(x, z); if (LUtile == null && checkT != charStartTile) { mPathAStar = new Path_AStar(false, charStartTile, checkT, mTileGraph, startW, endW, startH, endH); // There is valid pathfinder to this tile if (mPathAStar.Length() != 0) { Debug.Log("Add LUtile to patrol point : " + checkT.X + "," + checkT.Z); LUtile = checkT; } } } } } // Right-Upper Q ,X(max > min),Z(max > min) // Loop the X axis farest tile if (RUtile == null) { for (int x = endW - 1; x >= charStartTile.X; x--) { for (int z = endH - 1; z >= charStartTile.Z; z--) { Tile checkT = world.GetTileAt(x, z); // Check the farest tile first and Check it not CharTile if (RUtile == null && checkT != charStartTile) { mPathAStar = new Path_AStar(false, charStartTile, checkT, mTileGraph, startW, endW, startH, endH); // There is valid pathfinder to this tile if (mPathAStar.Length() != 0) { Debug.Log("Add RUtile to patrol point : " + checkT.X + "," + checkT.Z); RUtile = checkT; } } } } } // Right-Lower Q ,X(max > min),Z(min > max) // Loop the X axis farest tile if (RLtile == null) { for (int x = endW - 1; x >= charStartTile.X; x--) { for (int z = startH + 1; z < charStartTile.Z; z++) { Tile checkT = world.GetTileAt(x, z); // Check the farest tile first and Check it not CharTile if (RLtile == null && checkT != charStartTile) { mPathAStar = new Path_AStar(false, charStartTile, checkT, mTileGraph, startW, endW, startH, endH); // There is valid pathfinder to this tile if (mPathAStar.Length() != 0) { Debug.Log("Add RLtile to patrol point : " + checkT.X + "," + checkT.Z); RLtile = checkT; } } } } } if (LUtile != null) { patrolPoints.Add(LUtile); } if (RUtile != null) { patrolPoints.Add(RUtile); } if (LLtile != null) { patrolPoints.Add(LLtile); } if (RLtile != null) { patrolPoints.Add(RLtile); } }
public void InvalidateTileGraph() { //called whenever a change to the world changes the pathfinding info tileGraph = null; }
public void BuildPathfinding() { WorldController.Instance.World.SetupPathfindingExample(); Path_TileGraph tileGraph = new Path_TileGraph(WorldController.Instance.World); }