forked from torandi/omgspaceponnies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
326 lines (269 loc) · 6.3 KB
/
player.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "player.h"
#include "common.h"
#include "render.h"
#include "level.h"
#include "texture.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <math.h>
#include <sys/time.h>
Player::Player(const char * n, int _id) {
nick = std::string(n);
init(_id);
}
Player::Player(int _id) {
init(_id);
}
void Player::init(int _id) {
char texture[64];
angle = PI;
power = 1.0;
fire = false;
id = _id;
dead = 0;
da = 0;
full_shield = false;
shield_angle = PI;
velocity = vector_t(0.0f,0.0f);
current_base_texture = TEXTURE_BASE;
//Load textures:
vector_t size = vector_t(PLAYER_W, PLAYER_H);
shield = new Texture("gfx/shield.png",1);
//Base
sprintf(texture,"gfx/player%i/base.png", id+1);
textures[TEXTURE_BASE] = RenderObject(texture, 1, 25, size);
//Dash
sprintf(texture,"gfx/dash.png");
textures[TEXTURE_DASH] = RenderObject(texture, 1, 25, size);
//Fwd
sprintf(texture,"gfx/fwd.png");
textures[TEXTURE_FWD] = RenderObject(texture, 1, 25, size);
//Left
sprintf(texture,"gfx/left.png");
textures[TEXTURE_LEFT] = RenderObject(texture, 1, 25, size);
//Right
sprintf(texture,"gfx/right.png");
textures[TEXTURE_RIGHT] = RenderObject(texture, 1, 25, size);
//Tail
sprintf(texture,"gfx/tail.png");
textures[TEXTURE_TAIL] = RenderObject(texture, 9, 25, size);
//Dispencer
sprintf(texture,"gfx/dispencer.png");
textures[TEXTURE_DISPENCER] = RenderObject(texture, 6, 25, size);
}
void Player::spawn() {
pos = get_rand_spawn();
dead = 0;
power = 1.0;
da = 0;
angle = PI;
}
void Player::render(double dt) {
texture_colors[1]=1;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(pos.x, pos.y, 0);
glRotatef(radians_to_degrees(angle+PI/2.0), 0, 0, 1.0);
glTranslatef(-PLAYER_W*0.5,-PLAYER_H*0.5, 0);
//Draw textures:
textures[TEXTURE_BASE].render(dt);
textures[TEXTURE_TAIL].render(dt);
if(current_base_texture != TEXTURE_BASE)
textures[current_base_texture].render(dt);
if(fire)
textures[TEXTURE_DISPENCER].render(dt);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
if(!fire) {
glPushMatrix();
glTranslatef(pos.x-shield->width()/2, pos.y-shield->height()/2,0);
shield->bind();
if(full_shield) {
glBegin(GL_QUADS);
glTexCoord2f(0,1.0); glVertex2f(0,200.0);
glTexCoord2f(0,0); glVertex2f(0,0);
glTexCoord2f(1.0,0); glVertex2f(200.0,0);
glTexCoord2f(1.0,1.0); glVertex2f(200.0,200.0);
glEnd();
} else {
float sx1, sy1, sx2, sy2;
shield_coords(shield_angle-PI/4.0,sx1,sy1);
shield_coords(shield_angle+PI/4.0,sx2,sy2);
int corner = (int)floor(period(shield_angle)/(PI/2.0));
float cx, cy;
switch(corner) {
case 0:
cx = 0;
cy = 1;
break;
case 1:
cx = 0;
cy = 0;
break;
case 2:
cx = 1;
cy = 0;
break;
case 3:
cx = 1;
cy = 1;
break;
}
glBegin(GL_TRIANGLES);
glTexCoord2f(sx1,sy1); glVertex2f(200.0*sx1,200.0*sy1);
glTexCoord2f(sx2,sy2); glVertex2f(200.0*sx2,200.0*sy2);
glTexCoord2f(0.5, 0.5); glVertex2f(100.0, 100.0);
glTexCoord2f(sx1,sy1); glVertex2f(200.0*sx1,200.0*sy1);
glTexCoord2f(sx2,sy2); glVertex2f(200.0*sx2,200.0*sy2);
glTexCoord2f(cx,cy); glVertex2f(200.0*cx,200.0*cy);
glEnd();
}
glPopMatrix();
}
texture_colors[1]=0.5;
}
void Player::shield_coords(float a, float &x, float &y) {
a = period(a+PI/2.0);
if(a <= PI/4.0 || a >= 7.0*PI/4.0) {
x = 1.0;
y = tan(a);
} else if(a > PI/4.0 && a <= 3.0*PI/4.0) {
x = cos(a)/sin(a);
y = 1;
} else if(a > 3.0*PI/4.0 && a <= 5.0*PI/4.0) {
x = -1.0;
y = -tan(a);
} else {
x = -cos(a)/sin(a);
y = -1.0;
}
x = (x+1.0)*0.5;
y = (y+1.0)*0.5;
}
void Player::render_fire(double dt) {
glDisable(GL_TEXTURE_2D);
if(fire) {
glLineWidth(2.0f);
glBegin(GL_LINES);
for(int i=0;i<12;++i) {
float dx = i*cos(angle+PI/2.0) - 6*cos(angle+PI/2.0) + cos(angle)*PLAYER_H/2.0;
float dy = i*sin(angle+PI/2.0) - 6*sin(angle+PI/2.0) + sin(angle)*PLAYER_H/2.0;
glColor3f(rbcolors[i][0],rbcolors[i][1],rbcolors[i][2]);
glVertex2f(pos.x+dx, pos.y+dy);
glVertex2f(fire_end.x+dx, fire_end.y+dy);
glVertex2f(pos.x+dx, pos.y+dy);
glVertex2f(pos.x+cos(angle)*PLAYER_H*0.1,pos.y+sin(angle)*PLAYER_H*0.1);
}
glEnd();
glLineWidth(1.0f);
}
glEnable(GL_TEXTURE_2D);
}
void Player::logic(double dt) {
pos += velocity * dt;
angle += dt*da;
if(fire) {
calc_fire(false);
}
if(dead > 0) {
++dead;
if(dead > RESPAWN_TIME)
spawn();
}
power+=(power*0.3+1)*dt*PWR_REGEN_FACTOR;
if(power > 1.0)
power = 1.0;
}
bool Player::use_power(float amount) {
if(power >= amount) {
power -= amount;
return true;
} else {
if(this == me && flash_power <= 0)
flash_power = 1;
return false;
}
}
void Player::calc_fire(bool detect_kill) {
//fire_end.x = ((pos.x/64)+1)*64;
//fire_end.y = ((pos.y/64)+1)*64;
fire_end = pos;
int len = 0;
while(len < MAX_FIRE_LENGHT) {
len += 32;
vector_t prev(fire_end);
fire_end.x+= 32*cos(angle);
fire_end.y+= 32*sin(angle);
for(int i=0; i < NUM_PLAYERS; ++i) {
if(i != id && players[i] != NULL && players[i]->dead == 0) {
vector_t d;
d.x = fire_end.x - players[i]->pos.x;
d.y = fire_end.y - players[i]->pos.y;
if(d.norm() < PLAYER_W/2.0) {
players[i]->dead = 1;
printf("Killed player %i\n", i);
char buffer[128];
sprintf(buffer, "omg kil %i", i);
}
}
int mx, my;
calc_map_index(fire_end, mx, my);
if(map_value(mx, my) > 0) {
fire_end = prev;
//map[my][mx]+=10;
break;
}
}
}
}
void Player::accelerate(const vector_t &dv) {
velocity+=dv;
if(velocity.norm() > MAX_VELOCITY) {
velocity = velocity.normalized() * MAX_VELOCITY;
}
}
/**
* Fetches the specified collision point
* Set a to an angle to use that instead of this->angle
*/
vector_t Player::collision_point(int i, const float * a) const{
float ax, ay;
if(a == NULL)
a = ∠
ax = PLAYER_W/2.0;
ay = PLAYER_H/2.0;
vector_t v(0,0);
switch(i) {
case 0:
v.x -= ax-17.0;
v.y -= ay-13.0;
break;
case 1:
v.y -= ay-10.0;
break;
case 2:
v.x += ax-14.0;
v.y -= ay-16.0;
break;
case 3:
v.x -= ax-20.0;
break;
case 4:
v.x += ax-20.0;
break;
case 5:
v.x -= ax-8.0;
v.y += ay-23.0;
break;
case 6:
v.y += ay;
break;
case 7:
v.x += ax-12.0;
v.y += ay-18.0;
break;
}
return pos+v.rotate(*a-PI*0.5);
}