ac037079e1b0ef042ce8b873d429e96b669fbd88
[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     } else if(mode == BOMB_EXPLODE) {
485       remove_me();
486       return;
487     }
488   }
489
490   // move
491   physic.apply(frame_ratio, base.x, base.y);                 
492   collision_swept_object_map(&old_base,&base);
493 }
494
495 void
496 BadGuy::action_stalactite(float frame_ratio)
497 {
498   Player& tux = *World::current()->get_tux();
499
500   static const int SHAKETIME = 800;
501   static const int RANGE = 40;
502     
503   if(mode == NORMAL) {
504     // start shaking when tux is below the stalactite and at least 40 pixels
505     // near
506     if(tux.base.x + 32 > base.x - RANGE && tux.base.x < base.x + 32 + RANGE
507             && tux.base.y + tux.base.height > base.y) {
508       timer.start(SHAKETIME);
509       mode = STALACTITE_SHAKING;
510     }
511   } if(mode == STALACTITE_SHAKING) {
512     base.x = old_base.x + (rand() % 6) - 3; // TODO this could be done nicer...
513     if(!timer.check()) {
514       mode = STALACTITE_FALL;
515     }
516   } else if(mode == STALACTITE_FALL) {
517     fall();
518     /* Destroy if we collides with land */
519     if(issolid(base.x+base.width/2, base.y+base.height))
520     {
521       timer.start(2000);
522       dying = DYING_SQUISHED;
523       mode = FLAT;
524       set_sprite(img_stalactite_broken, img_stalactite_broken);
525     }
526   } else if(mode == FLAT) {
527     fall();
528   }
529
530   // move
531   physic.apply(frame_ratio, base.x, base.y);
532
533   if(dying == DYING_SQUISHED && !timer.check())
534     remove_me();
535 }
536
537 void
538 BadGuy::action_flame(float frame_ratio)
539 {
540     static const float radius = 100;
541     static const float speed = 0.02;
542     base.x = old_base.x + cos(base.ym) * radius;
543     base.y = old_base.y + sin(base.ym) * radius;
544
545     base.ym = fmodf(base.ym + frame_ratio * speed, 2*M_PI);
546 }
547
548 void
549 BadGuy::action_fish(float frame_ratio)
550 {
551   static const float JUMPV = 6;
552   static const int WAITTIME = 1000;
553     
554   // go in wait mode when back in water
555   if(dying == DYING_NOT && gettile(base.x, base.y+ base.height)->water
556         && physic.get_velocity_y() <= 0 && mode == NORMAL)
557     {
558       mode = FISH_WAIT;
559       set_sprite(0, 0);
560       physic.set_velocity(0, 0);
561       physic.enable_gravity(false);
562       timer.start(WAITTIME);
563     }
564   else if(mode == FISH_WAIT && !timer.check())
565     {
566       // jump again
567       set_sprite(img_fish, img_fish);
568       mode = NORMAL;
569       physic.set_velocity(0, JUMPV);
570       physic.enable_gravity(true);
571     }
572
573   physic.apply(frame_ratio, base.x, base.y);
574   if(dying == DYING_NOT)
575     collision_swept_object_map(&old_base, &base);
576
577   if(physic.get_velocity_y() < 0)
578     set_sprite(img_fish_down, img_fish_down);
579 }
580
581 void
582 BadGuy::action_bouncingsnowball(float frame_ratio)
583 {
584   static const float JUMPV = 4.5;
585     
586   fall();
587
588   // jump when on ground
589   if(dying == DYING_NOT && issolid(base.x, base.y+32))
590     {
591       physic.set_velocity_y(JUMPV);
592       physic.enable_gravity(true);
593     }                                                     
594   else
595     {
596       mode = NORMAL;
597     }
598
599   // check for right/left collisions
600   check_horizontal_bump();
601
602   physic.apply(frame_ratio, base.x, base.y);
603   if(dying == DYING_NOT)
604     collision_swept_object_map(&old_base, &base);
605
606   // Handle dying timer:
607   if (dying == DYING_SQUISHED && !timer.check())
608     {
609       /* Remove it if time's up: */
610       remove_me();
611       return;
612     }
613 }
614
615 void
616 BadGuy::action_flyingsnowball(float frame_ratio)
617 {
618   static const float FLYINGSPEED = 1;
619   static const int DIRCHANGETIME = 1000;
620     
621   // go into flyup mode if none specified yet
622   if(dying == DYING_NOT && mode == NORMAL) {
623     mode = FLY_UP;
624     physic.set_velocity_y(FLYINGSPEED);
625     timer.start(DIRCHANGETIME/2);
626   }
627
628   if(dying == DYING_NOT && !timer.check()) {
629     if(mode == FLY_UP) {
630       mode = FLY_DOWN;
631       physic.set_velocity_y(-FLYINGSPEED);
632     } else if(mode == FLY_DOWN) {
633       mode = FLY_UP;
634       physic.set_velocity_y(FLYINGSPEED);
635     }
636     timer.start(DIRCHANGETIME);
637   }
638
639   if(dying != DYING_NOT)
640     physic.enable_gravity(true);
641
642   physic.apply(frame_ratio, base.x, base.y);
643   if(dying == DYING_NOT || dying == DYING_SQUISHED)
644     collision_swept_object_map(&old_base, &base);
645
646   // Handle dying timer:
647   if (dying == DYING_SQUISHED && !timer.check())
648     {
649       /* Remove it if time's up: */
650       remove_me();
651       return;
652     }                                                          
653 }
654
655 void
656 BadGuy::action_spiky(float frame_ratio)
657 {
658   if (dying == DYING_NOT)
659     check_horizontal_bump();
660
661   fall();
662 #if 0
663   // jump when we're about to fall
664   if (physic.get_velocity_y() == 0 && 
665           !issolid(base.x+base.width/2, base.y + base.height)) {
666     physic.enable_gravity(true);
667     physic.set_velocity_y(2);
668   }
669 #endif
670
671   physic.apply(frame_ratio, base.x, base.y);
672   if (dying != DYING_FALLING)
673     collision_swept_object_map(&old_base,&base);   
674 }
675
676 void
677 BadGuy::action_snowball(float frame_ratio)
678 {
679   if (dying == DYING_NOT)
680     check_horizontal_bump();
681
682   fall();
683
684   physic.apply(frame_ratio, base.x, base.y);
685   if (dying != DYING_FALLING)
686     collision_swept_object_map(&old_base,&base);
687 }
688
689 void
690 BadGuy::action(float frame_ratio)
691 {
692   // Remove if it's far off the screen:
693   if (base.x < scroll_x - OFFSCREEN_DISTANCE)
694     {
695       remove_me();                                                
696       return;
697     }
698
699   // BadGuy fall below the ground
700   if (base.y > screen->h) {
701     remove_me();
702     return;
703   }
704
705   // Once it's on screen, it's activated!
706   if (base.x <= scroll_x + screen->w + OFFSCREEN_DISTANCE)
707     seen = true;
708
709   if(!seen)
710     return;
711
712   switch (kind)
713     {
714     case BAD_BSOD:
715       action_bsod(frame_ratio);
716       break;
717
718     case BAD_LAPTOP:
719       action_laptop(frame_ratio);
720       break;
721   
722     case BAD_MONEY:
723       action_money(frame_ratio);
724       break;
725
726     case BAD_MRBOMB:
727       action_mrbomb(frame_ratio);
728       break;
729     
730     case BAD_BOMB:
731       action_bomb(frame_ratio);
732       break;
733
734     case BAD_STALACTITE:
735       action_stalactite(frame_ratio);
736       break;
737
738     case BAD_FLAME:
739       action_flame(frame_ratio);
740       break;
741
742     case BAD_FISH:
743       action_fish(frame_ratio);
744       break;
745
746     case BAD_BOUNCINGSNOWBALL:
747       action_bouncingsnowball(frame_ratio);
748       break;
749
750     case BAD_FLYINGSNOWBALL:
751       action_flyingsnowball(frame_ratio);
752       break;
753
754     case BAD_SPIKY:
755       action_spiky(frame_ratio);
756       break;
757
758     case BAD_SNOWBALL:
759       action_snowball(frame_ratio);
760       break;
761     }
762 }
763
764 void
765 BadGuy::draw()
766 {
767   // Don't try to draw stuff that is outside of the screen
768   if(base.x <= scroll_x - base.width || base.x >= scroll_x + screen->w)
769     return;
770   
771   if(sprite_left == 0 || sprite_right == 0)
772     {
773       return;
774     }
775
776   Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
777   sprite->draw(base.x - scroll_x, base.y);
778
779   if (debug_mode)
780     fillrect(base.x - scroll_x, base.y, base.width, base.height, 75,0,75, 150);
781 }
782
783 void
784 BadGuy::set_sprite(Sprite* left, Sprite* right) 
785 {
786   if (1)
787     {
788       base.width = 32;
789       base.height = 32;
790     }
791   else
792     {
793       // FIXME: Using the image size for the physics and collision is
794       // a bad idea, since images should always overlap there physical
795       // representation
796       if(left != 0) {
797         if(base.width == 0 && base.height == 0) {
798           base.width  = left->get_width();
799           base.height = left->get_height();
800         } else if(base.width != left->get_width() || base.height != left->get_height()) {
801           base.x -= (left->get_width() - base.width) / 2;
802           base.y -= left->get_height() - base.height;
803           base.width = left->get_width();
804           base.height = left->get_height();
805           old_base = base;
806         }
807       } else {
808         base.width = base.height = 0;
809       }
810     }
811
812   animation_offset = 0;
813   sprite_left  = left;
814   sprite_right = right;
815 }
816
817 void
818 BadGuy::bump()
819 {
820   if(kind == BAD_BSOD || kind == BAD_LAPTOP || kind == BAD_MRBOMB
821       || kind == BAD_BOUNCINGSNOWBALL) {
822     kill_me();
823   }
824 }
825
826 void
827 BadGuy::make_player_jump(Player* player)
828 {
829   player->physic.set_velocity_y(2);
830   player->base.y = base.y - player->base.height - 2;
831 }
832
833 void
834 BadGuy::squish_me(Player* player)
835 {
836   make_player_jump(player);
837     
838   World::current()->add_score(base.x - scroll_x,
839                               base.y, 50 * player_status.score_multiplier);
840   play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
841   player_status.score_multiplier++;
842
843   dying = DYING_SQUISHED;
844   timer.start(2000);
845   physic.set_velocity(0, 0);
846 }
847
848 void
849 BadGuy::squish(Player* player)
850 {
851   if(kind == BAD_MRBOMB) {
852     // mrbomb transforms into a bomb now
853     World::current()->add_bad_guy(base.x, base.y, BAD_BOMB);
854     
855     make_player_jump(player);
856     World::current()->add_score(base.x - scroll_x, base.y, 50 * player_status.score_multiplier);
857     play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
858     player_status.score_multiplier++;
859     remove_me();
860     return;
861
862   } else if(kind == BAD_BSOD) {
863     squish_me(player);
864     set_sprite(img_bsod_squished_left, img_bsod_squished_right);
865     physic.set_velocity_x(0);
866     return;
867       
868   } else if (kind == BAD_LAPTOP) {
869     if (mode == NORMAL || mode == KICK)
870       {
871         /* Flatten! */
872         play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
873         mode = FLAT;
874         set_sprite(img_laptop_flat_left, img_laptop_flat_right);
875         physic.set_velocity_x(0);
876
877         timer.start(4000);
878       } else if (mode == FLAT) {
879         /* Kick! */
880         play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
881
882         if (player->base.x < base.x + (base.width/2)) {
883           physic.set_velocity_x(5);
884           dir = RIGHT;
885         } else {
886           physic.set_velocity_x(-5);
887           dir = LEFT;
888         }
889
890         mode = KICK;
891         set_sprite(img_laptop_flat_left, img_laptop_flat_right);
892       }
893
894     make_player_jump(player);
895               
896     World::current()->add_score(base.x - scroll_x, base.y, 25 * player_status.score_multiplier);
897     player_status.score_multiplier++;
898     return;
899   } else if(kind == BAD_FISH) {
900     // fish can only be killed when falling down
901     if(physic.get_velocity_y() >= 0)
902       return;
903       
904     make_player_jump(player);
905               
906     World::current()->add_score(base.x - scroll_x, base.y, 25 * player_status.score_multiplier);
907     player_status.score_multiplier++;
908      
909     // simply remove the fish...
910     remove_me();
911     return;
912   } else if(kind == BAD_BOUNCINGSNOWBALL) {
913     squish_me(player);
914     set_sprite(img_bouncingsnowball_squished,img_bouncingsnowball_squished);
915     return;
916   } else if(kind == BAD_FLYINGSNOWBALL) {
917     squish_me(player);
918     set_sprite(img_flyingsnowball_squished,img_flyingsnowball_squished);
919     return;
920   } else if(kind == BAD_SNOWBALL) {
921     squish_me(player);
922     set_sprite(img_snowball_squished_left, img_snowball_squished_right);
923     return;
924   }
925 }
926
927 void
928 BadGuy::kill_me()
929 {
930   if(kind == BAD_BOMB || kind == BAD_STALACTITE || kind == BAD_FLAME)
931     return;
932
933   dying = DYING_FALLING;
934   if(kind == BAD_LAPTOP)
935     set_sprite(img_laptop_falling_left, img_laptop_falling_right);
936   else if(kind == BAD_BSOD)
937     set_sprite(img_bsod_falling_left, img_bsod_falling_right);
938   
939   physic.enable_gravity(true);
940   physic.set_velocity_y(0);
941
942   /* Gain some points: */
943   if (kind == BAD_BSOD)
944     World::current()->add_score(base.x - scroll_x, base.y,
945                     50 * player_status.score_multiplier);
946   else 
947     World::current()->add_score(base.x - scroll_x, base.y,                                 
948                     25 * player_status.score_multiplier);
949
950   /* Play death sound: */
951   play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
952 }
953
954 void
955 BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
956 {
957   BadGuy* pbad_c    = NULL;
958
959   if(type == COLLISION_BUMP) {
960     bump();
961     return;
962   }
963
964   if(type == COLLISION_SQUISH) {
965     Player* player = static_cast<Player*>(p_c_object);
966     squish(player);
967     return;
968   }
969
970   /* COLLISION_NORMAL */
971   switch (c_object)
972     {
973     case CO_BULLET:
974       kill_me();
975       break;
976
977     case CO_BADGUY:
978       pbad_c = (BadGuy*) p_c_object;
979
980       /* If we're a kicked mriceblock, kill any badguys we hit */
981       if(kind == BAD_LAPTOP && mode == KICK &&
982             pbad_c->kind != BAD_FLAME && pbad_c->kind != BAD_BOMB)
983         {
984           pbad_c->kill_me();
985         }
986
987       /* Kill badguys that run into exploding bomb */
988       else if (kind == BAD_BOMB && dying == DYING_NOT)
989       {
990         if (pbad_c->kind == BAD_MRBOMB)
991         {
992           // FIXME: this is where other MrBombs *should* explode istead of dying
993           pbad_c->kill_me(); 
994         }
995         else if (pbad_c->kind != BAD_BOMB)
996         {
997           pbad_c->kill_me();
998         }
999       }
1000
1001       /* Kill any badguys that get hit by stalactite */
1002       else if (kind == BAD_STALACTITE && dying == DYING_NOT)
1003       {
1004         pbad_c->kill_me();
1005       }
1006
1007       /* When enemies run into eachother, make them change directions */
1008       else
1009       {
1010         // Jumpy is an exception
1011         if (pbad_c->kind == BAD_MONEY)
1012           break;
1013
1014         // Bounce off of other badguy if we land on top of him
1015         if (base.y + base.height < pbad_c->base.y + pbad_c->base.height)
1016         {
1017           Direction old_dir = dir;
1018           if (pbad_c->dir == LEFT)
1019             dir = RIGHT;
1020           else if (pbad_c->dir == RIGHT)
1021             dir = LEFT;
1022
1023           if (dir != old_dir)
1024             physic.inverse_velocity_x();
1025
1026           physic.set_velocity(fabs(physic.get_velocity_x()), 2);
1027
1028           break;
1029         }
1030         else if (base.y + base.height > pbad_c->base.y + pbad_c->base.height)
1031           break;
1032
1033         if (dir == LEFT)
1034           dir = RIGHT;
1035         else if (dir == RIGHT)
1036           dir = LEFT;
1037
1038         physic.inverse_velocity_x();
1039       }
1040       
1041       break;
1042
1043     case CO_PLAYER:
1044       Player* player = static_cast<Player*>(p_c_object);
1045       /* Get kicked if were flat */
1046       if (mode == FLAT && !dying)
1047       {
1048         play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
1049
1050         // Hit from left side
1051         if (player->base.x < base.x) {
1052           physic.set_velocity_x(5);
1053           dir = RIGHT;
1054         }
1055         // Hit from right side
1056         else {
1057           physic.set_velocity_x(-5);
1058           dir = LEFT;
1059         }
1060
1061         mode = KICK;
1062         set_sprite(img_laptop_flat_left, img_laptop_flat_right);
1063       }
1064       break;
1065
1066     }
1067 }
1068
1069 //---------------------------------------------------------------------------
1070
1071 void load_badguy_gfx()
1072 {
1073   img_bsod_squished_left = sprite_manager->load("bsod-squished-left");
1074   img_bsod_squished_right = sprite_manager->load("bsod-squished-right");
1075   img_bsod_falling_left = sprite_manager->load("bsod-falling-left");
1076   img_bsod_falling_right = sprite_manager->load("bsod-falling-right");
1077   img_laptop_flat_left = sprite_manager->load("laptop-flat-left");
1078   img_laptop_flat_right = sprite_manager->load("laptop-flat-right");
1079   img_laptop_falling_left = sprite_manager->load("laptop-falling-left");
1080   img_laptop_falling_right = sprite_manager->load("laptop-falling-right");
1081   img_bsod_left = sprite_manager->load("bsod-left");
1082   img_bsod_right = sprite_manager->load("bsod-right");
1083   img_laptop_left = sprite_manager->load("laptop-left");
1084   img_laptop_right = sprite_manager->load("laptop-right");
1085   img_jumpy_left_up = sprite_manager->load("jumpy-left-up");
1086   img_jumpy_left_down = sprite_manager->load("jumpy-left-down");
1087   img_jumpy_left_middle = sprite_manager->load("jumpy-left-middle");
1088   img_mrbomb_left = sprite_manager->load("mrbomb-left");
1089   img_mrbomb_right = sprite_manager->load("mrbomb-right");
1090   img_mrbomb_ticking_left = sprite_manager->load("mrbomb-ticking-left");
1091   img_mrbomb_ticking_right = sprite_manager->load("mrbomb-ticking-right");
1092   img_mrbomb_explosion = sprite_manager->load("mrbomb-explosion");
1093   img_stalactite = sprite_manager->load("stalactite");
1094   img_stalactite_broken = sprite_manager->load("stalactite-broken");
1095   img_flame = sprite_manager->load("flame");
1096   img_fish = sprite_manager->load("fish");
1097   img_fish_down = sprite_manager->load("fish-down");
1098   img_bouncingsnowball_left = sprite_manager->load("bouncingsnowball-left");
1099   img_bouncingsnowball_right = sprite_manager->load("bouncingsnowball-right");
1100   img_bouncingsnowball_squished = sprite_manager->load("bouncingsnowball-squished");
1101   img_flyingsnowball = sprite_manager->load("flyingsnowball");
1102   img_flyingsnowball_squished = sprite_manager->load("flyingsnowball-squished");
1103   img_spiky_left = sprite_manager->load("spiky-left");
1104   img_spiky_right = sprite_manager->load("spiky-right");
1105   img_snowball_left = sprite_manager->load("snowball-left");
1106   img_snowball_right = sprite_manager->load("snowball-right");
1107   img_snowball_squished_left = sprite_manager->load("snowball-squished-left");
1108   img_snowball_squished_right = sprite_manager->load("snowball-squished-right");
1109 #if 0
1110   /* (BSOD) */
1111   img_bsod_left[0] = new Surface(datadir + "/images/shared/bsod-left-0.png", USE_ALPHA);
1112
1113   img_bsod_left[1] = new Surface(datadir +
1114                                  "/images/shared/bsod-left-1.png",
1115                                  USE_ALPHA);
1116
1117   img_bsod_left[2] = new Surface(datadir +
1118                                  "/images/shared/bsod-left-2.png",
1119                                  USE_ALPHA);
1120
1121   img_bsod_left[3] = new Surface(datadir +
1122                                  "/images/shared/bsod-left-3.png",
1123                                  USE_ALPHA);
1124
1125   img_bsod_right[0] = new Surface(datadir +
1126                                   "/images/shared/bsod-right-0.png",
1127                                   USE_ALPHA);
1128
1129   img_bsod_right[1] = new Surface(datadir +
1130                                   "/images/shared/bsod-right-1.png",
1131                                   USE_ALPHA);
1132
1133   img_bsod_right[2] = new Surface(datadir +
1134                                   "/images/shared/bsod-right-2.png",
1135                                   USE_ALPHA);
1136
1137   img_bsod_right[3] = new Surface(datadir +
1138                                   "/images/shared/bsod-right-3.png",
1139                                   USE_ALPHA);
1140
1141   img_bsod_squished_left[0] = new Surface(datadir +
1142                                           "/images/shared/bsod-squished-left.png",
1143                                           USE_ALPHA);
1144
1145   img_bsod_squished_right[0] = new Surface(datadir +
1146                                            "/images/shared/bsod-squished-right.png",
1147                                            USE_ALPHA);
1148
1149   img_bsod_falling_left[0] = new Surface(datadir +
1150                                          "/images/shared/bsod-falling-left.png",
1151                                          USE_ALPHA);
1152
1153   img_bsod_falling_right[0] = new Surface(datadir +
1154                                           "/images/shared/bsod-falling-right.png",
1155                                           USE_ALPHA);
1156
1157
1158   /* (Laptop) */
1159
1160   img_laptop_left[0] = new Surface(datadir + "/images/shared/mriceblock-left-0.png", USE_ALPHA);
1161   img_laptop_left[1] = new Surface(datadir + "/images/shared/mriceblock-left-1.png", USE_ALPHA);
1162   img_laptop_left[2] = new Surface(datadir + "/images/shared/mriceblock-left-2.png", USE_ALPHA);
1163   img_laptop_left[3] = new Surface(datadir + "/images/shared/mriceblock-left-1.png", USE_ALPHA);
1164
1165   img_laptop_right[0] = new Surface(datadir + "/images/shared/mriceblock-right-0.png", USE_ALPHA);
1166   img_laptop_right[1] = new Surface(datadir + "/images/shared/mriceblock-right-1.png", USE_ALPHA);
1167   img_laptop_right[2] = new Surface(datadir + "/images/shared/mriceblock-right-2.png", USE_ALPHA);
1168   img_laptop_right[3] = new Surface(datadir + "/images/shared/mriceblock-right-1.png", USE_ALPHA);
1169   
1170   img_laptop_flat_left[0] = new Surface(
1171                                         datadir + "/images/shared/laptop-flat-left.png",
1172                                         USE_ALPHA);
1173
1174   img_laptop_flat_right[0] = new Surface(datadir +
1175                                          "/images/shared/laptop-flat-right.png",
1176                                          USE_ALPHA);
1177
1178   img_laptop_falling_left[0] = new Surface(datadir +
1179                                            "/images/shared/laptop-falling-left.png",
1180                                            USE_ALPHA);
1181
1182   img_laptop_falling_right[0] = new Surface(datadir +
1183                                             "/images/shared/laptop-falling-right.png",
1184                                             USE_ALPHA);
1185
1186
1187   /* (Money) */
1188   img_jumpy_left_up   = new Surface(datadir +
1189                                     "/images/shared/jumpy-left-up-0.png",
1190                                     USE_ALPHA);
1191   img_jumpy_left_down = new Surface(datadir +
1192                                     "/images/shared/jumpy-left-down-0.png",
1193                                     USE_ALPHA);
1194   img_jumpy_left_middle = new Surface(datadir +
1195                                       "/images/shared/jumpy-left-middle-0.png",
1196                                       USE_ALPHA);
1197
1198   /* Mr. Bomb */
1199   for(int i=0; i<4; ++i) {
1200     char num[4];
1201     snprintf(num, 4, "%d", i);
1202     img_mrbomb_left[i] = new Surface(
1203                                      datadir + "/images/shared/mrbomb-left-" + num + ".png", USE_ALPHA);
1204     img_mrbomb_right[i] = new Surface(
1205                                       datadir + "/images/shared/mrbomb-right-" + num + ".png", USE_ALPHA);
1206   }
1207   img_mrbomb_ticking_left[0] = new Surface(
1208                                            datadir + "/images/shared/mrbombx-left-0.png", USE_ALPHA);
1209   img_mrbomb_ticking_right[0] = new Surface(
1210                                             datadir + "/images/shared/mrbombx-right-0.png", USE_ALPHA);
1211   img_mrbomb_explosion[0] = new Surface(
1212                                         datadir + "/images/shared/mrbomb-explosion.png", USE_ALPHA);
1213
1214   /* stalactite */
1215   img_stalactite[0] = new Surface(
1216                                   datadir + "/images/shared/stalactite.png", USE_ALPHA);
1217   img_stalactite_broken[0] = new Surface(
1218                                          datadir + "/images/shared/stalactite-broken.png", USE_ALPHA);
1219
1220   /* flame */
1221   img_flame[0] = new Surface(
1222                              datadir + "/images/shared/flame-0.png", USE_ALPHA);
1223   img_flame[1] = new Surface(
1224                              datadir + "/images/shared/flame-1.png", USE_ALPHA);  
1225
1226   /* fish */
1227   img_fish[0] = new Surface(
1228                             datadir + "/images/shared/fish-left-0.png", USE_ALPHA);
1229   img_fish[1] = new Surface(
1230                             datadir + "/images/shared/fish-left-1.png", USE_ALPHA);
1231   img_fish_down[0] = new Surface(
1232                                  datadir + "/images/shared/fish-down-0.png", USE_ALPHA);
1233
1234   /* bouncing snowball */
1235   for(int i=0; i<6; ++i) {
1236     char num[4];
1237     snprintf(num, 4, "%d", i);
1238     img_bouncingsnowball_left[i] = new Surface(
1239                                                datadir + "/images/shared/bouncingsnowball-left-" + num + ".png",
1240                                                USE_ALPHA);
1241     img_bouncingsnowball_right[i] = new Surface(
1242                                                 datadir + "/images/shared/bouncingsnowball-right-" + num + ".png",
1243                                                 USE_ALPHA);
1244   } 
1245   img_bouncingsnowball_squished[0] = new Surface(
1246                                                  datadir + "/images/shared/bsod-squished-left.png", USE_ALPHA);
1247
1248   /* flying snowball */
1249   img_flyingsnowball[0] = new Surface(
1250                                       datadir + "/images/shared/flyingsnowball-left-0.png", USE_ALPHA);
1251   img_flyingsnowball[1] = new Surface(
1252                                       datadir + "/images/shared/flyingsnowball-left-1.png", USE_ALPHA);  
1253   img_flyingsnowball_squished[0] = new Surface(
1254                                                datadir + "/images/shared/bsod-squished-left.png", USE_ALPHA);
1255
1256   /* spiky */
1257   for(int i = 0; i < 3; ++i) {
1258     char num[4];
1259     snprintf(num, 4, "%d", i);
1260     img_spiky_left[i] = new Surface(                                
1261                                     datadir + "/images/shared/spiky-left-" + num + ".png",   
1262                                     USE_ALPHA);
1263     img_spiky_right[i] = new Surface(
1264                                      datadir + "/images/shared/spiky-right-" + num + ".png",
1265                                      USE_ALPHA);
1266   }
1267
1268   /** snowball */
1269   img_snowball_left[0] = new Surface(datadir + "/images/shared/snowball-left-0.png", USE_ALPHA);
1270   img_snowball_left[1] = new Surface(datadir + "/images/shared/snowball-left-1.png", USE_ALPHA);
1271   img_snowball_left[2] = new Surface(datadir + "/images/shared/snowball-left-2.png", USE_ALPHA);
1272   img_snowball_left[3] = new Surface(datadir + "/images/shared/snowball-left-1.png", USE_ALPHA);
1273
1274   img_snowball_right[0] = new Surface(datadir + "/images/shared/snowball-right-0.png", USE_ALPHA);
1275   img_snowball_right[1] = new Surface(datadir + "/images/shared/snowball-right-1.png", USE_ALPHA);
1276   img_snowball_right[2] = new Surface(datadir + "/images/shared/snowball-right-2.png", USE_ALPHA);
1277   img_snowball_right[3] = new Surface(datadir + "/images/shared/snowball-right-1.png", USE_ALPHA);
1278
1279   img_snowball_squished_left[0] = new Surface(
1280                                               datadir + "/images/shared/bsod-squished-left.png", USE_ALPHA);
1281   img_snowball_squished_right[0] = new Surface(
1282                                                datadir + "/images/shared/bsod-squished-right.png", USE_ALPHA);  
1283 #endif
1284 }
1285
1286 void free_badguy_gfx()
1287 {
1288 }
1289
1290 // EOF //