mercredi 22 avril 2020

Is this Polymorphism and is this bad practice?

I'm setting up a State system for my game.

class State
{
protected:
 enum State_
 {
     STATE_INTRO,
     STATE_GAME,
     STATE_PAUSE,
     STATE_CUTSCENE,
 };
public:
 State();
 virtual void run(State_) = 0;
 virtual ~State(); // virtual destructor cus we will be inheriting this class
};

I then have inherited classes which represent each state

class IntroState : public State
{

public:
    void run(State_ STATE);
};

I want the run function to have different functionality based off of what state the game is in, is it bad practice to achieve that like this:

void IntroState::run(State_ STATE)
{
    if (STATE == STATE_INTRO)
    {
        // load the intro
    }
}

I'm not sure how else to do this, thanks (and keep in my mind I'm JUST learning about states so I might be completely off here)

Aucun commentaire:

Enregistrer un commentaire