- make badguys bounce up when bumped from beneath
[supertux.git] / src / badguy.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de>
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU General Public License
10 //  as published by the Free Software Foundation; either version 2
11 //  of the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU General Public License for more details.
17 // 
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 //  02111-1307, USA.
22
23 #include <iostream>
24 #include <math.h>
25
26 #include "globals.h"
27 #include "defines.h"
28 #include "badguy.h"
29 #include "scene.h"
30 #include "screen.h"
31 #include "world.h"
32 #include "tile.h"
33 #include "resources.h"
34 #include "sprite_manager.h"
35
36 Sprite* img_mriceblock_flat_left;
37 Sprite* img_mriceblock_flat_right;
38 Sprite* img_mriceblock_falling_left;
39 Sprite* img_mriceblock_falling_right;
40 Sprite* img_mriceblock_left;
41 Sprite* img_mriceblock_right;
42 Sprite* img_jumpy_left_up;
43 Sprite* img_jumpy_left_down;
44 Sprite* img_jumpy_left_middle;
45 Sprite* img_mrbomb_left;
46 Sprite* img_mrbomb_right;
47 Sprite* img_mrbomb_ticking_left;
48 Sprite* img_mrbomb_ticking_right;
49 Sprite* img_mrbomb_explosion;
50 Sprite* img_stalactite;
51 Sprite* img_stalactite_broken;
52 Sprite* img_flame;
53 Sprite* img_fish;
54 Sprite* img_fish_down;
55 Sprite* img_bouncingsnowball_left;
56 Sprite* img_bouncingsnowball_right;
57 Sprite* img_bouncingsnowball_squished;
58 Sprite* img_flyingsnowball;
59 Sprite* img_flyingsnowball_squished;
60 Sprite* img_spiky_left;
61 Sprite* img_spiky_right;
62 Sprite* img_snowball_left;
63 Sprite* img_snowball_right;
64 Sprite* img_snowball_squished_left;
65 Sprite* img_snowball_squished_right;
66
67 #define BADGUY_WALK_SPEED .8f
68
69 BadGuyKind  badguykind_from_string(const std::string& str)
70 {
71   if (str == "money" || str == "jumpy") // was money in old maps
72     return BAD_JUMPY;
73   else if (str == "laptop" || str == "mriceblock") // was laptop in old maps
74     return BAD_MRICEBLOCK;
75   else if (str == "mrbomb")
76     return BAD_MRBOMB;
77   else if (str == "stalactite")
78     return BAD_STALACTITE;
79   else if (str == "flame")
80     return BAD_FLAME;
81   else if (str == "fish")
82     return BAD_FISH;
83   else if (str == "bouncingsnowball")
84     return BAD_BOUNCINGSNOWBALL;
85   else if (str == "flyingsnowball")
86     return BAD_FLYINGSNOWBALL;
87   else if (str == "spiky")
88     return BAD_SPIKY;
89   else if (str == "snowball" || str == "bsod") // was bsod in old maps
90     return BAD_SNOWBALL;
91   else
92     {
93       printf("Couldn't convert badguy: '%s'\n", str.c_str());
94       return BAD_SNOWBALL;
95     }
96 }
97
98 std::string badguykind_to_string(BadGuyKind kind)
99 {
100   switch(kind)
101     {
102     case BAD_JUMPY:
103       return "jumpy";
104       break;
105     case BAD_MRICEBLOCK:
106       return "mriceblock";
107       break;
108     case BAD_MRBOMB:
109       return "mrbomb";
110       break;
111     case BAD_STALACTITE:
112       return "stalactite";
113       break;
114     case BAD_FLAME:
115       return "flame";
116       break;
117     case BAD_FISH:
118       return "fish";
119       break;
120     case BAD_BOUNCINGSNOWBALL:
121       return "bouncingsnowball";
122       break;
123     case BAD_FLYINGSNOWBALL:
124       return "flyingsnowball";
125       break;
126     case BAD_SPIKY:
127       return "spiky";
128       break;
129     case BAD_SNOWBALL:
130       return "snowball";
131       break;
132     default:
133       return "snowball";
134     }
135 }
136
137 BadGuy::BadGuy(float x, float y, BadGuyKind kind_, bool stay_on_platform_)
138   : removable(false), squishcount(0)
139 {
140   base.x   = x;
141   base.y   = y;    
142   base.width  = 0;
143   base.height = 0;
144   base.xm  = 0;
145   base.ym  = 0;
146
147   stay_on_platform = stay_on_platform_;
148   mode     = NORMAL;
149   dying    = DYING_NOT;
150   kind     = kind_;
151   old_base = base;
152   dir      = LEFT;
153   seen     = false;
154   animation_offset = 0;
155   sprite_left = sprite_right = 0;
156   physic.reset();
157   timer.init(true);
158
159   if(kind == BAD_MRBOMB) {
160     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
161     set_sprite(img_mrbomb_left, img_mrbomb_right);
162   } else if (kind == BAD_MRICEBLOCK) {
163     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
164     set_sprite(img_mriceblock_left, img_mriceblock_right);
165   } else if(kind == BAD_JUMPY) {
166     set_sprite(img_jumpy_left_up, img_jumpy_left_up);
167   } else if(kind == BAD_BOMB) {
168     set_sprite(img_mrbomb_ticking_left, img_mrbomb_ticking_right);
169     // hack so that the bomb doesn't hurt until it expldes...
170     dying = DYING_SQUISHED;
171   } else if(kind == BAD_FLAME) {
172     base.ym = 0; // we misuse base.ym as angle for the flame
173     physic.enable_gravity(false);
174     set_sprite(img_flame, img_flame);
175   } else if(kind == BAD_BOUNCINGSNOWBALL) {
176     physic.set_velocity(-1.3, 0);
177     set_sprite(img_bouncingsnowball_left, img_bouncingsnowball_right);
178   } else if(kind == BAD_STALACTITE) {
179     physic.enable_gravity(false);
180     set_sprite(img_stalactite, img_stalactite);
181   } else if(kind == BAD_FISH) {
182     set_sprite(img_fish, img_fish);
183     physic.enable_gravity(true);
184   } else if(kind == BAD_FLYINGSNOWBALL) {
185     set_sprite(img_flyingsnowball, img_flyingsnowball);
186     physic.enable_gravity(false);
187   } else if(kind == BAD_SPIKY) {
188     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
189     set_sprite(img_spiky_left, img_spiky_right);
190   } else if(kind == BAD_SNOWBALL) {
191     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
192     set_sprite(img_snowball_left, img_snowball_right);
193   }
194
195   // if we're in a solid tile at start correct that now
196   if(kind != BAD_FLAME && kind != BAD_FISH && collision_object_map(base)) {
197     printf("Warning: badguy started in wall!.\n");
198     while(collision_object_map(base))
199       --base.y;
200   }
201 }
202
203 void
204 BadGuy::action_mriceblock(float frame_ratio)
205 {
206   Player& tux = *World::current()->get_tux();
207
208   if(mode != HELD)
209     fall();
210   
211   /* Move left/right: */
212   if (mode != HELD)
213     {
214       // move
215       physic.apply(frame_ratio, base.x, base.y);
216       if (dying != DYING_FALLING)
217         collision_swept_object_map(&old_base,&base);
218     }
219   else if (mode == HELD)
220     { /* FIXME: The pbad object shouldn't know about pplayer objects. */
221       /* If we're holding the iceblock */
222       dir = tux.dir;
223       if(dir==RIGHT)
224         {
225           base.x = tux.base.x + 16;
226           base.y = tux.base.y + tux.base.height/1.5 - base.height;
227         }
228       else /* facing left */
229         {
230           base.x = tux.base.x - 16;
231           base.y = tux.base.y + tux.base.height/1.5 - base.height;
232         }
233       if(collision_object_map(base))
234         {
235           base.x = tux.base.x;
236           base.y = tux.base.y + tux.base.height/1.5 - base.height;
237         }
238
239       if(tux.input.fire != DOWN) /* SHOOT! */
240         {
241           if(dir == LEFT)
242             base.x -= 24;
243           else
244             base.x += 24;
245           old_base = base;
246
247           mode=KICK;
248           tux.kick_timer.start(KICKING_TIME);
249           set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
250           physic.set_velocity_x((dir == LEFT) ? -3.5 : 3.5);
251           play_sound(sounds[SND_KICK],SOUND_CENTER_SPEAKER);
252         }
253     }
254
255   if (!dying)
256     {
257       int changed = dir;
258       check_horizontal_bump();
259       if(mode == KICK && changed != dir)
260         {
261           /* handle stereo sound (number 10 should be tweaked...)*/
262           if (base.x < scroll_x + screen->w/2 - 10)
263             play_sound(sounds[SND_RICOCHET], SOUND_LEFT_SPEAKER);
264           else if (base.x > scroll_x + screen->w/2 + 10)
265             play_sound(sounds[SND_RICOCHET], SOUND_RIGHT_SPEAKER);
266           else
267             play_sound(sounds[SND_RICOCHET], SOUND_CENTER_SPEAKER);
268         }
269     }
270
271   /* Handle mode timer: */
272   if (mode == FLAT)
273     {
274       if(!timer.check())
275         {
276           mode = NORMAL;
277           set_sprite(img_mriceblock_left, img_mriceblock_right);
278           physic.set_velocity( (dir == LEFT) ? -.8 : .8, 0);
279         }
280     }
281 }
282
283 void
284 BadGuy::check_horizontal_bump(bool checkcliff)
285 {
286     float halfheight = base.height / 2;
287     if (dir == LEFT && issolid( base.x, (int) base.y + halfheight))
288     {
289         dir = RIGHT;
290         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
291         return;
292     }
293     if (dir == RIGHT && issolid( base.x + base.width, (int)base.y + halfheight))
294     {
295         dir = LEFT;
296         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
297         return;
298     }
299
300     // don't check for cliffs when we're falling
301     if(!checkcliff)
302         return;
303     if(!issolid(base.x + base.width/2, base.y + base.height))
304         return;
305     
306     if(dir == LEFT && !issolid(base.x, (int) base.y + base.height + halfheight))
307     {
308         dir = RIGHT;
309         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
310         return;
311     }
312     if(dir == RIGHT && !issolid(base.x + base.width,
313                 (int) base.y + base.height + halfheight))
314     {
315         dir = LEFT;
316         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
317         return;
318     }
319 }
320
321 void
322 BadGuy::fall()
323 {
324   /* Fall if we get off the ground: */
325   if (dying != DYING_FALLING)
326     {
327       if (!issolid(base.x+base.width/2, base.y + base.height))
328         {
329           // not solid below us? enable gravity
330           physic.enable_gravity(true);
331         }
332       else
333         {
334           /* Land: */
335           if (physic.get_velocity_y() < 0)
336             {
337               base.y = int((base.y + base.height)/32) * 32 - base.height;
338               physic.set_velocity_y(0);
339             }
340           // no gravity anymore please
341           physic.enable_gravity(false);
342
343           if (stay_on_platform && mode == NORMAL)
344             {
345               if (!issolid(base.x + ((dir == LEFT) ? 0 : base.width),
346                            base.y + base.height))
347                 {
348                   if (dir == LEFT)
349                   {
350                     dir = RIGHT;
351                     physic.set_velocity_x(fabs(physic.get_velocity_x()));
352                   } 
353                   else
354                   {
355                     dir = LEFT;
356                     physic.set_velocity_x(-fabs(physic.get_velocity_x()));
357                   }
358                 }
359             }
360         }
361     }
362   else
363     {
364       physic.enable_gravity(true);
365     }
366 }
367
368 void
369 BadGuy::remove_me()
370 {
371   removable = true;
372 }
373
374 void
375 BadGuy::action_jumpy(float frame_ratio)
376 {
377   if (fabsf(physic.get_velocity_y()) < 2.5f)
378     set_sprite(img_jumpy_left_middle, img_jumpy_left_middle);
379   else if (physic.get_velocity_y() < 0)
380     set_sprite(img_jumpy_left_up, img_jumpy_left_up);
381   else 
382     set_sprite(img_jumpy_left_down, img_jumpy_left_down);
383
384   Player& tux = *World::current()->get_tux();
385
386   static const float JUMPV = 6;
387     
388   fall();
389   // jump when on ground
390   if(dying == DYING_NOT && issolid(base.x, base.y+32))
391     {
392       physic.set_velocity_y(JUMPV);
393       physic.enable_gravity(true);
394
395       mode = JUMPY_JUMP;
396     }
397   else if(mode == JUMPY_JUMP)
398     {
399       mode = NORMAL;
400     }
401
402   // set direction based on tux
403   if(tux.base.x > base.x)
404     dir = RIGHT;
405   else
406     dir = LEFT;
407
408   // move
409   physic.apply(frame_ratio, base.x, base.y);
410   if(dying == DYING_NOT)
411     collision_swept_object_map(&old_base, &base);
412 }
413
414 void
415 BadGuy::action_mrbomb(float frame_ratio)
416 {
417   if (dying == DYING_NOT)
418     check_horizontal_bump(true);
419
420   fall();
421
422   physic.apply(frame_ratio, base.x, base.y);
423   if (dying != DYING_FALLING)
424     collision_swept_object_map(&old_base,&base); 
425 }
426
427 void
428 BadGuy::action_bomb(float frame_ratio)
429 {
430   static const int TICKINGTIME = 1000;
431   static const int EXPLODETIME = 1000;
432     
433   fall();
434
435   if(mode == NORMAL) {
436     mode = BOMB_TICKING;
437     timer.start(TICKINGTIME);
438   } else if(!timer.check()) {
439     if(mode == BOMB_TICKING) {
440       mode = BOMB_EXPLODE;
441       set_sprite(img_mrbomb_explosion, img_mrbomb_explosion);
442       dying = DYING_NOT; // now the bomb hurts
443       timer.start(EXPLODETIME);
444
445       /* play explosion sound */  // FIXME: is the stereo all right? maybe we should use player cordinates...
446       if (base.x < scroll_x + screen->w/2 - 10)
447         play_sound(sounds[SND_EXPLODE], SOUND_LEFT_SPEAKER);
448       else if (base.x > scroll_x + screen->w/2 + 10)
449         play_sound(sounds[SND_EXPLODE], SOUND_RIGHT_SPEAKER);
450       else
451         play_sound(sounds[SND_EXPLODE], SOUND_CENTER_SPEAKER);
452
453     } else if(mode == BOMB_EXPLODE) {
454       remove_me();
455       return;
456     }
457   }
458
459   // move
460   physic.apply(frame_ratio, base.x, base.y);                 
461   collision_swept_object_map(&old_base,&base);
462 }
463
464 void
465 BadGuy::action_stalactite(float frame_ratio)
466 {
467   Player& tux = *World::current()->get_tux();
468
469   static const int SHAKETIME = 800;
470   static const int RANGE = 40;
471     
472   if(mode == NORMAL) {
473     // start shaking when tux is below the stalactite and at least 40 pixels
474     // near
475     if(tux.base.x + 32 > base.x - RANGE && tux.base.x < base.x + 32 + RANGE
476             && tux.base.y + tux.base.height > base.y) {
477       timer.start(SHAKETIME);
478       mode = STALACTITE_SHAKING;
479     }
480   } if(mode == STALACTITE_SHAKING) {
481     base.x = old_base.x + (rand() % 6) - 3; // TODO this could be done nicer...
482     if(!timer.check()) {
483       mode = STALACTITE_FALL;
484     }
485   } else if(mode == STALACTITE_FALL) {
486     fall();
487     /* Destroy if we collides with land */
488     if(issolid(base.x+base.width/2, base.y+base.height))
489     {
490       timer.start(2000);
491       dying = DYING_SQUISHED;
492       mode = FLAT;
493       set_sprite(img_stalactite_broken, img_stalactite_broken);
494     }
495   } else if(mode == FLAT) {
496     fall();
497   }
498
499   // move
500   physic.apply(frame_ratio, base.x, base.y);
501
502   if(dying == DYING_SQUISHED && !timer.check())
503     remove_me();
504 }
505
506 void
507 BadGuy::action_flame(float frame_ratio)
508 {
509     static const float radius = 100;
510     static const float speed = 0.02;
511     base.x = old_base.x + cos(base.ym) * radius;
512     base.y = old_base.y + sin(base.ym) * radius;
513
514     base.ym = fmodf(base.ym + frame_ratio * speed, 2*M_PI);
515 }
516
517 void
518 BadGuy::action_fish(float frame_ratio)
519 {
520   static const float JUMPV = 6;
521   static const int WAITTIME = 1000;
522     
523   // go in wait mode when back in water
524   if(dying == DYING_NOT && gettile(base.x, base.y+ base.height)->water
525         && physic.get_velocity_y() <= 0 && mode == NORMAL)
526     {
527       mode = FISH_WAIT;
528       set_sprite(0, 0);
529       physic.set_velocity(0, 0);
530       physic.enable_gravity(false);
531       timer.start(WAITTIME);
532     }
533   else if(mode == FISH_WAIT && !timer.check())
534     {
535       // jump again
536       set_sprite(img_fish, img_fish);
537       mode = NORMAL;
538       physic.set_velocity(0, JUMPV);
539       physic.enable_gravity(true);
540     }
541
542   physic.apply(frame_ratio, base.x, base.y);
543   if(dying == DYING_NOT)
544     collision_swept_object_map(&old_base, &base);
545
546   if(physic.get_velocity_y() < 0)
547     set_sprite(img_fish_down, img_fish_down);
548 }
549
550 void
551 BadGuy::action_bouncingsnowball(float frame_ratio)
552 {
553   static const float JUMPV = 4.5;
554     
555   fall();
556
557   // jump when on ground
558   if(dying == DYING_NOT && issolid(base.x, base.y+32))
559     {
560       physic.set_velocity_y(JUMPV);
561       physic.enable_gravity(true);
562     }                                                     
563   else
564     {
565       mode = NORMAL;
566     }
567
568   // check for right/left collisions
569   check_horizontal_bump();
570
571   physic.apply(frame_ratio, base.x, base.y);
572   if(dying == DYING_NOT)
573     collision_swept_object_map(&old_base, &base);
574
575   // Handle dying timer:
576   if (dying == DYING_SQUISHED && !timer.check())
577     {
578       /* Remove it if time's up: */
579       remove_me();
580       return;
581     }
582 }
583
584 void
585 BadGuy::action_flyingsnowball(float frame_ratio)
586 {
587   static const float FLYINGSPEED = 1;
588   static const int DIRCHANGETIME = 1000;
589     
590   // go into flyup mode if none specified yet
591   if(dying == DYING_NOT && mode == NORMAL) {
592     mode = FLY_UP;
593     physic.set_velocity_y(FLYINGSPEED);
594     timer.start(DIRCHANGETIME/2);
595   }
596
597   if(dying == DYING_NOT && !timer.check()) {
598     if(mode == FLY_UP) {
599       mode = FLY_DOWN;
600       physic.set_velocity_y(-FLYINGSPEED);
601     } else if(mode == FLY_DOWN) {
602       mode = FLY_UP;
603       physic.set_velocity_y(FLYINGSPEED);
604     }
605     timer.start(DIRCHANGETIME);
606   }
607
608   if(dying != DYING_NOT)
609     physic.enable_gravity(true);
610
611   physic.apply(frame_ratio, base.x, base.y);
612   if(dying == DYING_NOT || dying == DYING_SQUISHED)
613     collision_swept_object_map(&old_base, &base);
614
615   // Handle dying timer:
616   if (dying == DYING_SQUISHED && !timer.check())
617     {
618       /* Remove it if time's up: */
619       remove_me();
620       return;
621     }                                                          
622 }
623
624 void
625 BadGuy::action_spiky(float frame_ratio)
626 {
627   if (dying == DYING_NOT)
628     check_horizontal_bump();
629
630   fall();
631 #if 0
632   // jump when we're about to fall
633   if (physic.get_velocity_y() == 0 && 
634           !issolid(base.x+base.width/2, base.y + base.height)) {
635     physic.enable_gravity(true);
636     physic.set_velocity_y(2);
637   }
638 #endif
639
640   physic.apply(frame_ratio, base.x, base.y);
641   if (dying != DYING_FALLING)
642     collision_swept_object_map(&old_base,&base);   
643 }
644
645 void
646 BadGuy::action_snowball(float frame_ratio)
647 {
648   if (dying == DYING_NOT)
649     check_horizontal_bump();
650
651   fall();
652
653   physic.apply(frame_ratio, base.x, base.y);
654   if (dying != DYING_FALLING)
655     collision_swept_object_map(&old_base,&base);
656 }
657
658 void
659 BadGuy::action(float frame_ratio)
660 {
661   // Remove if it's far off the screen:
662   if (base.x < scroll_x - OFFSCREEN_DISTANCE)
663     {
664       remove_me();                                                
665       return;
666     }
667
668   // BadGuy fall below the ground
669   if (base.y > screen->h) {
670     remove_me();
671     return;
672   }
673
674   // Once it's on screen, it's activated!
675   if (base.x <= scroll_x + screen->w + OFFSCREEN_DISTANCE)
676     seen = true;
677
678   if(!seen)
679     return;
680
681   switch (kind)
682     {
683     case BAD_MRICEBLOCK:
684       action_mriceblock(frame_ratio);
685       break;
686   
687     case BAD_JUMPY:
688       action_jumpy(frame_ratio);
689       break;
690
691     case BAD_MRBOMB:
692       action_mrbomb(frame_ratio);
693       break;
694     
695     case BAD_BOMB:
696       action_bomb(frame_ratio);
697       break;
698
699     case BAD_STALACTITE:
700       action_stalactite(frame_ratio);
701       break;
702
703     case BAD_FLAME:
704       action_flame(frame_ratio);
705       break;
706
707     case BAD_FISH:
708       action_fish(frame_ratio);
709       break;
710
711     case BAD_BOUNCINGSNOWBALL:
712       action_bouncingsnowball(frame_ratio);
713       break;
714
715     case BAD_FLYINGSNOWBALL:
716       action_flyingsnowball(frame_ratio);
717       break;
718
719     case BAD_SPIKY:
720       action_spiky(frame_ratio);
721       break;
722
723     case BAD_SNOWBALL:
724       action_snowball(frame_ratio);
725       break;
726     }
727 }
728
729 void
730 BadGuy::draw()
731 {
732   // Don't try to draw stuff that is outside of the screen
733   if(base.x <= scroll_x - base.width || base.x >= scroll_x + screen->w)
734     return;
735   
736   if(sprite_left == 0 || sprite_right == 0)
737     {
738       return;
739     }
740
741   Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
742   sprite->draw(base.x - scroll_x, base.y);
743
744   if (debug_mode)
745     fillrect(base.x - scroll_x, base.y, base.width, base.height, 75,0,75, 150);
746 }
747
748 void
749 BadGuy::set_sprite(Sprite* left, Sprite* right) 
750 {
751   if (1)
752     {
753       base.width = 32;
754       base.height = 32;
755     }
756   else
757     {
758       // FIXME: Using the image size for the physics and collision is
759       // a bad idea, since images should always overlap there physical
760       // representation
761       if(left != 0) {
762         if(base.width == 0 && base.height == 0) {
763           base.width  = left->get_width();
764           base.height = left->get_height();
765         } else if(base.width != left->get_width() || base.height != left->get_height()) {
766           base.x -= (left->get_width() - base.width) / 2;
767           base.y -= left->get_height() - base.height;
768           base.width = left->get_width();
769           base.height = left->get_height();
770           old_base = base;
771         }
772       } else {
773         base.width = base.height = 0;
774       }
775     }
776
777   animation_offset = 0;
778   sprite_left  = left;
779   sprite_right = right;
780 }
781
782 void
783 BadGuy::bump()
784 {
785   // these can't be bumped
786   if(kind == BAD_FLAME || kind == BAD_BOMB || kind == BAD_FISH
787       || kind == BAD_FLYINGSNOWBALL)
788     return;
789
790   physic.set_velocity_y(3);
791   kill_me(25);
792 }
793
794 void
795 BadGuy::make_player_jump(Player* player)
796 {
797   player->physic.set_velocity_y(2);
798   player->base.y = base.y - player->base.height - 2;
799 }
800
801 void
802 BadGuy::squish_me(Player* player)
803 {
804   make_player_jump(player);
805     
806   World::current()->add_score(base.x - scroll_x,
807                               base.y, 50 * player_status.score_multiplier);
808   play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
809   player_status.score_multiplier++;
810
811   dying = DYING_SQUISHED;
812   timer.start(2000);
813   physic.set_velocity(0, 0);
814 }
815
816 void
817 BadGuy::squish(Player* player)
818 {
819   static const int MAX_ICEBLOCK_SQUICHES = 10;
820     
821   if(kind == BAD_MRBOMB) {
822     // mrbomb transforms into a bomb now
823     World::current()->add_bad_guy(base.x, base.y, BAD_BOMB);
824     
825     make_player_jump(player);
826     World::current()->add_score(base.x - scroll_x, base.y, 50 * player_status.score_multiplier);
827     play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
828     player_status.score_multiplier++;
829     remove_me();
830     return;
831
832   } else if (kind == BAD_MRICEBLOCK) {
833     if (mode == NORMAL || mode == KICK)
834       {
835         /* Flatten! */
836         play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
837         mode = FLAT;
838         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
839         physic.set_velocity_x(0);
840
841         timer.start(4000);
842       } else if (mode == FLAT) {
843         /* Kick! */
844         play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
845
846         if (player->base.x < base.x + (base.width/2)) {
847           physic.set_velocity_x(5);
848           dir = RIGHT;
849         } else {
850           physic.set_velocity_x(-5);
851           dir = LEFT;
852         }
853
854         mode = KICK;
855         player->kick_timer.start(KICKING_TIME);
856         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
857       }
858
859     make_player_jump(player);
860
861     player_status.score_multiplier++;
862
863     // check for maximum number of squiches
864     squishcount++;
865     if(squishcount >= MAX_ICEBLOCK_SQUICHES) {
866       kill_me(50);
867       return;
868     }
869     
870     return;
871   } else if(kind == BAD_FISH) {
872     // fish can only be killed when falling down
873     if(physic.get_velocity_y() >= 0)
874       return;
875       
876     make_player_jump(player);
877               
878     World::current()->add_score(base.x - scroll_x, base.y, 25 * player_status.score_multiplier);
879     player_status.score_multiplier++;
880      
881     // simply remove the fish...
882     remove_me();
883     return;
884   } else if(kind == BAD_BOUNCINGSNOWBALL) {
885     squish_me(player);
886     set_sprite(img_bouncingsnowball_squished,img_bouncingsnowball_squished);
887     return;
888   } else if(kind == BAD_FLYINGSNOWBALL) {
889     squish_me(player);
890     set_sprite(img_flyingsnowball_squished,img_flyingsnowball_squished);
891     return;
892   } else if(kind == BAD_SNOWBALL) {
893     squish_me(player);
894     set_sprite(img_snowball_squished_left, img_snowball_squished_right);
895     return;
896   }
897 }
898
899 void
900 BadGuy::kill_me(int score)
901 {
902   if(kind == BAD_BOMB || kind == BAD_STALACTITE || kind == BAD_FLAME)
903     return;
904
905   dying = DYING_FALLING;
906   if(kind == BAD_MRICEBLOCK) {
907     set_sprite(img_mriceblock_falling_left, img_mriceblock_falling_right);
908     if(mode == HELD) {
909       mode = NORMAL;
910       Player& tux = *World::current()->get_tux();  
911       tux.holding_something = false;
912     }
913   }
914   
915   physic.enable_gravity(true);
916
917   /* Gain some points: */
918     World::current()->add_score(base.x - scroll_x, base.y,
919                     score * player_status.score_multiplier);
920
921   /* Play death sound: */
922   play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
923 }
924
925 void
926 BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
927 {
928   BadGuy* pbad_c    = NULL;
929
930   if(type == COLLISION_BUMP) {
931     bump();
932     return;
933   }
934
935   if(type == COLLISION_SQUISH) {
936     Player* player = static_cast<Player*>(p_c_object);
937     squish(player);
938     return;
939   }
940
941   /* COLLISION_NORMAL */
942   switch (c_object)
943     {
944     case CO_BULLET:
945       kill_me(10);
946       break;
947
948     case CO_BADGUY:
949       pbad_c = (BadGuy*) p_c_object;
950
951       /* If we're a kicked mriceblock, kill any badguys we hit */
952       if(kind == BAD_MRICEBLOCK && mode == KICK)
953         {
954           pbad_c->kill_me(25);
955         }
956
957       // a held mriceblock gets kills the enemy too but falls to ground then
958       else if(kind == BAD_MRICEBLOCK && mode == HELD)
959         {
960           pbad_c->kill_me(25);
961           kill_me(0);
962         }
963
964       /* Kill badguys that run into exploding bomb */
965       else if (kind == BAD_BOMB && dying == DYING_NOT)
966       {
967         if (pbad_c->kind == BAD_MRBOMB)
968         {
969           // mrbomb transforms into a bomb now
970           World::current()->add_bad_guy(pbad_c->base.x, pbad_c->base.y,
971                                         BAD_BOMB);
972           pbad_c->remove_me();
973           return;
974         }
975         else if (pbad_c->kind != BAD_MRBOMB)
976         {
977           pbad_c->kill_me(50);
978         }
979       }
980
981       /* Kill any badguys that get hit by stalactite */
982       else if (kind == BAD_STALACTITE && dying == DYING_NOT)
983       {
984         pbad_c->kill_me(50);
985       }
986
987       /* When enemies run into eachother, make them change directions */
988       else
989       {
990         // Jumpy, fish, flame, stalactites are exceptions
991         if (pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FLAME
992             || pbad_c->kind == BAD_STALACTITE || pbad_c->kind == BAD_FISH)
993           break;
994
995         // Bounce off of other badguy if we land on top of him
996         if (base.y + base.height < pbad_c->base.y + pbad_c->base.height)
997         {
998           if (pbad_c->dir == LEFT)
999           {
1000             dir = RIGHT;
1001             physic.set_velocity(fabs(physic.get_velocity_x()), 2);
1002           }
1003           else if (pbad_c->dir == RIGHT)
1004           {
1005             dir = LEFT;
1006             physic.set_velocity(-fabs(physic.get_velocity_x()), 2);
1007           }
1008
1009
1010
1011           break;
1012         }
1013         else if (base.y + base.height > pbad_c->base.y + pbad_c->base.height)
1014           break;
1015
1016         if (pbad_c->kind != BAD_FLAME)
1017           {
1018             if (dir == LEFT)
1019             {
1020               dir = RIGHT;
1021               physic.set_velocity_x(fabs(physic.get_velocity_x()));
1022             }
1023             else if (dir == RIGHT)
1024             {
1025               dir = LEFT;
1026               physic.set_velocity_x(-fabs(physic.get_velocity_x()));
1027             }
1028
1029           }
1030       }
1031       
1032       break;
1033
1034     case CO_PLAYER:
1035       Player* player = static_cast<Player*>(p_c_object);
1036       /* Get kicked if were flat */
1037       if (mode == FLAT && !dying)
1038       {
1039         play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
1040
1041         // Hit from left side
1042         if (player->base.x < base.x) {
1043           physic.set_velocity_x(5);
1044           dir = RIGHT;
1045         }
1046         // Hit from right side
1047         else {
1048           physic.set_velocity_x(-5);
1049           dir = LEFT;
1050         }
1051
1052         mode = KICK;
1053         player->kick_timer.start(KICKING_TIME);
1054         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1055       }
1056       break;
1057
1058     }
1059 }
1060
1061 //---------------------------------------------------------------------------
1062
1063 void load_badguy_gfx()
1064 {
1065   img_mriceblock_flat_left = sprite_manager->load("mriceblock-flat-left");
1066   img_mriceblock_flat_right = sprite_manager->load("mriceblock-flat-right");
1067   img_mriceblock_falling_left = sprite_manager->load("mriceblock-falling-left");
1068   img_mriceblock_falling_right = sprite_manager->load("mriceblock-falling-right");
1069   img_mriceblock_left = sprite_manager->load("mriceblock-left");
1070   img_mriceblock_right = sprite_manager->load("mriceblock-right");
1071   img_jumpy_left_up = sprite_manager->load("jumpy-left-up");
1072   img_jumpy_left_down = sprite_manager->load("jumpy-left-down");
1073   img_jumpy_left_middle = sprite_manager->load("jumpy-left-middle");
1074   img_mrbomb_left = sprite_manager->load("mrbomb-left");
1075   img_mrbomb_right = sprite_manager->load("mrbomb-right");
1076   img_mrbomb_ticking_left = sprite_manager->load("mrbomb-ticking-left");
1077   img_mrbomb_ticking_right = sprite_manager->load("mrbomb-ticking-right");
1078   img_mrbomb_explosion = sprite_manager->load("mrbomb-explosion");
1079   img_stalactite = sprite_manager->load("stalactite");
1080   img_stalactite_broken = sprite_manager->load("stalactite-broken");
1081   img_flame = sprite_manager->load("flame");
1082   img_fish = sprite_manager->load("fish");
1083   img_fish_down = sprite_manager->load("fish-down");
1084   img_bouncingsnowball_left = sprite_manager->load("bouncingsnowball-left");
1085   img_bouncingsnowball_right = sprite_manager->load("bouncingsnowball-right");
1086   img_bouncingsnowball_squished = sprite_manager->load("bouncingsnowball-squished");
1087   img_flyingsnowball = sprite_manager->load("flyingsnowball");
1088   img_flyingsnowball_squished = sprite_manager->load("flyingsnowball-squished");
1089   img_spiky_left = sprite_manager->load("spiky-left");
1090   img_spiky_right = sprite_manager->load("spiky-right");
1091   img_snowball_left = sprite_manager->load("snowball-left");
1092   img_snowball_right = sprite_manager->load("snowball-right");
1093   img_snowball_squished_left = sprite_manager->load("snowball-squished-left");
1094   img_snowball_squished_right = sprite_manager->load("snowball-squished-right");
1095 }
1096
1097 void free_badguy_gfx()
1098 {
1099 }
1100
1101 // EOF //