1 // $Id: player.cpp 2620 2005-06-18 12:12:10Z matzebraun $
2 //
3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
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.
19
20 #include <math.h>
21 #include "gameloop.h"
22 #include "globals.h"
23 #include "player.h"
24 #include "defines.h"
25 #include "scene.h"
26 #include "tile.h"
27 #include "sprite.h"
28 #include "screen.h"
29
30 #define AUTOSCROLL_DEAD_INTERVAL 300
31
32 Surface* tux_life;
33
34 Sprite* smalltux_gameover;
35 Sprite* smalltux_star;
36 Sprite* largetux_star;
37
38 PlayerSprite smalltux;
39 PlayerSprite largetux;
40 PlayerSprite firetux;
41
42 PlayerKeymap keymap;
43
PlayerKeymap()44 PlayerKeymap::PlayerKeymap()
45 {
46 keymap.jump = SDLK_SPACE;
47 keymap.duck = SDLK_DOWN;
48 keymap.left = SDLK_LEFT;
49 keymap.right = SDLK_RIGHT;
50 keymap.fire = SDLK_LCTRL;
51 }
52
player_input_init(player_input_type * pplayer_input)53 void player_input_init(player_input_type* pplayer_input)
54 {
55 pplayer_input->down = UP;
56 pplayer_input->fire = UP;
57 pplayer_input->left = UP;
58 pplayer_input->old_fire = UP;
59 pplayer_input->right = UP;
60 pplayer_input->up = UP;
61 pplayer_input->old_up = UP;
62 }
63
64 void
init()65 Player::init()
66 {
67 Level* plevel = World::current()->get_level();
68
69 holding_something = false;
70
71 base.width = 32;
72 base.height = 32;
73
74 size = SMALL;
75 got_coffee = false;
76
77 base.x = plevel->start_pos_x;
78 base.y = plevel->start_pos_y;
79 base.xm = 0;
80 base.ym = 0;
81 previous_base = old_base = base;
82 dir = RIGHT;
83 old_dir = dir;
84 duck = false;
85
86 dying = DYING_NOT;
87 jumping = false;
88 can_jump = true;
89
90 frame_main = 0;
91 frame_ = 0;
92
93 player_input_init(&input);
94
95 invincible_timer.init(true);
96 skidding_timer.init(true);
97 safe_timer.init(true);
98 frame_timer.init(true);
99 kick_timer.init(true);
100
101 physic.reset();
102 }
103
104 int
key_event(SDLKey key,int state)105 Player::key_event(SDLKey key, int state)
106 {
107 if(key == keymap.right)
108 {
109 input.right = state;
110 return true;
111 }
112 else if(key == keymap.left)
113 {
114 input.left = state;
115 return true;
116 }
117 else if(key == keymap.jump)
118 {
119 input.up = state;
120 return true;
121 }
122 else if(key == keymap.duck)
123 {
124 input.down = state;
125 return true;
126 }
127 else if(key == keymap.fire)
128 {
129 if (state == UP)
130 input.old_fire = UP;
131 input.fire = state;
132 return true;
133 }
134 else
135 return false;
136 }
137
138 void
level_begin()139 Player::level_begin()
140 {
141 base.x = 100;
142 base.y = 170;
143 base.xm = 0;
144 base.ym = 0;
145 previous_base = old_base = base;
146 duck = false;
147
148 dying = DYING_NOT;
149
150 player_input_init(&input);
151
152 invincible_timer.init(true);
153 skidding_timer.init(true);
154 safe_timer.init(true);
155 frame_timer.init(true);
156
157 physic.reset();
158 }
159
160 void
action(double frame_ratio)161 Player::action(double frame_ratio)
162 {
163 bool jumped_in_solid = false;
164
165 if (input.fire == UP)
166 holding_something = false;
167
168 /* Move tux: */
169 previous_base = base;
170
171 /* --- HANDLE TUX! --- */
172 if(dying == DYING_NOT)
173 handle_input();
174
175 physic.apply(frame_ratio, base.x, base.y);
176
177 if(dying == DYING_NOT)
178 {
179 base_type target = base;
180
181 collision_swept_object_map(&old_base, &base);
182
183 // Don't accelerate Tux if he is running against a wall
184 if (target.x != base.x)
185 {
186 physic.set_velocity_x(0);
187 }
188
189 // special exception for cases where we're stuck under tiles after
190 // being ducked. In this case we drift out
191 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
192 && collision_object_map(base))
193 {
194 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
195 previous_base = old_base = base;
196 }
197
198 // Land:
199 if (!on_ground())
200 {
201 physic.enable_gravity(true);
202 if(under_solid())
203 {
204 // fall down
205 physic.set_velocity_y(0);
206 jumped_in_solid = true;
207 }
208 }
209 else
210 {
211 /* Land: */
212 if (physic.get_velocity_y() < 0)
213 {
214 base.y = (int)(((int)base.y / 32) * 32);
215 physic.set_velocity_y(0);
216 }
217
218 physic.enable_gravity(false);
219 /* Reset score multiplier (for multi-hits): */
220 player_status.score_multiplier = 1;
221 }
222
223 if(jumped_in_solid)
224 {
225 if (isbrick(base.x, base.y) ||
226 isfullbox(base.x, base.y))
227 {
228 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
229 World::current()->trybumpbadguy(base.x, base.y - 64);
230
231 World::current()->trybreakbrick(base.x, base.y, size == SMALL, RIGHT);
232
233 bumpbrick(base.x, base.y);
234 World::current()->tryemptybox(base.x, base.y, RIGHT);
235 }
236
237 if (isbrick(base.x+ 31, base.y) ||
238 isfullbox(base.x+ 31, base.y))
239 {
240 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
241 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
242
243 if(size == BIG)
244 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL, LEFT);
245
246 bumpbrick(base.x+ 31, base.y);
247 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
248 }
249 }
250
251 grabdistros();
252
253 if (jumped_in_solid)
254 {
255 ++base.y;
256 ++old_base.y;
257 if(on_ground())
258 {
259 /* Make sure jumping is off. */
260 jumping = false;
261 }
262 }
263 }
264
265 /* ---- DONE HANDLING TUX! --- */
266
267 // check some timers
268 skidding_timer.check();
269 invincible_timer.check();
270 safe_timer.check();
271 kick_timer.check();
272 }
273
274 bool
on_ground()275 Player::on_ground()
276 {
277 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
278 issolid(base.x + 1, base.y + base.height) ||
279 issolid(base.x + base.width - 1, base.y + base.height) );
280 }
281
282 bool
under_solid()283 Player::under_solid()
284 {
285 return ( issolid(base.x + base.width / 2, base.y) ||
286 issolid(base.x + 1, base.y) ||
287 issolid(base.x + base.width - 1, base.y) );
288 }
289
290 void
handle_horizontal_input()291 Player::handle_horizontal_input()
292 {
293 float vx = physic.get_velocity_x();
294 float vy = physic.get_velocity_y();
295 float ax = physic.get_acceleration_x();
296 float ay = physic.get_acceleration_y();
297
298 float dirsign = 0;
299 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
300 old_dir = dir;
301 dir = LEFT;
302 dirsign = -1;
303 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
304 old_dir = dir;
305 dir = RIGHT;
306 dirsign = 1;
307 }
308
309 if (input.fire == UP) {
310 ax = dirsign * WALK_ACCELERATION_X;
311 // limit speed
312 if(vx >= MAX_WALK_XM && dirsign > 0) {
313 vx = MAX_WALK_XM;
314 ax = 0;
315 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
316 vx = -MAX_WALK_XM;
317 ax = 0;
318 }
319 } else {
320 ax = dirsign * RUN_ACCELERATION_X;
321 // limit speed
322 if(vx >= MAX_RUN_XM && dirsign > 0) {
323 vx = MAX_RUN_XM;
324 ax = 0;
325 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
326 vx = -MAX_RUN_XM;
327 ax = 0;
328 }
329 }
330
331 // we can reach WALK_SPEED without any acceleration
332 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
333 vx = dirsign * WALK_SPEED;
334 }
335
336 // changing directions?
337 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
338 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
339 skidding_timer.start(SKID_TIME);
340 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
341 ax *= 2.5;
342 } else {
343 ax *= 2;
344 }
345 }
346
347 // we get slower when not pressing any keys
348 if(dirsign == 0) {
349 if(fabs(vx) < WALK_SPEED) {
350 vx = 0;
351 ax = 0;
352 } else if(vx < 0) {
353 ax = WALK_ACCELERATION_X * 1.5;
354 } else {
355 ax = WALK_ACCELERATION_X * -1.5;
356 }
357 }
358
359 // if we're on ice slow down acceleration or deceleration
360 if (isice(base.x, base.y + base.height))
361 {
362 /* the acceleration/deceleration rate on ice is inversely proportional to
363 * the current velocity.
364 */
365
366 // increasing 1 will increase acceleration/deceleration rate
367 // decreasing 1 will decrease acceleration/deceleration rate
368 // must stay above zero, though
369 if (ax != 0) ax *= 1 / fabs(vx);
370 }
371
372 physic.set_velocity(vx, vy);
373 physic.set_acceleration(ax, ay);
374 }
375
376 void
handle_vertical_input()377 Player::handle_vertical_input()
378 {
379 // Press jump key
380 if(input.up == DOWN && can_jump)
381 {
382 if (on_ground())
383 {
384 // jump higher if we are running
385 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
386 physic.set_velocity_y(5.8);
387 else
388 physic.set_velocity_y(5.2);
389
390 --base.y;
391 jumping = true;
392 can_jump = false;
393 if (size == SMALL)
394 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
395 else
396 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
397 }
398 }
399 // Let go of jump key
400 else if(input.up == UP && jumping)
401 {
402 jumping = false;
403 if(physic.get_velocity_y() > 0) {
404 physic.set_velocity_y(0);
405 }
406 }
407
408 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
409 issolid(base.x + 1, base.y + base.height + 64) ||
410 issolid(base.x + base.width - 1, base.y + base.height + 64))
411 && jumping == false
412 && can_jump == false
413 && input.up == DOWN
414 && input.old_up == UP)
415 {
416 can_jump = true;
417 }
418
419 input.old_up = input.up;
420 }
421
422 void
handle_input()423 Player::handle_input()
424 {
425 /* Handle horizontal movement: */
426 handle_horizontal_input();
427
428 /* Jump/jumping? */
429
430 if (on_ground() && input.up == UP)
431 can_jump = true;
432 if (input.up == DOWN || (input.up == UP && jumping))
433 {
434 handle_vertical_input();
435 }
436
437 /* Shoot! */
438
439 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
440 {
441 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
442 input.old_fire = DOWN;
443 }
444
445 /* tux animations: */
446 if(!frame_timer.check())
447 {
448 frame_timer.start(25);
449 if (input.right == UP && input.left == UP)
450 {
451 frame_main = 1;
452 frame_ = 1;
453 }
454 else
455 {
456 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
457 (global_frame_counter % 4) == 0)
458 frame_main = (frame_main + 1) % 4;
459
460 frame_ = frame_main;
461
462 if (frame_ == 3)
463 frame_ = 1;
464 }
465 }
466
467 /* Duck! */
468 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
469 {
470 duck = true;
471 base.height = 32;
472 base.y += 32;
473 // changing base size confuses collision otherwise
474 old_base = previous_base = base;
475 }
476 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
477 {
478 duck = false;
479 base.y -= 32;
480 base.height = 64;
481 // changing base size confuses collision otherwise
482 old_base = previous_base = base;
483 }
484 }
485
486 void
grow()487 Player::grow()
488 {
489 if(size == BIG)
490 return;
491
492 size = BIG;
493 base.height = 64;
494 base.y -= 32;
495
496 old_base = previous_base = base;
497 }
498
499 void
jump_of_badguy(BadGuy * badguy)500 Player::jump_of_badguy(BadGuy* badguy)
501 {
502 if(input.up)
503 physic.set_velocity_y(5.2);
504 else
505 physic.set_velocity_y(2.0);
506 base.y = badguy->base.y - base.height-2;
507 }
508
509 void
grabdistros()510 Player::grabdistros()
511 {
512 /* Grab distros: */
513 if (!dying)
514 {
515 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
516 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
517
518 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
519 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
520
521 if(size == BIG)
522 {
523 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
524 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
525 }
526
527 }
528
529 /* Enough distros for a One-up? */
530 if (player_status.distros >= DISTROS_LIFEUP)
531 {
532 player_status.distros = player_status.distros - DISTROS_LIFEUP;
533 if(player_status.lives < MAX_LIVES)
534 ++player_status.lives;
535 /*We want to hear the sound even, if MAX_LIVES is reached*/
536 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
537 }
538 }
539
540 void
draw()541 Player::draw()
542 {
543 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
544 {
545 if (dying == DYING_SQUISHED)
546 {
547 smalltux_gameover->draw(base.x - scroll_x, base.y);
548 }
549 else
550 {
551 PlayerSprite* sprite;
552
553 if (size == SMALL)
554 sprite = &smalltux;
555 else if (got_coffee)
556 sprite = &firetux;
557 else
558 sprite = &largetux;
559
560 if (duck && size != SMALL)
561 {
562 if (dir == RIGHT)
563 sprite->duck_right->draw(base.x - scroll_x, base.y);
564 else
565 sprite->duck_left->draw(base.x - scroll_x, base.y);
566 }
567 else if (skidding_timer.started())
568 {
569 if (dir == RIGHT)
570 sprite->skid_right->draw(base.x - scroll_x, base.y);
571 else
572 sprite->skid_left->draw(base.x - scroll_x, base.y);
573 }
574 else if (kick_timer.started())
575 {
576 if (dir == RIGHT)
577 sprite->kick_right->draw(base.x - scroll_x, base.y);
578 else
579 sprite->kick_left->draw(base.x - scroll_x, base.y);
580 }
581 else if (physic.get_velocity_y() != 0)
582 {
583 if (dir == RIGHT)
584 sprite->jump_right->draw(base.x - scroll_x, base.y);
585 else
586 sprite->jump_left->draw(base.x - scroll_x, base.y);
587 }
588 else
589 {
590 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
591 {
592 if (dir == RIGHT)
593 sprite->stand_right->draw( base.x - scroll_x, base.y);
594 else
595 sprite->stand_left->draw( base.x - scroll_x, base.y);
596 }
597 else // moving
598 {
599 if (dir == RIGHT)
600 sprite->walk_right->draw(base.x - scroll_x, base.y);
601 else
602 sprite->walk_left->draw(base.x - scroll_x, base.y);
603 }
604 }
605
606 // Draw arm overlay graphics when Tux is holding something
607 if (holding_something && physic.get_velocity_y() == 0 && !duck)
608 {
609 if (dir == RIGHT)
610 sprite->grab_right->draw(base.x - scroll_x, base.y);
611 else
612 sprite->grab_left->draw(base.x - scroll_x, base.y);
613 }
614
615 // Draw blinking star overlay
616 if (invincible_timer.started() &&
617 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
618 {
619 if (size == SMALL || duck)
620 smalltux_star->draw(base.x - scroll_x, base.y);
621 else
622 largetux_star->draw(base.x - scroll_x, base.y);
623 }
624 }
625 }
626
627 if (debug_mode)
628 fillrect(base.x - scroll_x, base.y,
629 base.width, base.height, 75,75,75, 150);
630 }
631
632 void
collision(void * p_c_object,int c_object)633 Player::collision(void* p_c_object, int c_object)
634 {
635 BadGuy* pbad_c = NULL;
636
637 switch (c_object)
638 {
639 case CO_BADGUY:
640 pbad_c = (BadGuy*) p_c_object;
641
642 /* Hurt player if he touches a badguy */
643 if (!pbad_c->dying && !dying &&
644 !safe_timer.started() &&
645 pbad_c->mode != BadGuy::HELD)
646 {
647 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
648 && !holding_something)
649 {
650 holding_something = true;
651 pbad_c->mode = BadGuy::HELD;
652 pbad_c->base.y-=8;
653 }
654 else if (pbad_c->mode == BadGuy::FLAT)
655 {
656 // Don't get hurt if we're kicking a flat badguy!
657 }
658 else if (pbad_c->mode == BadGuy::KICK)
659 {
660 /* Hurt if you get hit by kicked laptop: */
661 if (!invincible_timer.started())
662 {
663 kill(SHRINK);
664 }
665 else
666 {
667 pbad_c->dying = DYING_FALLING;
668 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
669 World::current()->add_score(pbad_c->base.x - scroll_x,
670 pbad_c->base.y,
671 25 * player_status.score_multiplier);
672 }
673 }
674 else
675 {
676 if (!invincible_timer.started())
677 {
678 kill(SHRINK);
679 }
680 else
681 {
682 pbad_c->kill_me(25);
683 }
684 }
685 player_status.score_multiplier++;
686 }
687 break;
688 default:
689 break;
690 }
691
692 }
693
694 /* Kill Player! */
695
696 void
kill(HurtMode mode)697 Player::kill(HurtMode mode)
698 {
699 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
700
701 physic.set_velocity_x(0);
702
703 if (mode == SHRINK && size == BIG)
704 {
705 if (got_coffee)
706 {
707 got_coffee = false;
708 }
709 else
710 {
711 size = SMALL;
712 base.height = 32;
713 duck = false;
714 }
715 safe_timer.start(TUX_SAFE_TIME);
716 }
717 else
718 {
719 physic.enable_gravity(true);
720 physic.set_acceleration(0, 0);
721 physic.set_velocity(0, 7);
722 if(dying != DYING_SQUISHED)
723 --player_status.lives;
724 dying = DYING_SQUISHED;
725 }
726 }
727
728 void
is_dying()729 Player::is_dying()
730 {
731 remove_powerups();
732 dying = DYING_NOT;
733 }
734
is_dead()735 bool Player::is_dead()
736 {
737 if(base.y > screen->h || base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // last condition can happen in auto-scrolling
738 return true;
739 else
740 return false;
741 }
742
743 /* Remove Tux's power ups */
744 void
remove_powerups()745 Player::remove_powerups()
746 {
747 got_coffee = false;
748 size = SMALL;
749 base.height = 32;
750 }
751
752 void
check_bounds(bool back_scrolling,bool hor_autoscroll)753 Player::check_bounds(bool back_scrolling, bool hor_autoscroll)
754 {
755 /* Keep tux in bounds: */
756 if (base.x < 0)
757 { // Lock Tux to the size of the level, so that he doesn't fall of
758 // on the left side
759 base.x = 0;
760 }
761
762 /* Keep in-bounds, vertically: */
763 if (base.y > screen->h)
764 {
765 kill(KILL);
766 }
767
768 if(base.x < scroll_x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled
769 base.x = scroll_x;
770
771 if(hor_autoscroll)
772 {
773 if(base.x == scroll_x)
774 if((issolid(base.x+32, base.y) || (size != SMALL && !duck && issolid(base.x+32, base.y+32))) && (dying == DYING_NOT))
775 kill(KILL);
776
777 if(base.x + base.width > scroll_x + screen->w)
778 base.x = scroll_x + screen->w - base.width;
779 }
780
781 }
782
783 // EOF //
784
785