分离节点功能,抽象为接口
This commit is contained in:
parent
2897cae260
commit
a394eb0595
|
|
@ -34,7 +34,8 @@ void e2d::CollisionManager::__removeCollider(Collider * collider)
|
|||
|
||||
void e2d::CollisionManager::__updateCollider(Collider* collider)
|
||||
{
|
||||
if (!_collisionEnabled)
|
||||
if (!_collisionEnabled ||
|
||||
Game::getInstance()->isTransitioning())
|
||||
return;
|
||||
|
||||
std::vector<Collider*> currColliders;
|
||||
|
|
@ -64,16 +65,16 @@ void e2d::CollisionManager::__updateCollider(Collider* collider)
|
|||
auto passiveNode = passive->getNode();
|
||||
// 触发两次碰撞事件
|
||||
Collision activeCollision(passiveNode, relation);
|
||||
if (dynamic_cast<EventHandler*>(activeNode->getParentScene()))
|
||||
dynamic_cast<EventHandler*>(activeNode->getParentScene())->handle(activeCollision);
|
||||
if (dynamic_cast<EventHandler*>(activeNode))
|
||||
dynamic_cast<EventHandler*>(activeNode)->handle(activeCollision);
|
||||
if (dynamic_cast<CollisionHandler*>(activeNode->getParentScene()))
|
||||
dynamic_cast<CollisionHandler*>(activeNode->getParentScene())->handle(activeCollision);
|
||||
if (dynamic_cast<CollisionHandler*>(activeNode))
|
||||
dynamic_cast<CollisionHandler*>(activeNode)->handle(activeCollision);
|
||||
|
||||
Collision passiveCollision(activeNode, passive->getRelationWith(collider));
|
||||
if (dynamic_cast<EventHandler*>(passiveNode->getParentScene()))
|
||||
dynamic_cast<EventHandler*>(passiveNode->getParentScene())->handle(passiveCollision);
|
||||
if (dynamic_cast<EventHandler*>(passiveNode))
|
||||
dynamic_cast<EventHandler*>(passiveNode)->handle(passiveCollision);
|
||||
if (dynamic_cast<CollisionHandler*>(passiveNode->getParentScene()))
|
||||
dynamic_cast<CollisionHandler*>(passiveNode->getParentScene())->handle(passiveCollision);
|
||||
if (dynamic_cast<CollisionHandler*>(passiveNode))
|
||||
dynamic_cast<CollisionHandler*>(passiveNode)->handle(passiveCollision);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,11 +84,20 @@ void e2d::Node::visit(Game * game)
|
|||
if (!_visible)
|
||||
return;
|
||||
|
||||
if (!game->isPaused())
|
||||
{
|
||||
auto updatableNode = dynamic_cast<Updatable*>(this);
|
||||
if (updatableNode)
|
||||
{
|
||||
updatableNode->update();
|
||||
}
|
||||
|
||||
// 更新转换矩阵
|
||||
updateTransform();
|
||||
_updateTransform();
|
||||
|
||||
// 保留差别属性
|
||||
_extrapolate = this->getProperty();
|
||||
}
|
||||
|
||||
auto renderer = game->getRenderer();
|
||||
auto renderTarget = renderer->getRenderTarget();
|
||||
|
|
@ -102,9 +111,13 @@ void e2d::Node::visit(Game * game)
|
|||
}
|
||||
|
||||
if (_children.empty())
|
||||
{
|
||||
auto drawableNode = dynamic_cast<Drawable*>(this);
|
||||
if (drawableNode)
|
||||
{
|
||||
renderTarget->SetTransform(_finalMatri);
|
||||
this->draw(renderer);
|
||||
drawableNode->draw(renderer);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -126,8 +139,12 @@ void e2d::Node::visit(Game * game)
|
|||
}
|
||||
}
|
||||
|
||||
auto drawableNode = dynamic_cast<Drawable*>(this);
|
||||
if (drawableNode)
|
||||
{
|
||||
renderTarget->SetTransform(_finalMatri);
|
||||
this->draw(renderer);
|
||||
drawableNode->draw(renderer);
|
||||
}
|
||||
|
||||
// 访问剩余节点
|
||||
for (; i < _children.size(); ++i)
|
||||
|
|
@ -173,7 +190,7 @@ void e2d::Node::drawCollider()
|
|||
}
|
||||
}
|
||||
|
||||
void e2d::Node::updateTransform()
|
||||
void e2d::Node::_updateTransform()
|
||||
{
|
||||
if (!_needTransform)
|
||||
return;
|
||||
|
|
@ -228,12 +245,12 @@ bool e2d::Node::dispatch(const MouseEvent & e, bool handled)
|
|||
{
|
||||
if (_visible)
|
||||
{
|
||||
auto dispatcher = dynamic_cast<EventHandler*>(this);
|
||||
if (dispatcher)
|
||||
dispatcher->handle(e);
|
||||
|
||||
for (auto riter = _children.crbegin(); riter != _children.crend(); ++riter)
|
||||
handled = (*riter)->dispatch(e, handled);
|
||||
|
||||
auto handler = dynamic_cast<MouseEventHandler*>(this);
|
||||
if (handler)
|
||||
handler->handle(e);
|
||||
}
|
||||
|
||||
return handled;
|
||||
|
|
@ -243,12 +260,12 @@ bool e2d::Node::dispatch(const KeyEvent & e, bool handled)
|
|||
{
|
||||
if (_visible)
|
||||
{
|
||||
auto dispatcher = dynamic_cast<EventHandler*>(this);
|
||||
if (dispatcher)
|
||||
dispatcher->handle(e);
|
||||
|
||||
for (auto riter = _children.crbegin(); riter != _children.crend(); ++riter)
|
||||
handled = (*riter)->dispatch(e, handled);
|
||||
|
||||
auto handler = dynamic_cast<KeyEventHandler*>(this);
|
||||
if (handler)
|
||||
handler->handle(e);
|
||||
}
|
||||
|
||||
return handled;
|
||||
|
|
@ -820,7 +837,7 @@ void e2d::Node::stopAction(const String& name)
|
|||
bool e2d::Node::containsPoint(const Point& point)
|
||||
{
|
||||
// 更新转换矩阵
|
||||
updateTransform();
|
||||
_updateTransform();
|
||||
|
||||
// 为节点创建一个轮廓
|
||||
BOOL ret = 0;
|
||||
|
|
@ -850,8 +867,8 @@ bool e2d::Node::containsPoint(const Point& point)
|
|||
bool e2d::Node::intersects(Node * node)
|
||||
{
|
||||
// 更新转换矩阵
|
||||
updateTransform();
|
||||
node->updateTransform();
|
||||
_updateTransform();
|
||||
node->_updateTransform();
|
||||
|
||||
// 为节点创建轮廓
|
||||
D2D1_GEOMETRY_RELATION relation = D2D1_GEOMETRY_RELATION_UNKNOWN;
|
||||
|
|
|
|||
|
|
@ -136,19 +136,4 @@ protected:
|
|||
Collider::Relation _relation;
|
||||
};
|
||||
|
||||
|
||||
// 消息处理
|
||||
class EventHandler
|
||||
{
|
||||
public:
|
||||
// 处理按键消息
|
||||
virtual void handle(KeyEvent e) { }
|
||||
|
||||
// 处理鼠标消息
|
||||
virtual void handle(MouseEvent e) { }
|
||||
|
||||
// 处理碰撞消息
|
||||
virtual void handle(Collision collision) { }
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -6,18 +6,59 @@ namespace e2d
|
|||
{
|
||||
|
||||
|
||||
class Scene;
|
||||
class Action;
|
||||
class Transition;
|
||||
class CollisionManager;
|
||||
|
||||
|
||||
// 绘图接口
|
||||
class Drawable
|
||||
{
|
||||
public:
|
||||
// 渲染图形
|
||||
virtual void draw(Renderer * renderer) const = 0;
|
||||
};
|
||||
|
||||
|
||||
// 更新接口
|
||||
class Updatable
|
||||
{
|
||||
public:
|
||||
// 渲染图形
|
||||
virtual void update() = 0;
|
||||
};
|
||||
|
||||
|
||||
// 按键消息处理接口
|
||||
class KeyEventHandler
|
||||
{
|
||||
public:
|
||||
// 处理按键消息
|
||||
virtual void handle(KeyEvent e) = 0;
|
||||
};
|
||||
|
||||
|
||||
// 鼠标消息处理接口
|
||||
class MouseEventHandler
|
||||
{
|
||||
public:
|
||||
// 处理鼠标消息
|
||||
virtual void handle(MouseEvent e) = 0;
|
||||
};
|
||||
|
||||
|
||||
// 碰撞消息处理接口
|
||||
class CollisionHandler
|
||||
{
|
||||
public:
|
||||
// 处理碰撞消息
|
||||
virtual void handle(Collision collision) = 0;
|
||||
};
|
||||
|
||||
|
||||
// 节点
|
||||
class Node :
|
||||
public Ref
|
||||
{
|
||||
friend class Scene;
|
||||
friend class Collider;
|
||||
friend class Transition;
|
||||
friend class CollisionManager;
|
||||
|
||||
public:
|
||||
// 节点属性
|
||||
|
|
@ -46,11 +87,6 @@ public:
|
|||
|
||||
virtual ~Node();
|
||||
|
||||
// 渲染节点
|
||||
virtual void draw(
|
||||
Renderer * renderer
|
||||
) const {}
|
||||
|
||||
// 获取节点显示状态
|
||||
virtual bool isVisible() const;
|
||||
|
||||
|
|
@ -381,9 +417,6 @@ public:
|
|||
// 停止所有动作
|
||||
void stopAllActions();
|
||||
|
||||
// 更新转换矩阵
|
||||
void updateTransform();
|
||||
|
||||
// 分发鼠标消息
|
||||
virtual bool dispatch(
|
||||
const MouseEvent& e,
|
||||
|
|
@ -420,6 +453,9 @@ protected:
|
|||
// 子节点排序
|
||||
virtual void _sortChildren();
|
||||
|
||||
// 更新转换矩阵
|
||||
void _updateTransform();
|
||||
|
||||
// 更新节点透明度
|
||||
virtual void _updateOpacity();
|
||||
|
||||
|
|
@ -502,7 +538,8 @@ protected:
|
|||
|
||||
// 精灵
|
||||
class Sprite :
|
||||
public Node
|
||||
public Node,
|
||||
public Drawable
|
||||
{
|
||||
public:
|
||||
Sprite();
|
||||
|
|
@ -569,7 +606,8 @@ protected:
|
|||
|
||||
// 文本
|
||||
class Text :
|
||||
public Node
|
||||
public Node,
|
||||
public Drawable
|
||||
{
|
||||
public:
|
||||
// 文本对齐方式
|
||||
|
|
|
|||
Loading…
Reference in New Issue