在游戏开发中处理状态和状态变化有哪些好的技术?
我是一位经验丰富的游戏开发新手,我制作了一款相当简单但有效的 3D 平台游戏,并且正在寻找使架构更具可扩展性的方法。
在游戏开发中处理状态的好方法是什么?
一种非常幼稚的方法是使用多个布尔值,例如:
time_stopped = True
time_slow_motion = False
level_loaded = True
level_paused = True
level_completed = False
这是非常不灵活的,因为开发人员错误可能会同时导致 time_stopped = True
和 time_slow_motion = True
,这将破坏游戏逻辑。
我正在寻找一种更好的方法:
class TimeState:
STOPPED = 0
SLOW_MOTION = 1
NORMAL = 2
current_state = 0
def isStopped(self):
if current_state == self.STOPPED: return True
但是我如何以良好的方式处理多个状态组?我可以创建一个具有 LOADED、PAUSED 和 COMPLETED 状态的 LevelState。但是,如何处理重叠组,例如当 LevelState
和 TimeState
PAUSED
应始终相同时? 如何解决状态管理有没有好的设计模式或者好的实践?
处理状态变化的好方法是什么?
我的想法是要么给出状态对象,例如。 LevelState
回调函数在任何给定的状态更改时执行 - 或在每个游戏循环中轮询状态对象,例如 if level_state.changedTo() == LevelState.COMPLETED: doStuff()
。有更好的方法来处理这个问题吗?消息队列/事件?我不想将逻辑嵌入到状态对象中。
我还感兴趣的是,是否应该同时处理 changedFrom
和 changedTo
事件,以及如果由 changedFrom
触发的逻辑更改了状态会发生什么触发 changedTo
逻辑之前的对象 - 谁赢了?
我不需要特定于实现/语言的答案,而是需要有关明智地思考架构的好方法的提示。
I'm an experienced developer new to game development, and I have made a reasonably simple but working 3D platformer game and am looking for ways to make the architecture more scalable.
What is a good way to handle state in game development?
A very naive approach is to have multiple booleans like:
time_stopped = True
time_slow_motion = False
level_loaded = True
level_paused = True
level_completed = False
This is very inflexible since a developer error could lead to time_stopped = True
and time_slow_motion = True
at the same time, which would destroy game logic.
I'm looking for a better approach in the direction of:
class TimeState:
STOPPED = 0
SLOW_MOTION = 1
NORMAL = 2
current_state = 0
def isStopped(self):
if current_state == self.STOPPED: return True
But how would I handle multiple groups of state in a good way? I could create a LevelState
which had LOADED
, PAUSED
and COMPLETED
states. But how do I handle overlapping groups like when LevelState
and TimeState
PAUSED
should always be the same?
Is there a good design pattern or a good practice on how to solve state management?
What is a good way to handle state change?
My thoughts are to either give the state object eg. LevelState
callback functions to execute on any given state change - or to poll the state object each game loop like if level_state.changedTo() == LevelState.COMPLETED: doStuff()
. Are there better ways to handle this? Message queues/events? I don't want to embed logic into the state objects.
I'm also interested in if one should handle both changedFrom
and changedTo
events, and what would happen if logic triggered by changedFrom
changes the state of the object before the changedTo
logic is triggered - who wins?
I don't want implementation/language specific answers but tips on a good way to think architecturally wise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是很常见的事情,甚至有一个模式: 状态模式
一开始,它似乎这种方法有一些开销,但随着逻辑变得更加复杂,您将看到好处。
This is such a common thing that there is even a pattern: The State Pattern
At the start, it seems like this approach has some overhead, but as the logic gets more complicated you will see the benefits.