public Map() { Id = Guid.NewGuid(); Layers = new List<MapLayer>(); CollisionLayer = new MapCollisionLayer(Width, Height); MapEvents = new List<Event>(); MapEnemies = new List<Enemy>(); MapNpcs = new List<Npc>(); MapObjects = new List<MapObject>(); MapMatrix = new MapMatrix(); }
public Map(int width, int height) { Id = Guid.NewGuid(); Width = width; Height = height; Layers = new List<MapLayer>(); CollisionLayer = new MapCollisionLayer(Width, Height); MapMatrix = new MapMatrix(width, height); MapEvents = new List<Event>(); MapEnemies = new List<Enemy>(); MapNpcs = new List<Npc>(); MapObjects = new List<MapObject>(); }
public void CheckUnwalkableTiles(MapCollisionLayer collLayer) { Vector2 playerTile = MapHelper.GetTileFromPixels(Position); Vector2? cellUp, cellDown, cellLeft, cellRight; cellUp = cellDown = cellLeft = cellRight = null; var charRect = new Rectangle((int) (Position.X - CollisionRadius), (int) (Position.Y - CollisionRadius), (int) CollisionRadius*2, (int) CollisionRadius*2); if (playerTile.X > 0) cellLeft = new Vector2(playerTile.X - 1, playerTile.Y); if (playerTile.Y > 0) cellUp = new Vector2(playerTile.X, playerTile.Y - 1); if (playerTile.X < collLayer.Width - 1) cellRight = new Vector2(playerTile.X + 1, playerTile.Y); if (playerTile.Y < collLayer.Height - 1) cellDown = new Vector2(playerTile.X, playerTile.Y + 1); if ((cellUp != null) && (collLayer.GetCollisionValue(cellUp.Value) == CollisionValues.Impassable)) { Rectangle cellRect = MapHelper.GetRectangleForTile(cellUp.Value); if (cellRect.Intersects(charRect)) Position = new Vector2(Position.X, playerTile.Y*MapHelper.TileSize + CollisionRadius); } if ((cellDown != null) && (collLayer.GetCollisionValue(cellDown.Value) == CollisionValues.Impassable)) { Rectangle cellRect = MapHelper.GetRectangleForTile(cellDown.Value); if (cellRect.Intersects(charRect)) Position = new Vector2(Position.X, cellDown.Value.Y*MapHelper.TileSize - CollisionRadius); } if ((cellLeft != null) && (collLayer.GetCollisionValue(cellLeft.Value) == CollisionValues.Impassable)) { Rectangle cellRect = MapHelper.GetRectangleForTile(cellLeft.Value); if (cellRect.Intersects(charRect)) Position = new Vector2(playerTile.X*MapHelper.TileSize + CollisionRadius, Position.Y); } if ((cellRight != null) && (collLayer.GetCollisionValue(cellRight.Value) == CollisionValues.Impassable)) { Rectangle cellRect = MapHelper.GetRectangleForTile(cellRight.Value); if (cellRect.Intersects(charRect)) Position = new Vector2(cellRight.Value.X*MapHelper.TileSize - CollisionRadius, Position.Y); } }
public static void TrimMap(Map map, int width, int height) { map.Width = width; map.Height = height; var newCollisionLayer = new MapCollisionLayer(width, height); for (int y = 0; y < newCollisionLayer.Height; y++) { for (int x = 0; x < newCollisionLayer.Width; x++) { var tile = new Vector2(x, y); CollisionValues value = map.CollisionLayer.Width > tile.X && map.CollisionLayer.Height > tile.Y ? map.CollisionLayer.GetCollisionValue(tile) : CollisionValues.Passable; newCollisionLayer.SetCollisionValue(tile, value); } } map.CollisionLayer = newCollisionLayer; for (int i = 0; i < map.Layers.Count; i++) { var newLayer = new MapLayer(width, height); newLayer.Id = map.Layers[i].Id; for (int y = 0; y < newLayer.Height; y++) for (int x = 0; x < newLayer.Width; x++) { var tile = new Vector2(x, y); if (map.Layers[i].Width > x && map.Layers[i].Height > y) { newLayer.SetTexture(tile, map.Layers[i].Map[y, x]); } } map.Layers[i] = newLayer; } }
public MovementEngine(MapCollisionLayer collisionMap, int stepsPerUpdate) { PathFinding = new AStar(collisionMap, stepsPerUpdate); }
public AStar(MapCollisionLayer collisionMap, int stepsPerUpdate) { Collision = collisionMap; ProcessCollisionMap(collisionMap); this.stepsPerUpdate = stepsPerUpdate; }
private void ProcessCollisionMap(MapCollisionLayer collisionMap) { width = collisionMap.Width; height = collisionMap.Height; map = new int[collisionMap.Height, collisionMap.Width]; for (int x = 0; x < collisionMap.Width; x++) { for (int y = 0; y < collisionMap.Height; y++) { CollisionValues cv = collisionMap.GetCollisionValue(x, y); switch (cv) { case CollisionValues.Passable: case CollisionValues.Impassable: case CollisionValues.Slower80pc: map[y, x] = (int) cv; break; default: map[y, x] = 0; break; } } } }