-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollision.hpp
111 lines (90 loc) · 2.23 KB
/
Collision.hpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#pragma once
#include <plugin/math/vec2.hpp>
#include "Rectangle.hpp"
#include<functional>
#include <typeinfo>
#include <memory>
namespace plugin::physics::bump
{
class CollisionResolution;
class CollisionResponse;
class World;
class Item;
typedef std::function<bool(Item*, Item*, Collision*)> Filter;
typedef std::function<bool(Item* a)> FilterSingle;
typedef std::vector<std::shared_ptr<Collision>> Collisions;
class CollisionResponse
{
friend class World;
public:
CollisionResponse(){};
virtual void Resolve(
CollisionResolution& resolution,
Collision* col,
const Rectangle& rect,
math::vec2 goal,
Filter filter
){};
void Resolve(
CollisionResolution& resolution,
Collision* col,
const Rectangle& rect,
math::vec2 goal)
{
return Resolve(resolution,col,rect,goal, nullptr);
}
void SetWorld(World* world){this->world = world;}
protected:
World* world=nullptr;
};
class Collision
{
public:
friend class World;
Collision(){}
template<typename T>
bool Is()
{
if (dynamic_cast<T*>(response.get()))
{
return true;
}
return false;
}
template<typename T>
void Respond(){
response = std::make_shared<T>();
}
CollisionResponse* Response()
{
return response.get();
}
public:
bool overlaps;
float ti;
math::vec2 move;
math::vec2 normal;
math::vec2 touch;
Rectangle itemRect;
Rectangle otherRect;
Item* item=nullptr;
Item* other=nullptr;
protected:
std::shared_ptr<CollisionResponse> response;
};
class CollisionResolution
{
public:
CollisionResolution(math::vec2 pos, Collisions cols)
{
this->pos = pos;
this->collisions = cols;
}
CollisionResolution()
{
this->pos = math::vec2(0,0);
}
math::vec2 pos;
Collisions collisions;
};
}