Ejemplo n.º 1
0
        // Called each frame
        public override void Update(GameTime gameTime)
        {
            // Update base Sprite
            base.Update(gameTime);

            // Fire a projectile if not currently cooling down
            if (!projectileCoolDown.Active)
            {
                // Generate projectile information
                Vector2 projectilePosition = new Vector2(position.X, position.Y + SpriteHeight / 2);
                Vector2 projectileVelocity = new Vector2(-10.0f, 0.0f);

                // Fire the projectile
                root.FireProjectile(projectilePosition, projectileVelocity, ProjectileType.Enemy);

                // Kick off the cool down process
                projectileCoolDown.StartTimer();
            }

            // Update the cool down timer
            projectileCoolDown.Update(gameTime);

            // Update position based on velocity
            position += velocity;
        }
Ejemplo n.º 2
0
        // Called each frame
        public override void Update(GameTime gameTime)
        {
            // Update sprite
            base.Update(gameTime);

            // Default up/down acceleration to zero
            acceleration.Y = 0;

            // Get current keyboard state
            KeyboardState currentKeyboardState = Keyboard.GetState();

            // Handle any movement input
            HandleInput(currentKeyboardState);

            // Fire a projectile if the cool down is not active and Space is pressed
            if (!projectileCoolDown.Active && currentKeyboardState.IsKeyDown(Keys.Space))
            {
                // Generate the projectile information
                Vector2 projectilePosition = new Vector2(position.X + SpriteWidth, position.Y + SpriteHeight / 2);
                Vector2 projectileVelocity = new Vector2(10.0f, 0.0f);

                // Fire the projectile
                root.FireProjectile(projectilePosition, projectileVelocity, ProjectileType.Player);

                // Kick off the cool down timer process
                projectileCoolDown.StartTimer();
            }

            // Update the cool down
            projectileCoolDown.Update(gameTime);

            // Update position based on velocity
            position += velocity;

            // Update velocity based on acceleration
            velocity += acceleration;

            // Update if colliding
            handleBounce();

            // Make sure the player does not move faster than the maximum speed
            velocity.Y = MathHelper.Clamp(velocity.Y, -maxMovementSpeed, maxMovementSpeed);
            velocity.X = MathHelper.Clamp(velocity.X, -maxMovementSpeed, maxMovementSpeed);

            // Control for extra tiny movements
            if (velocity.Y >= -0.1f && velocity.Y <= 0.1f)
            {
                velocity.Y = 0;
            }

            // Control for extra tiny movements
            if (velocity.X >= -0.1f && velocity.X <= 0.1f)
            {
                velocity.X = 0;
            }
        }