Tuesday, October 29, 2013

Components and Actions

In my last post, i talked about implementing the input system. Apparently, i it's much more work than i thought. However, i now have a working input system, which works as expected.

The whole system is implemented using several concepts: a device, which maps OS events into an Event structure, a key binding system, which maps an Event structure into an Intent structure, and the IntentSystem, which acts like a message dispatcher for Intents, which is poll based (hey mr. intent system, do you by any chance have a JUMP intent that i could use?).
At the top of the food chain, there's an IntentHandler action, which can have any number of entities registered into it, and they are set as targets of every output Intent. Then, at least one other action will poll for each Intent, and execute some logic for it.

Sounds pretty simple, but took me over a month to finally implement it. Though, to be fair, i was returning from my business trip from Sweden, had a 5k puzzle to finish, see a bunch of people, and was kinda lazy. :)

My next step is going to be designing the actions so they have clearly defined inputs and outputs as far as components are concerned. For instance, right now i'm thinking about how to make the physics simulation Box2D gives me work together with the components. An obvious one is that the output of the Physics2d action is the new position after Box2D has updated all bodies. The position is later used by the Render action to draw sprites at the right places on the screen. However, i'm discussing with myself (who else) about what  should be the input components. The input components should contain data required by the action to do its per frame update logic. Something like, in the case of Physics2d, read the Velocity2d component, and give the body a push in the right direction so that after the simulation, the velocity of the body within Box2D is the same as the one in the Velocity2d component. This is easily done by using impulses every frame, the faster the body is going, the smaller the impulse strength needs to be. Also, acceleration is done by providing the body with a force. If i'd want the body to have a specific acceleration, the formula given in physics classes is F=m*a, so i would just multiply the wanted acceleration with the body mass, and apply the given force to the body each frame.

The same goes for pretty much all actions, but the Physics2d is the most interesting one because a lot of the game depends on the simulation (collision detection and response being the biggest factor), so i need to be careful here.
Also, i think i'm gonna need a better way of handling intents, as right now each intent needs to be handled in three ways, either as a one time action (jumping, opening doors), a continuous action (running left, shooting an automatic weapon) or a range based action (moving the mouse around the screen, panning the camera). Any of these three actions could in theory be applied to any intent, like moving the entity based on the range a mouse movement causes, or having a jump that peeks only after you've held down your button for 2 seconds, making you able to do small jumps or large jumps.

Hope i'll make it for next weeks post. :)

No comments:

Post a Comment