#pragma once #include "e2dbase.h" namespace e2d { class ColliderManager; // 碰撞体 class Collider : public Object { friend ColliderManager; friend Node; public: Collider(); virtual ~Collider(); // 判断两碰撞体的交集关系 virtual Relation getRelationWith( Collider * pCollider ) const; // 获取父节点 Node * getParentNode() const; // 获取绘制颜色 Color getColor() const; // 启用或关闭该碰撞体 virtual void setEnable( bool enable ); // 设置碰撞体的可见性 void setVisiable( bool bVisiable ); // 设置绘制颜色 void setColor( Color color ); // 设置大小跟随 void setAutoResize( bool enable ); // 获取 ID2D1Geometry 对象 virtual ID2D1Geometry * getD2dGeometry() const = 0; protected: // 转换碰撞体 virtual void _transform(); // 重设大小 virtual void _resize() = 0; // 渲染碰撞体 virtual void _render(); protected: bool _enable; bool _visiable; bool _autoResize; Color _color; Node * _parentNode; ID2D1TransformedGeometry * _transformed; }; // 矩形碰撞体 class ColliderRect : public Collider { public: ColliderRect(); ColliderRect( double x, double y, double width, double height ); ColliderRect( Node * node ); // 创建一个默认矩形碰撞体 static ColliderRect * create(); // 根据左上角坐标和宽高创建矩形碰撞体 static ColliderRect * create( double x, double y, double width, double height ); // 创建一个和节点位置大小相同的矩形碰撞体 static ColliderRect * create( Node * node ); virtual ~ColliderRect(); // 修改矩形碰撞体大小 void setRect( double left, double top, double right, double bottom ); // 获取 ID2D1Geometry 对象 virtual ID2D1RectangleGeometry * getD2dGeometry() const override; protected: // 重设大小 virtual void _resize(); protected: ID2D1RectangleGeometry * _d2dRectangle; }; // 圆形碰撞体 class ColliderCircle : public Collider { public: ColliderCircle(); ColliderCircle( Point center, double radius ); ColliderCircle( Node * node ); // 创建一个默认圆形碰撞体 static ColliderCircle * create(); // 根据圆心和半径创建圆形碰撞体 static ColliderCircle * create( Point center, double radius ); // 创建一个和节点位置大小相同的圆形碰撞体 static ColliderCircle * create( Node * node ); virtual ~ColliderCircle(); // 修改圆形碰撞体大小 void setCircle( Point center, double radius ); // 获取 ID2D1Geometry 对象 virtual ID2D1EllipseGeometry * getD2dGeometry() const override; protected: // 重设大小 virtual void _resize(); protected: ID2D1EllipseGeometry * _d2dCircle; }; // 椭圆形碰撞体 class ColliderEllipse : public Collider { public: ColliderEllipse(); ColliderEllipse( Point center, double radiusX, double radiusY ); ColliderEllipse( Node * node ); // 创建一个默认椭圆碰撞体 static ColliderEllipse * create(); // 根据圆心和半径创建椭圆碰撞体 static ColliderEllipse * create( Point center, double radiusX, double radiusY ); // 创建一个和节点位置大小相同的椭圆碰撞体 static ColliderEllipse * create( Node * node ); virtual ~ColliderEllipse(); // 修改椭圆碰撞体大小 void setEllipse( Point center, double radiusX, double radiusY ); // 获取 ID2D1Geometry 对象 virtual ID2D1EllipseGeometry * getD2dGeometry() const override; protected: // 重设大小 virtual void _resize(); protected: ID2D1EllipseGeometry * _d2dEllipse; }; }