我正在开发一个 2D 游戏的基础。我的一般设计是这样的:
class Entity:
Every object class (like a wall, an enemy, floor etc.) derives
from this class. Most of the functions are pure virtual. There is
a hitbox as well.
class Scene:
Contains pointers to Entity-objects. When an Entity-pointer is added,
it will be given a Scene-pointer to its parent so it may access that.
场景还具有碰撞检测功能:
getIntersections<T> (Entity *)
getIntersections<T...> (Entity *)
(both return a std::vector<Entity *>)
这基本上让所有Entity *
s 与参数相交Entity *
- (通过检查命中框)然后尝试dynamic_cast<T *>
它们。然后返回所有匹配Entity *
的 s(不是强制转换的)。可变参数模板用于检查多个相交类。
我背后的基本想法是,如果我有一个Player
-class(显然代表玩家)和其他一些类,如Enemy
,等,那么检查一个-object 是否与一个(或更多)碰撞Wall
将是一件容易的事Player
) 这些:
// (inside Player::tick(); quick and dirty)
{
if ( (this->parentScene->getIntersections<Wall>(this)).empty() )
// player does not collide with a wall, just move.
else
// player does collide with a wall, do whatever.
}
但是,我对此有两个问题:
- 我的(一般)设计是否显示出需要
dynamic_cast<T *>
替换的缺陷instanceof
(例如在 Java 中) - 它是任务的高性能解决方案吗?因为对于每一次碰撞检查,
Scene
基本上都会遍历它包含的每一个 ,检查它是否与给定的碰撞,最后将其强制转换以检查它是否来自另一个给定的类。如果那不是高性能的,那么我的设计需要进行哪些更改以使其具有性能?Entity *
Entity *