]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/player.cpp
Import of version 1.14 minus music and sounds.
[quix0rs-blobwars.git] / src / player.cpp
1 /*
2 Copyright (C) 2004 Parallel Realities
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "player.h"
22
23 void resetPlayer()
24 {
25         game.getCheckPoint(&player.x, &player.y);
26         
27         player.dx = 0;
28         player.dy = 0;
29         player.immune = 120;
30         player.environment = ENV_AIR;
31         player.oxygen = 7;
32         player.fuel = 7;
33         addTeleportParticles(player.x + 10, player.y + 10, 50, SND_TELEPORT2);
34         
35         Math::removeBit(&player.flags, ENT_FLIES);
36         Math::removeBit(&player.flags, ENT_TELEPORTING);
37         player.setSprites(graphics.getSprite("BobRight", true), graphics.getSprite("BobLeft", true), graphics.getSprite("BobSpin", true));
38         
39         map.windPower = 0;
40         map.windChangeTime = 90;
41 }
42
43 void gibPlayer()
44 {
45         float x, y, dx, dy;
46         int amount = (game.gore) ? 25 : 150;
47
48         for (int i = 0 ; i < amount ; i++)
49         {
50                 x = player.x + Math::rrand(-3, 3);
51                 y = player.y + Math::rrand(-3, 3);
52                 
53                 if (game.gore)
54                 {
55                         dx = Math::rrand(-5, 5);
56                         dy = Math::rrand(-15, -5);
57                         addEffect(x, y, dx, dy, EFF_BLEEDS);
58                 }
59                 else
60                 {
61                         dx = Math::rrand(-5, 5);
62                         dy = Math::rrand(-5, 5);
63                         addColoredEffect(x, y, dx, dy, graphics.yellow, EFF_COLORED + EFF_WEIGHTLESS);
64                 }
65         }
66
67         (game.gore) ? audio.playSound(SND_SPLAT, CH_ANY) : audio.playSound(SND_POP, CH_ANY);
68 }
69
70 void checkPlayerBulletCollisions(Entity *bullet)
71 {
72         if ((bullet->health < 1) || (player.health <= -60))
73         {
74                 return;
75         }
76
77         if ((player.flags & ENT_TELEPORTING) || (game.missionOver > 0))
78         {
79                 return;
80         }
81
82         if (bullet->owner != &player)
83         {
84                 if (Collision::collision(&player, bullet))
85                 {
86                         if ((!player.immune) && (!(bullet->flags & ENT_EXPLODES)))
87                         {
88                                 addBlood(&player, bullet->dx / 4, Math::rrand(-6, -3), 1);
89                                 audio.playSound(SND_HIT, CH_ANY);
90                                 if (game.missionOverReason == MIS_INPROGRESS)
91                                 {
92                                         player.health -= bullet->damage;
93                                         player.immune = 120;
94                                 }
95                         }
96
97                         Math::removeBit(&bullet->flags, ENT_SPARKS);
98                         Math::removeBit(&bullet->flags, ENT_PUFFS);
99
100                         if (player.health <= 0)
101                         {
102                                 player.dx = bullet->dx;
103                                 player.dy = -5;
104                                 audio.playSound(SND_DEATH1 + Math::prand() % 3, CH_DEATH);
105                                 player.health = 0;
106                         }
107
108                         if (bullet->id == WP_LASER)
109                         {
110                                 debug(("Laser Hit Player"));
111                                 throwAndDamageEntity(&player, 0, -3, 3, -8);
112                         }
113                         else
114                         {
115                                 bullet->health = 0;
116                         }
117                 }
118         }
119 }
120
121 void doPlayer()
122 {
123         if (engine.cheatHealth)
124         {
125                 player.health = MAX_HEALTH;
126         }
127
128         if (game.missionOverReason == MIS_PLAYERESCAPE)
129         {
130                 return;
131         }
132
133         if (player.y > (map.limitDown + 500))
134         {
135                 if (game.missionOver == 0)
136                 {
137                         player.health--;
138                 }
139
140                 if (player.health > 0)
141                 {
142                         game.setMissionOver(MIS_PLAYEROUT);
143                 }
144                 else
145                 {
146                         game.setMissionOver(MIS_PLAYERDEAD);
147                 }
148         }
149         
150         if (player.flags & ENT_TELEPORTING)
151         {
152                 moveEntity(&player);
153                 return;
154         }
155
156         if (game.missionOverReason == MIS_PLAYEROUT)
157         {
158                 graphics.blit(player.getFaceImage(), (int)(player.x - engine.playerPosX), (int)(player.y - engine.playerPosY), graphics.screen, false);
159                 moveEntity(&player);
160                 return;
161         }
162
163         if ((player.health < 1) || (player.immune > 120))
164         {
165                 if (player.health <= -60)
166                 {
167                         return;
168                 }
169
170                 player.think();
171                 
172                 if (game.hasAquaLung)
173                 {
174                         player.oxygen = 7;
175                 }
176
177                 moveEntity(&player);
178
179                 graphics.blit(player.getFaceImage(), (int)(player.x - engine.playerPosX), (int)(player.y - engine.playerPosY), graphics.screen, false);
180
181                 if (player.health < 1)
182                 {
183                         player.health--;
184                 }
185
186                 if (player.health <= -60)
187                 {
188                         gibPlayer();
189                 }
190
191                 return;
192         }
193
194         if ((!(player.flags & ENT_FLIES)) && (!map.isIceLevel))
195         {
196                 player.dx = 0;
197         }
198         
199         // toggles the Jetpack if available
200         if (config.isControl(CONTROL::JETPACK))
201         {
202                 if ((game.hasJetPack) || (engine.cheatExtras))
203                 {
204                         if (player.flags & ENT_FLIES)
205                         {
206                                 Math::removeBit(&player.flags, ENT_FLIES);
207                                 player.setSprites(graphics.getSprite("BobRight", true), graphics.getSprite("BobLeft", true), graphics.getSprite("BobSpin", true));
208                         }
209                         else
210                         {
211                                 if ((player.fuel > 3) && (player.environment == ENV_AIR))
212                                 {
213                                         Math::addBit(&player.flags, ENT_FLIES);
214                                         player.setSprites(graphics.getSprite("JPBobRight", true), graphics.getSprite("JPBobLeft", true), graphics.getSprite("BobSpin", true));
215                                 }
216                                 else if ((player.environment == ENV_WATER))
217                                 {
218                                         engine.setInfoMessage("Jetpack cannot be used underwater", 0, INFO_NORMAL);
219                                 }
220                                 else
221                                 {
222                                         engine.setInfoMessage("Jetpack is recharging...", 0, INFO_NORMAL);
223                                 }
224                         }
225                 }
226                 else
227                 {
228                         engine.setInfoMessage("Don't have jetpack!", 0, INFO_NORMAL);
229                 }
230                 
231                 config.resetControl(CONTROL::JETPACK);
232         }
233         
234         if (map.isBlizzardLevel)
235         {
236                 if ((!config.isControl(CONTROL::LEFT)) && (!config.isControl(CONTROL::RIGHT)))
237                 {
238                         if ((player.dx > -2) && (player.dx < 2))
239                         {
240                                 player.dx += map.windPower * 0.1;
241                         }
242                 }
243         }
244
245         if (config.isControl(CONTROL::LEFT))
246         {
247                 player.face = 1;
248                 
249                 if (player.flags & ENT_FLIES)
250                 {
251                         player.dx -= 0.1;
252                         Math::limitFloat(&player.dx, -PLAYER_FLY_SPEED, PLAYER_FLY_SPEED);
253                 }
254                 else if (map.isIceLevel)
255                 {
256                         player.dx -= 0.1;
257                         Math::limitFloat(&player.dx, -PLAYER_WALK_SPEED * 1.25, PLAYER_WALK_SPEED * 1.25);
258                         player.animate();
259                 }
260                 else
261                 {
262                         player.dx = -PLAYER_WALK_SPEED;
263                         player.animate();
264                 }
265
266                 if (engine.cheatSpeed)
267                 {
268                         player.dx *= 3;
269                 }
270         }
271         else if (config.isControl(CONTROL::RIGHT))
272         {
273                 player.face = 0;
274
275                 if (player.flags & ENT_FLIES)
276                 {
277                         player.dx += 0.1;
278                         Math::limitFloat(&player.dx, -PLAYER_FLY_SPEED, PLAYER_FLY_SPEED);
279                 }
280                 else if (map.isIceLevel)
281                 {
282                         player.dx += 0.1;
283                         player.animate();
284                         Math::limitFloat(&player.dx, -PLAYER_WALK_SPEED * 1.25, PLAYER_WALK_SPEED * 1.25);
285                 }
286                 else
287                 {
288                         player.dx = PLAYER_WALK_SPEED;
289                         player.animate();
290                 }
291                 
292                 if (engine.cheatSpeed)
293                 {
294                         player.dx *= 3;
295                 }
296         }
297         else if (!(player.flags & ENT_FLIES))
298         {
299                 // usual idle frame
300                 player.currentFrame = 0;
301         }
302
303         Math::limitFloat(&player.x, 10, map.limitRight + 608);
304         Math::limitFloat(&player.y, map.limitUp + 5, (MAPHEIGHT * BRICKSIZE) + 64);
305
306         // Keyboard break fix - Feb 09
307         if ((config.isControl(CONTROL::UP)) || (config.isControl(CONTROL::JUMP)))
308         {
309                 if (player.flags & ENT_FLIES)
310                 {
311                         player.dy -= 0.1;
312                         Math::limitFloat(&player.dy, -PLAYER_FLY_SPEED, PLAYER_FLY_SPEED);
313                         if (engine.cheatSpeed)
314                         {
315                                 player.dy *= 2;
316                         }
317                 }
318                 else if ((player.environment == ENV_AIR) && (player.falling == false))
319                 {
320                         player.falling = true;
321                         player.dy = PLAYER_JUMP_SPEED;
322                         config.resetControl(CONTROL::UP);
323                         config.resetControl(CONTROL::JUMP);
324                 }
325                 else if (player.environment != ENV_AIR)
326                 {
327                         player.dy = -PLAYER_WALK_SPEED;
328                         player.animate();
329                         if (engine.cheatSpeed)
330                         {
331                                 player.dy *= 3;
332                         }
333                 }
334         }
335
336         if (config.isControl(CONTROL::DOWN))
337         {
338                 if (player.flags & ENT_FLIES)
339                 {
340                         player.dy += 0.1;
341                         Math::limitFloat(&player.dy, -PLAYER_FLY_SPEED, PLAYER_FLY_SPEED);
342                 }
343                 else if (player.environment != ENV_AIR)
344                 {
345                         player.dy = 2;
346                         player.animate();
347                 }
348
349                 if (engine.cheatSpeed)
350                 {
351                         player.dy *= 3;
352                 }
353         }
354         
355         #if !USEPAK
356         if (engine.keyState[SDLK_1])
357                 player.currentWeapon = &weapon[WP_PISTOL];
358         else if (engine.keyState[SDLK_2])
359                 player.currentWeapon = &weapon[WP_MACHINEGUN];
360         else if (engine.keyState[SDLK_3])
361                 player.currentWeapon = &weapon[WP_GRENADES];
362         else if (engine.keyState[SDLK_4])
363                 player.currentWeapon = &weapon[WP_LASER];
364         else if (engine.keyState[SDLK_5])
365                 player.currentWeapon = &weapon[WP_SPREAD];
366         #endif
367         
368         moveEntity(&player);
369
370         // Math::limitFloat(&player.x, 10, map.limitRight + 608);
371         // moveEntity() limits x < 10
372         if (player.x > map.limitRight + 608)
373         {
374                 player.x = map.limitRight + 608;
375                 if (player.dx > 0)
376                 {
377                         player.dx = 0;
378                 }
379         }
380         // Math::limitFloat(&player.y, map.limitUp + 5, (MAPHEIGHT * BRICKSIZE) + 64);
381         if (player.y < map.limitUp + 5)
382         {
383                 player.y = map.limitUp + 5;
384                 if (player.dy < 0)
385                 {
386                         player.dy = 0;
387                 }
388         }
389         else if (player.y > (MAPHEIGHT * BRICKSIZE) + 64)
390         {
391                 player.y = (MAPHEIGHT * BRICKSIZE) + 64;
392         }
393
394         if (config.isControl(CONTROL::FIRE))
395         {
396                 if (player.reload == 0)
397                 {
398                         addBullet(&player, player.currentWeapon->getSpeed(player.face), 0);
399                         if (player.currentWeapon == &weapon[WP_SPREAD])
400                         {
401                                 addBullet(&player, player.currentWeapon->getSpeed(player.face), 2);
402                                 addBullet(&player, player.currentWeapon->getSpeed(player.face), -2);
403                         }
404                 }
405         }
406         
407         if (player.environment == ENV_WATER)
408         {
409                 Math::removeBit(&player.flags, ENT_FLIES);
410
411                 addBubble(player.x, player.y);
412                 
413                 if ((player.thinktime == 30) && (player.oxygen == 0))
414                 {
415                         audio.playSound(SND_DROWNING, CH_ANY);
416                 }
417         }
418
419         player.think();
420         
421         if (player.fuel == 0)
422         {
423                 player.setSprites(graphics.getSprite("BobRight", true), graphics.getSprite("BobLeft", true), graphics.getSprite("BobSpin", true));
424         }
425         
426         if ((game.hasAquaLung) || (engine.cheatExtras))
427         {
428                 player.oxygen = 7;
429         }
430                 
431         if (engine.cheatFuel)
432         {
433                 player.fuel = 7;
434         }
435
436         if (((player.immune % 5) == 0) && (!(player.flags & ENT_TELEPORTING)))
437         {
438                 if ((game.missionOverReason == MIS_COMPLETE) || (game.missionOverReason == MIS_INPROGRESS) || (game.missionOverReason == MIS_GAMECOMPLETE))
439                 {
440                         if (player.flags & ENT_FLIES)
441                         {
442                                 player.animate();
443                                 addFireTrailParticle(player.x + (player.face * 16) + Math::rrand(-1, 1), player.y + 10 + Math::rrand(-1, 1));
444                         }
445                         graphics.blit(player.getFaceImage(), (int)(player.x - engine.playerPosX), (int)(player.y - engine.playerPosY), graphics.screen, false);
446                 }
447         }
448 }