private void InitializeSearchNodes(Level level) { searchNodes = new SearchNode[Width * Height]; for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { SearchNode node = new SearchNode(); node.Position = new Point(x, y); node.Walkable = !level.GetTile(x, y).IsSolid(); if (node.Walkable) { node.Neighbors = new SearchNode[4]; searchNodes[x + y * Width] = node; } } } for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { SearchNode node = searchNodes[x + y * Width]; if (node == null || node.Walkable == false) { continue; } Point[] neighbors = new Point[] { new Point(x, y - 1), new Point(x, y + 1), new Point(x - 1, y), new Point(x + 1, y) }; for (int i = 0; i < neighbors.Length; i++) { Point position = neighbors[i]; if (position.X < 0 || position.X >= Width || position.Y < 0 || position.Y >= Height) { continue; } SearchNode neighbor = searchNodes[position.X + position.Y * Width]; if (neighbor == null || neighbor.Walkable == false) { continue; } node.Neighbors[i] = neighbor; } } } }
public override void Update(float elapsed) { if (shootTimer > 0) { shootTimer -= elapsed; } if (damageTimer > 0) { damageTimer -= elapsed; } for (int i = 0; i < Bullets.Count; i++) { Bullets[i].Update(elapsed); foreach (Entity e in Level.Entities) { if (Bullets[i].CollisionBox.Intersects(e.CollisionBox)) { Bullets[i].OnCollide(e); } } if (Level.GetTile(Bullets[i].X / Tile.Size, Bullets[i].Y / Tile.Size).IsSolid()) { Bullets[i].IsActive = false; } if (!Bullets[i].IsActive) { Bullets.RemoveAt(i); i--; } } if (Level.Complete && CollisionBox.Intersects(Level.ExitTile)) { Level.GoToNextLevel = true; } base.Update(elapsed); }
private void Move(float elapsed) { Vector2 wantedPosition = position; canMoveHorizontal = true; canMoveVertical = true; if (!CanFly) { // Moving LEFT if (velocity.X < 0) { wantedPosition.X += velocity.X * elapsed; int x1 = (int)(wantedPosition.X / Tile.Size); int x2 = (int)((wantedPosition.X + Width) / Tile.Size); int y1 = (int)(wantedPosition.Y / Tile.Size); int y2 = (int)((wantedPosition.Y + Height) / Tile.Size); for (int y = y1; y <= y2; y++) { if (Level.GetTile(x1, y).IsSolid()) { canMoveHorizontal = false; velocity.X = 0; } } } // Moving RIGHT if (velocity.X > 0) { wantedPosition.X += velocity.X * elapsed; int x1 = (int)(wantedPosition.X / Tile.Size); int x2 = (int)((wantedPosition.X + Width) / Tile.Size); int y1 = (int)(wantedPosition.Y / Tile.Size); int y2 = (int)((wantedPosition.Y + Height) / Tile.Size); for (int y = y1; y <= y2; y++) { if (Level.GetTile(x2, y).IsSolid()) { canMoveHorizontal = false; velocity.X = 0; } } } // reset X position wantedPosition.X = position.X; // Moving UP if (velocity.Y < 0) { wantedPosition.Y += velocity.Y * elapsed; int x1 = (int)(wantedPosition.X / Tile.Size); int x2 = (int)((wantedPosition.X + Width) / Tile.Size); int y1 = (int)(wantedPosition.Y / Tile.Size); int y2 = (int)((wantedPosition.Y + Height) / Tile.Size); for (int x = x1; x <= x2; x++) { if (Level.GetTile(x, y1).IsSolid()) { canMoveVertical = false; velocity.Y = 0; } } } // Moving DOWN if (velocity.Y > 0) { wantedPosition.Y += velocity.Y * elapsed; int x1 = (int)(wantedPosition.X / Tile.Size); int x2 = (int)((wantedPosition.X + Width) / Tile.Size); int y1 = (int)(wantedPosition.Y / Tile.Size); int y2 = (int)((wantedPosition.Y + Height) / Tile.Size); for (int x = x1; x <= x2; x++) { if (Level.GetTile(x, y2).IsSolid()) { canMoveVertical = false; velocity.Y = 0; } } } } if (canMoveHorizontal) { position.X += velocity.X * elapsed; } if (canMoveVertical) { position.Y += velocity.Y * elapsed; } }