// Adds a series of enemies to the level randomly. public async Task SpawnEnemies() { // spawn snails in the level for (var x = 0; x < TileMap.Width; x++) { // flag for whether there's ground on this column of the level var groundFound = false; for (var y = 0; y < TileMap.Height; y++) { if (!groundFound) { if (TileMap.Tiles[y, x].Id == Super50Bros.Instance.TileIdGround) { groundFound = true; // random chance, 1, in 20 if (Super50Bros.Instance.Random.Next(20) == 0) { // instantiate snail, ceclaring in advane so we can pass it into state machine var snail = new Snail( "creatures", x * Super50Bros.Instance.TileSize, (y - 1) * Super50Bros.Instance.TileSize + 2, 16, 16, null, TileMap, Level); snail.StateMachine = new StateMachine(new Dictionary <string, State> { ["idle"] = new SnailIdleState(TileMap, Player, snail), ["moving"] = new SnailMovingState(TileMap, Player, snail), ["chasing"] = new SnailChasingState(TileMap, Player, snail) }); await snail.ChangeState("idle", new Dictionary <string, object> { ["wait"] = TimeSpan.FromSeconds(Super50Bros.Instance.Random.Next(5) + 1) }); Level.Entities.Add(snail); } } } } } }
public override async Task Update(TimeSpan dt) { if (WaitTimer < WaitPeriod) { WaitTimer += dt; } else { await Snail.ChangeState("moving"); } // calculate difference between snail and player on X axis // and only chase if <= 5 tiles var diffX = Math.Abs(Player.X - Snail.X); if (diffX < 5 * Super50Bros.Instance.TileSize) { await Snail.ChangeState("chasing"); } }
public override async Task Update(TimeSpan dt) { Snail.CurrentAnimation.Update(dt); // calculate difference between snail and player on X axis // and only chase if <= 5 tiles var diffX = Math.Abs(Player.X - Snail.X); if (diffX > 5 * Super50Bros.Instance.TileSize) { await Snail.ChangeState("moving"); } else if (Player.X < Snail.X) { Snail.Direction = Direction.Left; Snail.X -= Super50Bros.Instance.SnailMoveSpeed * dt.TotalSeconds; // stop the snail if there's a missing tile on the floor to the left or a solid tile directly left var tileLeft = TileMap.PointToTile(Snail.X, Snail.Y); var tileBottomLeft = TileMap.PointToTile(Snail.X, Snail.Y + Snail.Height); if ((tileLeft != null & tileBottomLeft != null) && (tileLeft.Collidable() || !tileBottomLeft.Collidable())) { Snail.X += Super50Bros.Instance.SnailMoveSpeed * dt.TotalSeconds; } } else { Snail.Direction = Direction.Right; Snail.X += Super50Bros.Instance.SnailMoveSpeed * dt.TotalSeconds; // stop the snail if there's a missing tile on the floor to the right or a solid tile directly right var tileRight = TileMap.PointToTile(Snail.X + Snail.Width, Snail.Y); var tileBottomRight = TileMap.PointToTile(Snail.X + Snail.Width, Snail.Y + Snail.Height); if ((tileRight != null & tileBottomRight != null) && (tileRight.Collidable() || !tileBottomRight.Collidable())) { Snail.X -= Super50Bros.Instance.SnailMoveSpeed * dt.TotalSeconds; } } }
public override async Task Update(TimeSpan dt) { MovingTimer += dt; Snail.CurrentAnimation.Update(dt); // reset movement direction and timer if timer is above duration if (MovingTimer > MovingDuration) { // chance to go into idle state randomly if (Super50Bros.Instance.Random.Next(4) == 0) { await Snail.ChangeState("idle", new System.Collections.Generic.Dictionary <string, object> { // random ammount of time for snail to be idle ["wait"] = TimeSpan.FromSeconds(Super50Bros.Instance.Random.Next(5) + 1) }); } else { MovingDirection = Super50Bros.Instance.Random.Next(2) == 0 ? Direction.Left : Direction.Right; Snail.Direction = MovingDirection; MovingDuration = TimeSpan.FromSeconds(Super50Bros.Instance.Random.Next(5)); MovingTimer = TimeSpan.Zero; } } else if (Snail.Direction == Direction.Left) { Snail.X -= Super50Bros.Instance.SnailMoveSpeed * dt.TotalSeconds; // stop the snail if there's a missing tile on the floor to the left or a solid tile directly left var tileLeft = Tilemap.PointToTile(Snail.X, Snail.Y); var tileBottomLeft = Tilemap.PointToTile(Snail.X, Snail.Y + Snail.Height); if ((tileLeft != null && tileBottomLeft != null) && (tileLeft.Collidable() || !tileBottomLeft.Collidable())) { Snail.X += Super50Bros.Instance.SnailMoveSpeed * dt.TotalSeconds; // reset direction if we hit a wall MovingDirection = Direction.Right; Snail.Direction = MovingDirection; MovingDuration = TimeSpan.FromSeconds(Super50Bros.Instance.Random.Next(5)); MovingTimer = TimeSpan.Zero; } } else { Snail.Direction = Direction.Right; Snail.X += Super50Bros.Instance.SnailMoveSpeed * dt.TotalSeconds; // stop the snail if there's a missing tile on the floor to the left or a solid tile directly left var tileRight = Tilemap.PointToTile(Snail.X + Snail.Width, Snail.Y); var tileBottomRight = Tilemap.PointToTile(Snail.X + Snail.Width, Snail.Y + Snail.Height); if ((tileRight != null && tileBottomRight != null) && (tileRight.Collidable() || !tileBottomRight.Collidable())) { Snail.X -= Super50Bros.Instance.SnailMoveSpeed * dt.TotalSeconds; // reset direction if we hit a wall MovingDirection = Direction.Left; Snail.Direction = MovingDirection; MovingDuration = TimeSpan.FromSeconds(Super50Bros.Instance.Random.Next(5)); MovingTimer = TimeSpan.Zero; } } // calculate difference between snail and player on X axis // and only chase if <= 5 tiles var diffX = Math.Abs(Player.X - Snail.X); if (diffX < 5 * Super50Bros.Instance.TileSize) { await Snail.ChangeState("chasing"); } }