Collider::Type改为Collider::Shape;增加节点变化差值。

This commit is contained in:
Nomango 2018-07-10 00:30:17 +08:00
parent 0c6044491e
commit 2ac7ac6591
6 changed files with 104 additions and 51 deletions

View File

@ -8,9 +8,9 @@ e2d::Collider::Collider(Node * parent)
, _parentNode(parent) , _parentNode(parent)
, _geometry(nullptr) , _geometry(nullptr)
, _enabled(true) , _enabled(true)
, _type(Collider::Type::None) , _shape(Collider::Shape::None)
{ {
_type = Game::getInstance()->getConfig()->getDefaultColliderType(); _shape = Game::getInstance()->getConfig()->getDefaultColliderShape();
} }
e2d::Collider::~Collider() e2d::Collider::~Collider()
@ -23,11 +23,22 @@ e2d::Color e2d::Collider::getColor() const
return _color; return _color;
} }
e2d::Collider::Shape e2d::Collider::getShape() const
{
return _shape;
}
ID2D1Geometry * e2d::Collider::getGeometry() const ID2D1Geometry * e2d::Collider::getGeometry() const
{ {
return _geometry; return _geometry;
} }
void e2d::Collider::setShape(Shape shape)
{
_shape = shape;
this->_recreate();
}
void e2d::Collider::setEnabled(bool enabled) void e2d::Collider::setEnabled(bool enabled)
{ {
_enabled = enabled; _enabled = enabled;
@ -81,12 +92,12 @@ void e2d::Collider::_recreate()
{ {
SafeRelease(_geometry); SafeRelease(_geometry);
if (_type == Type::None) if (!_enabled || _shape == Shape::None)
return; return;
switch (_type) switch (_shape)
{ {
case Type::Rect: case Shape::Rect:
{ {
ID2D1RectangleGeometry* rectangle = nullptr; ID2D1RectangleGeometry* rectangle = nullptr;
Renderer::getFactory()->CreateRectangleGeometry( Renderer::getFactory()->CreateRectangleGeometry(
@ -101,7 +112,7 @@ void e2d::Collider::_recreate()
} }
break; break;
case Type::Circle: case Shape::Circle:
{ {
double minSide = std::min(_parentNode->getRealWidth(), _parentNode->getRealHeight()); double minSide = std::min(_parentNode->getRealWidth(), _parentNode->getRealHeight());
@ -121,7 +132,7 @@ void e2d::Collider::_recreate()
} }
break; break;
case Type::Ellipse: case Shape::Ellipse:
{ {
float halfWidth = float(_parentNode->getWidth() / 2), float halfWidth = float(_parentNode->getWidth() / 2),
halfHeight = float(_parentNode->getHeight() / 2); halfHeight = float(_parentNode->getHeight() / 2);

View File

@ -3,12 +3,12 @@
e2d::Config::Config() e2d::Config::Config()
: _gameName() : _gameName()
, _nodeDefPivot() , _defaultNodePivot()
, _soundEnabled(true) , _soundEnabled(true)
, _outlineVisible(false) , _outlineVisible(false)
, _collisionEnabled(false) , _collisionEnabled(false)
, _colliderVisible(false) , _colliderVisible(false)
, _nodeDefColliderType(Collider::Type::None) , _defaultColliderShape(Collider::Shape::None)
, _unconfigured(true) , _unconfigured(true)
{ {
} }
@ -43,15 +43,15 @@ void e2d::Config::setCollisionEnabled(bool enabled)
void e2d::Config::setNodeDefaultPivot(Point pivot) void e2d::Config::setNodeDefaultPivot(Point pivot)
{ {
_nodeDefPivot = Point( _defaultNodePivot = Point(
std::min(std::max(pivot.x, 0.0), 1.0), std::min(std::max(pivot.x, 0.0), 1.0),
std::min(std::max(pivot.y, 0.0), 1.0) std::min(std::max(pivot.y, 0.0), 1.0)
); );
} }
void e2d::Config::setDefaultColliderType(Collider::Type type) void e2d::Config::setDefaultColliderShape(Collider::Shape shape)
{ {
_nodeDefColliderType = type; _defaultColliderShape = shape;
} }
void e2d::Config::setColliderVisible(bool visible) void e2d::Config::setColliderVisible(bool visible)
@ -81,12 +81,12 @@ bool e2d::Config::isCollisionEnabled() const
e2d::Point e2d::Config::getNodeDefaultPivot() const e2d::Point e2d::Config::getNodeDefaultPivot() const
{ {
return _nodeDefPivot; return _defaultNodePivot;
} }
e2d::Collider::Type e2d::Config::getDefaultColliderType() const e2d::Collider::Shape e2d::Config::getDefaultColliderShape() const
{ {
return _nodeDefColliderType; return _defaultColliderShape;
} }
bool e2d::Config::isColliderVisible() const bool e2d::Config::isColliderVisible() const

View File

@ -47,7 +47,7 @@ void e2d::CollisionManager::updateCollider(Node * node)
{ {
if (node) if (node)
{ {
if (node->getCollider()->_type != Collider::Type::None) if (node->getCollider()->_shape != Collider::Shape::None)
{ {
node->getCollider()->_recreate(); node->getCollider()->_recreate();
_collisionNodes.insert(node); _collisionNodes.insert(node);

View File

@ -3,9 +3,45 @@
#include "..\e2daction.h" #include "..\e2daction.h"
#include <algorithm> #include <algorithm>
const e2d::Node::Property e2d::Node::Property::Origin = { 0 };
e2d::Node::Property e2d::Node::Property::operator+(Property const & prop) const
{
Property result;
result.posX = this->posX + prop.posX;
result.posY = this->posY + prop.posY;
result.width = this->width + prop.width;
result.height = this->height + prop.height;
result.pivotX = this->pivotX + prop.pivotX;
result.pivotY = this->pivotY + prop.pivotY;
result.scaleX = this->scaleX + prop.scaleX;
result.scaleY = this->scaleY + prop.scaleY;
result.rotation = this->rotation + prop.rotation;
result.skewAngleX = this->skewAngleX + prop.skewAngleX;
result.skewAngleY = this->skewAngleY + prop.skewAngleY;
return std::move(result);
}
e2d::Node::Property e2d::Node::Property::operator-(Property const & prop) const
{
Property result;
result.posX = this->posX - prop.posX;
result.posY = this->posY - prop.posY;
result.width = this->width - prop.width;
result.height = this->height - prop.height;
result.pivotX = this->pivotX - prop.pivotX;
result.pivotY = this->pivotY - prop.pivotY;
result.scaleX = this->scaleX - prop.scaleX;
result.scaleY = this->scaleY - prop.scaleY;
result.rotation = this->rotation - prop.rotation;
result.skewAngleX = this->skewAngleX - prop.skewAngleX;
result.skewAngleY = this->skewAngleY - prop.skewAngleY;
return std::move(result);
}
e2d::Node::Node() e2d::Node::Node()
: _nOrder(0) : _order(0)
, _posX(0) , _posX(0)
, _posY(0) , _posY(0)
, _width(0) , _width(0)
@ -31,6 +67,7 @@ e2d::Node::Node()
, _positionFixed(false) , _positionFixed(false)
, _outline(nullptr) , _outline(nullptr)
, _collider(this) , _collider(this)
, _extrapolate(Property::Origin)
{ {
// ÉèÖÃĬÈÏÖÐÐĵãλÖà // ÉèÖÃĬÈÏÖÐÐĵãλÖÃ
Point defPivot = Game::getInstance()->getConfig()->getNodeDefaultPivot(); Point defPivot = Game::getInstance()->getConfig()->getNodeDefaultPivot();
@ -374,12 +411,10 @@ double e2d::Node::getOpacity() const
e2d::Node::Property e2d::Node::getProperty() const e2d::Node::Property e2d::Node::getProperty() const
{ {
Property prop; Property prop;
prop.visible = _visible;
prop.posX = _posX; prop.posX = _posX;
prop.posY = _posY; prop.posY = _posY;
prop.width = _width; prop.width = _width;
prop.height = _height; prop.height = _height;
prop.opacity = _realOpacity;
prop.pivotX = _pivotX; prop.pivotX = _pivotX;
prop.pivotY = _pivotY; prop.pivotY = _pivotY;
prop.scaleX = _scaleX; prop.scaleX = _scaleX;
@ -387,7 +422,7 @@ e2d::Node::Property e2d::Node::getProperty() const
prop.rotation = _rotation; prop.rotation = _rotation;
prop.skewAngleX = _skewAngleX; prop.skewAngleX = _skewAngleX;
prop.skewAngleY = _skewAngleY; prop.skewAngleY = _skewAngleY;
return prop; return std::move(prop);
} }
e2d::Collider* e2d::Node::getCollider() e2d::Collider* e2d::Node::getCollider()
@ -397,12 +432,12 @@ e2d::Collider* e2d::Node::getCollider()
int e2d::Node::getOrder() const int e2d::Node::getOrder() const
{ {
return _nOrder; return _order;
} }
void e2d::Node::setOrder(int order) void e2d::Node::setOrder(int order)
{ {
_nOrder = order; _order = order;
} }
void e2d::Node::setPosX(double x) void e2d::Node::setPosX(double x)
@ -427,6 +462,8 @@ void e2d::Node::setPos(double x, double y)
_posX = float(x); _posX = float(x);
_posY = float(y); _posY = float(y);
_extrapolate.posX += x;
_extrapolate.posY += y;
_needTransform = true; _needTransform = true;
} }
@ -481,6 +518,8 @@ void e2d::Node::setScale(double scaleX, double scaleY)
_scaleX = float(scaleX); _scaleX = float(scaleX);
_scaleY = float(scaleY); _scaleY = float(scaleY);
_extrapolate.scaleX += scaleX;
_extrapolate.scaleY += scaleY;
_needTransform = true; _needTransform = true;
} }
@ -501,6 +540,8 @@ void e2d::Node::setSkew(double angleX, double angleY)
_skewAngleX = float(angleX); _skewAngleX = float(angleX);
_skewAngleY = float(angleY); _skewAngleY = float(angleY);
_extrapolate.skewAngleX += angleX;
_extrapolate.skewAngleY += angleY;
_needTransform = true; _needTransform = true;
} }
@ -510,6 +551,7 @@ void e2d::Node::setRotation(double angle)
return; return;
_rotation = float(angle); _rotation = float(angle);
_extrapolate.rotation += angle;
_needTransform = true; _needTransform = true;
} }
@ -540,6 +582,8 @@ void e2d::Node::setPivot(double pivotX, double pivotY)
_pivotX = std::min(std::max(float(pivotX), 0.f), 1.f); _pivotX = std::min(std::max(float(pivotX), 0.f), 1.f);
_pivotY = std::min(std::max(float(pivotY), 0.f), 1.f); _pivotY = std::min(std::max(float(pivotY), 0.f), 1.f);
_extrapolate.pivotX += pivotX;
_extrapolate.pivotY += pivotY;
_needTransform = true; _needTransform = true;
} }
@ -560,6 +604,8 @@ void e2d::Node::setSize(double width, double height)
_width = float(width); _width = float(width);
_height = float(height); _height = float(height);
_extrapolate.width += width;
_extrapolate.height += height;
_needTransform = true; _needTransform = true;
} }
@ -570,25 +616,14 @@ void e2d::Node::setSize(Size size)
void e2d::Node::setProperty(Property prop) void e2d::Node::setProperty(Property prop)
{ {
this->setVisible(prop.visible);
this->setPos(prop.posX, prop.posY); this->setPos(prop.posX, prop.posY);
this->setSize(prop.width, prop.height); this->setSize(prop.width, prop.height);
this->setOpacity(prop.opacity);
this->setPivot(prop.pivotX, prop.pivotY); this->setPivot(prop.pivotX, prop.pivotY);
this->setScale(prop.scaleX, prop.scaleY); this->setScale(prop.scaleX, prop.scaleY);
this->setRotation(prop.rotation); this->setRotation(prop.rotation);
this->setSkew(prop.skewAngleX, prop.skewAngleY); this->setSkew(prop.skewAngleX, prop.skewAngleY);
} }
void e2d::Node::setColliderType(Collider::Type type)
{
if (_collider._type != type)
{
_collider._type = type;
_needTransform = true;
}
}
void e2d::Node::addChild(Node * child, int order /* = 0 */) void e2d::Node::addChild(Node * child, int order /* = 0 */)
{ {
WARN_IF(child == nullptr, "Node::addChild NULL pointer exception."); WARN_IF(child == nullptr, "Node::addChild NULL pointer exception.");

View File

@ -450,8 +450,8 @@ class Collider
friend class CollisionManager; friend class CollisionManager;
public: public:
// 碰撞体类别 // 碰撞体形状
enum class Type enum class Shape
{ {
None, /* 无 */ None, /* 无 */
Rect, /* 矩形 */ Rect, /* 矩形 */
@ -470,6 +470,11 @@ public:
}; };
public: public:
// 设置碰撞体形状
virtual void setShape(
Shape shape
);
// 启用或关闭该碰撞体 // 启用或关闭该碰撞体
virtual void setEnabled( virtual void setEnabled(
bool enabled bool enabled
@ -493,6 +498,9 @@ public:
// 获取绘制颜色 // 获取绘制颜色
Color getColor() const; Color getColor() const;
// 获取形状
Shape getShape() const;
// 获取 ID2D1Geometry* 对象 // 获取 ID2D1Geometry* 对象
ID2D1Geometry* getGeometry() const; ID2D1Geometry* getGeometry() const;
@ -516,7 +524,7 @@ protected:
bool _visible; bool _visible;
Color _color; Color _color;
Node * _parentNode; Node * _parentNode;
Type _type; Shape _shape;
ID2D1Geometry* _geometry; ID2D1Geometry* _geometry;
}; };
@ -797,10 +805,10 @@ public:
Point pivot Point pivot
); );
// 设置节点的默认碰撞体类型 // 设置节点的默认碰撞体形状
// 默认Collider::Type::None // 默认Collider::Shape::None
void setDefaultColliderType( void setDefaultColliderShape(
Collider::Type type Collider::Shape shape
); );
// 打开或关闭碰撞体可视化 // 打开或关闭碰撞体可视化
@ -825,7 +833,7 @@ public:
Point getNodeDefaultPivot() const; Point getNodeDefaultPivot() const;
// 获取节点的默认碰撞体类型 // 获取节点的默认碰撞体类型
Collider::Type getDefaultColliderType() const; Collider::Shape getDefaultColliderShape() const;
// 获取碰撞体可视化状态 // 获取碰撞体可视化状态
bool isColliderVisible() const; bool isColliderVisible() const;
@ -840,8 +848,8 @@ protected:
bool _collisionEnabled; bool _collisionEnabled;
bool _colliderVisible; bool _colliderVisible;
String _gameName; String _gameName;
Point _nodeDefPivot; Point _defaultNodePivot;
Collider::Type _nodeDefColliderType; Collider::Shape _defaultColliderShape;
}; };

View File

@ -19,12 +19,10 @@ public:
// 节点属性 // 节点属性
struct Property struct Property
{ {
bool visible; // 可见性
double posX; // X 坐标 double posX; // X 坐标
double posY; // Y 坐标 double posY; // Y 坐标
double width; // 宽度 double width; // 宽度
double height; // 高度 double height; // 高度
double opacity; // 透明度
double pivotX; // 中心点 X 坐标 double pivotX; // 中心点 X 坐标
double pivotY; // 中心点 Y 坐标 double pivotY; // 中心点 Y 坐标
double scaleX; // 横向缩放 double scaleX; // 横向缩放
@ -32,6 +30,11 @@ public:
double rotation; // 旋转角度 double rotation; // 旋转角度
double skewAngleX; // 横向倾斜角度 double skewAngleX; // 横向倾斜角度
double skewAngleY; // 纵向倾斜角度 double skewAngleY; // 纵向倾斜角度
Property operator+ (Property const & prop) const;
Property operator- (Property const & prop) const;
static const Property Origin;
}; };
public: public:
@ -331,11 +334,6 @@ public:
Property prop Property prop
); );
// 设置碰撞体类型
virtual void setColliderType(
Collider::Type type
);
// 添加子节点 // 添加子节点
virtual void addChild( virtual void addChild(
Node * child, Node * child,
@ -425,7 +423,7 @@ protected:
float _realOpacity; float _realOpacity;
float _pivotX; float _pivotX;
float _pivotY; float _pivotY;
int _nOrder; int _order;
bool _visible; bool _visible;
bool _autoUpdate; bool _autoUpdate;
bool _needSort; bool _needSort;
@ -434,6 +432,7 @@ protected:
Collider _collider; Collider _collider;
Scene * _parentScene; Scene * _parentScene;
Node * _parent; Node * _parent;
Property _extrapolate;
ID2D1Geometry* _outline; ID2D1Geometry* _outline;
D2D1::Matrix3x2F _initialMatri; D2D1::Matrix3x2F _initialMatri;
D2D1::Matrix3x2F _finalMatri; D2D1::Matrix3x2F _finalMatri;