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