public static Board getBoardFromBattleGameData(BattleGameData battleGameData, BattleGame battleGame) { Board b = new Board(battleGame, battleGameData.tileMapData.battleTileArray.GetLength(0)); b.board = copyTileArray(battleGameData.tileMapData.battleTileArray); return b; }
public static List<Point> Pathfind(Board board, int x, int y, int x2, int y2) { int Width = board.board.GetLength(1); int Height = board.board.GetLength(0); int[,] cost = new int[Width, Height]; cost[x, y] = 1; //floor type List<Point> active = new List<Point>(); active.Add(new Point(x, y)); // pathfind while (true) { // get lowest cost point in active list Point point = active[0]; for (int i = 1; i < active.Count; i++) { Point p = active[i]; if (cost[p.x, p.y] < cost[point.x, point.y]) point = p; } // if end point if (point.x == x2 && point.y == y2) break; // move in directions int currentCost = cost[point.x, point.y]; if (point.x - 1 >= 0 && cost[point.x - 1, point.y] == 0) { active.Add(new Point(point.x - 1, point.y)); cost[point.x - 1, point.y] = currentCost + getCost(board.board[point.x - 1, point.y]); } if (point.x + 1 < Width && cost[point.x + 1, point.y] == 0) { active.Add(new Point(point.x + 1, point.y)); cost[point.x + 1, point.y] = currentCost + getCost(board.board[point.x + 1, point.y]); } if (point.y - 1 >= 0 && cost[point.x, point.y - 1] == 0) { active.Add(new Point(point.x, point.y - 1)); cost[point.x, point.y - 1] = currentCost + getCost(board.board[point.x, point.y - 1]); } if (point.y + 1 < Height && cost[point.x, point.y + 1] == 0) { active.Add(new Point(point.x, point.y + 1)); cost[point.x, point.y + 1] = currentCost + getCost(board.board[point.x, point.y + 1]); } active.Remove(point); } // work backwards and find path List<Point> points = new List<Point>(); Point current = new Point(x2, y2); points.Add(current); while (current.x != x || current.y != y) { int highest = cost[current.x, current.y]; int left = highest, right = highest, up = highest, down = highest; // get cost of each direction if (current.x - 1 >= 0 && cost[current.x - 1, current.y] != 0) { left = cost[current.x - 1, current.y]; } if (current.x + 1 < Width && cost[current.x + 1, current.y] != 0) { right = cost[current.x + 1, current.y]; } if (current.y - 1 >= 0 && cost[current.x, current.y - 1] != 0) { up = cost[current.x, current.y - 1]; } if (current.y + 1 < Height && cost[current.x, current.y + 1] != 0) { down = cost[current.x, current.y + 1]; } // move in the lowest direction if (left <= GetMin(up, down, right)) { points.Add(current = new Point(current.x - 1, current.y)); } else if (right <= GetMin(left, down, up)) { points.Add(current = new Point(current.x + 1, current.y)); } else if (up <= GetMin(left, right, down)) { points.Add(current = new Point(current.x, current.y - 1)); } else { points.Add(current = new Point(current.x, current.y + 1)); } } points.Reverse(); return points; }
public static GameCharacter findNearestPlayer( GameCharacter enemy, Board board,List<GameCharacter> charList) { GameCharacter retval = null; int dist = 999; foreach(GameCharacter c in charList) { if (c.type == CharacterType.Player) { var pointList = PathFind.Pathfind(board, enemy.x, enemy.y, c.x, c.y); if (pointList.Count < dist) { dist = pointList.Count; retval = c; } } } return retval; }
//iterates over the path find and moves single spaces public static void moveToPlayer(GameCharacter enemy, GameCharacter target, Board board) { var pointList = PathFind.Pathfind(board, enemy.x, enemy.y, target.x, target.y); foreach(var p in pointList) { board.MoveCharacter(enemy, board.getTileFromLocation(p.x, p.y)); } }
private void LoadBoardFromData() { board = BoardFactory.getBoardFromBattleGameData(this.gameData, this); }