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