public override void Update(GameTime gameTime)
        {
            // Get running worlds.
            IReadOnlyList <IWorld> worlds = worldManager.GetWorldsInState(WorldState.Running);

            // For each running world, update physics.
            foreach (IWorld world in worlds)
            {
                // Store component manager.
                IComponentManager componentManager = world.ComponentManager;

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

                // Get component registries.
                IComponentRegistry <PhysicsComponent> physicsComponents =
                    (IComponentRegistry <PhysicsComponent>)componentManager.GetRegistry <PhysicsComponent>();
                IComponentRegistry <PositionComponent> positionComponents =
                    (IComponentRegistry <PositionComponent>)componentManager.GetRegistry <PositionComponent>();

                // Iterate physics components.
                for (int i = 0; i < physicsComponents.Count; i++)
                {
                    // Get components
                    PhysicsComponent  physics  = physicsComponents[i];
                    PositionComponent position = positionComponents.Get(physics.EntityID);

                    // Update physics.

                    // Calculate delta time.
                    float dt = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000;

                    // Calculate velocity.
                    physics.VelocityX += (physics.AccelerationX * dt) + physics.GravityX;
                    physics.VelocityY += (physics.AccelerationY * dt) + physics.GravityY;

                    // Update position.
                    position.X += physics.VelocityX * dt;
                    position.Y += physics.VelocityY * dt;

                    // Store.
                    physicsComponents[i] = physics;
                    positionComponents.Set(position);
                }
            }
        }
Beispiel #2
0
        public override void Update(GameTime gameTime)
        {
            // Check if there events to process.
            if (scoreEvents.Count == 0)
            {
                return;
            }

            // Get HUD world.
            IWorld world = worldManager.GetWorld((int)WorldID.HudWorld);

            // Get text render component registry.
            IComponentRegistry <TextRenderComponent> textRenders =
                (IComponentRegistry <TextRenderComponent>)world.ComponentManager.GetRegistry <TextRenderComponent>();

            foreach (ScoreEvent score in scoreEvents)
            {
                // Update score and text.
                if (score.Player == 1)
                {
                    player1Score += score.Amount;
                    TextRenderComponent component = textRenders.Get(player1Text);
                    component.Text = player1Score.ToString();
                    textRenders.Set(component);
                }
                else
                {
                    player2Score += score.Amount;
                    TextRenderComponent component = textRenders.Get(player2Text);
                    component.Text = player2Score.ToString();
                    textRenders.Set(component);
                }
            }

            // Clear list.
            scoreEvents.Clear();
        }