Defines a concrete physics algorithm. It provides methods to update the world objects' positions depending on a simple physics algorithm, which only contains gravity and the difficulty level.
Наследование: Physics
        public void UpdateCollisionTest()
        {
            WorldObject object1 = new WorldObject();
            object1.Position = new Vector2(0.0f, 0.0f);
            object1.Radius = 0.51f;

            WorldObject object2 = new WorldObject();
            object2.Position = new Vector2(0.0f, 1.0f);
            object2.Radius = 0.5f;

            WorldObject object3 = new WorldObject();
            object3.Position = new Vector2(200, 200);
            object3.Radius = 0.3f;

            WorldObject object4 = new WorldObject();
            object4.Position = new Vector2(200, 201);
            object4.Radius = 0.8f;

            WorldObject object5 = new WorldObject();
            object5.Position = new Vector2(200, 200.5f);
            object5.Radius = 0.1f;

            world.AddWorldObject(object1);
            world.AddWorldObject(object2);

            mockCollisionHandler.Setup(m => m.OnCollision(object1, object2));

            target = new SimplePhysicsAlgorithm(mockCollisionHandler.Object, world, configuration);
            target.Update(new GameTime(new TimeSpan(0, 0, 10, 3, 0), new TimeSpan(0, 0, 0, 0, 1)));

            world.AddWorldObject(object3);
            world.AddWorldObject(object4);
            world.AddWorldObject(object5);
            target.Update(new GameTime(new TimeSpan(0, 0, 10, 3, 1), new TimeSpan(0, 0, 0, 0, 1)));

            mockCollisionHandler.Verify(m => m.OnCollision(object1, object2), Times.Exactly(1));
            mockCollisionHandler.Verify(m => m.OnCollision(object3, object4), Times.Exactly(1));
            mockCollisionHandler.Verify(m => m.OnCollision(object4, object5), Times.Exactly(1));
        }
        public void UpdateMaxSpeedTest()
        {
            float MAX_VELOCITY = 299792458.0f;

            // create worldObjects with different speeds
            WorldObject worldObject1 = new WorldObject();
            worldObject1.Position = new Vector2(0.0f, 0.0f);
            worldObject1.Velocity = new Vector2(MAX_VELOCITY + 5.0f, MAX_VELOCITY + 1.0f);
            world.AddWorldObject(worldObject1);

            WorldObject worldObject2 = new WorldObject();
            worldObject2.Position = new Vector2(200, 200);
            worldObject2.Velocity = Vector2.One;
            world.AddWorldObject(worldObject2);

            WorldObject worldObject3 = new WorldObject();
            worldObject3.Position = new Vector2(-13.0f, 80.0f);
            /* a² + b² = c²
             * a = 50
             * b = Sqrt(MAX_VELOCITY² - 50²)
             * */

            worldObject3.Velocity = new Vector2(50, (float) Math.Sqrt(MAX_VELOCITY * MAX_VELOCITY - 50 * 50));
            world.AddWorldObject(worldObject3);

            planet.Mass = 0.0;

            Vector2 expectedVelocity1 = worldObject1.Velocity;
            expectedVelocity1.Normalize();
            expectedVelocity1 *= MAX_VELOCITY;
            Vector2 expectedVelocity2 = worldObject2.Velocity;
            Vector2 expectedVelocity3 = worldObject3.Velocity;

            target = new SimplePhysicsAlgorithm(mockCollisionHandler.Object, world, configuration);
            target.Update(new GameTime(new TimeSpan(0, 0, 10, 3, 0), new TimeSpan(0, 0, 0, 0, 1)));

            Assert.AreEqual(worldObject1.Velocity, expectedVelocity1);
            Assert.AreEqual(worldObject2.Velocity, expectedVelocity2);
            Assert.AreEqual(worldObject3.Velocity, expectedVelocity3);
        }
        public void UpdateDifficultyTest()
        {
            Mock<ConfigurationRetriever> mockConfRet = new Mock<ConfigurationRetriever>();
            mockConfRet.SetupGet(m => m.Difficulty).Returns(1);

            // set up a big planet
            Planet bigPlanet = new Planet();
            bigPlanet.IsFlexible = false;
            bigPlanet.Position = new Vector2(0, 0);
            bigPlanet.Radius = 300;
            world.AddWorldObject(bigPlanet);

            // set up a small planet
            float desiredDistance = 700;

            Planet smallPlanet = new Planet();
            smallPlanet.IsFlexible = true;
            smallPlanet.Position = new Vector2(desiredDistance, 0);

            // calculate velocity needed to circuit in orbit
            float smallPlanetVelocity = (float)Math.Sqrt(bigPlanet.Mass * G / desiredDistance / (N * 1000));
            smallPlanet.Velocity = new Vector2(0, smallPlanetVelocity);
            smallPlanet.Radius = 150;
            world.AddWorldObject(smallPlanet);

            target = new SimplePhysicsAlgorithm(mockCollisionHandler.Object, world, mockConfRet.Object);

            target.Update(new GameTime(new TimeSpan(0, 0, 10, 3, 0), new TimeSpan(0, 0, 0, 5, 10)));

            // check whether the distance between the planets is still about the same
            float distance = Vector2.Distance(bigPlanet.Position, smallPlanet.Position);

            Assert.IsTrue(distance <= desiredDistance + 30);
        }
        public void UpdateDeleteExplosionTest()
        {
            // create explosions
            Explosion explosion1 = new Explosion();
            Explosion explosion2 = new Explosion();
            Explosion explosion3 = new Explosion();
            explosion1.Health = 100;
            explosion2.Health = 100;
            explosion3.Health = 100;
            explosion1.CreationTime = new TimeSpan(0, 0, 0, 0, 1); // should be destroyed at (0, 0, 0, 0, 100)
            explosion2.CreationTime = new TimeSpan(0, 0, 0, 0, 250); // should be destroyed at (0, 0, 0, 0, 351)
            explosion3.CreationTime = new TimeSpan(0, 0, 0, 0, 34); // should be destroyed at (0, 0, 0, 0, 135)

            // create a mock world
            Mock<World> mockWorld = new Mock<World>();
            WorldObject[] worldObjects = { explosion1, explosion2, explosion3 };
            mockWorld.SetupGet(m => m.WorldObjects).Returns(worldObjects);
            mockWorld.Setup(m => m.RemoveWorldObject(explosion1));
            mockWorld.Setup(m => m.RemoveWorldObject(explosion3));

            // create target and call update
            target = new SimplePhysicsAlgorithm(mockCollisionHandler.Object, mockWorld.Object, configuration);
            target.Update(new GameTime(new TimeSpan(0, 0, 0, 0, 135), new TimeSpan(0, 0, 0, 0, 1)));

            // explosions 1 and 3 should be destroyed, explosion 2 should still be there
            Assert.IsTrue(explosion1.Health <= 0);
            Assert.IsTrue(explosion2.Health > 0);
            Assert.IsTrue(explosion3.Health <= 0);

            mockWorld.Verify(m => m.RemoveWorldObject(explosion1), Times.Exactly(1));
            mockWorld.Verify(m => m.RemoveWorldObject(explosion3), Times.Exactly(1));
        }
Пример #5
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;
        }
Пример #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;
        }