2018-09-05 13:33:39 +08:00
|
|
|
|
#include "..\e2dutil.h"
|
2018-09-05 13:17:07 +08:00
|
|
|
|
#include "..\e2dimpl.h"
|
|
|
|
|
|
#include "..\e2dmodule.h"
|
2018-04-21 21:24:46 +08:00
|
|
|
|
#include "..\e2dmanager.h"
|
2018-09-06 23:26:32 +08:00
|
|
|
|
#include "..\e2dobject.h"
|
2018-04-02 23:01:38 +08:00
|
|
|
|
|
2018-07-07 15:12:18 +08:00
|
|
|
|
e2d::Collider::Collider(Node * parent)
|
2018-09-04 22:42:34 +08:00
|
|
|
|
: visible_(true)
|
|
|
|
|
|
, border_color_(Color::Blue, 0.6f)
|
|
|
|
|
|
, parent_node_(parent)
|
|
|
|
|
|
, geometry_(nullptr)
|
|
|
|
|
|
, enabled_(true)
|
|
|
|
|
|
, shape_(Collider::Shape::None)
|
|
|
|
|
|
, notify_(true)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::Collider::~Collider()
|
|
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
SafeRelease(geometry_);
|
2018-09-07 00:28:54 +08:00
|
|
|
|
|
|
|
|
|
|
CollisionManager::GetInstance()->RemoveCollider(this);
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
const e2d::Color& e2d::Collider::GetBorderColor() const
|
2018-04-22 13:15:57 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return border_color_;
|
2018-04-22 13:15:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
e2d::Collider::Shape e2d::Collider::GetShape() const
|
2018-07-10 00:30:17 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return shape_;
|
2018-07-10 00:30:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
e2d::Node * e2d::Collider::GetNode() const
|
2018-07-13 00:45:39 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return parent_node_;
|
2018-07-13 00:45:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
ID2D1Geometry * e2d::Collider::GetGeometry() const
|
2018-07-05 00:20:53 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return geometry_;
|
2018-07-05 00:20:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
void e2d::Collider::SetShape(Shape shape)
|
2018-07-10 00:30:17 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (shape_ == shape)
|
2018-08-23 16:37:51 +08:00
|
|
|
|
return;
|
2018-07-28 18:44:37 +08:00
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
shape_ = shape;
|
2018-07-28 18:44:37 +08:00
|
|
|
|
if (shape == Shape::None)
|
|
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
SafeRelease(geometry_);
|
|
|
|
|
|
CollisionManager::GetInstance()->RemoveCollider(this);
|
2018-07-28 18:44:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
this->Recreate();
|
|
|
|
|
|
CollisionManager::GetInstance()->AddCollider(this);
|
2018-07-28 18:44:37 +08:00
|
|
|
|
}
|
2018-07-13 00:45:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
void e2d::Collider::SetCollisionNotify(bool notify)
|
2018-07-13 00:45:39 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
notify_ = notify;
|
2018-07-10 00:30:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
void e2d::Collider::SetEnabled(bool enabled)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
enabled_ = enabled;
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
void e2d::Collider::SetVisible(bool visible)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
visible_ = visible;
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
void e2d::Collider::SetBorderColor(const Color& color)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
border_color_ = color;
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
void e2d::Collider::Draw()
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (geometry_ && enabled_ && visible_)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
auto renderer = Renderer::GetInstance();
|
2018-04-03 11:12:30 +08:00
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1>ɫ<EFBFBD><C9AB>ˢ
|
2018-09-04 22:42:34 +08:00
|
|
|
|
ID2D1SolidColorBrush * brush = renderer->GetSolidBrush();
|
2018-04-03 11:12:30 +08:00
|
|
|
|
// <20><><EFBFBD>û<EFBFBD>ˢ<EFBFBD><CBA2>ɫ<EFBFBD><C9AB><EFBFBD><CDB8><EFBFBD><EFBFBD>
|
2018-09-04 22:42:34 +08:00
|
|
|
|
brush->SetColor((D2D1_COLOR_F)border_color_);
|
2018-07-05 22:09:21 +08:00
|
|
|
|
brush->SetOpacity(1.f);
|
2018-04-02 23:01:38 +08:00
|
|
|
|
// <20><><EFBFBD>Ƽ<EFBFBD><C6BC><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
|
2018-09-04 22:42:34 +08:00
|
|
|
|
renderer->GetRenderTarget()->DrawGeometry(geometry_, brush, 1.5f);
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
e2d::Collider::Relation e2d::Collider::GetRelationWith(Collider * collider) const
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (geometry_ && collider->geometry_)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (enabled_ && collider->enabled_)
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
D2D1_GEOMETRY_RELATION relation;
|
2018-09-04 22:42:34 +08:00
|
|
|
|
geometry_->CompareWithGeometry(
|
|
|
|
|
|
collider->geometry_,
|
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-09-04 22:42:34 +08:00
|
|
|
|
bool e2d::Collider::IsEnabled() const
|
2018-07-13 00:45:39 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return enabled_;
|
2018-07-13 00:45:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
bool e2d::Collider::IsVisible() const
|
2018-07-13 00:45:39 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return visible_;
|
2018-07-13 00:45:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
bool e2d::Collider::IsCollisionNotify() const
|
2018-07-13 00:45:39 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return notify_;
|
2018-07-13 00:45:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
void e2d::Collider::Recreate()
|
2018-04-02 23:01:38 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (!enabled_ || shape_ == Shape::None)
|
2018-07-07 18:04:18 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
SafeRelease(geometry_);
|
|
|
|
|
|
auto factory = Renderer::GetFactory();
|
2018-07-28 18:44:37 +08:00
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
switch (shape_)
|
2018-07-07 15:12:18 +08:00
|
|
|
|
{
|
2018-07-10 00:30:17 +08:00
|
|
|
|
case Shape::Rect:
|
2018-07-07 15:12:18 +08:00
|
|
|
|
{
|
|
|
|
|
|
ID2D1RectangleGeometry* rectangle = nullptr;
|
2018-08-15 00:06:03 +08:00
|
|
|
|
factory->CreateRectangleGeometry(
|
2018-09-04 22:42:34 +08:00
|
|
|
|
D2D1::RectF(0, 0, parent_node_->GetRealWidth(), parent_node_->GetRealHeight()),
|
2018-07-07 15:12:18 +08:00
|
|
|
|
&rectangle
|
|
|
|
|
|
);
|
2018-09-04 22:42:34 +08:00
|
|
|
|
geometry_ = rectangle;
|
2018-07-07 15:12:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
2018-04-02 23:01:38 +08:00
|
|
|
|
|
2018-07-10 00:30:17 +08:00
|
|
|
|
case Shape::Circle:
|
2018-07-07 15:12:18 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
float minSide = std::min(parent_node_->GetRealWidth(), parent_node_->GetRealHeight());
|
2018-07-07 15:12:18 +08:00
|
|
|
|
|
|
|
|
|
|
ID2D1EllipseGeometry* circle = nullptr;
|
2018-08-15 00:06:03 +08:00
|
|
|
|
factory->CreateEllipseGeometry(
|
2018-07-07 15:12:18 +08:00
|
|
|
|
D2D1::Ellipse(
|
|
|
|
|
|
D2D1::Point2F(
|
2018-09-04 22:42:34 +08:00
|
|
|
|
parent_node_->GetRealWidth() / 2,
|
|
|
|
|
|
parent_node_->GetRealHeight() / 2
|
2018-07-05 00:20:53 +08:00
|
|
|
|
),
|
2018-07-28 20:06:27 +08:00
|
|
|
|
minSide / 2,
|
|
|
|
|
|
minSide / 2
|
2018-07-07 15:12:18 +08:00
|
|
|
|
),
|
|
|
|
|
|
&circle
|
|
|
|
|
|
);
|
2018-09-04 22:42:34 +08:00
|
|
|
|
geometry_ = circle;
|
2018-07-07 15:12:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
2018-07-05 00:20:53 +08:00
|
|
|
|
|
2018-07-10 00:30:17 +08:00
|
|
|
|
case Shape::Ellipse:
|
2018-07-07 15:12:18 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
float halfWidth = parent_node_->GetWidth() / 2,
|
|
|
|
|
|
halfHeight = parent_node_->GetHeight() / 2;
|
2018-07-07 15:12:18 +08:00
|
|
|
|
|
|
|
|
|
|
ID2D1EllipseGeometry* ellipse = nullptr;
|
2018-08-15 00:06:03 +08:00
|
|
|
|
factory->CreateEllipseGeometry(
|
2018-07-07 15:12:18 +08:00
|
|
|
|
D2D1::Ellipse(
|
|
|
|
|
|
D2D1::Point2F(
|
|
|
|
|
|
halfWidth,
|
|
|
|
|
|
halfHeight),
|
|
|
|
|
|
halfWidth,
|
|
|
|
|
|
halfHeight),
|
|
|
|
|
|
&ellipse
|
|
|
|
|
|
);
|
2018-09-04 22:42:34 +08:00
|
|
|
|
geometry_ = ellipse;
|
2018-07-07 15:12:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
2018-07-05 00:20:53 +08:00
|
|
|
|
}
|
2018-04-02 23:01:38 +08:00
|
|
|
|
|
2018-09-01 23:00:08 +08:00
|
|
|
|
ID2D1TransformedGeometry * transformed;
|
2018-08-15 00:06:03 +08:00
|
|
|
|
factory->CreateTransformedGeometry(
|
2018-09-04 22:42:34 +08:00
|
|
|
|
geometry_,
|
|
|
|
|
|
parent_node_->final_matrix_,
|
2018-09-01 23:00:08 +08:00
|
|
|
|
&transformed
|
2018-07-07 18:04:18 +08:00
|
|
|
|
);
|
2018-09-04 22:42:34 +08:00
|
|
|
|
SafeRelease(geometry_);
|
|
|
|
|
|
geometry_ = transformed;
|
2018-04-02 23:01:38 +08:00
|
|
|
|
}
|