【发布时间】:2011-08-05 13:43:43
【问题描述】:
我通过将玩家的工作设置为一个数字来跟踪玩家的“工作”,如果他改变工作,则将其加一,并通过该数字是偶数还是奇数来确定他当前的工作。 (现在只有两个工作)。但是,我知道有更好的方法可以做到这一点,很快我就需要实施第三和第四个工作,所以我不能继续使用偶数/奇数检查。
这是我的代码供参考:(请注意,我只包含相关代码)
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(这是我检查当前工作的地方)
#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();
}
我认为作业的数组或枚举将是处理此问题的更好方法,但我不确定如何在我的代码中实现这一点。如果您知道更好的方法,请包括示例或至少一个正确方向的点。我将不胜感激!
【问题讨论】:
标签: c++ arrays enums static-methods code-organization