-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectile.h
157 lines (110 loc) · 2.97 KB
/
projectile.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* File: projectile_t.h
* Author: 2019202344
*
* Created on 6 de Dezembro de 2019, 15:21
*/
#ifndef PROJECTILE_T_H
#define PROJECTILE_T_H
#include <GL/glu.h>
#include <vector>
#include "shapes.h"
/**
* An object that can stop a projectile.
*/
class obstacle_t {
public:
virtual float getRadius() const = 0;
virtual point3f getPosition() const = 0;
bool overlaps(const point3f& p, float radius) {
float bounds = radius + getRadius();
return point3f::distanceSqr(getPosition(), p) < bounds * bounds;
}
bool overlaps(const obstacle_t* o) {
return overlaps(o->getPosition(), o->getRadius());
}
virtual bool isAlive() const {
return true;
}
};
/**
* An abstract class that uses a projectile movement to move the object.
*/
class projectile_t : public obstacle_t {
private:
static std::vector<projectile_t*> sProjectileQueue;
/**
* The initial point that the projectile started.
*/
point3f offset;
/**
* The current position of the projectile.
*/
point3f position;
/**
* The current velocity of the projectile.
*/
vector3f velocity;
float accumulatedTime = 0;
bool dead = false;
public:
projectile_t(const point3f& offset, const vector3f& velocity);
point3f getPosition() const override;
const vector3f& getVelocity() const {
return velocity;
}
vector3f& getVelocity() {
return velocity;
}
void update(int millis);
virtual void move(float time);
bool isDead() const {
return dead;
}
void kill() {
dead = true;
}
void transformAndDraw() const;
virtual void hit(obstacle_t* other);
static void addProjectile(projectile_t* p);
void clip(float radius, float height) {
float r = radius - getRadius();
if (position.z < getRadius() || position.z + getRadius() > height ||
position.x * position.x + position.y * position.y >= r * r) {
kill();
}
}
virtual void draw() const = 0;
};
/**
* A class that keeps track of all projectiles and hadling collision between the
* projectiles and obstacles. Both projectiles and obstacles can be added.
*/
class projectile_manager_t {
private:
/**
* Obstacles that cannot die, but can stop a projectile.
*/
std::vector<obstacle_t*> inanimateObstacles;
/**
* Obstacles that can die and stops a projectile.
*/
std::vector<obstacle_t*> animatedObstacles;
std::vector<projectile_t*> projectiles;
public:
void addObstacles(obstacle_t* obs, bool animated);
void addProjectile(projectile_t* p);
void update(int millis);
void removeOutsideOfArena(float radius, float height);
void draw();
void collectGarbage();
void reset() {
for (projectile_t* p : projectiles) {
delete p;
}
projectiles.clear();
}
private:
void hit(obstacle_t* o);
};
#endif /* PROJECTILE_T_H */