有什么比使用静态整数更好的存储信息的方法呢? C++

发布于 2024-11-01 11:07:49 字数 2253 浏览 2 评论 0原文

我通过将玩家的工作设置为一个数字来跟踪玩家的“工作”,如果他更换工作,则将其加一,并根据数字是偶数还是奇数来确定他当前的工作。 (现在只有两份工作)。但是,我知道有更好的方法可以做到这一点,很快我就需要实现第三个和第四个工作,所以我不能继续使用偶/奇检查。

这是我的参考代码:(请注意,我只包含相关代码)

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< /strong> (这是我检查当前作业的地方)

#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 landman
        {
            ZoneParser::getSingleton().load("../media/zones/landman_zone.xml", false);
        }
        transitionHandler->go();
    }

我认为作业的数组或枚举将是处理此问题的更好方法,但我不确定如何将其实现到我的代码中。如果您知道更好的方法,请提供示例或至少指出正确方向的一点。我将不胜感激!

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往事随风而去 2024-11-08 11:07:49

不要使用静态变量在类中保存类似的内容。请改用成员变量。

IMO 做类似事情并使其可扩展的最简单方法是使用枚举:

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;
}

此外,我还会考虑以某种方式将此类“与工作相关”的内容组织到某种数组或列表或其他任何内容中,以便更容易调用“特定于工作” “ 事物:

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

...

transitToZone(jobzones[job]);

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]);
随梦而飞# 2024-11-08 11:07:49

枚举很好,您还可以考虑使用 std::stack 或类似的 GameState,以便您可以推送/弹出等。

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.

愿与i 2024-11-08 11:07:49

您可能需要查看状态模式

You may want to look at the State pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文