Some Window$ specific edits.
[dbw.git] / src / player.cpp
blob94b6c418700b31d00b32db234ac63127cc90dac2
1 /*
2 Copyright © 2004 Parallel Realities
3 Copyright © 2007-2008 Kővágó Zoltán <DirtY.iCE.hu@gmail.com>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "player.h"
23 #include "gettext.hpp"
24 #include "console/console.hpp"
26 void resetPlayer()
28 game.getCheckPoint(&player.x, &player.y);
30 player.dx = 0;
31 player.dy = 0;
32 player.immune = 120;
33 player.environment = ENV_AIR;
34 player.oxygen = 7;
35 player.fuel = 7;
36 addTeleportParticles(player.x + 10, player.y + 10, 50, SND_TELEPORT2);
38 Math::removeBit(&player.flags, ENT_FLIES);
39 Math::removeBit(&player.flags, ENT_TELEPORTING);
40 player.setSprites(graphics.getSprite("BobRight", true), graphics.getSprite("BobLeft", true), graphics.getSprite("BobSpin", true));
42 map.windPower = 0;
43 map.windChangeTime = 90;
46 void gibPlayer()
48 float x, y, dx, dy;
49 int amount = (game.gore) ? 25 : 150;
51 for (int i = 0 ; i < amount ; i++)
53 x = player.x + Math::rrand(-3, 3);
54 y = player.y + Math::rrand(-3, 3);
56 if (game.gore)
58 dx = Math::rrand(-5, 5);
59 dy = Math::rrand(-15, -5);
60 addEffect(x, y, dx, dy, EFF_BLEEDS);
62 else
64 dx = Math::rrand(-5, 5);
65 dy = Math::rrand(-5, 5);
66 addColoredEffect(x, y, dx, dy, graphics.yellow, EFF_COLORED + EFF_WEIGHTLESS);
70 (game.gore) ? audio.playSound(SND_SPLAT, CH_ANY) : audio.playSound(SND_POP, CH_ANY);
73 void checkPlayerBulletCollisions(Entity *bullet)
75 if ((bullet->health < 1) || (player.health <= -60))
77 return;
80 if ((player.flags & ENT_TELEPORTING) || (game.missionOver > 0))
82 return;
85 if (bullet->owner != &player)
87 if (Collision::collision(&player, bullet))
89 if ((!player.immune) && (!(bullet->flags & ENT_EXPLODES)))
91 addBlood(&player, bullet->dx / 4, Math::rrand(-6, -3), 1);
92 audio.playSound(SND_HIT, CH_ANY);
93 if (game.missionOverReason == MIS_INPROGRESS)
95 player.health -= bullet->damage;
96 player.immune = 120;
100 Math::removeBit(&bullet->flags, ENT_SPARKS);
101 Math::removeBit(&bullet->flags, ENT_PUFFS);
103 if (player.health <= 0)
105 player.dx = bullet->dx;
106 player.dy = -5;
107 audio.playSound(SND_DEATH1 + rand() % 3, CH_DEATH);
108 player.health = 0;
111 if (bullet->id == WP_LASER)
113 debug << "Laser Hit Player" << std::endl;
114 throwAndDamageEntity(&player, 0, -3, 3, -8);
116 else
118 bullet->health = 0;
124 void doPlayer()
127 int x = (int) player.x - map.offsetX;
128 int y = (int) player.y - map.offsetY;
130 if (engine.cheatHealth)
132 player.health = MAX_HEALTH;
135 if (game.missionOverReason == MIS_PLAYERESCAPE)
137 return;
140 if (player.y > (map.limitDown + 500))
142 if (game.missionOver == 0)
144 player.health--;
147 if (player.health > 0)
149 game.setMissionOver(MIS_PLAYEROUT);
151 else
153 game.setMissionOver(MIS_PLAYERDEAD);
157 if (player.flags & ENT_TELEPORTING)
159 moveEntity(&player);
160 return;
163 if (game.missionOverReason == MIS_PLAYEROUT)
165 graphics.blit(player.getFaceImage(), x, y, graphics.screen, false);
166 moveEntity(&player);
167 return;
170 if ((player.health < 1) || (player.immune > 120))
172 if (player.health <= -60)
174 return;
177 player.think();
179 if (game.hasAquaLung)
181 player.oxygen = 7;
184 moveEntity(&player);
186 graphics.blit(player.getFaceImage(), x, y, graphics.screen, false);
188 if (player.health < 1)
190 player.health--;
193 if (player.health <= -60)
195 gibPlayer();
198 return;
201 if ((!(player.flags & ENT_FLIES)) && (!map.isIceLevel))
203 player.dx = 0;
206 // toggles the Jetpack if available
207 if (engine.isKey(KEY_JETPACK))
209 if ((game.hasJetPack) || (engine.cheatExtras))
211 if (player.flags & ENT_FLIES)
213 Math::removeBit(&player.flags, ENT_FLIES);
214 player.setSprites(graphics.getSprite("BobRight", true), graphics.getSprite("BobLeft", true), graphics.getSprite("BobSpin", true));
216 else
218 if ((player.fuel > 3) && (player.environment == ENV_AIR))
220 Math::addBit(&player.flags, ENT_FLIES);
221 player.setSprites(graphics.getSprite("JPBobRight", true), graphics.getSprite("JPBobLeft", true), graphics.getSprite("BobSpin", true));
223 else if ((player.environment == ENV_WATER))
225 engine.setInfoMessage(_("Jetpack cannot be used underwater"), 0, INFO_NORMAL);
227 else
229 engine.setInfoMessage(_("Jetpack is recharging..."), 0, INFO_NORMAL);
233 else
235 engine.setInfoMessage(_("Don't have jetpack!"), 0, INFO_NORMAL);
238 engine.clearKey(KEY_JETPACK);
241 if (map.isBlizzardLevel)
243 if ((!engine.isKey(KEY_LEFT)) && (!engine.isKey(KEY_RIGHT)))
245 if ((player.dx > -2) && (player.dx < 2))
247 player.dx += map.windPower * 0.1;
252 signed char dir = 0;
253 if (engine.isKey(KEY_LEFT) || engine.getJoyLeft())
254 dir = -1;
255 else if (engine.isKey(KEY_RIGHT) || engine.getJoyRight())
256 dir = 1;
257 if (dir != 0)
259 player.face = dir < 0 ? 1 : 0;
261 if (player.flags & ENT_FLIES || map.isIceLevel)
262 player.dx += (engine.cheatSpeed ? 0.3f : 0.1f) * dir;
263 else
264 player.dx = (engine.cheatSpeed ? 3 * dir : dir) * PLAYER_WALK_SPEED;
266 if (player.flags & ENT_FLIES)
267 Math::limitFloat(&player.dx, (engine.cheatSpeed ? -3 : -1) * PLAYER_FLY_SPEED, (engine.cheatSpeed ? 3 : 1) * PLAYER_FLY_SPEED);
268 else if (map.isIceLevel)
269 Math::limitFloat(&player.dx, (engine.cheatSpeed ? -3.75f : -1.25f) * PLAYER_WALK_SPEED, (engine.cheatSpeed ? 3.75f : 1.25f) * PLAYER_WALK_SPEED);
271 if (!(player.flags & ENT_FLIES))
272 player.animate();
275 else if (!(player.flags & ENT_FLIES))
277 // usual idle frame
278 player.currentFrame = 0;
281 Math::limitFloat(&player.x, 10, map.limitRight + 608);
282 Math::limitFloat(&player.y, map.limitUp + 5, (MAPHEIGHT * BRICKSIZE) + 64);
284 dir = 0;
285 if ((engine.isKey(KEY_UP)) || engine.getJoyUp())
286 dir = -1;
287 if (engine.isKey(KEY_DOWN) || engine.getJoyDown())
288 dir = 1;
290 if (player.flags & ENT_FLIES)
292 player.dy += (engine.cheatSpeed ? 0.3f : 0.1f) * dir;
293 Math::limitFloat(&player.dy, (engine.cheatSpeed ? -3 : -1) * PLAYER_FLY_SPEED, (engine.cheatSpeed ? 3 : 1) * PLAYER_FLY_SPEED);
295 else if (dir == -1 && player.environment == ENV_AIR && player.falling == false)
297 player.falling = true;
298 player.dy = (engine.cheatSpeed ? 3 : 1) * PLAYER_JUMP_SPEED;
299 engine.clearKey(KEY_UP);
301 else if (player.environment != ENV_AIR)
303 player.dy = (engine.cheatSpeed ? 3 : 1) * (dir < 0 ? -PLAYER_WALK_SPEED : 2);
304 player.animate();
308 /// DEBUG / CHEAT
309 if (engine.keyState[SDLK_1])
310 player.currentWeapon = &weapon[WP_PISTOL];
311 else if (engine.keyState[SDLK_2])
312 player.currentWeapon = &weapon[WP_MACHINEGUN];
313 else if (engine.keyState[SDLK_3])
314 player.currentWeapon = &weapon[WP_GRENADES];
315 else if (engine.keyState[SDLK_4])
316 player.currentWeapon = &weapon[WP_LASER];
317 else if (engine.keyState[SDLK_5])
318 player.currentWeapon = &weapon[WP_SPREAD];
320 moveEntity(&player);
322 // Math::limitFloat(&player.x, 10, map.limitRight + 608);
323 // moveEntity() limits x < 10
324 if (player.x > map.limitRight + 608)
326 player.x = map.limitRight + 608;
327 if (player.dx > 0)
329 player.dx = 0;
332 // Math::limitFloat(&player.y, map.limitUp + 5, (MAPHEIGHT * BRICKSIZE) + 64);
333 if (player.y < map.limitUp + 5)
335 player.y = map.limitUp + 5;
336 if (player.dy < 0)
338 player.dy = 0;
341 else if (player.y > (MAPHEIGHT * BRICKSIZE) + 64)
343 player.y = (MAPHEIGHT * BRICKSIZE) + 64;
346 if (engine.isKey(KEY_FIRE) || engine.mouseLeft)
348 if (player.reload == 0)
350 float dx, dy, speed;
351 Math::calculateSlope(engine.getMouseX(), engine.getMouseY(), x, y, &dx, &dy);
352 speed = player.currentWeapon->getSpeed(false);
353 dx *= speed; dy *= speed;
354 addBullet(&player, dx, dy);
355 if (player.currentWeapon == &weapon[WP_SPREAD])
357 addBullet(&player, dx, dy + 2);
358 addBullet(&player, dx, dy - 2);
363 if (player.environment == ENV_WATER)
365 Math::removeBit(&player.flags, ENT_FLIES);
367 addBubble(player.x, player.y);
369 if ((player.thinktime == 30) && (player.oxygen == 0))
371 audio.playSound(SND_DROWNING, CH_ANY);
375 player.think();
377 if (player.fuel == 0)
379 player.setSprites(graphics.getSprite("BobRight", true), graphics.getSprite("BobLeft", true), graphics.getSprite("BobSpin", true));
382 if ((game.hasAquaLung) || (engine.cheatExtras))
384 player.oxygen = 7;
387 if (engine.cheatFuel)
389 player.fuel = 7;
392 if (((player.immune % 5) == 0) && (!(player.flags & ENT_TELEPORTING)))
394 if ((game.missionOverReason == MIS_COMPLETE) || (game.missionOverReason == MIS_INPROGRESS) || (game.missionOverReason == MIS_GAMECOMPLETE))
396 if (player.flags & ENT_FLIES)
398 player.animate();
399 addFireTrailParticle(player.x + (player.face * 16) + Math::rrand(-1, 1), player.y + 10 + Math::rrand(-1, 1));
401 graphics.blit(player.getFaceImage(), x, y, graphics.screen, false);