コード例 #1
0
        public void TerminateEntity()
        {
            // Arrange.
            int entityID = 76;

            CollidableComponent      collidable = new CollidableComponent(entityID);
            PositionComponent        position   = new PositionComponent(entityID, 43f, 0f);
            Texture2DRenderComponent texture    = new Texture2DRenderComponent(entityID, 7, null);

            componentManager
            .RegisterType <CollidableComponent, CollidableComponentCR>()
            .RegisterType <PositionComponent, PositionCR>()
            .RegisterType <Texture2DRenderComponent, Texture2DRenderCR>()
            .SetComponent(collidable)
            .SetComponent(position)
            .SetComponent(texture);

            // Act.
            ((IComponentTerminator)componentManager).TerminateEntity(entityID);

            // Assert.
            Assert.IsTrue(!componentManager.ContainsComponent <CollidableComponent>(entityID) &&
                          !componentManager.ContainsComponent <PositionComponent>(entityID) &&
                          !componentManager.ContainsComponent <Texture2DRenderComponent>(entityID));
        }
コード例 #2
0
        public void Remove_ShouldBeInOrder()
        {
            // Arrange.
            registry.Set(component1);
            registry.Set(component2);
            registry.Set(component3);
            registry.Set(component4);
            registry.Set(component5);
            registry.Set(component6);

            Texture2DRenderComponent[] orderedComponents = new Texture2DRenderComponent[] {
                component1, component5, component6, component2, component4
            };

            // Act.
            registry.Remove(component3.EntityID);

            // Assert.
            for (int i = 0; i < registry.Count; i++)
            {
                if (registry[i].EntityID != orderedComponents[i].EntityID)
                {
                    Assert.Fail();
                }
            }
            Assert.Pass();
        }
コード例 #3
0
        public override void Update(GameTime gameTime)
        {
            // Get pong world.
            IWorld world = worldManager.GetWorld((int)WorldID.PongWorld);

            // Iterate ball IDs.
            foreach (int id in ballIDs)
            {
                // Get ball position and physics.
                PhysicsComponent  physics  = world.ComponentManager.GetComponent <PhysicsComponent>(id);
                PositionComponent position = world.ComponentManager.GetComponent <PositionComponent>(id);

                // Flag to store whether the ball has been reset.
                bool reset = false;

                // If the ball is off screen on x axis, replace in middle, reset velocity and emit score event.
                if (position.X <= 0 && Math.Sign(physics.VelocityX) == -1)
                {
                    physics.VelocityX = 150;
                    eventManager.Emit(new ScoreEvent(1, 2));
                    reset = true;
                }
                else if (position.X >= engine.ScreenWidth && Math.Sign(physics.VelocityX) == 1)
                {
                    physics.VelocityX = -150;
                    eventManager.Emit(new ScoreEvent(1, 1));
                    reset = true;
                }

                // Check flag
                if (reset)
                {
                    // Get ball texture.
                    Texture2DRenderComponent texture = world.ComponentManager.GetComponent <Texture2DRenderComponent>(id);

                    // Update position.
                    position.X = (engine.ScreenWidth / 2) - (texture.Texture.Width / 2);
                    position.Y = (engine.ScreenHeight / 2) - (texture.Texture.Height / 2);

                    // Update components.
                    world.ComponentManager.SetComponent(physics);
                    world.ComponentManager.SetComponent(position);
                }

                // If ball is off y axis, flip direction.
                if (position.Y >= engine.ScreenHeight && Math.Sign(physics.VelocityY) == 1)
                {
                    physics.VelocityY = -150;
                    world.ComponentManager.SetComponent(physics);
                }
                else if (position.Y <= 0 && Math.Sign(physics.VelocityY) == -1)
                {
                    physics.VelocityY = 150;
                    world.ComponentManager.SetComponent(physics);
                }
            }
        }
