Magic_Game/core/Common/Collider.cpp

198 lines
3.5 KiB
C++
Raw Normal View History

#include "..\e2dcommon.h"
2018-04-21 21:24:46 +08:00
#include "..\e2dmanager.h"
#include "..\e2dnode.h"
e2d::Collider::Collider(Node * parent)
2018-07-07 01:48:39 +08:00
: _visible(true)
2018-07-28 20:06:27 +08:00
, _color(Color::Blue, 0.6f)
, _parentNode(parent)
2018-07-04 17:23:47 +08:00
, _geometry(nullptr)
2018-07-04 17:00:21 +08:00
, _enabled(true)
, _shape(Collider::Shape::None)
2018-07-13 00:45:39 +08:00
, _notify(true)
{
}
e2d::Collider::~Collider()
{
2018-07-04 17:23:47 +08:00
SafeRelease(_geometry);
}
e2d::Color e2d::Collider::getColor() const
{
return _color;
}
e2d::Collider::Shape e2d::Collider::getShape() const
{
return _shape;
}
2018-07-13 00:45:39 +08:00
e2d::Node * e2d::Collider::getNode() const
{
return _parentNode;
}
ID2D1Geometry * e2d::Collider::getGeometry() const
{
return _geometry;
}
void e2d::Collider::setShape(Shape shape)
{
if (_shape == shape)
return;
_shape = shape;
if (shape == Shape::None)
{
SafeRelease(_geometry);
CollisionManager::getInstance()->__removeCollider(this);
}
else
{
this->recreate();
CollisionManager::getInstance()->__addCollider(this);
}
2018-07-13 00:45:39 +08:00
}
void e2d::Collider::setCollisionNotify(bool notify)
{
_notify = notify;
}
2018-07-04 17:00:21 +08:00
void e2d::Collider::setEnabled(bool enabled)
{
2018-07-04 17:00:21 +08:00
_enabled = enabled;
}
2018-07-07 01:48:39 +08:00
void e2d::Collider::setVisible(bool visible)
{
2018-07-07 01:48:39 +08:00
_visible = visible;
}
void e2d::Collider::setColor(Color color)
{
_color = color;
}
2018-07-13 00:45:39 +08:00
void e2d::Collider::render()
{
2018-07-13 00:45:39 +08:00
if (_geometry && _enabled && _visible)
{
2018-08-15 00:06:03 +08:00
auto renderer = Game::getInstance()->getRenderer();
2018-04-03 11:12:30 +08:00
// <20><>ȡ<EFBFBD><C8A1>ɫ<EFBFBD><C9AB>ˢ
2018-07-05 22:09:21 +08:00
ID2D1SolidColorBrush * brush = renderer->getSolidColorBrush();
2018-04-03 11:12:30 +08:00
// <20><><EFBFBD>û<EFBFBD>ˢ<EFBFBD><CBA2>ɫ<EFBFBD><C9AB>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>
2018-07-29 02:24:34 +08:00
brush->SetColor((D2D1_COLOR_F)_color);
2018-07-05 22:09:21 +08:00
brush->SetOpacity(1.f);
// <20><><EFBFBD>Ƽ<EFBFBD><C6BC><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
renderer->getRenderTarget()->DrawGeometry(_geometry, brush, 1.5f);
}
}
2018-07-07 18:04:18 +08:00
e2d::Collider::Relation e2d::Collider::getRelationWith(Collider * collider) const
{
2018-07-07 18:04:18 +08:00
if (_geometry && collider->_geometry)
{
2018-07-07 18:04:18 +08:00
if (_enabled && collider->_enabled)
{
D2D1_GEOMETRY_RELATION relation;
2018-07-07 18:04:18 +08:00
_geometry->CompareWithGeometry(
collider->_geometry,
D2D1::Matrix3x2F::Identity(),
&relation
);
return Relation(relation);
}
}
2018-05-24 20:37:34 +08:00
return Relation::Unknown;
}
2018-07-13 00:45:39 +08:00
bool e2d::Collider::isEnabled() const
{
return _enabled;
}
bool e2d::Collider::isVisible() const
{
return _visible;
}
bool e2d::Collider::isCollisionNotify() const
{
return _notify;
}
void e2d::Collider::recreate()
{
if (!_enabled || _shape == Shape::None)
2018-07-07 18:04:18 +08:00
return;
SafeRelease(_geometry);
2018-08-15 00:06:03 +08:00
auto factory = Game::getInstance()->getRenderer()->getFactory();
switch (_shape)
{
case Shape::Rect:
{
ID2D1RectangleGeometry* rectangle = nullptr;
2018-08-15 00:06:03 +08:00
factory->CreateRectangleGeometry(
2018-07-28 20:06:27 +08:00
D2D1::RectF(0, 0, _parentNode->getRealWidth(), _parentNode->getRealHeight()),
&rectangle
);
_geometry = rectangle;
}
break;
case Shape::Circle:
{
2018-07-28 20:06:27 +08:00
float minSide = std::min(_parentNode->getRealWidth(), _parentNode->getRealHeight());
ID2D1EllipseGeometry* circle = nullptr;
2018-08-15 00:06:03 +08:00
factory->CreateEllipseGeometry(
D2D1::Ellipse(
D2D1::Point2F(
2018-07-28 20:06:27 +08:00
_parentNode->getRealWidth() / 2,
_parentNode->getRealHeight() / 2
),
2018-07-28 20:06:27 +08:00
minSide / 2,
minSide / 2
),
&circle
);
_geometry = circle;
}
break;
case Shape::Ellipse:
{
2018-07-28 20:06:27 +08:00
float halfWidth = _parentNode->getWidth() / 2,
halfHeight = _parentNode->getHeight() / 2;
ID2D1EllipseGeometry* ellipse = nullptr;
2018-08-15 00:06:03 +08:00
factory->CreateEllipseGeometry(
D2D1::Ellipse(
D2D1::Point2F(
halfWidth,
halfHeight),
halfWidth,
halfHeight),
&ellipse
);
_geometry = ellipse;
}
break;
}
2018-07-07 18:04:18 +08:00
ID2D1TransformedGeometry * _transformed;
2018-08-15 00:06:03 +08:00
factory->CreateTransformedGeometry(
2018-07-07 18:04:18 +08:00
_geometry,
_parentNode->_finalMatri,
&_transformed
);
SafeRelease(_geometry);
_geometry = _transformed;
}