• 10
• 10
• 12
• 12
• 14

# OOD- Abusing States?

This topic is 2262 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Say I have a player class. This player is made up of a few things: A bounding box, a moveable entity, and animations.

When this player moves left, what is actually happening is a force pushing the moveable entity in the negative direction. A bounding box detects any on-coming obstacles. On the case of contact, the force applied needs to be halted immediately. Also, the animation state knows that RUN_LEFT should be playing as the player moves.

Cool.

But what about when the player jumps? The animation consists of an elaborate transition from a squat to an upright launch and finally the "in the air jumping" pose is reached. During this time of the animation, the user cannot make the player move or do anything else. When the player is "in the air" he can do something again.

Okay.

I thought about breaking up the player into pieces, or states. One state consists of the update moving left and checking for all left-stuff. Another state for moving right. Another state for mid-jump, and when the jump is completed; a final state for being in the air ( abs(Y velocity) > 0 && no_contact ).

If each state is represented as a class that the player can "be in", would this be over kill? The pros would be that it will 1) be easy to debug. We'd know exactly what state it is in if the logic breaks. 2) The pre-jump animation would play and move onto the next state satisfying the condition of the player being unable to do anything until this animation is completed and 3) the player's logic function doUpdate() will be very clean and not cluttered with flags.

The cons: More classes. Perhaps overkill.

If I choose to break the animation system into states instead, the player class would still be a tad bloated (but not too much) and I'd still have to clutter it with animation checks (i.e. if(inputRecieved() && !animation->is(PLAYER_PRE_JUMP) then ... )

So overkill? Or is there a better way to do this?

Thanks!

##### Share on other sites
It really depends on the scope of your project. If you know 100% that your player file won't get very complex, you're probably
best off defining a simple enumerated state and maybe defining some transition functions. If your player may get expansive,
I'd suggest using a more complex state system.

This prompted me to look up some of my old game source, in one of my games I found a polymorphic state machine.
Knowing what I know now, I would probably expand the state machine to use these states like a map, strictly defining
possible state transitions. In this example code, states transition themselves. I would maintain that behavior, but restrict
it to sibling nodes.

Using states like these are a great way to switch from hard-coded to scripted behavior. Instead of defining an object for
every state, you can have a ScriptedState object with a loaded script reference.

The global state defines character interactions with the world regardless of what state they are in.
You may be better off omitting this state and maintaining that behavior within the player class, it's up to you.

BaseObject.h
 template <class ENTITY_TYPE> class IBaseObjectState { public: IBaseObjectState(){} virtual ~IBaseObjectState(){} virtual void Begin(ENTITY_TYPE*)=0; virtual void Execute(ENTITY_TYPE*)=0; virtual void End(ENTITY_TYPE*)=0; }; 

CharacterStateIdle.h
 class CharacterStateIdle : public IBaseObjectState<PlayerObject> { private: float m_fTimeSpent; // The time we've been idle (to trigger the "waiting" animation) SCTimer *m_pTimer; // Constructor List CharacterStateIdle(); ~CharacterStateIdle(); CharacterStateIdle(const CharacterStateIdle &ref); CharacterStateIdle &operator=(const CharacterStateIdle &ref); public: void Begin(PlayerObject *pOwner); void Execute(PlayerObject *pOwner); void End(PlayerObject *pOwner); }; 

IObjectStateMachine.h
 template <class ENTITY_TYPE> class IObjectStateMachine { private: ENTITY_TYPE* m_pOwner; IBaseObjectState<ENTITY_TYPE>* m_pCurrentState; IBaseObjectState<ENTITY_TYPE>* m_pPreviousState; IBaseObjectState<ENTITY_TYPE>* m_pGlobalState; public: IObjectStateMachine(ENTITY_TYPE* owner) : m_pOwner(owner), m_pCurrentState(0), m_pPreviousState(0), m_pGlobalState(0); void SetCurrentState(IBaseObjectState<ENTITY_TYPE>* s){m_pCurrentState = s;} void SetGlobalState(IBaseObjectState<ENTITY_TYPE>* s) {m_pGlobalState = s;} void SetPreviousState(IBaseObjectState<ENTITY_TYPE>* s){m_pPreviousState = s;} void Update() const { if (m_pGlobalState) m_pGlobalState->Execute(m_pOwner); if (m_pCurrentState) m_pCurrentState->Execute(m_pOwner); } void ChangeState(IBaseObjectState<ENTITY_TYPE>* pNewState) { if (pNewState == m_pCurrentState) return; m_pPreviousState = m_pCurrentState; m_pCurrentState->End(m_pOwner); m_pCurrentState = pNewState; m_pCurrentState->Begin(m_pOwner); } void RevertState() { ChangeState(m_pPreviousState); } IBaseObjectState<ENTITY_TYPE>* CurrentState() const{return m_pCurrentState;} IBaseObjectState<ENTITY_TYPE>* GlobalState() const{return m_pGlobalState;} IBaseObjectState<ENTITY_TYPE>* PreviousState() const{return m_pPreviousState;} bool isInState(const IBaseObjectState<ENTITY_TYPE>& st) const; };