Exemple #1
0
        /// <summary>
        /// Initiates a new game object.
        /// </summary>
        public Game()
        {
            _gameTime = new GameTime();
            ID = Interlocked.Increment(ref GameIDs);
            CollisionManager = new CollisionManager();

            Configuration = new GameConfiguration(GameRunner.Instance.Register(this));
        }
        public void CollisionManagerDetectsCollisions()
        {
            var cm = new CollisionManager();
            var rect = new Collidable(new BoundingRectangle(Vector2d.Zero, new Size2d(30, 50)));
            var circle = new Collidable(new BoundingCircle(Vector2d.Zero, 10));
            bool rectCollisionTriggered = false;
            bool circleCollisionTriggered = false;
            bool managerCollisionTriggered = false;

            cm.Monitor(rect);
            cm.Monitor(circle);

            rect.OnCollision += collisionData =>
            {
                Assert.Equal(circle, collisionData.With);
                rectCollisionTriggered = true;
            };

            circle.OnCollision += collisionData =>
            {
                Assert.Equal(rect, collisionData.With);
                circleCollisionTriggered = true;
            };

            cm.OnCollision += (first, second) =>
            {
                Assert.NotNull(first);
                Assert.NotNull(second);
                Assert.Equal(first, rect);
                Assert.Equal(second, circle);
                managerCollisionTriggered = true;
            };

            cm.Update(null);

            Assert.True(rectCollisionTriggered);
            Assert.True(circleCollisionTriggered);
            Assert.True(managerCollisionTriggered);

            rectCollisionTriggered = false;
            circleCollisionTriggered = false;
            managerCollisionTriggered = false;

            cm.Unmonitor(circle);
            cm.Update(null);

            Assert.False(rectCollisionTriggered);
            Assert.False(circleCollisionTriggered);
            Assert.False(managerCollisionTriggered);

            cm.Monitor(circle);
            cm.Update(null);

            Assert.True(rectCollisionTriggered);
            Assert.True(circleCollisionTriggered);
            Assert.True(managerCollisionTriggered);

            rectCollisionTriggered = false;
            circleCollisionTriggered = false;
            managerCollisionTriggered = false;

            circle.Dispose();

            cm.Update(null);

            Assert.False(rectCollisionTriggered);
            Assert.False(circleCollisionTriggered);
            Assert.False(managerCollisionTriggered);
        }