1
\$\begingroup\$

I'm creating a 2D game in which each possible direction of movement for the player, (Up, Down, Left, Right), has its own distinct animation, i.e., they aren't rotations of each other. Currently I am using a FSM design to manage this, but its getting messy already, and I know it will only get worse.

For example: (in Lua)

StateComponent = {
    states = {
        walking_left = {set_animation("walking_left")},
        walking_right = {set_animation("walking_right")},
        ...
    }
},

Now say I want to add attacking, the collection of states then becomes:

StateComponent = {
    states = {
        walking_left = {set_animation("walking_left")},
        walking_right = {set_animation("walking_right")},
        ...

        attacking_left = {...},
        attacking_right = {...},
        ...
    }
},

Okay, but now what about opening up a chest from each different side... This quickly becomes very complex. I read about a using a Behavior Tree, but it doesn't seem to support the discrete division of states necessary for having a different animation for walking/attacking/etc in each cardinal direction. What kind of design would be elegant and scalable for this problem? Or how else might I represent these states?

\$\endgroup\$
  • \$\begingroup\$ First of all, if you're able to further sort them, to stats = { walking = { left, right ...}, attacking = { left, right .. } ... }, It'd be a small change, but it's more organized than simply throwing it in a big table. \$\endgroup\$ – Giora Guttsait Aug 28 '16 at 7:11
  • \$\begingroup\$ Also, by the look of it, you can definitely load the StateComponent from a file, since it all looks template-y. So why not throw it in a configuration file? that way your code contains the logic of loading, and is not dealing with the data itself \$\endgroup\$ – Giora Guttsait Aug 28 '16 at 7:12
  • \$\begingroup\$ @GioraGuttsait your first idea is very interesting, i would like to see that expanded as an answer. But as for your second comment, i could definitely move it to a new file, however the states are more about functionality than data. \$\endgroup\$ – Nikolai97 Aug 28 '16 at 7:15
1
\$\begingroup\$

Since you asked to expand on my first part, here it is.

Currently, under states you save all your states, regardless of the relation between them, but we can clearly see that you can group them up to make it more organized.

So, this:

StateComponent = {
    states = {
        walking_left = {set_animation("walking_left")},
        walking_right = {set_animation("walking_right")},
        ...

        attacking_left = {...},
        attacking_right = {...},
        ...
    }
},

Becomes this:

StateComponent = {
    states = {
        walking = {
             left = {set_animation("walking_left")},
             right = {set_animation("walking_right")},
        }, 
        ...

        attacking = {
            left = { ... },
            right = { ...},
        }
        ....
    }
},

And then you would save two states in your code: state type and state direction (maybe word it a bit differently).

And then, you'd set the state by stats[type][direction] or something like that

\$\endgroup\$
  • \$\begingroup\$ This would certainly be a nice way to organize everything. Right now I've decided to try out multiple FSMs, one for animations, one for actions, and see if that will help with organizing everything. If not, I will definitely try this out. \$\endgroup\$ – Nikolai97 Aug 28 '16 at 7:27

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.