From a394eb0595a724d8813d414ecbc91d4f4e0a6f16 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Sun, 19 Aug 2018 17:46:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=A6=BB=E8=8A=82=E7=82=B9=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E6=8A=BD=E8=B1=A1=E4=B8=BA=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Manager/CollisionManager.cpp | 19 ++++---- core/Node/Node.cpp | 59 +++++++++++++++--------- core/e2devent.h | 15 ------ core/e2dnode.h | 76 +++++++++++++++++++++++-------- 4 files changed, 105 insertions(+), 64 deletions(-) diff --git a/core/Manager/CollisionManager.cpp b/core/Manager/CollisionManager.cpp index a8694932..d1f56555 100644 --- a/core/Manager/CollisionManager.cpp +++ b/core/Manager/CollisionManager.cpp @@ -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 currColliders; @@ -64,16 +65,16 @@ void e2d::CollisionManager::__updateCollider(Collider* collider) auto passiveNode = passive->getNode(); // 触发两次碰撞事件 Collision activeCollision(passiveNode, relation); - if (dynamic_cast(activeNode->getParentScene())) - dynamic_cast(activeNode->getParentScene())->handle(activeCollision); - if (dynamic_cast(activeNode)) - dynamic_cast(activeNode)->handle(activeCollision); + if (dynamic_cast(activeNode->getParentScene())) + dynamic_cast(activeNode->getParentScene())->handle(activeCollision); + if (dynamic_cast(activeNode)) + dynamic_cast(activeNode)->handle(activeCollision); Collision passiveCollision(activeNode, passive->getRelationWith(collider)); - if (dynamic_cast(passiveNode->getParentScene())) - dynamic_cast(passiveNode->getParentScene())->handle(passiveCollision); - if (dynamic_cast(passiveNode)) - dynamic_cast(passiveNode)->handle(passiveCollision); + if (dynamic_cast(passiveNode->getParentScene())) + dynamic_cast(passiveNode->getParentScene())->handle(passiveCollision); + if (dynamic_cast(passiveNode)) + dynamic_cast(passiveNode)->handle(passiveCollision); } } } diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index accb7eb2..5b96e7e2 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -84,11 +84,20 @@ void e2d::Node::visit(Game * game) if (!_visible) return; - // 更新转换矩阵 - updateTransform(); + if (!game->isPaused()) + { + auto updatableNode = dynamic_cast(this); + if (updatableNode) + { + updatableNode->update(); + } - // 保留差别属性 - _extrapolate = this->getProperty(); + // 更新转换矩阵 + _updateTransform(); + + // 保留差别属性 + _extrapolate = this->getProperty(); + } auto renderer = game->getRenderer(); auto renderTarget = renderer->getRenderTarget(); @@ -103,8 +112,12 @@ void e2d::Node::visit(Game * game) if (_children.empty()) { - renderTarget->SetTransform(_finalMatri); - this->draw(renderer); + auto drawableNode = dynamic_cast(this); + if (drawableNode) + { + renderTarget->SetTransform(_finalMatri); + drawableNode->draw(renderer); + } } else { @@ -125,9 +138,13 @@ void e2d::Node::visit(Game * game) break; } } - - renderTarget->SetTransform(_finalMatri); - this->draw(renderer); + + auto drawableNode = dynamic_cast(this); + if (drawableNode) + { + renderTarget->SetTransform(_finalMatri); + 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(this); - if (dispatcher) - dispatcher->handle(e); - for (auto riter = _children.crbegin(); riter != _children.crend(); ++riter) handled = (*riter)->dispatch(e, handled); + + auto handler = dynamic_cast(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(this); - if (dispatcher) - dispatcher->handle(e); - for (auto riter = _children.crbegin(); riter != _children.crend(); ++riter) handled = (*riter)->dispatch(e, handled); + + auto handler = dynamic_cast(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; diff --git a/core/e2devent.h b/core/e2devent.h index d50c0641..abc0835b 100644 --- a/core/e2devent.h +++ b/core/e2devent.h @@ -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) { } -}; - } \ No newline at end of file diff --git a/core/e2dnode.h b/core/e2dnode.h index bc61510d..07ea41ec 100644 --- a/core/e2dnode.h +++ b/core/e2dnode.h @@ -6,32 +6,73 @@ 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: // 节点属性 struct Property { - float posX; // X 坐标 - float posY; // Y 坐标 + float posX; // X 坐标 + float posY; // Y 坐标 float width; // 宽度 float height; // 高度 float pivotX; // 中心点 X 坐标 float pivotY; // 中心点 Y 坐标 float scaleX; // 横向缩放 float scaleY; // 纵向缩放 - float rotation; // 旋转角度 + float rotation; // 旋转角度 float skewAngleX; // 横向倾斜角度 float skewAngleY; // 纵向倾斜角度 @@ -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: // 文本对齐方式