/// <summary>
        /// Checks collision between two rotated rectangles
        /// </summary>
        /// <param name="drawRectangle1">First rectangle</param>
        /// <param name="drawRectangle2">Second Rectangle</param>
        /// <returns>True or False</returns>
        public static bool CheckRotatedCollision(Asteroid asteroid, Projectile projectile)
        {
            float distance = 0.0f;

            Vector2 position1 = asteroid.Position;
            Vector2 position2 = projectile.Position;

            float Cathetus1 = Math.Abs(position1.X - position2.X);
            float Cathetus2 = Math.Abs(position1.Y - position2.Y);

            Cathetus1 *= Cathetus1;
            Cathetus2 *= Cathetus2;

            distance = (float)Math.Sqrt(Cathetus1 + Cathetus2);

            if ((int)distance < asteroid.CollisionRectangle.Width)
            {
                return true;
            }

            return false;
        }
 /// <summary>
 /// Adds the given projectile to the game
 /// </summary>
 /// <param name="projectile">the projectile to add</param>
 public static void AddProjectile(Projectile projectile)
 {
     projectiles.Add(projectile);
 }
Ejemplo n.º 3
0
        public void Update(GameTime gameTime, KeyboardState keyboard)
        {
            //Ship moves only if still has health
            if (health > 0)
            {
                if (keyboard.IsKeyDown(Keys.Up))
                {
                    AccelerateShip();
                }

                if (keyboard.IsKeyDown(Keys.Down))
                {
                    DecelerateShip();
                }

                if (keyboard.IsKeyDown(Keys.Left))
                {
                    this.Rotation -= 0.05f;
                }

                if (keyboard.IsKeyDown(Keys.Right))
                {
                    this.Rotation += 0.05f;
                }

                //Move the ship
                this.Position += this.Velocity;

                if (this.Position.X + this.drawRectangle.Width < 0)
                {
                    this.Position = new Vector2(GameConstants.VIEWPORT_WIDTH, this.Position.Y);
                }
                if (this.Position.X - this.drawRectangle.Width > GameConstants.VIEWPORT_WIDTH)
                {
                    this.Position = new Vector2(0, this.Position.Y);
                }
                if (this.Position.Y + this.drawRectangle.Height < 0)
                {
                    this.Position = new Vector2(this.Position.X, GameConstants.VIEWPORT_HEIGHT);
                }
                if (this.Position.Y - this.drawRectangle.Height > GameConstants.VIEWPORT_HEIGHT)
                {
                    this.Position = new Vector2(this.Position.X, 0);
                }

                //Shooting logic
                if (!canShoot)
                {
                    elapsedCooldownTime += gameTime.ElapsedGameTime.Milliseconds;
                    if (elapsedCooldownTime >= GameConstants.SHIP_COOLDOWN_MILLISECONDS ||
                        keyboard.IsKeyUp(Keys.Space))
                    {
                        canShoot = true;
                        elapsedCooldownTime = 0;
                    }
                }

                //Shoot if alive
                if (health > 0 && keyboard.IsKeyDown(Keys.Space) && canShoot)
                {
                    canShoot = false;

                    Vector2 velocity = new Vector2(
                        (float)Math.Cos(this.Rotation - (float)MathHelper.PiOver2),
                        (float)Math.Sin(this.Rotation - (float)MathHelper.PiOver2));

                    velocity.Normalize();
                    velocity *= GameConstants.PROJECTILE_SPEED;

                    Vector2 position = this.position + velocity;

                    Projectile projectile = new Projectile(MainGameScreen.GetProjectileSprite(), position, velocity);

                    MainGameScreen.AddProjectile(projectile);
                    shootSound.Play();

                }

            }
        }