forked from kyleconroy/starfighter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathship.cpp
More file actions
113 lines (95 loc) · 2.84 KB
/
ship.cpp
File metadata and controls
113 lines (95 loc) · 2.84 KB
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
#include "ship.h"
#include "shipgeometry.h"
Ship::Ship() {
position = Eigen::Vector3f(0, 0, 0);
velocity = Eigen::Vector3f(0, 0, -0.2);
acceleration = Eigen::Vector3f(0, 0, 0);
rotate = Eigen::Vector3f(0, 90.0, 0);
mesh = new ShipGeometry("geometry/new_LGS2.obj");
bs = new BoundingSphere(this);
bs->center = position;
bs->radius = 0.084;
direction = NONE;
radius = 0.084;
health = 100;
score = -1;
damage = -1;
barrelRoll = -1;
shipPart = true;
frozen = false;
}
void Ship::hit(Model *m) {
health -= m->damage;
}
void Ship::update() {
if (direction != NONE) {
unsigned int v = (direction == LEFT || direction == RIGHT) ? 0 : 1;
float decceleration = calculateDecceleration(velocity[v]);
if (direction == RIGHT || direction == UP) {
decceleration = -decceleration;
}
velocity[v] += decceleration;
if (fequals(velocity[v], 0.0)) {
direction = NONE;
}
}
updateBarrelRoll();
if (!frozen){
velocity.z() = -0.05;
position += velocity;
}
bs->center = position;
}
void Ship::updateDirection(unsigned int mouseX, unsigned int mouseY,
unsigned int width, unsigned int height) {
float mouseZSpace = position.z() - radius - 0.05;
float mouseXSpace = (float) mouseX/width - 0.5;
float mouseYSpace = -((float) mouseY/height - 0.5);
Eigen::Vector3f mousePos = Eigen::Vector3f(mouseXSpace, mouseYSpace, mouseZSpace);
float rotateY = RADIANS_TO_DEGREES(atan((mouseXSpace - position.x()) / (mouseZSpace - position.z()))) + 90.0;
float rotateX = -RADIANS_TO_DEGREES(atan((mouseYSpace - position.y()) / (mouseZSpace - position.z())));
rotate.y() = rotateY;
rotate.x() = rotateX;
Eigen::Vector3f direction = mousePos - position;
velocity.normalize();
velocity = direction / 20.0;
velocity.z() -= 0.01;
}
void Ship::draw() {
glPushMatrix();
glTranslatef(position.x(), position.y(), position.z());
glRotatef(rotate.z(), 0, 0, 1);
glRotatef(rotate.x(), 1, 0, 0);
glRotatef(rotate.y(), 0, 1, 0);
glScalef(scale.x(), scale.y(), scale.z());
mesh->draw();
glPopMatrix();
}
float Ship::calculateDecceleration(float current) {
current = fabs(current);
if (current > 0.0005) {
return 0.0002;
}
return current;
}
void Ship::doABarrelRoll() {
playAudio("barrelroll");
if (barrelRoll >= 0) {
// barrel roll in progress, cannot start a new one
return;
} else {
// start a barrel roll
barrelRoll = 0;
}
}
void Ship::updateBarrelRoll() {
if (barrelRoll < 0) {
return;
} else if (barrelRoll >= BARREL_ROLL_FRAMES) {
// barrel roll completed
barrelRoll = -1;
return;
}
rotate.z() += 360.0*BARREL_ROLL_REVOLUTIONS / BARREL_ROLL_FRAMES;
barrelRoll++;
}