/// <summary> /// Update the enemy's movement /// </summary> /// <param name="gameTime">A representation of the time since the last update</param> public void UpdateMovement(GameTime gameTime) { float relitiveX = World.player.position.X + World.player.offset.X - position.X; float relitiveY = World.player.position.Y + World.player.offset.Y - position.Y; float relitiveMagnitude = (float)Math.Sqrt(Math.Pow(relitiveX, 2) + Math.Pow(relitiveY, 2)); float tanDirection = MathHelper.ToDegrees((float)Math.Atan(relitiveY / relitiveX)); PlayerDirection = relitiveX < 0 ? tanDirection + 180 : tanDirection; float timeDeltaMultiplier = (float)((double)gameTime.ElapsedGameTime.TotalSeconds * (double)Constants.TIMEDELTACONSTANT); if (FollowPlayer && FollowingPlayer) { turnCounter = 0; turnTime = 0; turnRate = 0; breakTime = 0; breakTimer = 0; if (direction < PlayerDirection) { direction += timeDeltaMultiplier; } else if (direction > PlayerDirection) { direction -= timeDeltaMultiplier; } if (relitiveMagnitude < 75) { FollowingPlayer = false; turnCounter = 0; turnTime = 1; turnRate = World.random.Next(0, 2) == 0 ? -1 : 1; breakTime = turnTime + World.random.Next(2, 6); } } else if (FollowPlayer && !FollowingPlayer) { FollowingPlayer = breakTimer > breakTime; if (turnCounter < 2) { direction += turnRate * timeDeltaMultiplier; } turnCounter += gameTime.ElapsedGameTime.TotalSeconds; breakTimer += gameTime.ElapsedGameTime.TotalSeconds; } position.X += (float)(Velocity * timeDeltaMultiplier * Math.Cos(MathHelper.ToRadians(direction))); position.Y += (float)(Velocity * timeDeltaMultiplier * Math.Sin(MathHelper.ToRadians(direction))); drawPosition = position + offset; drawDirection = direction + 90; rotatedRectangle = new RotatedRectangle(CollisionRectangle, MathHelper.ToRadians(drawDirection)); }
/// <summary> /// Construct the enemy /// </summary> /// <param name="sprite">The sprite to render</param> /// <param name="startPos">The initial position of the enemy</param> /// <param name="followPlayer">If the enemy should follow the player</param> /// <param name="health">The maximum health of the player</param> /// <param name="velocity">The maximum velocity of the player</param> public Enemy(SpriteSheet.Sprite sprite, Vector2 startPos, bool followPlayer, int health, float velocity) : base(sprite, startPos, 0) { FollowingPlayer = FollowPlayer = followPlayer; Explosions = new Explosion[2]; for (int i = 0; i < Explosions.Length; i++) { Explosions[i] = new Explosion(SpriteSheet.ExplosionSprites); } Lasers = new Laser[10]; for (int i = 0; i < Lasers.Length; i++) { Lasers[i] = new Laser(SpriteSheet.Sprite.LaserRed, 6f); } Health = health; doUpdate = true; Velocity = velocity; PlayerDirection = 0; Alive = true; rotatedRectangle = new RotatedRectangle(CollisionRectangle, MathHelper.ToRadians(drawDirection)); }
public void Update(GameTime gameTime) { if (Keyboard.GetState().IsKeyDown(Keys.P) && debugSwitch) { debugPositions ^= true; debugSwitch = false; } else if (!Keyboard.GetState().IsKeyDown(Keys.P)) { debugSwitch = true; } if (0 == flickerCount++) { Stars[random.Next(Stars.Count)].doDraw ^= true; } flickerCount %= 20; foreach (Entity entity in Entities) { entity.Update(gameTime); } if (debugPositions) { int currentIndex = 0; for (int i = 0; i < player.CollisionPoints.Length; i++) { hitPointVisuals[currentIndex].coordinates = player.CollisionPoints[currentIndex++]; } hitPointVisuals[currentIndex++].coordinates = player.position; for (int i = 0; i < enemies.Length; i++) { RotatedRectangle newRectangle = (enemies[i].rotatedRectangle + enemies[i].position); hitPointVisuals[currentIndex++].coordinates = newRectangle.TopLeft; hitPointVisuals[currentIndex++].coordinates = newRectangle.TopRight; hitPointVisuals[currentIndex++].coordinates = newRectangle.BottomLeft; hitPointVisuals[currentIndex++].coordinates = newRectangle.BottomRight; } } CollisionDetection(); }
public bool Intersects(RotatedRectangle rectangle) { return(false); }