gpt4 book ai didi

c++ - 游戏状态管理器 : how to point to and update current state

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:48:33 29 4
gpt4 key购买 nike

所以我一直在尝试遵循大学引擎开发任务的空白指南,但我在弄清楚如何使用我的 GameStateManager 访问和更新状态时遇到了问题在堆栈的顶部,在我的代码中也称为 TopState

这很简单,我在 main 中初始化并运行引擎,然后设置并推送第一个状态:

void main()
{
EApplication::Init();

EApplication* pApp = new EApplication();

pApp->GetGameStateManager()->SetState("TEST", new ETestState(pApp));

pApp->GetGameStateManager()->PushState("TEST");

pApp->GetGameStateManager()->UpdateGameStates(ETime::deltaTime);

EApplication::RunApp();
delete pApp;
}

然而,正如您在下面看到的,我的 GameStateManager 中的 UpdateGameStates 函数尚未执行任何操作,因为我不确定如何访问或指向当前状态,从而调用自己对应的Update函数:

GameStateManager:

#include "AppGSM\EGamestateManager.h"

EGameStateManager::EGameStateManager(){ topState = new char[8]; }

EGameStateManager::~EGameStateManager(){}

void EGameStateManager::SetState(const char *szName, EBaseGameState *state){}

void EGameStateManager::PushState(const char *szName){}

void EGameStateManager::PopState(){}

void EGameStateManager::ChangeState(const char *szName){}

const char* EGameStateManager::GetTopStateName()
{
return topState;
}

EBaseGameState* EGameStateManager::GetTopState()
{

}

void EGameStateManager::UpdateGameStates(float deltaTime)
{


}

void EGameStateManager::DrawGameStates(){}

我只是想知道当我尝试使用 GetTopState() 时应该指向什么,以及如何访问该状态的更新函数?

最佳答案

你想如何存储你的状态?对于“PushState”和“TopState”等,我假设您的讲师希望您实现自己的 EBaseGameState* 策略堆栈。

现在有很多假设。我假设 EBaseGameState 只是一个抽象数据类型,您必须实现从该状态继承的所有不同状态。因此,访问该状态的更新函数的一种简单方法是具有以下内容:

class EBaseGameState
{
public:
//other functions omitted
virtual void Update(float deltaTime) = 0;

};

class MenuState : EBaseGameState
{
public:
//other functions and implementation of Update() omitted
void Update(float deltaTime);
}

那么在调用代码中就很简单了

void EGameStateManager::UpdateGameStates(float deltaTime)
{
topStatePointer->Update(deltaTime);
}

实现堆栈的一个好方法是使用 Vector 并将新状态添加到它的后面并在需要时从它的后面弹出状态,但是我不明白为什么要存储状态的名称。你的导师想让你检查名字吗?我会问他/她一些澄清

关于c++ - 游戏状态管理器 : how to point to and update current state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19624539/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com