-
Notifications
You must be signed in to change notification settings - Fork 0
/
gltrail.cpp
93 lines (84 loc) · 2.38 KB
/
gltrail.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
83
84
85
86
87
88
89
90
91
92
93
#include "gltrail.h"
#include "ship.h"
#include "particle.h"
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/glut.h>
#endif
#include <math.h>
#include <list>
using namespace std;
GLTrail::GLTrail(GLShip* ship, float deviation, Point offset, float speed, float rotation, int type, float life)
: type(type), ship(ship), offset(offset), deviation(deviation), rotation(rotation), speed(speed), life(life) {
last_add_time = glutGet(GLUT_ELAPSED_TIME);
point_size = 3.5f;
}
GLTrail::~GLTrail() {
while(!trail.empty()) {
delete trail.back();
trail.pop_back();
}
}
void GLTrail::draw() {
list<Particle*>::iterator p;
glPointSize(point_size);
glBegin(GL_POINTS);
for(p = trail.begin(); p != trail.end(); p++) {
glColor4f(1.0-ship->color[0], 1.0-ship->color[1], 1.0-ship->color[2],(*p)->aliveness());
glVertex2fv((*p)->position);
}
glEnd();
}
void GLTrail::collide_grid(Grid &grid) {
list<Particle*>::iterator t = trail.begin();
Object *object;
while(t != trail.end()) {
object = grid.collide(*(*t));
if(object != NULL) {
delete *t;
t = trail.erase(t);
} else {
t++;
}
}
}
void GLTrail::step(float delta) {
list<Particle*>::iterator t = trail.begin();
while(t != trail.end()) {
(*t)->step(delta);
if(!(*t)->is_alive()) {
delete *t;
t = trail.erase(t);
} else {
t++;
}
}
if(type & ALWAYS ||
(type & THRUSTING && ship->ship->thrusting) ||
(type & REVERSING && ship->ship->reversing) ||
(type & LEFT && ship->ship->rotation_direction == Ship::LEFT) ||
(type & RIGHT && ship->ship->rotation_direction == Ship::RIGHT)) {
int current_time = glutGet(GLUT_ELAPSED_TIME);
if(last_add_time + add_interval < current_time) {
add();
last_add_time = current_time;
}
}
}
void GLTrail::add() {
Point velocity;
Point position;
Point direction;
//TODO: cross product or something?
position = ship->ship->tail() + ship->ship->facing * offset.y() + ship->ship->facing.perpendicular() * offset.x();
direction = ship->ship->facing*-1.0;
direction.rotate(rotation);
velocity = direction*speed + ship->ship->velocity;
velocity.rotate((rand() / (float)RAND_MAX) * deviation - deviation / 2.0);
Particle *particle = new Particle(position, velocity, life);
trail.push_back(particle);
}