コード例 #1
0
ファイル: Game.cs プロジェクト: w8w8w8/AsteroidsWasm
        /// <summary>
        /// Main game-play routine to determine object state and screen refresh.
        /// </summary>
        public void DrawScreen()
        {
            if (_paused)
            {
                // Pause flashes on and off
                if (_pauseTimer > PAUSE_INTERVAL / 2)
                {
                    _textDraw.DrawText(
                        "PAUSE"
                        , TextDraw.Justify.CENTER
                        , ScreenCanvas.CANVAS_HEIGHT / 3
                        , 200, 400
                        );
                }

                if (--_pauseTimer < 0)
                {
                    _pauseTimer = PAUSE_INTERVAL;
                }
            }
            else // Do all game processing if game is not paused
            {
                var origScore = _score.CurrentScore;

                // If no ship displaying, after explosions are done
                // get a new one - or end the game
                var noExplosions = _cache.ExplosionCount() == 0;

                if (!_cache.Ship.IsAlive && noExplosions)
                {
                    if (!_score.HasReserveShips())
                    {
                        // Game over
                        _inProcess = false;
                    }
                    else if (_collisionManager.IsCenterSafe())
                    {
                        _score.DecrementReserveShips();
                        _cache.UpdatedShip(new Ship());
                    }
                }

                // Create a new asteroid belt if no explosions and no asteroids
                if (noExplosions && _cache.Belt.Count() == 0)
                {
                    _cache.UpdateBelt(new AsteroidBelt(++_currentLevel));
                }

                // Move all objects starting with the ship
                _cache.Ship.Move();

                //Move the saucer and its missile
                if (_cache.Saucer != null)
                {
                    if (_cache.Saucer.Move())
                    {
                        //Aim for the ship oe nothing
                        var target = _cache.Ship.IsAlive
                            ? _cache.Ship.GetCurrLoc()
                            : default(Point?);

                        _cache.Saucer.Target(target);
                    }
                    else
                    {
                        //Saucer has completed its passes
                        _cache.UpdateSaucer(null);
                        _neededSaucerPoints = SAUCER_SCORE;
                    }
                }

                //Move the bullets, asteroids, and explosions
                _collisionManager.MoveBullets();
                _collisionManager.MoveAsteroids();
                _collisionManager.MoveExplosions();

                // Check bullets for collisions
                foreach (var bullet in _cache.GetBulletsInFlight())
                {
                    var points = new List <Point> {
                        bullet.Location
                    };

                    if (_collisionManager.AsteroidBeltCollision(points) ||
                        _collisionManager.SaucerCollision(points) ||
                        _collisionManager.MissileCollision(bullet.ScreenObject.GetPoints()))
                    {
                        _cache.AddExplosion(bullet.Location);
                        bullet.ScreenObject.Disable();
                    }
                }

                // Check ship for collisions
                if (_cache.Ship.IsAlive)
                {
                    var shipPoints = _cache.ShipPoints;

                    if (_collisionManager.SaucerCollision(shipPoints) ||
                        _collisionManager.MissileCollision(shipPoints) ||
                        _collisionManager.AsteroidBeltCollision(shipPoints)
                        )
                    {
                        foreach (var explosion in _cache.Ship.Explode())
                        {
                            _cache.AddExplosion(explosion);
                        }
                    }
                }

                //See if a bullet or the ship hit the saucer
                if (_cache.Saucer != null && !_cache.Saucer.IsAlive)
                {
                    _cache.UpdateSaucer(null);
                    _neededSaucerPoints = SAUCER_SCORE;
                }

                //See if the score is enough to show a suacer
                else if (_cache.Saucer == null)
                {
                    _neededSaucerPoints -= _score.CurrentScore - origScore;

                    if (_neededSaucerPoints <= 0)
                    {
                        var pt = new Point(
                            Random.Next(2) == 0 ? 0 : ScreenCanvas.CANVAS_WIDTH
                            , (Random.Next(10, 90) * ScreenCanvas.CANVAS_HEIGHT) / 100
                            );

                        _cache.UpdateSaucer(new Saucer(pt));
                    }
                }
            }

            //Commit the changes
            _cache.Repopulate();

            // Draw all objects
            _drawingManager.DrawObjects();
            _score.Draw();
        }