Example #1
0
        public GameState Update(KeyboardState state, KeyboardState previousState)
        {
            if (state.IsKeyDown(Keys.Escape) && !previousState.IsKeyDown(Keys.Escape))
            {
                return(GameState.Title);
            }

            if (state.IsKeyDown(Keys.Down) && !previousState.IsKeyDown(Keys.Down))
            {
                this.HyperspaceJump();
            }

            if (state.IsKeyDown(Keys.Space) && !previousState.IsKeyDown(Keys.Space))
            {
                this.Shoot();
            }

            if (state.IsKeyDown(Keys.P) && !previousState.IsKeyDown(Keys.P))
            {
                this.Pause();
            }

            if (state.IsKeyDown(Keys.Left))
            {
                this.Left();
            }

            if (state.IsKeyDown(Keys.Right))
            {
                this.Right();
            }

            var upPressed = state.IsKeyDown(Keys.Up);

            this.Thrust(upPressed);

            //Draw(_screenCanvas, windowClientBounds.Width, windowClientBounds.Height, _score);
            if (_paused)
            {
                //if (_iPauseTimer > 30)
                //{
                //    sc.AddText("PAUSE", Justify.Center, 2500, 200, 400, width, height);
                //}

                _iPauseTimer--;
                if (_iPauseTimer < 0)
                {
                    _iPauseTimer = PauseInterval;
                }
            }
            else
            {
                if (!Ship.IsAlive() && _explosions.Count() == 0)
                {
                    if (!_score.HasReserveShips())
                    {
                        _inProcess = false;
                    }
                    else if (AsteroidBelt.IsCenterSafe())
                    {
                        _score.UseNewShip();
                        Ship = new Ship();
                    }
                }
                if (_explosions.Count() == 0 && !AsteroidBelt.Any())
                {
                    _iLevel++;
                    AsteroidBelt.StartBelt(_iLevel, AsteroidSize.Large);
                }

                Ship.Move();
                foreach (var bullet in _shipBullets)
                {
                    bullet.Move();
                }
                AsteroidBelt.Move();
                _explosions.Move();

                foreach (var bullet in _shipBullets)
                {
                    var point = bullet.GetCurrentLocation();
                    if (!bullet.Available() && CheckPointInAsteroid(point, _score))
                    {
                        _explosions.AddExplosion(point);
                        bullet.Disable();
                    }
                }

                if (Ship.IsAlive())
                {
                    foreach (var shipPoint in Ship.PointsTransformed)
                    {
                        var currLoc = Ship.GetCurrentLocation();
                        var point   = new Point(shipPoint.X + currLoc.X, shipPoint.Y + currLoc.Y);
                        if (!CheckPointInAsteroid(point, _score))
                        {
                            continue;
                        }
                        ExplodeShip();
                        break;
                    }
                }
            }

            // return the state that we should move to, title or game
            return(this.IsDone() ? GameState.Title : GameState.Game);
        }