1
\$\begingroup\$

I have implemented an ECS where more or less an Entity is just an ID, a Component is just a wrapper for data, and a System holds the logic on operating over different components. There is an Engine that has a list of the entities, a table of the components and a list of the different types of systems (update and draw systems).

I was working on handling input when I reached a conflict: how I would resolve opposing inputs (for example, pressing left and right at the same time). So I decided to try and implement the Command Pattern to work with the ECS.

But I'm stumped. I'm not sure how to create a command, let's say moveLeftCommand, without knowing the intimate details about each entity that would effect. Also, since all my logic is in the systems, (if I understand the Command Pattern) it should have an execute function which will carry out the command. How do I populate the execute command if there isn't any logic that "belongs" to an entity?

Thanks for any help!

\$\endgroup\$
1
\$\begingroup\$

How do I populate the execute command if there isn't any logic that "belongs" to an entity?

But there is a mechanism for applying logic to an entity: Systems. In ECS, Systems map behavior to entities that have a specific set of components (e.g. apply "move" logic to all entities that have a Transform and Velocity component). So in your case you would:

  1. Create a MoveCommand component with a direction.
  2. Create a MoveSystem that iterates over all entities with a MoveCommand component and a Position component (or its equivalent).
  3. In the MoveSystem modify the Position based on the direction in the MoveCommand and remove the component.
\$\endgroup\$
0
\$\begingroup\$

I hope this will be of some help.

I'm not sure it really matter if two keys are pressed at the same time because if I press forward and left then I move at diagonal left, forward and back does nothing because they cancel each other out its the same with left and right because they both have the same speed value they cancel each other out.

this is how I do my input handling:

 If Device.KeyBoredState.IsPressed(SharpDX.DirectInput.Key.A) Then
            _position.Z += _speed * CSng(Math.Sin(MathHelper.ToRadians(_yaw))) * _gametime.ElapsedGameTime.Milliseconds
            _position.X -= _speed * CSng(Math.Cos(MathHelper.ToRadians(_yaw))) * _gametime.ElapsedGameTime.Milliseconds

        End If
        If Device.KeyBoredState.IsPressed(SharpDX.DirectInput.Key.D) Then
            _position.Z -= _speed * CSng(Math.Sin(MathHelper.ToRadians(_yaw))) * _gametime.ElapsedGameTime.Milliseconds
            _position.X += _speed * CSng(Math.Cos(MathHelper.ToRadians(_yaw))) * _gametime.ElapsedGameTime.Milliseconds
        End If

        If Device.KeyBoredState.IsPressed(SharpDX.DirectInput.Key.W) Then
            _position.Z -= _speed * CSng(Math.Cos(MathHelper.ToRadians(_yaw))) * _gametime.ElapsedGameTime.Milliseconds
            _position.X -= _speed * CSng(Math.Sin(MathHelper.ToRadians(_yaw))) * _gametime.ElapsedGameTime.Milliseconds

        End If
        If Device.KeyBoredState.IsPressed(SharpDX.DirectInput.Key.S) Then
            _position.Z += _speed * CSng(Math.Cos(MathHelper.ToRadians(_yaw))) * _gametime.ElapsedGameTime.Milliseconds
            _position.X += _speed * CSng(Math.Sin(MathHelper.ToRadians(_yaw))) * _gametime.ElapsedGameTime.Milliseconds
        End If
\$\endgroup\$
  • \$\begingroup\$ This solves the left/right problem but doesn't directly answer the question about commands. \$\endgroup\$ – lozzajp Feb 14 '17 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.