Defines an object in the world.
        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));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reacts to a collision that happend in the game depending on 
        /// the type of the two assigned WorldObjects.
        /// This method creates an explosion, reduces the health of the WorldObject, if necessary,
        /// and checks, if one of the involved world objects has to be removed from the world.
        /// </summary>
        /// <param name="collisionObject1">The first WorldObject, which was involved in the collision.</param>
        /// <param name="collisionObject2">The second WorldObject, which was involved in the collision.</param>
        public void OnCollision(WorldObject collisionObject1, WorldObject collisionObject2)
        {
            Debug.Assert(gameModel != null);

            Vector2 posExplosion = new Vector2();
            Vector2 deltaPos = new Vector2(collisionObject2.Position.X - collisionObject1.Position.X, collisionObject2.Position.Y - collisionObject1.Position.Y);

            posExplosion.X = collisionObject1.Position.X + deltaPos.X / 2;
            posExplosion.Y = collisionObject1.Position.Y + deltaPos.Y / 2;

            // creating the Explosion
            Explosion explosion = gameModel.GetFactory().CreateExplosion(gameTime);
            explosion.Position = posExplosion;
            WorldObjectView explosionView = gameModel.GetFactory().CreateExplosionView(explosion);

            // adding the created Explosion to the Game
            gameModel.World.AddWorldObject(explosion);
            gameView.WorldView.AddWorldObjectView(explosionView);

            // reducing the health of the colliding objects
            collisionObject1.Health -= collisionObject2.Attack;
            collisionObject2.Health -= collisionObject1.Attack;

            /* checking whether the colliding objects are still "alive". Check the object with
             * less health first to make the player with less health lose the game in case both
             * spaceships died. */
            WorldObjectView[] worldObjectViews = gameView.WorldView.WorldObjectViews;

            if (collisionObject1.Health <= collisionObject2.Health)
            {
                checkDeath(collisionObject1, worldObjectViews);
                checkDeath(collisionObject2, worldObjectViews);
            }
            else
            {
                checkDeath(collisionObject2, worldObjectViews);
                checkDeath(collisionObject1, worldObjectViews);
            }
        }
        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);
        }
Exemplo n.º 4
0
        private void checkDeath(WorldObject collisionObject, WorldObjectView[] worldObjectViews)
        {
            // If the object is dead, remove it and its view from the World and -View.
            if (collisionObject.Health <= 0)    // --> object is dead
            {
                gameModel.World.RemoveWorldObject(collisionObject);

                /* If the object is a spaceship, delete the according player from the players list
                 * and check whether there was only two players left, meaning the other player has won
                 * the game. */
                if (collisionObject is Spaceship)
                {
                    Player[] players = gameModel.Players;
                    foreach (Player player in players)
                    {
                        if (player.Spaceship == collisionObject)
                        {
                            gameModel.RemovePlayer(player);
                            // checking whether the game has ended and determining the winner
                            if (players.Length == 2)
                            {
                                if (players[0] == player)
                                {
                                    gameHandler.OnGameEnded(players[1].PlayerId);
                                }
                                else
                                {
                                    gameHandler.OnGameEnded(players[0].PlayerId);
                                }
                            }
                        }
                    }
                }
            }
        }