开发者

What's a better way to store information than by using static ints? C++

I'm keeping track of a player's "job" by setting his job to a number, and incrementing it by one if he changes job, and determining which job he currently is by whether the number is even or odd. (Only two jobs right now). However, I know there are better ways of doing this, and soon I'll need to implement for a third and fourth job, so I cannot keep using the even/odd check.

Here's my code for reference: (Please note that I only include relevant code)

GameModeState.cpp

// If changeJob's parameter number is 1, it increments the job. If number is 2, it only  returns the current job
int GameModeState::changeJob(int number)
{
   // Default job is even (landman)
   static int job = 1;
   if (number == 1)
   {
    job = (job+1);
    return job;
   } 
   else 
   {
    return job;
   }
}

int GameModeState::getJob()
{
    int currentJob = (changeJob(2));
    return currentJob;
}

// If the player opens the "stat sheet", it changes their job
void GameModeState::_statSheet(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output)
{
    changeJob(1);
}

GameModeState.h

class GameModeState : public GameState::State
{
public:

    /// Changes the player's job if number is 1, or returns current job if number is 2
    static int changeJob(int number);

    /// Returns the current job number by calling changeJob appropriately
    static int getJob();

private:

    // Opening the player sheet will change the player's job
    void _statSheet(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output);
};

ZoneMovementState.cpp (This is where I check for current job)

#include "GameModeState.h"
#include <EnergyGraphics/ZoneParser.h>

    void ZoneMovementState::_changeZone(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output)
    {
        // If the number from getJob is even, the player is currently a geologist
        if (GameModeState::getJob()%2 == 0)
        {
            ZoneParser::getSingleton().load("../media/zones/geology_zone.xml", false);
        } 
        else //otherwise they are a开发者_如何学C landman
        {
            ZoneParser::getSingleton().load("../media/zones/landman_zone.xml", false);
        }
        transitionHandler->go();
    }

I'm thinking either arrays or enums of the jobs will be the better way to deal with this, but I'm not sure how to implement this into my code. If you know a better way, please include examples or at least a point in the right direction. I will greatly appreciate it!


Don't use static variables to save anything like that inside a class. Use a member variable instead.

IMO the easiest way to do something like that and make it extensible is using a enum:

enum PlayerJob
    JOB_NONE = 0,
    JOB_GEOLOGIST,
    JOB_LANDMAN,
    ...
    NUM_JOBS // this element is optional but can be useful for range checking.
};

...

PlayerJob job = JOB_NONE;

...

switch(job)
{
    case JOB_NONE:
        break;
    case JOB_GEOLOGIST:
        ...
        break;
    ...
    default:
        error("Unhandled palyer job: %d", job);
        break;
}

Also I'd think about somehow organizing such "job relevant" stuff into some kind of array or list or whatever to make it easier to call "job specific" things:

std::map<PlayerJob,std::string> jobzones;
jobzones.push_back(JOB_GEOLOGIST, "geozone.xml");

...

transitToZone(jobzones[job]);


Enums are nice, you may also think about using a std::stack or something similar for the GameState, so that you can push/pop etc.


You may want to look at the State pattern.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