游戏实体:处理碰撞
我正在尝试制作一款 2D 游戏(我的第一个游戏)。我不是在寻找算法来确定对象是否发生碰撞,而是在寻找如何组织一切。我很难弄清楚哪个班级应该承担什么责任,以至于我开始觉得自己很愚蠢。 =))
我想我的主要类是 Entity
(及其子类)和 EntityManager
。例如,实体应该提供什么接口?实体应该如何意识到它们与另一个实体发生冲突 - 管理器是否应该更新它们并将 CollisionEvent
传递给每个实体的 handleCollision
函数?任何建议都非常受欢迎。
I'm trying to make a 2D game (my first one). I'm not looking for algorithms to determine whether or not objects collide, but rather how I should organize everything. I'm having great difficulty in figuring out what responsibility should go to which so class, so much so that I started feeling stupid. =))
I guess my principal classes are Entity
(and its children) and EntityManager
. What interface should Entity provide, for example? How should entities become aware that they are in collision with another entity — should the manager perhaps update them and pass a CollisionEvent
to the handleCollision
function of each entity? Any suggestions are more than welcomed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设 EntityManager 包含所有实体,因此管理器是需要检查所有实体之间的冲突的人。像这样
第一个可能是最简单的。但是,如果还有其他对象对此碰撞事件感兴趣怎么办?比如声音、记录或计分系统?即使是与该碰撞无关的实体也可能想“知道”该事件,以便他们可以改变自己的行为。 (想象一下,当头目怪物的孩子被你杀死时,它会更加愤怒!)
因此,为了使其更加灵活,#2 来了。首先,您需要拥有自己的事件或消息系统(您可以将其视为 Windows 消息系统),对象可以在其中订阅它们想要处理的特定消息。然后,EntityManager 可以简单地通过发送消息来传播碰撞事件。实体可以订阅此冲突消息类型,并且它们应该通过检查信息知道是否需要处理此特定冲突。同样,您的评分系统可以订阅它并计算新的击杀分数。
如果游戏足够简单,你可以选择#1,但我强烈推荐#2,你会对它非常满意。祝你好运! :)
I assume that EntityManager contains all entities so the manager is the one who needs to check collisons between all entities. like this
The first one is probably the simplest one. However, what if there are some other objects which are interested in this collision event? like sound, logging or scoring system? Even entities are which not related to that collision might want to "know" this event so that they can change their behavior. (Think a boss monster gets more angry when its kids are killed by you!)
So, to make it more flexible, #2 has come. First, you need to have your own
event-or-message system
( you can think it as Windows message system ) where objects can subscribe specific messages they want to handle. Then, EntityManager can simply propagate collision events by sending messages. Entities can subscribe this collision message type and they should know if they need to handle this particular collision by examining the info. Likewise, your scoring system can subscribe it and calculate new score for kills.If the game is simple enough, you could go for #1 but I highly recommend #2 and you will be very satisfied with it. Good Luck! :)