Beispiel #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EnemyShip"/> class.
 /// </summary>
 /// <param name="entityManager">The entity manager.</param>
 /// <param name="position">The ship position.</param>
 /// <param name="speed">The ship speed.</param>
 /// <param name="size">The ship size.</param>
 /// <param name="player">The player.</param>
 public EnemyShip(EntityManager entityManager, Vector2 position, Vector2 speed, Sizes size, Player player)
 {
     this.entityManager = entityManager;
     random = new Random();
     Position = position;
     this.Size = size;
     this.speed = speed;
     this.player = player;
 }
Beispiel #2
0
        /// <summary>
        /// Remove dead asteroids from the map
        /// </summary>
        /// <param name="content">The content manager</param>
        /// <param name="graphicsDevice">The graphics device</param>
        /// <param name="player">The player</param>
        private void RemoveDeadAsteroids(ContentManager content, GraphicsDevice graphicsDevice, Player player)
        {
            /// Remove dead asteroids
            for (int i = asteroids.Count - 1; i >= 0; i--)
            {
                var asteroid = asteroids[i];
                if (!(asteroid.Killer is EnemyBullet) && asteroid.Dead)
                {
                    /// Add to the player's score
                    player.Score += asteroid.ScoreWorth;
                }

                if (asteroid.Dead)
                {
                    var nextSize = (asteroid.Size == Sizes.Large) ? Sizes.Medium : Sizes.Small;
                    var explosion = CreateExplosion(nextSize, asteroid.Position, content, graphicsDevice);
                    entityManager.Add(explosion);
                    explosion.PlayExplosionSound();

                    /// Split the asteroid into smaller asteroids.
                    var splitAsteroids = new List<Asteroid>();
                    if (asteroid.Size != Sizes.Small)
                    {

                        var a1 = new Asteroid(this, player, nextSize);
                        var a2 = new Asteroid(this, player, nextSize);
                        a1.Position = new Vector2(asteroid.Position.X, asteroid.Position.Y + 15);
                        a1.CurrentSpeed = asteroid.CurrentSpeed;
                        a2.Position = asteroid.Position;
                        a2.CurrentSpeed = new Vector2(-asteroid.CurrentSpeed.X, -asteroid.CurrentSpeed.Y);
                        splitAsteroids.AddRange(new Asteroid[] { a1, a2 });
                    }
                    /// Remove the asteroid
                    Remove(asteroid);

                    foreach (var newAsteroid in splitAsteroids)
                    {
                        entityManager.Add(newAsteroid);
                        asteroids.Add(newAsteroid);
                    }
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GameManager"/> class.
        /// </summary>
        /// <param name="entityManager">The entity manager.</param>
        /// <param name="player">The player.</param>
        public GameManager(EntityManager entityManager, Player player)
        {
            this.entityManager = entityManager;
            this.player = player;

            random = new Random();

            /// Set the time keepers to zero
            lastAsteroidSpawnTime = TimeSpan.Zero;
        }
Beispiel #4
0
        /// <summary>
        /// Update handled on game loop
        /// </summary>
        /// <param name="content">Content manager</param>
        /// <param name="graphicsDevice">Graphics device</param>
        /// <param name="player">The player</param>
        /// <param name="gameTime">The current game time</param>
        public void Update(ContentManager content, GraphicsDevice graphicsDevice, Player player, GameTime gameTime)
        {
            var spawnTimeReached = (gameTime.TotalGameTime - lastAsteroidSpawnTime) > asteroidSpawnTime;
            var spawnLimitReached = freshAsteroids.Count >= asteroidSpawnLimit;
            /// Determine if we can spawn an enemy ship
            if ((gameTime.TotalGameTime.TotalSeconds - lastShipSpawnTime) > shipSpawnTime
                && shipCount <= 0)
            {
                CreateEnemyShip(content, graphicsDevice);
                lastShipSpawnTime = gameTime.TotalGameTime.TotalSeconds;
            }
            /// Determine if we can spawn an asteroid
            var spawnNewAsteroid = spawnTimeReached && !spawnLimitReached;
            if (spawnNewAsteroid)
            {
                lastAsteroidSpawnTime = gameTime.TotalGameTime;

                /// Add an asteroid
                var asteroid = CreateAsteroid(content, graphicsDevice);
                freshAsteroids.Add(asteroid);
                entityManager.Add(asteroid);
            }
            /// Remove a dead asteroid so we don't have a memory leak
            RemoveDeadAsteroids(content, graphicsDevice, player);

            asteroidSpawnLimit = (int)Math.Floor((double)player.Score / 1000) + DefaultAsteroidCount;
            var previousLives = maxLives;
            maxLives = (int)Math.Floor((double)player.Score / 10000) + Player.DefaultLives;
            if (previousLives != maxLives)
            {
                player.Lives += 1;
            }
        }
Beispiel #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Titlescreen"/> class.
 /// </summary>
 /// <param name="player">The player.</param>
 public Titlescreen(Player player)
 {
     this.Player = player;
 }
Beispiel #6
0
        /// <summary>
        /// Initialize the new game. Creates any entities that need to be created
        /// in order to run the game.
        /// </summary>
        private void NewGame()
        {
            if (entityManager == null)
            {
                /// Create a new entity manager.
                entityManager = new EntityManager(Content, GraphicsDevice);
            }
            else
            {
                entityManager.Clear();
            }

            if (inputManager == null)
            {
                inputManager = new InputManager();
            }
            /// Create a new player.
            player = new Player(entityManager);
            if (asteroidManager == null)
            {
                asteroidManager = new GameManager(entityManager, player);
            }
            else
            {
                asteroidManager.Clear();
            }
            if (titleScreen == null)
            {
                /// Create a new title screen
                titleScreen = new Titlescreen(player);
            }
            else
            {
                titleScreen.Player = player;
            }
            entityManager.Add(new Background());
            entityManager.Add(player);
            entityManager.Add(new ScoreDisplay(new List<Player>(new Player[] { player })));
        }
Beispiel #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Asteroid"/> class.
 /// This is the class where we can pass in values to create an asteroid.
 /// </summary>
 /// <param name="asteroidManager">The asteroid manager.</param>
 /// <param name="player">The player.</param>
 /// <param name="newSize">The new size.</param>
 public Asteroid(GameManager asteroidManager, Player player, Sizes newSize)
 {
     this.asteroidManager = asteroidManager;
     this.player = player;
     this.Size = newSize;
 }
Beispiel #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Asteroid"/> class.
 /// This defaults to creating a large asteroid and uses default constructor values.
 /// </summary>
 /// <param name="asteroidManager">The asteroid manager.</param>
 /// <param name="player">The player.</param>
 public Asteroid(GameManager asteroidManager, Player player)
     : this(asteroidManager, player, Sizes.Large)
 {
 }