-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShip.cpp
More file actions
162 lines (143 loc) · 4.64 KB
/
Ship.cpp
File metadata and controls
162 lines (143 loc) · 4.64 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
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
157
158
159
160
161
162
using namespace std;
#include "Ship.h"
#include "data.h" //This must be included HERE so as not to have multiple declarations
#define PI 3.14159265358979
void Ship::draw()
{
if(nodraw) return;
glPushMatrix();
glDisable(GL_BLEND);
glScalef(0.8, 0.8, 1);
glTranslatef(pos.x(), pos.y(), pos.z());
glRotatef(fmodf((pos.az + 2*PI/4) * 180/PI, 360), 0.0, 0.0, 1.0);
glBindTexture(GL_TEXTURE_2D, texture);
/* This code came from the sample provided with the Milkshape header file.
* It's realy quite a mess, but it gets the job done for now.
*/
glBegin( GL_TRIANGLES );
for(int i=0; i<sph02.num_faces; i++)
{
//--- This is how to use the provided texture coordinates
glTexCoord2f( sph02.uvs[sph02.v_idx[i]].x, sph02.uvs[sph02.v_idx[i]].y );
glNormal3f( sph02.normals[sph02.n_idx[i]].x,
sph02.normals[sph02.n_idx[i]].y,
sph02.normals[sph02.n_idx[i]].z );
glVertex3f( sph02.vertices[sph02.v_idx[i]].x,
sph02.vertices[sph02.v_idx[i]].y,
sph02.vertices[sph02.v_idx[i]].z );
}
for(int i=0; i<box02.num_faces; i++)
{
//--- This is how to use the provided texture coordinates
glTexCoord2f( box02.uvs[box02.v_idx[i]].x, box02.uvs[box02.v_idx[i]].y );
glNormal3f( box02.normals[box02.n_idx[i]].x,
box02.normals[box02.n_idx[i]].y,
box02.normals[box02.n_idx[i]].z );
glVertex3f( box02.vertices[box02.v_idx[i]].x,
box02.vertices[box02.v_idx[i]].y,
box02.vertices[box02.v_idx[i]].z );
}
for(int i=0; i<dup00.num_faces; i++)
{
//--- This is how to use the provided texture coordinates
glTexCoord2f( dup00.uvs[dup00.v_idx[i]].x, dup00.uvs[dup00.v_idx[i]].y );
glNormal3f( dup00.normals[dup00.n_idx[i]].x,
dup00.normals[dup00.n_idx[i]].y,
dup00.normals[dup00.n_idx[i]].z );
glVertex3f( dup00.vertices[dup00.v_idx[i]].x,
dup00.vertices[dup00.v_idx[i]].y,
dup00.vertices[dup00.v_idx[i]].z );
}
for(int i=0; i<dup01.num_faces; i++)
{
//--- This is how to use the provided texture coordinates
glTexCoord2f( dup01.uvs[dup01.v_idx[i]].x, dup01.uvs[dup01.v_idx[i]].y );
glNormal3f( dup01.normals[dup01.n_idx[i]].x,
dup01.normals[dup01.n_idx[i]].y,
dup01.normals[dup01.n_idx[i]].z );
glVertex3f( dup01.vertices[dup01.v_idx[i]].x,
dup01.vertices[dup01.v_idx[i]].y,
dup01.vertices[dup01.v_idx[i]].z );
}
for(int i=0; i<cyl03.num_faces; i++)
{
//--- This is how to use the provided texture coordinates
glTexCoord2f( cyl03.uvs[cyl03.v_idx[i]].x, cyl03.uvs[cyl03.v_idx[i]].y );
glNormal3f( cyl03.normals[cyl03.n_idx[i]].x,
cyl03.normals[cyl03.n_idx[i]].y,
cyl03.normals[cyl03.n_idx[i]].z );
glVertex3f( cyl03.vertices[cyl03.v_idx[i]].x,
cyl03.vertices[cyl03.v_idx[i]].y,
cyl03.vertices[cyl03.v_idx[i]].z );
}
glEnd();
glEnable(GL_BLEND);
glPopMatrix();
}
bool Ship::init()
{
last_fire = 0.0;
fire_rate = 300;
//Initialize the variables needed to draw the ship
sph02.vertices = Sphere02_vertex;
sph02.v_idx = Sphere02_vidx;
sph02.normals = Sphere02_normal;
sph02.n_idx = Sphere02_nidx;
sph02.uvs = Sphere02_uv;
sph02.num_faces = (sizeof(Sphere02_vidx)/sizeof(long));
box02.vertices = Box02_vertex;
box02.v_idx = Box02_vidx;
box02.normals = Box02_normal;
box02.n_idx = Box02_nidx;
box02.uvs = Box02_uv;
box02.num_faces = (sizeof(Box02_vidx)/sizeof(long));
dup00.vertices = Duplicate00_vertex;
dup00.v_idx = Duplicate00_vidx;
dup00.normals = Duplicate00_normal;
dup00.n_idx = Duplicate00_nidx;
dup00.uvs = Duplicate00_uv;
dup00.num_faces = (sizeof(Duplicate00_vidx)/sizeof(long));
cyl03.vertices = Duplicate01_vertex;
cyl03.v_idx = Duplicate01_vidx;
cyl03.normals = Duplicate01_normal;
cyl03.n_idx = Duplicate01_nidx;
cyl03.uvs = Duplicate01_uv;
cyl03.num_faces = (sizeof(Duplicate01_vidx)/sizeof(long));
dup01.vertices = Cylinder03_vertex;
dup01.v_idx = Cylinder03_vidx;
dup01.normals = Cylinder03_normal;
dup01.n_idx = Cylinder03_nidx;
dup01.uvs = Cylinder03_uv;
dup01.num_faces = (sizeof(Cylinder03_vidx)/sizeof(long));
reset();
return true;
}
void Ship::reset()
{
pos.p = 15.0f;
pos.az = 0.0f;
pos.h = 32.0f;
stat1 = 100;
}
void Ship::print()
{
printf("New ship:\nx: %f\ny: %f\nz: %f\np: %f\nh: %f\naz: %f\n", pos.x(), pos.y(), pos.z(), pos.p, pos.h, pos.az);
}
void Ship::update(float dt)
{
translate(dt*velocity.x, dt*velocity.y, dt*velocity.z);
}
bool Ship::canFire(float new_update)
{
if( (int)(new_update - last_fire) > fire_rate ) {
//printf("%f\n", (new_update - last_fire));
last_fire = new_update;
return true;
}
else return false;
}
bool Ship::damage(int dmg)
{
if(stat1 > 0) stat1 = max(stat1 - dmg, 0);
return stat1 == 0;
}