The GameModel is responsible for updating the logic of the game repeatedly.
Ejemplo n.º 1
0
        public void RemovePlayerTest()
        {
            for (int i = 1; i < n; i++)
            {
                mockPlayers.Clear();
                players.Clear();

                for (int j = 0; j < i; j++)
                {
                    mockPlayers.Add(new Mock<Player>());
                    players.Add(mockPlayers[j].Object);
                }

                // create target
                GameModel target = new GameModel(mockFactory.Object, mockPhysics.Object, players, mockWorld.Object);

                for (int j = i - 1; j >= 0; j--)
                {
                    Player player = players[j];

                    // execute RemovePlayer(player)
                    target.RemovePlayer(player);

                    // verify that player has been removed from players
                    Assert.IsFalse(players.Contains(player));

                    // reset players for next test
                    players.Add(player);
                }
            }
        }
Ejemplo n.º 2
0
        public void UpdateTest()
        {
            for (int i = 1; i < n; i++)
            {
                mockPlayers.Clear();
                players.Clear();

                for (int j = 0; j < i; j++)
                {
                    mockPlayers.Add(new Mock<Player>());
                    players.Add(mockPlayers[j].Object);
                }

                // create target
                GameModel target = new GameModel(mockFactory.Object, mockPhysics.Object, players, mockWorld.Object);
                FrameState frameState = new FrameState();

                // set up mock functionalities
                mockPhysics.Setup(m => m.Update(frameState.GameTime));
                for (int j = 0; j < i; j++)
                {
                    mockPlayers[j].Setup(m => m.Update(frameState));
                }

                // execute Update(frameState)
                target.Update(frameState);

                // verify functionality of the Update function
                mockPhysics.Verify(m => m.Update(frameState.GameTime), Times.Exactly(1));
                for (int j = 0; j < i; j++)
                {
                    mockPlayers[j].Verify(m => m.Update(frameState), Times.Exactly(1));
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a new GameModel and its subcomponents.
        /// </summary>
        /// <returns>The created GameModel.</returns>
        public GameModel BuildModel()
        {
            // build game objects
            Planet planet = new Planet();
            planet.Radius = 300;

            // build planet in orbit
            int distance = 700;

            Planet planet2 = new Planet();
            planet2.Radius = 82;
            planet2.IsFlexible = true;
            planet2.Position = new Vector2(distance, 0);
            // calculate velocity needed to circuit in orbit
            float planet2Velocity = (float) Math.Sqrt(planet.Mass * GameAssets.G / distance / (GameAssets.N * 1000));
            planet2.Velocity = new Vector2(0, planet2Velocity);

            Spaceship spaceship1 = new Spaceship();
            Spaceship spaceship2 = new Spaceship();

            Player player1 = new HumanPlayer(1, this.playerHandler, Color.Green, this.configuration.GetKeyboardConfiguration(1));
            Player player2 = new HumanPlayer(2, this.playerHandler, Color.Orange, this.configuration.GetKeyboardConfiguration(2));
            player1.Spaceship = spaceship1;
            player2.Spaceship = spaceship2;

            player1.Spaceship.Position = new Vector2(-1900, 0);
            player2.Spaceship.Position = new Vector2(1900, 0);

            List<Player> players = new List<Player>();
            players.Add(player1);
            players.Add(player2);

            WorldObject[] worldObjects = {planet, planet2, spaceship1, spaceship2};

            // build ShortLifespanObjectFactory
            ShortLifespanObjectFactory shortLifespanObjectFactory = new SimpleShortLifespanObjectFactory();

            // build game model
            World world = new World(worldObjects);
            Physics physics = new SimplePhysicsAlgorithm(this.collisionHandler, world, configuration);
            GameModel gameModel = new GameModel(shortLifespanObjectFactory, physics, players, world);
            return gameModel;
        }
Ejemplo n.º 4
0
 private Player GetPlayerOfSpaceship(GameModel gameModel, Spaceship spaceship)
 {
     foreach (Player player in gameModel.Players)
     {
         if (player.Spaceship == spaceship)
         {
             return player;
         }
     }
     return null;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a new GameView and its subcomponents.
        /// </summary>
        /// <param name="model">The assigned GameModel.</param>
        /// <returns>The created GameView.</returns>
        public GameView BuildView(GameModel model)
        {
            WorldView worldView = new WorldView(model.World);
            GameView gameView;
            int i = 0; // to count the planets in the list of world objects

            foreach (WorldObject worldObject in model.World.WorldObjects)
            {
                if (worldObject is Spaceship)
                {
                    Player player = GetPlayerOfSpaceship(model, (Spaceship)worldObject);
                    worldView.AddWorldObjectView(new WorldObjectView(gameAssets.GetColoredSpaceship(player.PlayerColor), worldObject));
                }
                else if (worldObject is Planet)
                {
                    worldView.AddWorldObjectView(new WorldObjectView(gameAssets.GetModelPlanets()[i % gameAssets.GetModelPlanets().Length], worldObject));
                    i++;
                }
                else if (worldObject is Projectile)
                {
                    worldView.AddWorldObjectView(new WorldObjectView(gameAssets.ModelRocket, worldObject));
                }
            }

            gameView = new GameView(model, worldView);

            return gameView;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Builds a new game using the ruby script
        /// </summary>
        /// <returns>The new Game</returns>
        public GameModel BuildModel()
        {
            // build game objects
            LevelBuilder levelBuilder = new LevelBuilder(this.configuration, this.playerHandler);
            ScriptEngine scriptEngine = IronRuby.Ruby.CreateEngine();
            ScriptScope scriptScope = scriptEngine.CreateScope();
            scriptScope.SetVariable("level", levelBuilder);
            scriptScope.SetVariable("physic", new RubyPhysic(GameAssets.G, GameAssets.N));
            try
            {
                ScriptSource source = scriptEngine.CreateScriptSourceFromFile(this.path);
                source.Compile();
                source.Execute(scriptScope);
            }
            catch (Exception e)
            {
                string errorMessage = "[" + this.path + "]\nError: " + e.Message;
                System.Windows.Forms.MessageBox.Show(errorMessage);
            }

            // build ShortLifespanObjectFactory
            ShortLifespanObjectFactory shortLifespanObjectFactory = new SimpleShortLifespanObjectFactory();

            // build game model
            World world = new World(levelBuilder.WorldObjects);
            Physics physics = new SimplePhysicsAlgorithm(this.collisionHandler, world, configuration);
            GameModel gameModel = new GameModel(shortLifespanObjectFactory, physics, levelBuilder.Players, world);
            return gameModel;
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Creates a new game using its dedicated GameBuilder.
 /// A game consists of a model and its view.
 /// </summary>
 public virtual void CreateGame()
 {
     gameModel = gameBuilder.BuildModel();
     gameView = gameBuilder.BuildView(gameModel);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Creates a new GameView object.
 /// </summary>
 /// <param name="gameModel">The assigned GameModel..</param>
 /// <param name="worldView">The view of the World.</param>
 public GameView(GameModel gameModel, WorldView worldView)
 {
     this.gameModel = gameModel;
     this.worldView = worldView;
 }