public void InitComponent(int entityID)
        {
            // Arrange.
            HitboxCollisionComponent component;

            // Act.
            component = new HitboxCollisionComponent(entityID);

            // Assert.
            Assert.AreEqual(entityID, component.EntityID);
        }
        public void Equals_ShouldNotBeEqual(int entity1, int entity2)
        {
            // Arrange.
            HitboxCollisionComponent component1;
            HitboxCollisionComponent component2;

            // Act.
            component1 = new HitboxCollisionComponent(entity1);
            component2 = new HitboxCollisionComponent(entity2);

            // Assert.
            Assert.IsFalse(component1 == component2);
        }
        public void Equals_ShouldBeEqual(int entityID)
        {
            // Arrange.
            HitboxCollisionComponent component1;
            HitboxCollisionComponent component2;

            // Act.
            component1 = new HitboxCollisionComponent(entityID);
            component2 = new HitboxCollisionComponent(entityID);

            // Assert.
            Assert.IsTrue(component1 == component2);
        }
Exemple #4
0
        public override void Run()
        {
            // If there is no state, setup world.
            if (State == WorldState.None)
            {
                // Create ball.
                int ballID = EntityManager.CreateEntity();

                // Load texture.
                Texture2D ballTexture = ContentManager.Load <Texture2D>("square");

                // Create components.
                HitboxCollisionComponent ballHitbox   = new HitboxCollisionComponent(ballID);
                PhysicsComponent         ballPhysics  = new PhysicsComponent(ballID, 0, 0, 0, 0, 1, 150, 150);
                PositionComponent        ballPosition = new PositionComponent(ballID,
                                                                              (engine.ScreenWidth / 2) - (ballTexture.Width / 2), (engine.ScreenHeight / 2) - (ballTexture.Height / 2));
                Texture2DRenderComponent ballRender    = new Texture2DRenderComponent(ballID, 1, ballTexture);
                TransformComponent       ballTransform = new TransformComponent(ballID, ballTexture.Height, 0, 1, ballTexture.Width);

                // Add components.
                ComponentManager
                .SetComponent(ballHitbox)
                .SetComponent(ballPhysics)
                .SetComponent(ballPosition)
                .SetComponent(ballRender)
                .SetComponent(ballTransform);

                // Emit ball create event.
                EventManager.Emit(new BallCreateEvent(ballID));


                // Create player one paddle.
                int paddleAID = EntityManager.CreateEntity();

                // Load texture.
                Texture2D paddleATexture = ContentManager.Load <Texture2D>("paddle");

                // Create components.
                CollidableComponent      paddleACollidable = new CollidableComponent(paddleAID);
                PhysicsComponent         paddleAPhysics    = new PhysicsComponent(paddleAID, 0, 0, 0, 0, 1, 0, 0);
                PositionComponent        paddleAPosition   = new PositionComponent(paddleAID, 0, (engine.ScreenHeight / 2) - (paddleATexture.Height / 2));
                Texture2DRenderComponent paddleARender     = new Texture2DRenderComponent(paddleAID, 2, paddleATexture);
                TransformComponent       paddleATransform  = new TransformComponent(paddleAID, paddleATexture.Height, 0, 1, paddleATexture.Width);

                // Add components.
                ComponentManager
                .SetComponent(paddleACollidable)
                .SetComponent(paddleAPhysics)
                .SetComponent(paddleAPosition)
                .SetComponent(paddleARender)
                .SetComponent(paddleATransform);

                // Emit paddle create event.
                EventManager.Emit(new PaddleCreateEvent(paddleAID, 1));


                // Create player two paddle.
                int paddleBID = EntityManager.CreateEntity();

                // Load texture.
                Texture2D paddleBTexture = ContentManager.Load <Texture2D>("paddle");

                // Create components.
                CollidableComponent paddleBCollidable = new CollidableComponent(paddleBID);
                PhysicsComponent    paddleBPhysics    = new PhysicsComponent(paddleBID, 0, 0, 0, 0, 1, 0, 0);
                PositionComponent   paddleBPosition   = new PositionComponent(paddleBID, (engine.ScreenWidth - paddleBTexture.Width),
                                                                              (engine.ScreenHeight / 2) - (paddleBTexture.Height / 2));
                Texture2DRenderComponent paddleBRender    = new Texture2DRenderComponent(paddleBID, 2, paddleBTexture);
                TransformComponent       paddleBTransform = new TransformComponent(paddleBID, paddleBTexture.Height, 0, 1, paddleBTexture.Width);

                // Add components.
                ComponentManager
                .SetComponent(paddleBCollidable)
                .SetComponent(paddleBPhysics)
                .SetComponent(paddleBPosition)
                .SetComponent(paddleBRender)
                .SetComponent(paddleBTransform);

                // Emit paddle create event.
                EventManager.Emit(new PaddleCreateEvent(paddleBID, 2));
            }

            base.Run();
        }
        public override void Update(GameTime gameTime)
        {
            // Var to store collision event data.
            IList <CollisionEvent> collisionEvents = new List <CollisionEvent>();

            // Get running worlds.
            IReadOnlyList <IWorld> worlds = worldManager.GetWorldsInState(WorldState.Running);

            // Run through running worlds and check for collisions.
            foreach (IWorld world in worlds)
            {
                // Store component manager.
                IComponentManager componentManager = world.ComponentManager;

                // Check if the world has the required component registries.
                if (!componentManager.ContainsRegistry <CollidableComponent>() ||
                    !componentManager.ContainsRegistry <HitboxCollisionComponent>() ||
                    !componentManager.ContainsRegistry <PositionComponent>() ||
                    !componentManager.ContainsRegistry <TransformComponent>())
                {
                    continue;
                }

                // Get component registries.
                IComponentRegistry <CollidableComponent> collidableComponents =
                    (IComponentRegistry <CollidableComponent>)componentManager.GetRegistry <CollidableComponent>();
                IComponentRegistry <HitboxCollisionComponent> hitboxCollisionComponents =
                    (IComponentRegistry <HitboxCollisionComponent>)componentManager.GetRegistry <HitboxCollisionComponent>();
                IComponentRegistry <PositionComponent> positionComponents =
                    (IComponentRegistry <PositionComponent>)componentManager.GetRegistry <PositionComponent>();
                IComponentRegistry <TransformComponent> transformComponents =
                    (IComponentRegistry <TransformComponent>)componentManager.GetRegistry <TransformComponent>();

                // Iterate hitbox collision components.
                for (int i = 0; i < hitboxCollisionComponents.Count; i++)
                {
                    // Get components.
                    HitboxCollisionComponent hitbox    = hitboxCollisionComponents[i];
                    PositionComponent        position  = positionComponents.Get(hitbox.EntityID);
                    TransformComponent       transform = transformComponents.Get(hitbox.EntityID);

                    // Iterate collidable components and calculate collision.
                    for (int j = 0; j < collidableComponents.Count; j++)
                    {
                        // Get components for collidable entity.
                        CollidableComponent collidable          = collidableComponents[j];
                        PositionComponent   collidablePosition  = positionComponents.Get(collidable.EntityID);
                        TransformComponent  collidableTransform = transformComponents.Get(collidable.EntityID);

                        // Create hitboxes.
                        Rectangle entityA = new Rectangle((int)position.X, (int)position.Y, transform.Width, transform.Height);
                        Rectangle entityB = new Rectangle((int)collidablePosition.X, (int)collidablePosition.Y,
                                                          collidableTransform.Width, collidableTransform.Height);

                        // Perform collision detection.
                        if (entityA.Intersects(entityB))
                        {
                            // Entities collided.
                            collisionEvents.Add(new CollisionEvent(hitbox.EntityID, collidable.EntityID, world.ID));
                        }
                    }
                }
            }

            // An entity can be both a collider and a collidable, so filter out any events where this is true.
            // We could check this before performing the collision detection but we want to avoid
            // branch predictions in the hot path as much as possible.
            collisionEvents = collisionEvents.Where(x => x.Collidable != x.Collider).ToList();

            // Emit events.
            foreach (CollisionEvent collisionEvent in collisionEvents)
            {
                eventManager.Emit(collisionEvent);
            }
        }
        public override void Run()
        {
            // If there is no state, setup world.
            if (State == WorldState.None)
            {
                // Initialise random number generator.
                Random random = new Random();

                // Var to hold IDs of created balls.
                int[] ballIDs = new int[Pong.BallCount];

                // Create balls.
                for (int i = 0; i < ballIDs.Length; i++)
                {
                    // Create ball.
                    int ballID = EntityManager.CreateEntity();

                    // Load texture.
                    Texture2D ballTexture = ContentManager.Load <Texture2D>("square");

                    // Create components.
                    HitboxCollisionComponent ballHitbox   = new HitboxCollisionComponent(ballID);
                    PhysicsComponent         ballPhysics  = new PhysicsComponent(ballID, 0, 0, 0, 0, 1, 150, 150);
                    PositionComponent        ballPosition = new PositionComponent(ballID,
                                                                                  random.Next(200, engine.ScreenWidth - 201) - (ballTexture.Width / 2),
                                                                                  random.Next(150, engine.ScreenHeight - 151) - (ballTexture.Height / 2));
                    Texture2DRenderComponent ballRender    = new Texture2DRenderComponent(ballID, 1, ballTexture);
                    TransformComponent       ballTransform = new TransformComponent(ballID, ballTexture.Height, 0, 0.35f, ballTexture.Width);

                    // Add components.
                    ComponentManager
                    .SetComponent(ballHitbox)
                    .SetComponent(ballPhysics)
                    .SetComponent(ballPosition)
                    .SetComponent(ballRender)
                    .SetComponent(ballTransform);

                    // Add ID to array.
                    ballIDs[i] = ballID;
                }

                // Emit ball create event.
                EventManager.Emit(new BallCreateEvent(ballIDs));


                // Create player one paddle.
                int paddleAID = EntityManager.CreateEntity();

                // Load texture.
                Texture2D paddleATexture = ContentManager.Load <Texture2D>("paddle");

                // Create components.
                CollidableComponent      paddleACollidable = new CollidableComponent(paddleAID);
                PhysicsComponent         paddleAPhysics    = new PhysicsComponent(paddleAID, 0, 0, 0, 0, 1, 0, 0);
                PositionComponent        paddleAPosition   = new PositionComponent(paddleAID, 0, (engine.ScreenHeight / 2) - (paddleATexture.Height / 2));
                Texture2DRenderComponent paddleARender     = new Texture2DRenderComponent(paddleAID, 2, paddleATexture);
                TransformComponent       paddleATransform  = new TransformComponent(paddleAID, paddleATexture.Height, 0, 1, paddleATexture.Width);

                // Add components.
                ComponentManager
                .SetComponent(paddleACollidable)
                .SetComponent(paddleAPhysics)
                .SetComponent(paddleAPosition)
                .SetComponent(paddleARender)
                .SetComponent(paddleATransform);

                // Emit paddle create event.
                EventManager.Emit(new PaddleCreateEvent(paddleAID, 1));


                // Create player two paddle.
                int paddleBID = EntityManager.CreateEntity();

                // Load texture.
                Texture2D paddleBTexture = ContentManager.Load <Texture2D>("paddle");

                // Create components.
                CollidableComponent paddleBCollidable = new CollidableComponent(paddleBID);
                PhysicsComponent    paddleBPhysics    = new PhysicsComponent(paddleBID, 0, 0, 0, 0, 1, 0, 0);
                PositionComponent   paddleBPosition   = new PositionComponent(paddleBID, (engine.ScreenWidth - paddleBTexture.Width),
                                                                              (engine.ScreenHeight / 2) - (paddleBTexture.Height / 2));
                Texture2DRenderComponent paddleBRender    = new Texture2DRenderComponent(paddleBID, 2, paddleBTexture);
                TransformComponent       paddleBTransform = new TransformComponent(paddleBID, paddleBTexture.Height, 0, 1, paddleBTexture.Width);

                // Add components.
                ComponentManager
                .SetComponent(paddleBCollidable)
                .SetComponent(paddleBPhysics)
                .SetComponent(paddleBPosition)
                .SetComponent(paddleBRender)
                .SetComponent(paddleBTransform);

                // Emit paddle create event.
                EventManager.Emit(new PaddleCreateEvent(paddleBID, 2));
            }

            base.Run();
        }