public override void Update(GameTime time, EntityManager manager) { DijkstraMap map = manager.world.civilianMaps[mapToUse]; int x = this.getEntityX();//(int) entity.location.Center.X / Tile.SIDE_LENGTH; int y = this.getEntityY();//(int) entity.location.Center.Y / Tile.SIDE_LENGTH; if (x < 0 || x >= map.Map.Length || y < 0 || y >= map.Map[0].Length) return; if (map.Map[x][y] < 2 || !entity.currentWorld.tiles[x][y].IsWalkable) entity.markForDelete = true; if (entity.collidingEntites.Count > 0 && flee == null) { Entity.Entity e = entity.collidingEntites[0]; int modX = (int) e.location.X / Tile.SIDE_LENGTH - x + 3; int modY = (int) e.location.Y / Tile.SIDE_LENGTH - y + 3; flee = new DijkstraMap(entity.currentWorld, 6, 6, x - 3, y - 3, 1, new int[] { modX, modY }); flee = flee.GenerateFleeMap(entity.currentWorld); fleeCount = 30; } if (flee != null) { entity.direction = this.findPos(flee, 1); fleeCount--; if (fleeCount <= 0) flee = null; } else entity.direction = this.findPos(map, 1); entity.Move(); }
public override void Update(GameTime time, EntityManager manager) { DijkstraMap playerMap = manager.world.playerMap; DijkstraMap fleeMap = manager.world.fleeMap; // Floats --> Ints (D.G. 4/5/15, 920pm) int pX = this.getEntityX() - playerMap.modX; int pY = this.getEntityY() - playerMap.modY; if (pX < 0 || pY < 0 || pX >= playerMap.Map.Length || pY >= playerMap.Map[0].Length) { return; } int dist = playerMap.Map[pX][pY]; Direction furtherDir = findPos(fleeMap, -1); Direction closerDir = findPos(playerMap, 1); if (dist < 3) { entity.direction = furtherDir; entity.Move(); entity.Attack(0, entity.inventory.GetEquippedPrimary()); } else { entity.direction = closerDir; entity.Move(); if (dist < 8) { entity.Attack(0, entity.inventory.GetEquippedPrimary()); } } }
public DijkstraMap Clone() { DijkstraMap clone = new DijkstraMap(); clone.goals = (int[][]) goals.Clone(); clone.mapWidth = mapWidth; clone.mapHeight = mapHeight; clone.modX = modX; clone.modY = modY; clone.map = new int[map.Length][]; for (int x = 0; x < map.Length; x++) { clone.map[x] = new int[map[x].Length]; for (int y = 0; y < map[x].Length; y++) clone.map[x][y] = map[x][y]; } return clone; }
//Updates the world every frame. public void tick(GameTime time) { Player player = manager.GetPlayer(); int pX = player.location.IntX / Tile.SIDE_LENGTH; //The actual player location. int pY = player.location.IntY / Tile.SIDE_LENGTH; //The actual player location. int width = Game1.Instance.GraphicsDevice.Viewport.Width / Tile.SIDE_LENGTH + 10; int height = Game1.Instance.GraphicsDevice.Viewport.Height / Tile.SIDE_LENGTH + 10; if (playerMap == null) { playerMap = new DijkstraMap(this, width, height, pX - 5, pY - 5, 1, new int[] { 5, 5 }); fleeMap = playerMap.Clone().GenerateFleeMap(this); } int[] pLoc = playerMap.Goals[0]; //The player location for AI's. if (pX != pLoc[0] || pY != pLoc[1]) { playerMap = new DijkstraMap(this, width, height, pX - 5, pY - 5, 1, new int[] { 5, 5 }); fleeMap = playerMap.Clone().GenerateFleeMap(this); } manager.Update(time); }
public DijkstraMap Clone() { DijkstraMap clone = new DijkstraMap(); clone.goals = (int[][])goals.Clone(); clone.mapWidth = mapWidth; clone.mapHeight = mapHeight; clone.modX = modX; clone.modY = modY; clone.map = new int[map.Length][]; for (int x = 0; x < map.Length; x++) { clone.map[x] = new int[map[x].Length]; for (int y = 0; y < map[x].Length; y++) { clone.map[x][y] = map[x][y]; } } return(clone); }
public override void Update(GameTime time, EntityManager manager) { DijkstraMap map = manager.world.civilianMaps[mapToUse]; int x = this.getEntityX(); //(int) entity.location.Center.X / Tile.SIDE_LENGTH; int y = this.getEntityY(); //(int) entity.location.Center.Y / Tile.SIDE_LENGTH; if (x < 0 || x >= map.Map.Length || y < 0 || y >= map.Map[0].Length) { return; } if (map.Map[x][y] < 2 || !entity.currentWorld.tiles[x][y].IsWalkable) { entity.markForDelete = true; } if (entity.collidingEntites.Count > 0 && flee == null) { Entity.Entity e = entity.collidingEntites[0]; int modX = (int)e.location.X / Tile.SIDE_LENGTH - x + 3; int modY = (int)e.location.Y / Tile.SIDE_LENGTH - y + 3; flee = new DijkstraMap(entity.currentWorld, 6, 6, x - 3, y - 3, 1, new int[] { modX, modY }); flee = flee.GenerateFleeMap(entity.currentWorld); fleeCount = 30; } if (flee != null) { entity.direction = this.findPos(flee, 1); fleeCount--; if (fleeCount <= 0) { flee = null; } } else { entity.direction = this.findPos(map, 1); } entity.Move(); }
/// <summary> /// Finds a position around the entity. /// </summary> /// <param name="map">The map to search.</param> /// <param name="hl">-1 (further) to 1 (closer)</param> /// <returns></returns> protected Direction findPos(DijkstraMap map, int hl) { int ex = getEntityX() * map.scale - map.modX; int ey = getEntityY() * map.scale - map.modY; if (ex < 0 || ey < 0 || ex >= map.Map.Length || ey >= map.Map[ex].Length) { return(entity.direction); } if (ey - 1 > -1) { if (map.Map[ex][ey - 1] == map.Map[ex][ey] - hl) //up { return(Direction.Up); } } if (ey + 1 < map.Map[ex].Length) { if (map.Map[ex][ey + 1] == map.Map[ex][ey] - hl) //down { return(Direction.Down); } } if (ex - 1 > -1) { if (map.Map[ex - 1][ey] == map.Map[ex][ey] - hl) //left { return(Direction.Left); } } if (ex + 1 < map.Map.Length) { if (map.Map[ex + 1][ey] == map.Map[ex][ey] - hl) //right { return(Direction.Right); } } if (ex - 1 > -1) { if (ey - 1 > -1) { if (map.Map[ex - 1][ey - 1] == map.Map[ex][ey] - hl) //upleft { return(Direction.UpLeft); } } if (ey + 1 < map.Map[ex].Length) { if (map.Map[ex - 1][ey + 1] == map.Map[ex][ey] - hl) //downleft { return(Direction.DownLeft); } } } if (ex + 1 < map.Map.Length) { if (ey - 1 > -1) { if (map.Map[ex + 1][ey - 1] == map.Map[ex][ey] - hl) //upright { return(Direction.UpRight); } } if (ey + 1 < map.Map[ex].Length) { if (map.Map[ex + 1][ey + 1] == map.Map[ex][ey] - hl) //downright { return(Direction.DownRight); } } } return(entity.direction); }
/// <summary> /// Finds a position around the entity. /// </summary> /// <param name="map">The map to search.</param> /// <param name="hl">-1 (further) to 1 (closer)</param> /// <returns></returns> protected Direction findPos(DijkstraMap map, int hl) { int ex = getEntityX()*map.scale - map.modX; int ey = getEntityY()*map.scale - map.modY; if (ex < 0 || ey < 0 || ex >= map.Map.Length || ey >= map.Map[ex].Length) return entity.direction; if (ey - 1 > -1) if (map.Map[ex][ey - 1] == map.Map[ex][ey] - hl) //up return Direction.Up; if (ey + 1 < map.Map[ex].Length) if (map.Map[ex][ey + 1] == map.Map[ex][ey] - hl) //down return Direction.Down; if (ex - 1 > -1) if (map.Map[ex - 1][ey] == map.Map[ex][ey] - hl) //left return Direction.Left; if (ex + 1 < map.Map.Length) if (map.Map[ex + 1][ey] == map.Map[ex][ey] - hl) //right return Direction.Right; if (ex - 1 > -1) { if (ey - 1 > -1) if (map.Map[ex - 1][ey - 1] == map.Map[ex][ey] - hl) //upleft return Direction.UpLeft; if (ey + 1 < map.Map[ex].Length) if (map.Map[ex - 1][ey + 1] == map.Map[ex][ey] - hl) //downleft return Direction.DownLeft; } if (ex + 1 < map.Map.Length) { if (ey - 1 > -1) if (map.Map[ex + 1][ey - 1] == map.Map[ex][ey] - hl) //upright return Direction.UpRight; if (ey + 1 < map.Map[ex].Length) if (map.Map[ex + 1][ey + 1] == map.Map[ex][ey] - hl) //downright return Direction.DownRight; } return entity.direction; }