-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentity.cpp
62 lines (55 loc) · 2.35 KB
/
entity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "entity.h"
Entity::Entity(const Vortex2D::Renderer::Device& device,
const glm::ivec2& size,
float scale,
ShapeType type,
std::unique_ptr<Vortex2D::Renderer::Shape> shape,
Vortex2D::Renderer::RenderCommand cmd,
b2World& box2dWorld)
: mScale(scale), mShapeType(type), mShape(std::move(shape)), mCmd(std::move(cmd))
{
mShapeType.match(
[&](const Circle& circle) {
auto radius = circle.mRadius / scale;
mRigidbody = std::make_unique<CircleRigidbody>(device,
size,
box2dWorld,
b2_staticBody,
Vortex2D::Fluid::RigidBody::Type::eStatic,
radius);
},
[&](const Rectangle& rectangle) {
auto halfRectSize = rectangle.mSize / (glm::vec2(2.0f) * scale);
mRigidbody = std::make_unique<RectangleRigidbody>(device,
size,
box2dWorld,
b2_staticBody,
Vortex2D::Fluid::RigidBody::Type::eStatic,
halfRectSize);
},
[&](const Polygon& /*polygon*/) {
});
mRigidbody->SetTransform(mShape->Position / mScale, mShape->Rotation);
mRigidbody->mBody->SetUserData(this);
}
void Entity::SetTransform(const glm::vec2& pos, float angle)
{
mShape->Position = pos;
mShape->Rotation = angle;
mRigidbody->SetTransform(pos / mScale, angle);
}
void Entity::UpdateTransform()
{
auto pos = mScale * mRigidbody->mRigidbody->Position;
auto angle = mRigidbody->mRigidbody->Rotation;
mShape->Position = pos;
mShape->Rotation = angle;
}
bool IsValid(const ShapeType& type)
{
return type.match([&](const Circle& circle) { return circle.mRadius > 0.0f; },
[&](const Rectangle& rectangle) {
return rectangle.mSize.x > 0.0f && rectangle.mSize.y > 0.0f;
},
[&](const Polygon& /*polygon*/) { return false; });
}