At least my compiler (g++ (GCC) 3.3.2) needs this include.
[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_bsod_squished_left;
37 Sprite* img_bsod_squished_right;
38 Sprite* img_bsod_falling_left;
39 Sprite* img_bsod_falling_right;
40 Sprite* img_laptop_flat_left;
41 Sprite* img_laptop_flat_right;
42 Sprite* img_laptop_falling_left;
43 Sprite* img_laptop_falling_right;
44 Sprite* img_bsod_left;
45 Sprite* img_bsod_right;
46 Sprite* img_laptop_left;
47 Sprite* img_laptop_right;
48 Sprite* img_jumpy_left_up;
49 Sprite* img_jumpy_left_down;
50 Sprite* img_jumpy_left_middle;
51 Sprite* img_mrbomb_left;
52 Sprite* img_mrbomb_right;
53 Sprite* img_mrbomb_ticking_left;
54 Sprite* img_mrbomb_ticking_right;
55 Sprite* img_mrbomb_explosion;
56 Sprite* img_stalactite;
57 Sprite* img_stalactite_broken;
58 Sprite* img_flame;
59 Sprite* img_fish;
60 Sprite* img_fish_down;
61 Sprite* img_bouncingsnowball_left;
62 Sprite* img_bouncingsnowball_right;
63 Sprite* img_bouncingsnowball_squished;
64 Sprite* img_flyingsnowball;
65 Sprite* img_flyingsnowball_squished;
66 Sprite* img_spiky_left;
67 Sprite* img_spiky_right;
68 Sprite* img_snowball_left;
69 Sprite* img_snowball_right;
70 Sprite* img_snowball_squished_left;
71 Sprite* img_snowball_squished_right;
72
73 #define BADGUY_WALK_SPEED .8f
74
75 BadGuyKind  badguykind_from_string(const std::string& str)
76 {
77   if (str == "money")
78     return BAD_MONEY;
79   else if (str == "laptop" || str == "mriceblock")
80     return BAD_LAPTOP;
81   else if (str == "bsod")
82     return BAD_BSOD;
83   else if (str == "mrbomb")
84     return BAD_MRBOMB;
85   else if (str == "stalactite")
86     return BAD_STALACTITE;
87   else if (str == "flame")
88     return BAD_FLAME;
89   else if (str == "fish")
90     return BAD_FISH;
91   else if (str == "bouncingsnowball")
92     return BAD_BOUNCINGSNOWBALL;
93   else if (str == "flyingsnowball")
94     return BAD_FLYINGSNOWBALL;
95   else if (str == "spiky")
96     return BAD_SPIKY;
97   else if (str == "snowball")
98     return BAD_SNOWBALL;
99   else
100     {
101       printf("Couldn't convert badguy: '%s'\n", str.c_str());
102       return BAD_BSOD;
103     }
104 }
105
106 std::string badguykind_to_string(BadGuyKind kind)
107 {
108   switch(kind)
109     {
110     case BAD_MONEY:
111       return "money";
112       break;
113     case BAD_LAPTOP:
114       return "laptop";
115       break;
116     case BAD_BSOD:
117       return "bsod";
118       break;
119     case BAD_MRBOMB:
120       return "mrbomb";
121       break;
122     case BAD_STALACTITE:
123       return "stalactite";
124       break;
125     case BAD_FLAME:
126       return "flame";
127       break;
128     case BAD_FISH:
129       return "fish";
130       break;
131     case BAD_BOUNCINGSNOWBALL:
132       return "bouncingsnowball";
133       break;
134     case BAD_FLYINGSNOWBALL:
135       return "flyingsnowball";
136       break;
137     case BAD_SPIKY:
138       return "spiky";
139       break;
140     case BAD_SNOWBALL:
141       return "snowball";
142       break;
143     default:
144       return "bsod";
145     }
146 }
147
148 BadGuy::BadGuy(float x, float y, BadGuyKind kind_, bool stay_on_platform_)
149   : removable(false)
150 {
151   base.x   = x;
152   base.y   = y;    
153   base.width  = 0;
154   base.height = 0;
155   base.xm  = 0;
156   base.ym  = 0;
157
158   stay_on_platform = stay_on_platform_;
159   mode     = NORMAL;
160   dying    = DYING_NOT;
161   kind     = kind_;
162   old_base = base;
163   dir      = LEFT;
164   seen     = false;
165   animation_offset = 0;
166   sprite_left = sprite_right = 0;
167   physic.reset();
168   timer.init(true);
169
170   if(kind == BAD_BSOD) {
171     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
172     set_sprite(img_bsod_left, img_bsod_right);
173   } else if(kind == BAD_MRBOMB) {
174     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
175     set_sprite(img_mrbomb_left, img_mrbomb_right);
176   } else if (kind == BAD_LAPTOP) {
177     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
178     set_sprite(img_laptop_left, img_laptop_right);
179   } else if(kind == BAD_MONEY) {
180     set_sprite(img_jumpy_left_up, img_jumpy_left_up);
181   } else if(kind == BAD_BOMB) {
182     set_sprite(img_mrbomb_ticking_left, img_mrbomb_ticking_right);
183     // hack so that the bomb doesn't hurt until it expldes...
184     dying = DYING_SQUISHED;
185   } else if(kind == BAD_FLAME) {
186     base.ym = 0; // we misuse base.ym as angle for the flame
187     physic.enable_gravity(false);
188     set_sprite(img_flame, img_flame);
189   } else if(kind == BAD_BOUNCINGSNOWBALL) {
190     physic.set_velocity(-1.3, 0);
191     set_sprite(img_bouncingsnowball_left, img_bouncingsnowball_right);
192   } else if(kind == BAD_STALACTITE) {
193     physic.enable_gravity(false);
194     set_sprite(img_stalactite, img_stalactite);
195   } else if(kind == BAD_FISH) {
196     set_sprite(img_fish, img_fish);
197     physic.enable_gravity(true);
198   } else if(kind == BAD_FLYINGSNOWBALL) {
199     set_sprite(img_flyingsnowball, img_flyingsnowball);
200     physic.enable_gravity(false);
201   } else if(kind == BAD_SPIKY) {
202     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
203     set_sprite(img_spiky_left, img_spiky_right);
204   } else if(kind == BAD_SNOWBALL) {
205     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
206     set_sprite(img_snowball_left, img_snowball_right);
207   }
208
209   // if we're in a solid tile at start correct that now
210   if(kind != BAD_FLAME && kind != BAD_FISH && collision_object_map(base)) {
211     printf("Warning: badguy started in wall!.\n");
212     while(collision_object_map(base))
213       --base.y;
214   }
215 }
216
217 void
218 BadGuy::action_bsod(float frame_ratio)
219 {
220   static const float BSODJUMP = 2;
221     
222   if (dying == DYING_NOT)
223     check_horizontal_bump();
224
225   fall();
226
227   // jump when we're about to fall
228   if (physic.get_velocity_y() == 0 && 
229       !issolid(base.x+base.width/2, base.y + base.height)) 
230     {
231       physic.enable_gravity(true);
232       physic.set_velocity(physic.get_velocity_x(), BSODJUMP);
233     }
234
235   // Handle dying timer:
236   if (dying == DYING_SQUISHED && !timer.check())
237     {
238       /* Remove it if time's up: */
239       remove_me();
240       return;
241     }
242
243   // move
244   physic.apply(frame_ratio, base.x, base.y);
245   if(dying != DYING_FALLING)
246     collision_swept_object_map(&old_base, &base);
247 }
248
249 void
250 BadGuy::action_laptop(float frame_ratio)
251 {
252   Player& tux = *World::current()->get_tux();
253
254   if(dying == DYING_NOT)
255     fall();
256   
257   /* Move left/right: */
258   if (mode == NORMAL || mode == KICK)
259     {
260       // move
261       physic.apply(frame_ratio, base.x, base.y);
262       if (dying != DYING_FALLING)
263         collision_swept_object_map(&old_base,&base);
264     }
265   else if (mode == HELD)
266     { /* FIXME: The pbad object shouldn't know about pplayer objects. */
267       /* If we're holding the laptop */
268       dir = tux.dir;
269       if(dir==RIGHT)
270         {
271           base.x = tux.base.x + 16;
272           base.y = tux.base.y + tux.base.height/1.5 - base.height;
273         }
274       else /* facing left */
275         {
276           base.x = tux.base.x - 16;
277           base.y = tux.base.y + tux.base.height/1.5 - base.height;
278         }
279       if(collision_object_map(base))
280         {
281           base.x = tux.base.x;
282           base.y = tux.base.y + tux.base.height/1.5 - base.height;
283         }
284
285       if(tux.input.fire != DOWN) /* SHOOT! */
286         {
287           if(dir == LEFT)
288             base.x -= 24;
289           else
290             base.x += 24;
291           old_base = base;
292
293           mode=KICK;
294           set_sprite(img_laptop_flat_left, img_laptop_flat_right);
295           physic.set_velocity_x((dir == LEFT) ? -3.5 : 3.5);
296           play_sound(sounds[SND_KICK],SOUND_CENTER_SPEAKER);
297         }
298     }
299
300   if (!dying)
301     {
302       int changed = dir;
303       check_horizontal_bump();
304       if(mode == KICK && changed != dir)
305         {
306           /* handle stereo sound (number 10 should be tweaked...)*/
307           if (base.x < scroll_x + screen->w/2 - 10)
308             play_sound(sounds[SND_RICOCHET], SOUND_LEFT_SPEAKER);
309           else if (base.x > scroll_x + screen->w/2 + 10)
310             play_sound(sounds[SND_RICOCHET], SOUND_RIGHT_SPEAKER);
311           else
312             play_sound(sounds[SND_RICOCHET], SOUND_CENTER_SPEAKER);
313         }
314     }
315
316   /* Handle mode timer: */
317   if (mode == FLAT)
318     {
319       if(!timer.check())
320         {
321           mode = NORMAL;
322           set_sprite(img_laptop_left, img_laptop_right);
323           physic.set_velocity( (dir == LEFT) ? -.8 : .8, 0);
324         }
325     }
326 }
327
328 void
329 BadGuy::check_horizontal_bump(bool checkcliff)
330 {
331     float halfheight = base.height / 2;
332     if (dir == LEFT && issolid( base.x, (int) base.y + halfheight))
333     {
334         dir = RIGHT;
335         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
336         return;
337     }
338     if (dir == RIGHT && issolid( base.x + base.width, (int)base.y + halfheight))
339     {
340         dir = LEFT;
341         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
342         return;
343     }
344
345     // don't check for cliffs when we're falling
346     if(!checkcliff)
347         return;
348     if(!issolid(base.x + base.width/2, base.y + base.height))
349         return;
350     
351     if(dir == LEFT && !issolid(base.x, (int) base.y + base.height + halfheight))
352     {
353         dir = RIGHT;
354         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
355         return;
356     }
357     if(dir == RIGHT && !issolid(base.x + base.width,
358                 (int) base.y + base.height + halfheight))
359     {
360         dir = LEFT;
361         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
362         return;
363     }
364 }
365
366 void
367 BadGuy::fall()
368 {
369   /* Fall if we get off the ground: */
370   if (dying != DYING_FALLING)
371     {
372       if (!issolid(base.x+base.width/2, base.y + base.height))
373         {
374           // not solid below us? enable gravity
375           physic.enable_gravity(true);
376         }
377       else
378         {
379           /* Land: */
380           if (physic.get_velocity_y() < 0)
381             {
382               base.y = int((base.y + base.height)/32) * 32 - base.height;
383               physic.set_velocity_y(0);
384             }
385           // no gravity anymore please
386           physic.enable_gravity(false);
387
388           if (stay_on_platform && mode == NORMAL)
389             {
390               if (!issolid(base.x + ((dir == LEFT) ? 0 : base.width),
391                            base.y + base.height))
392                 {
393                   physic.set_velocity_x(-physic.get_velocity_x());
394                   if (dir == LEFT)
395                     dir = RIGHT;
396                   else
397                     dir = LEFT;
398                 }
399             }
400         }
401     }
402   else
403     {
404       physic.enable_gravity(true);
405     }
406 }
407
408 void
409 BadGuy::remove_me()
410 {
411   removable = true;
412 }
413
414 void
415 BadGuy::action_money(float frame_ratio)
416 {
417   if (fabsf(physic.get_velocity_y()) < 2.5f)
418     set_sprite(img_jumpy_left_middle, img_jumpy_left_middle);
419   else if (physic.get_velocity_y() < 0)
420     set_sprite(img_jumpy_left_up, img_jumpy_left_up);
421   else 
422     set_sprite(img_jumpy_left_down, img_jumpy_left_down);
423
424   Player& tux = *World::current()->get_tux();
425
426   static const float JUMPV = 6;
427     
428   fall();
429   // jump when on ground
430   if(dying == DYING_NOT && issolid(base.x, base.y+32))
431     {
432       physic.set_velocity_y(JUMPV);
433       physic.enable_gravity(true);
434
435       mode = MONEY_JUMP;
436     }
437   else if(mode == MONEY_JUMP)
438     {
439       mode = NORMAL;
440     }
441
442   // set direction based on tux
443   if(tux.base.x > base.x)
444     dir = RIGHT;
445   else
446     dir = LEFT;
447
448   // move
449   physic.apply(frame_ratio, base.x, base.y);
450   if(dying == DYING_NOT)
451     collision_swept_object_map(&old_base, &base);
452 }
453
454 void
455 BadGuy::action_mrbomb(float frame_ratio)
456 {
457   if (dying == DYING_NOT)
458     check_horizontal_bump(true);
459
460   fall();
461
462   physic.apply(frame_ratio, base.x, base.y);
463   if (dying != DYING_FALLING)
464     collision_swept_object_map(&old_base,&base); 
465 }
466
467 void
468 BadGuy::action_bomb(float frame_ratio)
469 {
470   static const int TICKINGTIME = 1000;
471   static const int EXPLODETIME = 1000;
472     
473   fall();
474
475   if(mode == NORMAL) {
476     mode = BOMB_TICKING;
477     timer.start(TICKINGTIME);
478   } else if(!timer.check()) {
479     if(mode == BOMB_TICKING) {
480       mode = BOMB_EXPLODE;
481       set_sprite(img_mrbomb_explosion, img_mrbomb_explosion);
482       dying = DYING_NOT; // now the bomb hurts
483       timer.start(EXPLODETIME);
484
485       /* play explosion sound */  // FIXME: is the stereo all right? maybe we should use player cordinates...
486       if (base.x < scroll_x + screen->w/2 - 10)
487         play_sound(sounds[SND_EXPLODE], SOUND_LEFT_SPEAKER);
488       else if (base.x > scroll_x + screen->w/2 + 10)
489         play_sound(sounds[SND_EXPLODE], SOUND_RIGHT_SPEAKER);
490       else
491         play_sound(sounds[SND_EXPLODE], SOUND_CENTER_SPEAKER);
492
493     } else if(mode == BOMB_EXPLODE) {
494       remove_me();
495       return;
496     }
497   }
498
499   // move
500   physic.apply(frame_ratio, base.x, base.y);                 
501   collision_swept_object_map(&old_base,&base);
502 }
503
504 void
505 BadGuy::action_stalactite(float frame_ratio)
506 {
507   Player& tux = *World::current()->get_tux();
508
509   static const int SHAKETIME = 800;
510   static const int RANGE = 40;
511     
512   if(mode == NORMAL) {
513     // start shaking when tux is below the stalactite and at least 40 pixels
514     // near
515     if(tux.base.x + 32 > base.x - RANGE && tux.base.x < base.x + 32 + RANGE
516             && tux.base.y + tux.base.height > base.y) {
517       timer.start(SHAKETIME);
518       mode = STALACTITE_SHAKING;
519     }
520   } if(mode == STALACTITE_SHAKING) {
521     base.x = old_base.x + (rand() % 6) - 3; // TODO this could be done nicer...
522     if(!timer.check()) {
523       mode = STALACTITE_FALL;
524     }
525   } else if(mode == STALACTITE_FALL) {
526     fall();
527     /* Destroy if we collides with land */
528     if(issolid(base.x+base.width/2, base.y+base.height))
529     {
530       timer.start(2000);
531       dying = DYING_SQUISHED;
532       mode = FLAT;
533       set_sprite(img_stalactite_broken, img_stalactite_broken);
534     }
535   } else if(mode == FLAT) {
536     fall();
537   }
538
539   // move
540   physic.apply(frame_ratio, base.x, base.y);
541
542   if(dying == DYING_SQUISHED && !timer.check())
543     remove_me();
544 }
545
546 void
547 BadGuy::action_flame(float frame_ratio)
548 {
549     static const float radius = 100;
550     static const float speed = 0.02;
551     base.x = old_base.x + cos(base.ym) * radius;
552     base.y = old_base.y + sin(base.ym) * radius;
553
554     base.ym = fmodf(base.ym + frame_ratio * speed, 2*M_PI);
555 }
556
557 void
558 BadGuy::action_fish(float frame_ratio)
559 {
560   static const float JUMPV = 6;
561   static const int WAITTIME = 1000;
562     
563   // go in wait mode when back in water
564   if(dying == DYING_NOT && gettile(base.x, base.y+ base.height)->water
565         && physic.get_velocity_y() <= 0 && mode == NORMAL)
566     {
567       mode = FISH_WAIT;
568       set_sprite(0, 0);
569       physic.set_velocity(0, 0);
570       physic.enable_gravity(false);
571       timer.start(WAITTIME);
572     }
573   else if(mode == FISH_WAIT && !timer.check())
574     {
575       // jump again
576       set_sprite(img_fish, img_fish);
577       mode = NORMAL;
578       physic.set_velocity(0, JUMPV);
579       physic.enable_gravity(true);
580     }
581
582   physic.apply(frame_ratio, base.x, base.y);
583   if(dying == DYING_NOT)
584     collision_swept_object_map(&old_base, &base);
585
586   if(physic.get_velocity_y() < 0)
587     set_sprite(img_fish_down, img_fish_down);
588 }
589
590 void
591 BadGuy::action_bouncingsnowball(float frame_ratio)
592 {
593   static const float JUMPV = 4.5;
594     
595   fall();
596
597   // jump when on ground
598   if(dying == DYING_NOT && issolid(base.x, base.y+32))
599     {
600       physic.set_velocity_y(JUMPV);
601       physic.enable_gravity(true);
602     }                                                     
603   else
604     {
605       mode = NORMAL;
606     }
607
608   // check for right/left collisions
609   check_horizontal_bump();
610
611   physic.apply(frame_ratio, base.x, base.y);
612   if(dying == DYING_NOT)
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_flyingsnowball(float frame_ratio)
626 {
627   static const float FLYINGSPEED = 1;
628   static const int DIRCHANGETIME = 1000;
629     
630   // go into flyup mode if none specified yet
631   if(dying == DYING_NOT && mode == NORMAL) {
632     mode = FLY_UP;
633     physic.set_velocity_y(FLYINGSPEED);
634     timer.start(DIRCHANGETIME/2);
635   }
636
637   if(dying == DYING_NOT && !timer.check()) {
638     if(mode == FLY_UP) {
639       mode = FLY_DOWN;
640       physic.set_velocity_y(-FLYINGSPEED);
641     } else if(mode == FLY_DOWN) {
642       mode = FLY_UP;
643       physic.set_velocity_y(FLYINGSPEED);
644     }
645     timer.start(DIRCHANGETIME);
646   }
647
648   if(dying != DYING_NOT)
649     physic.enable_gravity(true);
650
651   physic.apply(frame_ratio, base.x, base.y);
652   if(dying == DYING_NOT || dying == DYING_SQUISHED)
653     collision_swept_object_map(&old_base, &base);
654
655   // Handle dying timer:
656   if (dying == DYING_SQUISHED && !timer.check())
657     {
658       /* Remove it if time's up: */
659       remove_me();
660       return;
661     }                                                          
662 }
663
664 void
665 BadGuy::action_spiky(float frame_ratio)
666 {
667   if (dying == DYING_NOT)
668     check_horizontal_bump();
669
670   fall();
671 #if 0
672   // jump when we're about to fall
673   if (physic.get_velocity_y() == 0 && 
674           !issolid(base.x+base.width/2, base.y + base.height)) {
675     physic.enable_gravity(true);
676     physic.set_velocity_y(2);
677   }
678 #endif
679
680   physic.apply(frame_ratio, base.x, base.y);
681   if (dying != DYING_FALLING)
682     collision_swept_object_map(&old_base,&base);   
683 }
684
685 void
686 BadGuy::action_snowball(float frame_ratio)
687 {
688   if (dying == DYING_NOT)
689     check_horizontal_bump();
690
691   fall();
692
693   physic.apply(frame_ratio, base.x, base.y);
694   if (dying != DYING_FALLING)
695     collision_swept_object_map(&old_base,&base);
696 }
697
698 void
699 BadGuy::action(float frame_ratio)
700 {
701   // Remove if it's far off the screen:
702   if (base.x < scroll_x - OFFSCREEN_DISTANCE)
703     {
704       remove_me();                                                
705       return;
706     }
707
708   // BadGuy fall below the ground
709   if (base.y > screen->h) {
710     remove_me();
711     return;
712   }
713
714   // Once it's on screen, it's activated!
715   if (base.x <= scroll_x + screen->w + OFFSCREEN_DISTANCE)
716     seen = true;
717
718   if(!seen)
719     return;
720
721   switch (kind)
722     {
723     case BAD_BSOD:
724       action_bsod(frame_ratio);
725       break;
726
727     case BAD_LAPTOP:
728       action_laptop(frame_ratio);
729       break;
730   
731     case BAD_MONEY:
732       action_money(frame_ratio);
733       break;
734
735     case BAD_MRBOMB:
736       action_mrbomb(frame_ratio);
737       break;
738     
739     case BAD_BOMB:
740       action_bomb(frame_ratio);
741       break;
742
743     case BAD_STALACTITE:
744       action_stalactite(frame_ratio);
745       break;
746
747     case BAD_FLAME:
748       action_flame(frame_ratio);
749       break;
750
751     case BAD_FISH:
752       action_fish(frame_ratio);
753       break;
754
755     case BAD_BOUNCINGSNOWBALL:
756       action_bouncingsnowball(frame_ratio);
757       break;
758
759     case BAD_FLYINGSNOWBALL:
760       action_flyingsnowball(frame_ratio);
761       break;
762
763     case BAD_SPIKY:
764       action_spiky(frame_ratio);
765       break;
766
767     case BAD_SNOWBALL:
768       action_snowball(frame_ratio);
769       break;
770     }
771 }
772
773 void
774 BadGuy::draw()
775 {
776   // Don't try to draw stuff that is outside of the screen
777   if(base.x <= scroll_x - base.width || base.x >= scroll_x + screen->w)
778     return;
779   
780   if(sprite_left == 0 || sprite_right == 0)
781     {
782       return;
783     }
784
785   Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
786   sprite->draw(base.x - scroll_x, base.y);
787
788   if (debug_mode)
789     fillrect(base.x - scroll_x, base.y, base.width, base.height, 75,0,75, 150);
790 }
791
792 void
793 BadGuy::set_sprite(Sprite* left, Sprite* right) 
794 {
795   if (1)
796     {
797       base.width = 32;
798       base.height = 32;
799     }
800   else
801     {
802       // FIXME: Using the image size for the physics and collision is
803       // a bad idea, since images should always overlap there physical
804       // representation
805       if(left != 0) {
806         if(base.width == 0 && base.height == 0) {
807           base.width  = left->get_width();
808           base.height = left->get_height();
809         } else if(base.width != left->get_width() || base.height != left->get_height()) {
810           base.x -= (left->get_width() - base.width) / 2;
811           base.y -= left->get_height() - base.height;
812           base.width = left->get_width();
813           base.height = left->get_height();
814           old_base = base;
815         }
816       } else {
817         base.width = base.height = 0;
818       }
819     }
820
821   animation_offset = 0;
822   sprite_left  = left;
823   sprite_right = right;
824 }
825
826 void
827 BadGuy::bump()
828 {
829   if(kind == BAD_BSOD || kind == BAD_LAPTOP || kind == BAD_MRBOMB
830       || kind == BAD_BOUNCINGSNOWBALL) {
831     kill_me();
832   }
833 }
834
835 void
836 BadGuy::make_player_jump(Player* player)
837 {
838   player->physic.set_velocity_y(2);
839   player->base.y = base.y - player->base.height - 2;
840 }
841
842 void
843 BadGuy::squish_me(Player* player)
844 {
845   make_player_jump(player);
846     
847   World::current()->add_score(base.x - scroll_x,
848                               base.y, 50 * player_status.score_multiplier);
849   play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
850   player_status.score_multiplier++;
851
852   dying = DYING_SQUISHED;
853   timer.start(2000);
854   physic.set_velocity(0, 0);
855 }
856
857 void
858 BadGuy::squish(Player* player)
859 {
860   if(kind == BAD_MRBOMB) {
861     // mrbomb transforms into a bomb now
862     World::current()->add_bad_guy(base.x, base.y, BAD_BOMB);
863     
864     make_player_jump(player);
865     World::current()->add_score(base.x - scroll_x, base.y, 50 * player_status.score_multiplier);
866     play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
867     player_status.score_multiplier++;
868     remove_me();
869     return;
870
871   } else if(kind == BAD_BSOD) {
872     squish_me(player);
873     set_sprite(img_bsod_squished_left, img_bsod_squished_right);
874     physic.set_velocity_x(0);
875     return;
876       
877   } else if (kind == BAD_LAPTOP) {
878     if (mode == NORMAL || mode == KICK)
879       {
880         /* Flatten! */
881         play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
882         mode = FLAT;
883         set_sprite(img_laptop_flat_left, img_laptop_flat_right);
884         physic.set_velocity_x(0);
885
886         timer.start(4000);
887       } else if (mode == FLAT) {
888         /* Kick! */
889         play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
890
891         if (player->base.x < base.x + (base.width/2)) {
892           physic.set_velocity_x(5);
893           dir = RIGHT;
894         } else {
895           physic.set_velocity_x(-5);
896           dir = LEFT;
897         }
898
899         mode = KICK;
900         set_sprite(img_laptop_flat_left, img_laptop_flat_right);
901       }
902
903     make_player_jump(player);
904               
905     World::current()->add_score(base.x - scroll_x, base.y, 25 * player_status.score_multiplier);
906     player_status.score_multiplier++;
907     return;
908   } else if(kind == BAD_FISH) {
909     // fish can only be killed when falling down
910     if(physic.get_velocity_y() >= 0)
911       return;
912       
913     make_player_jump(player);
914               
915     World::current()->add_score(base.x - scroll_x, base.y, 25 * player_status.score_multiplier);
916     player_status.score_multiplier++;
917      
918     // simply remove the fish...
919     remove_me();
920     return;
921   } else if(kind == BAD_BOUNCINGSNOWBALL) {
922     squish_me(player);
923     set_sprite(img_bouncingsnowball_squished,img_bouncingsnowball_squished);
924     return;
925   } else if(kind == BAD_FLYINGSNOWBALL) {
926     squish_me(player);
927     set_sprite(img_flyingsnowball_squished,img_flyingsnowball_squished);
928     return;
929   } else if(kind == BAD_SNOWBALL) {
930     squish_me(player);
931     set_sprite(img_snowball_squished_left, img_snowball_squished_right);
932     return;
933   }
934 }
935
936 void
937 BadGuy::kill_me()
938 {
939   if(kind == BAD_BOMB || kind == BAD_STALACTITE || kind == BAD_FLAME)
940     return;
941
942   dying = DYING_FALLING;
943   if(kind == BAD_LAPTOP)
944     set_sprite(img_laptop_falling_left, img_laptop_falling_right);
945   else if(kind == BAD_BSOD)
946     set_sprite(img_bsod_falling_left, img_bsod_falling_right);
947   
948   physic.enable_gravity(true);
949   physic.set_velocity_y(0);
950
951   /* Gain some points: */
952   if (kind == BAD_BSOD)
953     World::current()->add_score(base.x - scroll_x, base.y,
954                     50 * player_status.score_multiplier);
955   else 
956     World::current()->add_score(base.x - scroll_x, base.y,                                 
957                     25 * player_status.score_multiplier);
958
959   /* Play death sound: */
960   play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
961 }
962
963 void
964 BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
965 {
966   BadGuy* pbad_c    = NULL;
967
968   if(type == COLLISION_BUMP) {
969     bump();
970     return;
971   }
972
973   if(type == COLLISION_SQUISH) {
974     Player* player = static_cast<Player*>(p_c_object);
975     squish(player);
976     return;
977   }
978
979   /* COLLISION_NORMAL */
980   switch (c_object)
981     {
982     case CO_BULLET:
983       kill_me();
984       break;
985
986     case CO_BADGUY:
987       pbad_c = (BadGuy*) p_c_object;
988
989       /* If we're a kicked mriceblock, kill any badguys we hit */
990       if(kind == BAD_LAPTOP && mode == KICK &&
991             pbad_c->kind != BAD_FLAME && pbad_c->kind != BAD_BOMB)
992         {
993           pbad_c->kill_me();
994         }
995
996       /* Kill badguys that run into exploding bomb */
997       else if (kind == BAD_BOMB && dying == DYING_NOT)
998       {
999         if (pbad_c->kind == BAD_MRBOMB)
1000         {
1001           // FIXME: this is where other MrBombs *should* explode istead of dying
1002           pbad_c->kill_me(); 
1003         }
1004         else if (pbad_c->kind != BAD_BOMB)
1005         {
1006           pbad_c->kill_me();
1007         }
1008       }
1009
1010       /* Kill any badguys that get hit by stalactite */
1011       else if (kind == BAD_STALACTITE && dying == DYING_NOT)
1012       {
1013         pbad_c->kill_me();
1014       }
1015
1016       /* When enemies run into eachother, make them change directions */
1017       else
1018       {
1019         // Jumpy is an exception
1020         if (pbad_c->kind == BAD_MONEY)
1021           break;
1022
1023         // Bounce off of other badguy if we land on top of him
1024         if (base.y + base.height < pbad_c->base.y + pbad_c->base.height)
1025         {
1026           Direction old_dir = dir;
1027           if (pbad_c->dir == LEFT)
1028             dir = RIGHT;
1029           else if (pbad_c->dir == RIGHT)
1030             dir = LEFT;
1031
1032           if (dir != old_dir)
1033             physic.inverse_velocity_x();
1034
1035           physic.set_velocity(fabs(physic.get_velocity_x()), 2);
1036
1037           break;
1038         }
1039         else if (base.y + base.height > pbad_c->base.y + pbad_c->base.height)
1040           break;
1041
1042         if (dir == LEFT)
1043           dir = RIGHT;
1044         else if (dir == RIGHT)
1045           dir = LEFT;
1046
1047         physic.inverse_velocity_x();
1048       }
1049       
1050       break;
1051
1052     case CO_PLAYER:
1053       Player* player = static_cast<Player*>(p_c_object);
1054       /* Get kicked if were flat */
1055       if (mode == FLAT && !dying)
1056       {
1057         play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
1058
1059         // Hit from left side
1060         if (player->base.x < base.x) {
1061           physic.set_velocity_x(5);
1062           dir = RIGHT;
1063         }
1064         // Hit from right side
1065         else {
1066           physic.set_velocity_x(-5);
1067           dir = LEFT;
1068         }
1069
1070         mode = KICK;
1071         set_sprite(img_laptop_flat_left, img_laptop_flat_right);
1072       }
1073       break;
1074
1075     }
1076 }
1077
1078 //---------------------------------------------------------------------------
1079
1080 void load_badguy_gfx()
1081 {
1082   img_bsod_squished_left = sprite_manager->load("bsod-squished-left");
1083   img_bsod_squished_right = sprite_manager->load("bsod-squished-right");
1084   img_bsod_falling_left = sprite_manager->load("bsod-falling-left");
1085   img_bsod_falling_right = sprite_manager->load("bsod-falling-right");
1086   img_laptop_flat_left = sprite_manager->load("laptop-flat-left");
1087   img_laptop_flat_right = sprite_manager->load("laptop-flat-right");
1088   img_laptop_falling_left = sprite_manager->load("laptop-falling-left");
1089   img_laptop_falling_right = sprite_manager->load("laptop-falling-right");
1090   img_bsod_left = sprite_manager->load("bsod-left");
1091   img_bsod_right = sprite_manager->load("bsod-right");
1092   img_laptop_left = sprite_manager->load("laptop-left");
1093   img_laptop_right = sprite_manager->load("laptop-right");
1094   img_jumpy_left_up = sprite_manager->load("jumpy-left-up");
1095   img_jumpy_left_down = sprite_manager->load("jumpy-left-down");
1096   img_jumpy_left_middle = sprite_manager->load("jumpy-left-middle");
1097   img_mrbomb_left = sprite_manager->load("mrbomb-left");
1098   img_mrbomb_right = sprite_manager->load("mrbomb-right");
1099   img_mrbomb_ticking_left = sprite_manager->load("mrbomb-ticking-left");
1100   img_mrbomb_ticking_right = sprite_manager->load("mrbomb-ticking-right");
1101   img_mrbomb_explosion = sprite_manager->load("mrbomb-explosion");
1102   img_stalactite = sprite_manager->load("stalactite");
1103   img_stalactite_broken = sprite_manager->load("stalactite-broken");
1104   img_flame = sprite_manager->load("flame");
1105   img_fish = sprite_manager->load("fish");
1106   img_fish_down = sprite_manager->load("fish-down");
1107   img_bouncingsnowball_left = sprite_manager->load("bouncingsnowball-left");
1108   img_bouncingsnowball_right = sprite_manager->load("bouncingsnowball-right");
1109   img_bouncingsnowball_squished = sprite_manager->load("bouncingsnowball-squished");
1110   img_flyingsnowball = sprite_manager->load("flyingsnowball");
1111   img_flyingsnowball_squished = sprite_manager->load("flyingsnowball-squished");
1112   img_spiky_left = sprite_manager->load("spiky-left");
1113   img_spiky_right = sprite_manager->load("spiky-right");
1114   img_snowball_left = sprite_manager->load("snowball-left");
1115   img_snowball_right = sprite_manager->load("snowball-right");
1116   img_snowball_squished_left = sprite_manager->load("snowball-squished-left");
1117   img_snowball_squished_right = sprite_manager->load("snowball-squished-right");
1118 #if 0
1119   /* (BSOD) */
1120   img_bsod_left[0] = new Surface(datadir + "/images/shared/bsod-left-0.png", USE_ALPHA);
1121
1122   img_bsod_left[1] = new Surface(datadir +
1123                                  "/images/shared/bsod-left-1.png",
1124                                  USE_ALPHA);
1125
1126   img_bsod_left[2] = new Surface(datadir +
1127                                  "/images/shared/bsod-left-2.png",
1128                                  USE_ALPHA);
1129
1130   img_bsod_left[3] = new Surface(datadir +
1131                                  "/images/shared/bsod-left-3.png",
1132                                  USE_ALPHA);
1133
1134   img_bsod_right[0] = new Surface(datadir +
1135                                   "/images/shared/bsod-right-0.png",
1136                                   USE_ALPHA);
1137
1138   img_bsod_right[1] = new Surface(datadir +
1139                                   "/images/shared/bsod-right-1.png",
1140                                   USE_ALPHA);
1141
1142   img_bsod_right[2] = new Surface(datadir +
1143                                   "/images/shared/bsod-right-2.png",
1144                                   USE_ALPHA);
1145
1146   img_bsod_right[3] = new Surface(datadir +
1147                                   "/images/shared/bsod-right-3.png",
1148                                   USE_ALPHA);
1149
1150   img_bsod_squished_left[0] = new Surface(datadir +
1151                                           "/images/shared/bsod-squished-left.png",
1152                                           USE_ALPHA);
1153
1154   img_bsod_squished_right[0] = new Surface(datadir +
1155                                            "/images/shared/bsod-squished-right.png",
1156                                            USE_ALPHA);
1157
1158   img_bsod_falling_left[0] = new Surface(datadir +
1159                                          "/images/shared/bsod-falling-left.png",
1160                                          USE_ALPHA);
1161
1162   img_bsod_falling_right[0] = new Surface(datadir +
1163                                           "/images/shared/bsod-falling-right.png",
1164                                           USE_ALPHA);
1165
1166
1167   /* (Laptop) */
1168
1169   img_laptop_left[0] = new Surface(datadir + "/images/shared/mriceblock-left-0.png", USE_ALPHA);
1170   img_laptop_left[1] = new Surface(datadir + "/images/shared/mriceblock-left-1.png", USE_ALPHA);
1171   img_laptop_left[2] = new Surface(datadir + "/images/shared/mriceblock-left-2.png", USE_ALPHA);
1172   img_laptop_left[3] = new Surface(datadir + "/images/shared/mriceblock-left-1.png", USE_ALPHA);
1173
1174   img_laptop_right[0] = new Surface(datadir + "/images/shared/mriceblock-right-0.png", USE_ALPHA);
1175   img_laptop_right[1] = new Surface(datadir + "/images/shared/mriceblock-right-1.png", USE_ALPHA);
1176   img_laptop_right[2] = new Surface(datadir + "/images/shared/mriceblock-right-2.png", USE_ALPHA);
1177   img_laptop_right[3] = new Surface(datadir + "/images/shared/mriceblock-right-1.png", USE_ALPHA);
1178   
1179   img_laptop_flat_left[0] = new Surface(
1180                                         datadir + "/images/shared/laptop-flat-left.png",
1181                                         USE_ALPHA);
1182
1183   img_laptop_flat_right[0] = new Surface(datadir +
1184                                          "/images/shared/laptop-flat-right.png",
1185                                          USE_ALPHA);
1186
1187   img_laptop_falling_left[0] = new Surface(datadir +
1188                                            "/images/shared/laptop-falling-left.png",
1189                                            USE_ALPHA);
1190
1191   img_laptop_falling_right[0] = new Surface(datadir +
1192                                             "/images/shared/laptop-falling-right.png",
1193                                             USE_ALPHA);
1194
1195
1196   /* (Money) */
1197   img_jumpy_left_up   = new Surface(datadir +
1198                                     "/images/shared/jumpy-left-up-0.png",
1199                                     USE_ALPHA);
1200   img_jumpy_left_down = new Surface(datadir +
1201                                     "/images/shared/jumpy-left-down-0.png",
1202                                     USE_ALPHA);
1203   img_jumpy_left_middle = new Surface(datadir +
1204                                       "/images/shared/jumpy-left-middle-0.png",
1205                                       USE_ALPHA);
1206
1207   /* Mr. Bomb */
1208   for(int i=0; i<4; ++i) {
1209     char num[4];
1210     snprintf(num, 4, "%d", i);
1211     img_mrbomb_left[i] = new Surface(
1212                                      datadir + "/images/shared/mrbomb-left-" + num + ".png", USE_ALPHA);
1213     img_mrbomb_right[i] = new Surface(
1214                                       datadir + "/images/shared/mrbomb-right-" + num + ".png", USE_ALPHA);
1215   }
1216   img_mrbomb_ticking_left[0] = new Surface(
1217                                            datadir + "/images/shared/mrbombx-left-0.png", USE_ALPHA);
1218   img_mrbomb_ticking_right[0] = new Surface(
1219                                             datadir + "/images/shared/mrbombx-right-0.png", USE_ALPHA);
1220   img_mrbomb_explosion[0] = new Surface(
1221                                         datadir + "/images/shared/mrbomb-explosion.png", USE_ALPHA);
1222
1223   /* stalactite */
1224   img_stalactite[0] = new Surface(
1225                                   datadir + "/images/shared/stalactite.png", USE_ALPHA);
1226   img_stalactite_broken[0] = new Surface(
1227                                          datadir + "/images/shared/stalactite-broken.png", USE_ALPHA);
1228
1229   /* flame */
1230   img_flame[0] = new Surface(
1231                              datadir + "/images/shared/flame-0.png", USE_ALPHA);
1232   img_flame[1] = new Surface(
1233                              datadir + "/images/shared/flame-1.png", USE_ALPHA);  
1234
1235   /* fish */
1236   img_fish[0] = new Surface(
1237                             datadir + "/images/shared/fish-left-0.png", USE_ALPHA);
1238   img_fish[1] = new Surface(
1239                             datadir + "/images/shared/fish-left-1.png", USE_ALPHA);
1240   img_fish_down[0] = new Surface(
1241                                  datadir + "/images/shared/fish-down-0.png", USE_ALPHA);
1242
1243   /* bouncing snowball */
1244   for(int i=0; i<6; ++i) {
1245     char num[4];
1246     snprintf(num, 4, "%d", i);
1247     img_bouncingsnowball_left[i] = new Surface(
1248                                                datadir + "/images/shared/bouncingsnowball-left-" + num + ".png",
1249                                                USE_ALPHA);
1250     img_bouncingsnowball_right[i] = new Surface(
1251                                                 datadir + "/images/shared/bouncingsnowball-right-" + num + ".png",
1252                                                 USE_ALPHA);
1253   } 
1254   img_bouncingsnowball_squished[0] = new Surface(
1255                                                  datadir + "/images/shared/bsod-squished-left.png", USE_ALPHA);
1256
1257   /* flying snowball */
1258   img_flyingsnowball[0] = new Surface(
1259                                       datadir + "/images/shared/flyingsnowball-left-0.png", USE_ALPHA);
1260   img_flyingsnowball[1] = new Surface(
1261                                       datadir + "/images/shared/flyingsnowball-left-1.png", USE_ALPHA);  
1262   img_flyingsnowball_squished[0] = new Surface(
1263                                                datadir + "/images/shared/bsod-squished-left.png", USE_ALPHA);
1264
1265   /* spiky */
1266   for(int i = 0; i < 3; ++i) {
1267     char num[4];
1268     snprintf(num, 4, "%d", i);
1269     img_spiky_left[i] = new Surface(                                
1270                                     datadir + "/images/shared/spiky-left-" + num + ".png",   
1271                                     USE_ALPHA);
1272     img_spiky_right[i] = new Surface(
1273                                      datadir + "/images/shared/spiky-right-" + num + ".png",
1274                                      USE_ALPHA);
1275   }
1276
1277   /** snowball */
1278   img_snowball_left[0] = new Surface(datadir + "/images/shared/snowball-left-0.png", USE_ALPHA);
1279   img_snowball_left[1] = new Surface(datadir + "/images/shared/snowball-left-1.png", USE_ALPHA);
1280   img_snowball_left[2] = new Surface(datadir + "/images/shared/snowball-left-2.png", USE_ALPHA);
1281   img_snowball_left[3] = new Surface(datadir + "/images/shared/snowball-left-1.png", USE_ALPHA);
1282
1283   img_snowball_right[0] = new Surface(datadir + "/images/shared/snowball-right-0.png", USE_ALPHA);
1284   img_snowball_right[1] = new Surface(datadir + "/images/shared/snowball-right-1.png", USE_ALPHA);
1285   img_snowball_right[2] = new Surface(datadir + "/images/shared/snowball-right-2.png", USE_ALPHA);
1286   img_snowball_right[3] = new Surface(datadir + "/images/shared/snowball-right-1.png", USE_ALPHA);
1287
1288   img_snowball_squished_left[0] = new Surface(
1289                                               datadir + "/images/shared/bsod-squished-left.png", USE_ALPHA);
1290   img_snowball_squished_right[0] = new Surface(
1291                                                datadir + "/images/shared/bsod-squished-right.png", USE_ALPHA);  
1292 #endif
1293 }
1294
1295 void free_badguy_gfx()
1296 {
1297 }
1298
1299 // EOF //