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