-
Notifications
You must be signed in to change notification settings - Fork 0
/
collisionMatrix.h
98 lines (85 loc) · 3.29 KB
/
collisionMatrix.h
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
#ifndef COLLISIONMATRIX_H
#define COLLISIONMATRIX_H
#include "gameobjecttype.h"
/**
* @brief The CollisionMatrix class is a helper class that stores a matrix of whether different types of objects can collide with each other.
*/
class CollisionMatrix
{
public:
/**
* @brief Construct a new Collision Matrix object.
* @note Alter this constructor definition to change the default collision matrix.
* @author Koda Koziol
* @date SPRING 2024
*/
CollisionMatrix()
{
setAllCanCollide(true);
setCanCollide(GameObjectType::Obstacle, GameObjectType::Obstacle, false);
setCanCollide(GameObjectType::PlayerTank, GameObjectType::PlayerProjectile, false);
setCanCollide(GameObjectType::EnemyTank, GameObjectType::EnemyProjectile, false);
}
/**
* @param type1 The first type of object
* @param type2 The second type of object
* @return True if type1 can collide with type2, false otherwise. Note that this is not symmetric.
* @author Koda Koziol
* @date SPRING 2024
*/
inline bool canCollide(GameObjectType type1, GameObjectType type2) const
{
return matrix[static_cast<int>(type1)][static_cast<int>(type2)];
}
/**
* @brief Set whether two types of objects can collide with each other.
* @param type1 The first type of object
* @param type2 The second type of object
* @param canCollide True if type1 should collide with type2, false otherwise
* @param symmetric If false, the collision will not be symmetric (i.e. type1 can collide with type2 but not vice versa). Default is true.
* @author Koda Koziol
* @date SPRING 2024
*/
inline void setCanCollide(GameObjectType type1, GameObjectType type2, bool canCollide, bool symmetric = true)
{
int t1 = static_cast<int>(type1);
int t2 = static_cast<int>(type2);
matrix[t1][t2] = canCollide;
if (symmetric)
matrix[t2][t1] = canCollide;
}
/**
* @brief Set whether a type of object can collide with all other types of objects.
* @param type1 The type of object
* @param canCollide True if type1 should collide with all other types, false otherwise
* @param symmetric If false, the collision will not be symmetric (i.e. type1 can collide with all other types but not vice versa). Default is true.
* @author Koda Koziol
* @date SPRING 2024
*/
inline void setCanCollide(GameObjectType type1, bool canCollide, bool symmetric = true)
{
for (int i = 0; i < NUM_GAME_OBJECT_TYPES; i++)
{
matrix[static_cast<int>(type1)][i] = canCollide;
if (symmetric)
matrix[i][static_cast<int>(type1)] = canCollide;
}
}
/**
* @brief Set whether all types of objects can collide with each other.
* @param canCollide True if all types should collide with each other, false otherwise.
* @author Koda Koziol
* @date SPRING 2024
*/
inline void setAllCanCollide(bool canCollide)
{
for (int i = 0; i < NUM_GAME_OBJECT_TYPES; i++)
{
for (int j = 0; j < NUM_GAME_OBJECT_TYPES; j++)
matrix[i][j] = canCollide;
}
}
private:
bool matrix[NUM_GAME_OBJECT_TYPES][NUM_GAME_OBJECT_TYPES];
};
#endif // COLLISIONMATRIX_H