2018-04-21 21:24:46 +08:00
|
|
|
|
#include "..\e2dcollider.h"
|
|
|
|
|
|
#include "..\e2dmanager.h"
|
|
|
|
|
|
#include "..\e2dnode.h"
|
2018-04-02 23:01:38 +08:00
|
|
|
|
|
|
|
|
|
|
e2d::Collider::Collider()
|
2018-05-09 00:34:15 +08:00
|
|
|
|
: _visiable(true)
|
2018-05-24 20:37:34 +08:00
|
|
|
|
, _color(Color::Red, 0.7)
|
2018-05-09 00:34:15 +08:00
|
|
|
|
, _parentNode(nullptr)
|
|
|
|
|
|
, _transformed(nullptr)
|
2018-07-04 17:23:47 +08:00
|
|
|
|
, _geometry(nullptr)
|
2018-07-04 17:00:21 +08:00
|
|
|
|
, _enabled(true)
|
2018-07-05 00:20:53 +08:00
|
|
|
|
, _type(Collider::Type::None)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::Collider::~Collider()
|
|
|
|
|
|
{
|
2018-05-22 12:24:43 +08:00
|
|
|
|
SafeRelease(_transformed);
|
2018-07-04 17:23:47 +08:00
|
|
|
|
SafeRelease(_geometry);
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::Node * e2d::Collider::getParentNode() const
|
|
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
return _parentNode;
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-22 13:15:57 +08:00
|
|
|
|
e2d::Color e2d::Collider::getColor() const
|
|
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
return _color;
|
2018-04-22 13:15:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-05 00:20:53 +08:00
|
|
|
|
ID2D1Geometry * e2d::Collider::getGeometry() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return _geometry;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ID2D1TransformedGeometry * e2d::Collider::getTransformedGeometry() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return _transformed;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-04 17:00:21 +08:00
|
|
|
|
void e2d::Collider::setEnabled(bool enabled)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-07-04 17:00:21 +08:00
|
|
|
|
_enabled = enabled;
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void e2d::Collider::setVisiable(bool bVisiable)
|
|
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
_visiable = bVisiable;
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-22 13:15:57 +08:00
|
|
|
|
void e2d::Collider::setColor(Color color)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
_color = color;
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void e2d::Collider::_render()
|
|
|
|
|
|
{
|
2018-07-04 17:00:21 +08:00
|
|
|
|
if (_transformed && _enabled)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-07-03 18:16:26 +08:00
|
|
|
|
auto renderer = Renderer::getInstance();
|
2018-04-03 11:12:30 +08:00
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1>ɫ<EFBFBD><C9AB>ˢ
|
2018-07-03 18:16:26 +08:00
|
|
|
|
ID2D1SolidColorBrush * pBrush = renderer->getSolidColorBrush();
|
2018-04-03 11:12:30 +08:00
|
|
|
|
// <20><><EFBFBD>û<EFBFBD>ˢ<EFBFBD><CBA2>ɫ<EFBFBD><C9AB><EFBFBD><CDB8><EFBFBD><EFBFBD>
|
2018-05-17 15:22:14 +08:00
|
|
|
|
pBrush->SetColor(_color.toD2DColorF());
|
2018-04-02 23:01:38 +08:00
|
|
|
|
// <20><><EFBFBD>Ƽ<EFBFBD><C6BC><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
|
2018-07-03 18:16:26 +08:00
|
|
|
|
renderer->getRenderTarget()->DrawGeometry(_transformed, pBrush);
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-17 15:22:14 +08:00
|
|
|
|
e2d::Collider::Relation e2d::Collider::getRelationWith(Collider * pCollider) const
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (_transformed && pCollider->_transformed)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-07-04 17:00:21 +08:00
|
|
|
|
if (_enabled && pCollider->_enabled)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
D2D1_GEOMETRY_RELATION relation;
|
|
|
|
|
|
|
2018-05-09 00:34:15 +08:00
|
|
|
|
_transformed->CompareWithGeometry(
|
|
|
|
|
|
pCollider->_transformed,
|
2018-04-02 23:01:38 +08:00
|
|
|
|
D2D1::Matrix3x2F::Identity(),
|
|
|
|
|
|
&relation
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2018-04-24 21:22:34 +08:00
|
|
|
|
return Relation(relation);
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-05-24 20:37:34 +08:00
|
|
|
|
return Relation::Unknown;
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-05 00:20:53 +08:00
|
|
|
|
void e2d::Collider::_recreate(Collider::Type type)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-07-05 00:20:53 +08:00
|
|
|
|
_type = type;
|
|
|
|
|
|
|
|
|
|
|
|
if (_parentNode)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-07-05 00:20:53 +08:00
|
|
|
|
SafeRelease(_geometry);
|
|
|
|
|
|
SafeRelease(_transformed);
|
|
|
|
|
|
|
|
|
|
|
|
switch (type)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-07-05 00:20:53 +08:00
|
|
|
|
case Type::Rect:
|
|
|
|
|
|
{
|
|
|
|
|
|
ID2D1RectangleGeometry* rectangle = nullptr;
|
|
|
|
|
|
Renderer::getFactory()->CreateRectangleGeometry(
|
|
|
|
|
|
D2D1::RectF(
|
|
|
|
|
|
0,
|
|
|
|
|
|
0,
|
|
|
|
|
|
float(_parentNode->getRealWidth()),
|
|
|
|
|
|
float(_parentNode->getRealHeight())),
|
|
|
|
|
|
&rectangle
|
|
|
|
|
|
);
|
|
|
|
|
|
_geometry = rectangle;
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
2018-07-05 00:20:53 +08:00
|
|
|
|
break;
|
2018-04-02 23:01:38 +08:00
|
|
|
|
|
2018-07-05 00:20:53 +08:00
|
|
|
|
case Type::Circle:
|
|
|
|
|
|
{
|
|
|
|
|
|
double minSide = std::min(_parentNode->getRealWidth(), _parentNode->getRealHeight());
|
|
|
|
|
|
|
|
|
|
|
|
ID2D1EllipseGeometry* circle = nullptr;
|
|
|
|
|
|
Renderer::getFactory()->CreateEllipseGeometry(
|
|
|
|
|
|
D2D1::Ellipse(
|
|
|
|
|
|
D2D1::Point2F(
|
|
|
|
|
|
float(_parentNode->getRealWidth() / 2),
|
|
|
|
|
|
float(_parentNode->getRealHeight() / 2)
|
|
|
|
|
|
),
|
|
|
|
|
|
float(minSide / 2),
|
|
|
|
|
|
float(minSide / 2)
|
|
|
|
|
|
),
|
|
|
|
|
|
&circle
|
|
|
|
|
|
);
|
|
|
|
|
|
_geometry = circle;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case Type::Ellipse:
|
|
|
|
|
|
{
|
|
|
|
|
|
float halfWidth = float(_parentNode->getWidth() / 2),
|
|
|
|
|
|
halfHeight = float(_parentNode->getHeight() / 2);
|
|
|
|
|
|
|
|
|
|
|
|
ID2D1EllipseGeometry* ellipse = nullptr;
|
|
|
|
|
|
Renderer::getFactory()->CreateEllipseGeometry(
|
|
|
|
|
|
D2D1::Ellipse(
|
|
|
|
|
|
D2D1::Point2F(
|
|
|
|
|
|
halfWidth,
|
|
|
|
|
|
halfHeight),
|
|
|
|
|
|
halfWidth,
|
|
|
|
|
|
halfHeight),
|
|
|
|
|
|
&ellipse
|
|
|
|
|
|
);
|
|
|
|
|
|
_geometry = ellipse;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-04-02 23:01:38 +08:00
|
|
|
|
|
2018-07-05 00:20:53 +08:00
|
|
|
|
void e2d::Collider::_transform()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_parentNode && _enabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
|
|
|
|
|
|
_recreate(_type);
|
|
|
|
|
|
// <20><>ά<EFBFBD>任
|
2018-07-03 18:16:26 +08:00
|
|
|
|
Renderer::getFactory()->CreateTransformedGeometry(
|
2018-07-04 17:23:47 +08:00
|
|
|
|
_geometry,
|
2018-05-09 00:34:15 +08:00
|
|
|
|
_parentNode->_finalMatri,
|
|
|
|
|
|
&_transformed
|
2018-04-02 23:01:38 +08:00
|
|
|
|
);
|
2018-07-05 00:20:53 +08:00
|
|
|
|
// ֪ͨ<CDA8><D6AA>ײ<EFBFBD><D7B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-04-02 23:01:38 +08:00
|
|
|
|
ColliderManager::__updateCollider(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|