コード例 #4
0
        public void Equals_ShouldNotBeEqual(int id1, int renderOrder1, int id2, int renderOrder2)
        {
            // Arrange.
            Texture2DRenderComponent component1;
            Texture2DRenderComponent component2;

            // Act.
            component1 = new Texture2DRenderComponent(id1, renderOrder1, null);
            component2 = new Texture2DRenderComponent(id2, renderOrder2, null);

            // Assert.
            Assert.IsFalse(component1 == component2);
        }
コード例 #5
0
        public void Equals_ShouldBeEqual(int entityID, int renderOrder)
        {
            // Arrange.
            Texture2DRenderComponent component1;
            Texture2DRenderComponent component2;

            // Act.
            component1 = new Texture2DRenderComponent(entityID, renderOrder, null);
            component2 = new Texture2DRenderComponent(entityID, renderOrder, null);

            // Assert.
            Assert.IsTrue(component1 == component2);
        }
コード例 #6
0
        public void InitComponent(int entityID, int renderOrder)
        {
            // Arrange.
            Texture2DRenderComponent component;

            // Act.
            component = new Texture2DRenderComponent(entityID, renderOrder, null);

            // Assert.
            Assert.IsTrue(entityID == component.EntityID &&
                          renderOrder == component.RenderOrder &&
                          component.Texture == null);
        }
コード例 #7
0
        public void Setup()
        {
            // Initialise registry.
            registry = new Texture2DRenderCR();

            // Initialise components.
            component1 = new Texture2DRenderComponent(40, 5, null);
            component2 = new Texture2DRenderComponent(600, 60, null);
            component3 = new Texture2DRenderComponent(435, 35, null);
            component4 = new Texture2DRenderComponent(143, 60, null);
            component5 = new Texture2DRenderComponent(369, 20, null);
            component6 = new Texture2DRenderComponent(3, 35, null);
        }
コード例 #8
0
        public void RemoveComponent()
        {
            // Arrange.
            Texture2DRenderComponent component = new Texture2DRenderComponent(55, 4, null);

            componentManager
            .RegisterType <Texture2DRenderComponent, Texture2DRenderCR>()
            .SetComponent(component);

            // Act.
            componentManager.RemoveComponent <Texture2DRenderComponent>(component.EntityID);

            // Assert.
            Assert.IsFalse(componentManager.ContainsComponent <Texture2DRenderComponent>(component.EntityID));
        }
コード例 #9
0
        /// <summary>
        /// Renders a world.
        /// </summary>
        /// <param name="world">The world to render.</param>
        private void RenderWorld(IWorld world)
        {
            // Store component manager.
            IComponentManager componentManager = world.ComponentManager;

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

            // Get component registries.
            IComponentRegistry <PositionComponent> positionComponents =
                (IComponentRegistry <PositionComponent>)componentManager.GetRegistry <PositionComponent>();
            IComponentRegistry <Texture2DRenderComponent> texture2DComponents =
                (IComponentRegistry <Texture2DRenderComponent>)componentManager.GetRegistry <Texture2DRenderComponent>();
            IComponentRegistry <TransformComponent> transformComponents =
                (IComponentRegistry <TransformComponent>)componentManager.GetRegistry <TransformComponent>();

            // Begin sprite batch.
            spriteBatch.Begin();

            // Iterate Texture 2D components.
            for (int i = 0; i < texture2DComponents.Count; i++)
            {
                // Get components.
                Texture2DRenderComponent texture   = texture2DComponents[i];
                PositionComponent        position  = positionComponents.Get(texture.EntityID);
                TransformComponent       transform = transformComponents.Get(texture.EntityID);

                // Render.
                spriteBatch.Draw(texture.Texture, new Vector2(position.X, position.Y), null, Color.White,
                                 transform.Rotation, new Vector2(0, 0), transform.Scale, SpriteEffects.None, 0);
            }

            // End sprite batch.
            spriteBatch.End();
        }
コード例 #10
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();
        }
コード例 #11
0
        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();
        }