-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoxCollider.cpp
82 lines (66 loc) · 2.28 KB
/
BoxCollider.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "BoxCollider.h"
#include <cassert>
#include "game.h"
#include "surface.h"
#include "Transform.h"
CollisionType BoxCollider::Collides(const Entity& entityA, const Entity& entityB) {
const std::vector<BoxCollider*> collidersA = entityA.GetComponents<BoxCollider>();
const std::vector<BoxCollider*> collidersB = entityB.GetComponents<BoxCollider>();
if(collidersA.empty() || collidersB.empty())
return CollisionType::none;
CollisionType highestCollision = CollisionType::none;
for (const auto a : collidersA)
{
for (const auto b : collidersB)
{
if(a->CollidesWith(*b))
{
if(b->type == CollisionType::block)
{
return CollisionType::block;
}
highestCollision = CollisionType::hurt;
}
}
}
return highestCollision;
}
bool BoxCollider::CollidesWith(const BoxCollider& collider) const
{
const tmpl8::vec2 min1 = {bounds.Left(), bounds.Top()};
const tmpl8::vec2 max1 = {bounds.Right(), bounds.Bottom()};
const tmpl8::vec2 min2 = {collider.bounds.Left(), collider.bounds.Top()};
const tmpl8::vec2 max2 = {collider.bounds.Right(), collider.bounds.Bottom()};
return !(max1.x < min2.x || min1.x > max2.x || max1.y < min2.y || min1.y > max2.y);
}
void BoxCollider::Render(Entity& entity, tmpl8::Surface& screen)
{
if (tmpl8::Game::Get().IsDebug())
{
screen.Box(static_cast<int>(bounds.Left()), static_cast<int>(bounds.Top()), static_cast<int>(bounds.Right()), static_cast<int>(bounds.Bottom()), 0x0);
}
}
float Bounds::Top() const
{
const Transform* transform = entity.GetComponent<Transform>();
assert(transform != nullptr);
return transform->GetPosition().y + offset.y;
}
float Bounds::Bottom() const
{
const Transform* transform = entity.GetComponent<Transform>();
assert(transform != nullptr);
return transform->GetPosition().y + offset.y + size.y;
}
float Bounds::Left() const
{
const Transform* transform = entity.GetComponent<Transform>();
assert(transform != nullptr);
return transform->GetPosition().x + offset.x;
}
float Bounds::Right() const
{
const Transform* transform = entity.GetComponent<Transform>();
assert(transform != nullptr);
return transform->GetPosition().x + offset.x + size.x;
}