Exemplo n.º 1
0
        public override void Update(GameTime gameTime)
        {
            // if gameover wait for key press and then restart the game
            if (PlayerStatus.IsGameOver)
            {
                timeUntilRespawn = 0.25f;

                if (Input.WasKeyPressed(Keys.Enter) || Input.WasButtonPressed(Buttons.Start))
                {
                    PlayerStatus.Reset();
                }
                return;
            }

            if (IsDead)
            {
                // wait for time to respawn
                timeUntilRespawn -= (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (timeUntilRespawn <= 0)
                {
                }
                return;
            }

            // update velocity and position
            velocity += speed * Input.GetMovementDirection();
            position += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;

            // make sure the ship is always in the screen bounds
            position = Vector2.Clamp(position, Size / 2, GameRoot.ScreenSize - Size / 2);

            // shoot a bullet in the aim direction when the cooldown is also finnished
            var aim = Input.GetAimDirection();

            if (aim.LengthSquared() > 0 && cooldownRemaining <= 0)
            {
                cooldownRemaining = cooldownTime;
                float      aimAngle = aim.ToAngle();
                Quaternion aimQuat  = Quaternion.CreateFromYawPitchRoll(0, 0, aimAngle);

                float   randomSpread = rand.NextFloat(-0.04f, 0.04f) + rand.NextFloat(-0.04f, 0.04f);
                Vector2 vel          = MathUtil.FromPolar(aimAngle + randomSpread, bulletSpeed);

                // spawn two bullets infront of the player. one on the left side and other on the right
                Vector2 offset = Vector2.Transform(new Vector2(25, -8), aimQuat);
                EntityManager.Add(new Bullet(position + offset, vel));

                offset = Vector2.Transform(new Vector2(25, 8), aimQuat);
                EntityManager.Add(new Bullet(position + offset, vel));

                Sound.Shot.Play(0.2f, rand.NextFloat(-0.2f, 0.2f), 0);
            }

            // make the ship look in the direction of player
            if (aim.LengthSquared() > 0)
            {
                orientation = aim.ToAngle();
            }

            if (cooldownRemaining > 0)
            {
                cooldownRemaining -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            }

            MakeExhaustFire();
            velocity = Vector2.Zero;
        }
Exemplo n.º 2
0
        static void HandleCollisions()
        {
            // check collision for every enemy with every other enemy in the list
            for (int i = 0; i < enemies.Count; i++)
            {
                // starting from i as previous enemies have already checked for collision with the enemy at i
                // therefore no need to check again. +1 because we donot want to check collisions with self
                for (int j = i + 1; j < enemies.Count; j++)
                {
                    if (IsColliding(enemies[i], enemies[j]))
                    {
                        enemies [i].HandleCollision(enemies [j]);
                        enemies [j].HandleCollision(enemies [i]);
                    }
                }
            }

            // check collision for every bullet with every enemy
            for (int i = 0; i < enemies.Count; i++)
            {
                for (int j = 0; j < bullets.Count; j++)
                {
                    if (IsColliding(enemies [i], bullets [j]))
                    {
                        enemies [i].WasShot();
                        PlayerStatus.AddPoints(enemies [i].PointValue);
                        PlayerStatus.IncreaseMultiplier();
                        bullets [j].isExpired = true;
                    }
                }
            }

            // check collision of every enemy with the player
            for (int i = 0; i < enemies.Count; i++)
            {
                if (enemies[i].IsActive && IsColliding(enemies [i], PlayerShip.Instance))
                {
                    KillPlayer();
                    break;
                }
            }

            // check collision of all the blackholes with enemies, bullets and player
            // bullets cause damage to the blackhole while player and enemies are killed
            for (int i = 0; i < blackHoles.Count; i++)
            {
                for (int j = 0; j < enemies.Count; j++)
                {
                    if (enemies [j].IsActive && IsColliding(blackHoles [i], enemies[j]))
                    {
                        enemies [j].WasShot();
                    }
                }

                for (int j = 0; j < bullets.Count; j++)
                {
                    if (IsColliding(blackHoles[i], bullets[j]))
                    {
                        bullets [j].isExpired = true;
                        blackHoles [i].WasShot();
                    }
                }

                if (IsColliding(PlayerShip.Instance, blackHoles[i]))
                {
                    KillPlayer();
                    break;
                }
            }
        }