Exemplo n.º 1
0
        public override void Update()
        {
            if (IsDead)
            {
                if (--framesUntilRespawn == 0)
                {
                    if (PlayerStatus.Lives == 0)
                    {
                        PlayerStatus.Reset();
                        Position = GameRoot.ScreenSize / 2;
                    }
                    GameRoot.Grid.ApplyDirectedForce(new Vector3(0, 0, 5000), new Vector3(Position, 0), 50);
                }

                return;
            }

            var aim = Input.GetAimDirection();

            if (aim.LengthSquared() > 0 && cooldowmRemaining <= 0)
            {
                cooldowmRemaining = cooldownFrames;
                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, 11f);

                Vector2 offset = Vector2.Transform(new Vector2(35, -8), aimQuat);
                EntityManager.Add(new Bullet(Position + offset, vel));

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

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

            if (cooldowmRemaining > 0)
            {
                cooldowmRemaining--;
            }

            const float speed = 8;

            Velocity += speed * Input.GetMovementDirection();
            Position += Velocity;
            Position  = Vector2.Clamp(Position, Size / 2, GameRoot.ScreenSize - Size / 2);

            if (Velocity.LengthSquared() > 0)
            {
                Orientation = Velocity.ToAngle();
            }

            MakeExhaustFire();
            Velocity = Vector2.Zero;
        }
Exemplo n.º 2
0
        /// <summary>
        /// The update loop for the player ship
        /// </summary>
        public override void Update()
        {
            // Check to see if the ship is dead
            if (IsDead)
            {
                // Wait until the ship can respawn
                if (--framesUntilRespawn == 0)
                {
                    // Check to see if the player has no more lives
                    if (PlayerStatus.Lives <= 0)
                    {
                        // Reset all game information and go back to the main menu
                        EntityManager.Reset();
                        PlayerStatus.Reset();
                        GameBase.State = GameBase.GameState.MainMenu;
                    }
                }

                // End the update loop
                return;
            }

            // NOTE: With regards to the firing, the maximum number of alloted shots on the screen is less than 10.
            // In this kind of game, I personally find it incredibly tedious to have to worry about when to fire and
            // a maximum carrying capacity of shots. Though it could be implemented easily, I prefer having an automatic
            // stream of bullets in this kind of game

            // Set the bullet aim direction as going directly up for the classic game mode
            Vector2 aim = new Vector2(0, -1);

            // Check to see if the player is in the free game mode
            if (GameBase.State == GameBase.GameState.FreeGameplay)
            {
                // Set the aim as the mouse direction
                aim = HandleInput.GetMouseAimDirection();
            }

            // If there is no more cooldown and there is a position to aim in
            if (aim.LengthSquared() > 0 && cooldowmRemaining <= 0)
            {
                // Reset the cooldown
                cooldowmRemaining = cooldownFrames;

                // Get the aim angle
                float      aimAngle = aim.ToAngle();
                Quaternion aimQuat  = Quaternion.CreateFromYawPitchRoll(0, 0, aimAngle);

                // Define the bullet velocity
                Vector2 vel = 8f * new Vector2((float)Math.Cos(aimAngle), (float)Math.Sin(aimAngle));

                // Define the bullet offset
                Vector2 offset = Vector2.Transform(new Vector2(40, -8), aimQuat);

                // Add the bullet to the entity manager
                EntityManager.Add(new Bullet(Position + offset, vel));

                // Player a shot sound effect
                Sound.Shot.Play(0.2f, rand.NextFloat(-0.2f, 0.2f), 0);
            }

            // If there is a cooldown remaining, continue to wait
            if (cooldowmRemaining > 0)
            {
                cooldowmRemaining--;
            }

            // Set the speed in pixels for the bullet
            const float speed = 8;

            // Set the velocity of the bullet as the speed times the movement direction
            Velocity += speed * HandleInput.GetMovementDirection();

            // Increase the position by the velocity and clamp it to the game window
            Position += Velocity;
            Position  = Vector2.Clamp(Position, Size / 2, GameBase.ScreenSize - Size / 2);

            // Set the bullet orientation
            if (Velocity.LengthSquared() > 0)
            {
                Orientation = Velocity.ToAngle();
            }

            // Generate the ship exhaust fire upon movement and reset the velocity
            MakeExhaustFire();
            Velocity = Vector2.Zero;
        }