Ejemplo n.º 1
0
        public void Update(GameCore game, World world, GameTime gameTime)
        {
            var delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (world.GameOver == true)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                {
                    game.ChangeState(GameCore.STATE_GAMEOVER);
                }
            }
            else
            {
                Input(game, world, delta);
                UpdateShip(world.Ship, delta);
                UpdateLasers(world.Lasers, delta);
                UpdateAsteroids(world.Asteroids, delta);
                ProcessCollisions(world);
                UpdateExplosions(world.Explosions, delta);
                if (AreAllAsteroidsDestroyed(world.Asteroids))
                {
                    world.TransitionToNewLevel = true;
                }
            }
        }
Ejemplo n.º 2
0
        private void Input(GameCore game, World world, float delta)
        {
            //Return to menu?
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                game.ChangeState(GameCore.STATE_GAMEOVER);
            }

            //countdown fire timer if _canFire is false
            if (!_canFire)
            {
                _fireTimer += delta;
                if (_fireTimer >= _fireDownTime)
                {
                    _canFire   = true;
                    _fireTimer = 0;
                }
            }
            float circle = MathHelper.Pi * 2;

            //Thrust
            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                var xVelocity = (float)(Math.Cos(world.Ship.Rotation) * world.Ship.Speed);
                var yVelocity = (float)(Math.Sin(world.Ship.Rotation) * world.Ship.Speed);

                world.Ship.Velocity = new Vector2(xVelocity, yVelocity);
            }

            //Brake
            if (Keyboard.GetState().IsKeyDown(Keys.Q))
            {
                world.Ship.Velocity = Vector2.Zero;
            }

            //Rotate to the left
            if (Keyboard.GetState().IsKeyDown(Keys.A))
            {
                world.Ship.Rotation -= world.Ship.TurnSpeed * delta;
                world.Ship.Rotation  = world.Ship.Rotation % circle;
            }
            //Rotate to the right
            if (Keyboard.GetState().IsKeyDown(Keys.D))
            {
                world.Ship.Rotation += world.Ship.TurnSpeed * delta;
                world.Ship.Rotation  = world.Ship.Rotation % circle;
            }

            //Fire
            if (Keyboard.GetState().IsKeyDown(Keys.Space) && _canFire)
            {
                var xVelocity = (float)(Math.Cos(world.Ship.Rotation) * _laserSpeed);
                var yVelocity = (float)(Math.Sin(world.Ship.Rotation) * _laserSpeed);
                var laser     = new Laser(world.LaserTex, world.LaserSnd, world.Ship.Position, world.Ship.Rotation, new Vector2(xVelocity, yVelocity));
                world.Lasers.Add(laser);
                _canFire = false;
            }
        }