464938697f0bdf530496cfaf576cedd5cd3c2dd5
[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_mriceblock_flat_left;
41 Sprite* img_mriceblock_flat_right;
42 Sprite* img_mriceblock_falling_left;
43 Sprite* img_mriceblock_falling_right;
44 Sprite* img_bsod_left;
45 Sprite* img_bsod_right;
46 Sprite* img_mriceblock_left;
47 Sprite* img_mriceblock_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" || str == "jumpy") // was money in old maps
78     return BAD_JUMPY;
79   else if (str == "laptop" || str == "mriceblock") // was laptop in old maps
80     return BAD_MRICEBLOCK;
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_JUMPY:
111       return "jumpy";
112       break;
113     case BAD_MRICEBLOCK:
114       return "mriceblock";
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), squishcount(0)
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_MRICEBLOCK) {
177     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
178     set_sprite(img_mriceblock_left, img_mriceblock_right);
179   } else if(kind == BAD_JUMPY) {
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_mriceblock(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 iceblock */
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           tux.kick_timer.start(KICKING_TIME);
295           set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
296           physic.set_velocity_x((dir == LEFT) ? -3.5 : 3.5);
297           play_sound(sounds[SND_KICK],SOUND_CENTER_SPEAKER);
298         }
299     }
300
301   if (!dying)
302     {
303       int changed = dir;
304       check_horizontal_bump();
305       if(mode == KICK && changed != dir)
306         {
307           /* handle stereo sound (number 10 should be tweaked...)*/
308           if (base.x < scroll_x + screen->w/2 - 10)
309             play_sound(sounds[SND_RICOCHET], SOUND_LEFT_SPEAKER);
310           else if (base.x > scroll_x + screen->w/2 + 10)
311             play_sound(sounds[SND_RICOCHET], SOUND_RIGHT_SPEAKER);
312           else
313             play_sound(sounds[SND_RICOCHET], SOUND_CENTER_SPEAKER);
314         }
315     }
316
317   /* Handle mode timer: */
318   if (mode == FLAT)
319     {
320       if(!timer.check())
321         {
322           mode = NORMAL;
323           set_sprite(img_mriceblock_left, img_mriceblock_right);
324           physic.set_velocity( (dir == LEFT) ? -.8 : .8, 0);
325         }
326     }
327 }
328
329 void
330 BadGuy::check_horizontal_bump(bool checkcliff)
331 {
332     float halfheight = base.height / 2;
333     if (dir == LEFT && issolid( base.x, (int) base.y + halfheight))
334     {
335         dir = RIGHT;
336         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
337         return;
338     }
339     if (dir == RIGHT && issolid( base.x + base.width, (int)base.y + halfheight))
340     {
341         dir = LEFT;
342         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
343         return;
344     }
345
346     // don't check for cliffs when we're falling
347     if(!checkcliff)
348         return;
349     if(!issolid(base.x + base.width/2, base.y + base.height))
350         return;
351     
352     if(dir == LEFT && !issolid(base.x, (int) base.y + base.height + halfheight))
353     {
354         dir = RIGHT;
355         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
356         return;
357     }
358     if(dir == RIGHT && !issolid(base.x + base.width,
359                 (int) base.y + base.height + halfheight))
360     {
361         dir = LEFT;
362         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
363         return;
364     }
365 }
366
367 void
368 BadGuy::fall()
369 {
370   /* Fall if we get off the ground: */
371   if (dying != DYING_FALLING)
372     {
373       if (!issolid(base.x+base.width/2, base.y + base.height))
374         {
375           // not solid below us? enable gravity
376           physic.enable_gravity(true);
377         }
378       else
379         {
380           /* Land: */
381           if (physic.get_velocity_y() < 0)
382             {
383               base.y = int((base.y + base.height)/32) * 32 - base.height;
384               physic.set_velocity_y(0);
385             }
386           // no gravity anymore please
387           physic.enable_gravity(false);
388
389           if (stay_on_platform && mode == NORMAL)
390             {
391               if (!issolid(base.x + ((dir == LEFT) ? 0 : base.width),
392                            base.y + base.height))
393                 {
394                   physic.set_velocity_x(-physic.get_velocity_x());
395                   if (dir == LEFT)
396                     dir = RIGHT;
397                   else
398                     dir = LEFT;
399                 }
400             }
401         }
402     }
403   else
404     {
405       physic.enable_gravity(true);
406     }
407 }
408
409 void
410 BadGuy::remove_me()
411 {
412   removable = true;
413 }
414
415 void
416 BadGuy::action_jumpy(float frame_ratio)
417 {
418   if (fabsf(physic.get_velocity_y()) < 2.5f)
419     set_sprite(img_jumpy_left_middle, img_jumpy_left_middle);
420   else if (physic.get_velocity_y() < 0)
421     set_sprite(img_jumpy_left_up, img_jumpy_left_up);
422   else 
423     set_sprite(img_jumpy_left_down, img_jumpy_left_down);
424
425   Player& tux = *World::current()->get_tux();
426
427   static const float JUMPV = 6;
428     
429   fall();
430   // jump when on ground
431   if(dying == DYING_NOT && issolid(base.x, base.y+32))
432     {
433       physic.set_velocity_y(JUMPV);
434       physic.enable_gravity(true);
435
436       mode = JUMPY_JUMP;
437     }
438   else if(mode == JUMPY_JUMP)
439     {
440       mode = NORMAL;
441     }
442
443   // set direction based on tux
444   if(tux.base.x > base.x)
445     dir = RIGHT;
446   else
447     dir = LEFT;
448
449   // move
450   physic.apply(frame_ratio, base.x, base.y);
451   if(dying == DYING_NOT)
452     collision_swept_object_map(&old_base, &base);
453 }
454
455 void
456 BadGuy::action_mrbomb(float frame_ratio)
457 {
458   if (dying == DYING_NOT)
459     check_horizontal_bump(true);
460
461   fall();
462
463   physic.apply(frame_ratio, base.x, base.y);
464   if (dying != DYING_FALLING)
465     collision_swept_object_map(&old_base,&base); 
466 }
467
468 void
469 BadGuy::action_bomb(float frame_ratio)
470 {
471   static const int TICKINGTIME = 1000;
472   static const int EXPLODETIME = 1000;
473     
474   fall();
475
476   if(mode == NORMAL) {
477     mode = BOMB_TICKING;
478     timer.start(TICKINGTIME);
479   } else if(!timer.check()) {
480     if(mode == BOMB_TICKING) {
481       mode = BOMB_EXPLODE;
482       set_sprite(img_mrbomb_explosion, img_mrbomb_explosion);
483       dying = DYING_NOT; // now the bomb hurts
484       timer.start(EXPLODETIME);
485
486       /* play explosion sound */  // FIXME: is the stereo all right? maybe we should use player cordinates...
487       if (base.x < scroll_x + screen->w/2 - 10)
488         play_sound(sounds[SND_EXPLODE], SOUND_LEFT_SPEAKER);
489       else if (base.x > scroll_x + screen->w/2 + 10)
490         play_sound(sounds[SND_EXPLODE], SOUND_RIGHT_SPEAKER);
491       else
492         play_sound(sounds[SND_EXPLODE], SOUND_CENTER_SPEAKER);
493
494     } else if(mode == BOMB_EXPLODE) {
495       remove_me();
496       return;
497     }
498   }
499
500   // move
501   physic.apply(frame_ratio, base.x, base.y);                 
502   collision_swept_object_map(&old_base,&base);
503 }
504
505 void
506 BadGuy::action_stalactite(float frame_ratio)
507 {
508   Player& tux = *World::current()->get_tux();
509
510   static const int SHAKETIME = 800;
511   static const int RANGE = 40;
512     
513   if(mode == NORMAL) {
514     // start shaking when tux is below the stalactite and at least 40 pixels
515     // near
516     if(tux.base.x + 32 > base.x - RANGE && tux.base.x < base.x + 32 + RANGE
517             && tux.base.y + tux.base.height > base.y) {
518       timer.start(SHAKETIME);
519       mode = STALACTITE_SHAKING;
520     }
521   } if(mode == STALACTITE_SHAKING) {
522     base.x = old_base.x + (rand() % 6) - 3; // TODO this could be done nicer...
523     if(!timer.check()) {
524       mode = STALACTITE_FALL;
525     }
526   } else if(mode == STALACTITE_FALL) {
527     fall();
528     /* Destroy if we collides with land */
529     if(issolid(base.x+base.width/2, base.y+base.height))
530     {
531       timer.start(2000);
532       dying = DYING_SQUISHED;
533       mode = FLAT;
534       set_sprite(img_stalactite_broken, img_stalactite_broken);
535     }
536   } else if(mode == FLAT) {
537     fall();
538   }
539
540   // move
541   physic.apply(frame_ratio, base.x, base.y);
542
543   if(dying == DYING_SQUISHED && !timer.check())
544     remove_me();
545 }
546
547 void
548 BadGuy::action_flame(float frame_ratio)
549 {
550     static const float radius = 100;
551     static const float speed = 0.02;
552     base.x = old_base.x + cos(base.ym) * radius;
553     base.y = old_base.y + sin(base.ym) * radius;
554
555     base.ym = fmodf(base.ym + frame_ratio * speed, 2*M_PI);
556 }
557
558 void
559 BadGuy::action_fish(float frame_ratio)
560 {
561   static const float JUMPV = 6;
562   static const int WAITTIME = 1000;
563     
564   // go in wait mode when back in water
565   if(dying == DYING_NOT && gettile(base.x, base.y+ base.height)->water
566         && physic.get_velocity_y() <= 0 && mode == NORMAL)
567     {
568       mode = FISH_WAIT;
569       set_sprite(0, 0);
570       physic.set_velocity(0, 0);
571       physic.enable_gravity(false);
572       timer.start(WAITTIME);
573     }
574   else if(mode == FISH_WAIT && !timer.check())
575     {
576       // jump again
577       set_sprite(img_fish, img_fish);
578       mode = NORMAL;
579       physic.set_velocity(0, JUMPV);
580       physic.enable_gravity(true);
581     }
582
583   physic.apply(frame_ratio, base.x, base.y);
584   if(dying == DYING_NOT)
585     collision_swept_object_map(&old_base, &base);
586
587   if(physic.get_velocity_y() < 0)
588     set_sprite(img_fish_down, img_fish_down);
589 }
590
591 void
592 BadGuy::action_bouncingsnowball(float frame_ratio)
593 {
594   static const float JUMPV = 4.5;
595     
596   fall();
597
598   // jump when on ground
599   if(dying == DYING_NOT && issolid(base.x, base.y+32))
600     {
601       physic.set_velocity_y(JUMPV);
602       physic.enable_gravity(true);
603     }                                                     
604   else
605     {
606       mode = NORMAL;
607     }
608
609   // check for right/left collisions
610   check_horizontal_bump();
611
612   physic.apply(frame_ratio, base.x, base.y);
613   if(dying == DYING_NOT)
614     collision_swept_object_map(&old_base, &base);
615
616   // Handle dying timer:
617   if (dying == DYING_SQUISHED && !timer.check())
618     {
619       /* Remove it if time's up: */
620       remove_me();
621       return;
622     }
623 }
624
625 void
626 BadGuy::action_flyingsnowball(float frame_ratio)
627 {
628   static const float FLYINGSPEED = 1;
629   static const int DIRCHANGETIME = 1000;
630     
631   // go into flyup mode if none specified yet
632   if(dying == DYING_NOT && mode == NORMAL) {
633     mode = FLY_UP;
634     physic.set_velocity_y(FLYINGSPEED);
635     timer.start(DIRCHANGETIME/2);
636   }
637
638   if(dying == DYING_NOT && !timer.check()) {
639     if(mode == FLY_UP) {
640       mode = FLY_DOWN;
641       physic.set_velocity_y(-FLYINGSPEED);
642     } else if(mode == FLY_DOWN) {
643       mode = FLY_UP;
644       physic.set_velocity_y(FLYINGSPEED);
645     }
646     timer.start(DIRCHANGETIME);
647   }
648
649   if(dying != DYING_NOT)
650     physic.enable_gravity(true);
651
652   physic.apply(frame_ratio, base.x, base.y);
653   if(dying == DYING_NOT || dying == DYING_SQUISHED)
654     collision_swept_object_map(&old_base, &base);
655
656   // Handle dying timer:
657   if (dying == DYING_SQUISHED && !timer.check())
658     {
659       /* Remove it if time's up: */
660       remove_me();
661       return;
662     }                                                          
663 }
664
665 void
666 BadGuy::action_spiky(float frame_ratio)
667 {
668   if (dying == DYING_NOT)
669     check_horizontal_bump();
670
671   fall();
672 #if 0
673   // jump when we're about to fall
674   if (physic.get_velocity_y() == 0 && 
675           !issolid(base.x+base.width/2, base.y + base.height)) {
676     physic.enable_gravity(true);
677     physic.set_velocity_y(2);
678   }
679 #endif
680
681   physic.apply(frame_ratio, base.x, base.y);
682   if (dying != DYING_FALLING)
683     collision_swept_object_map(&old_base,&base);   
684 }
685
686 void
687 BadGuy::action_snowball(float frame_ratio)
688 {
689   if (dying == DYING_NOT)
690     check_horizontal_bump();
691
692   fall();
693
694   physic.apply(frame_ratio, base.x, base.y);
695   if (dying != DYING_FALLING)
696     collision_swept_object_map(&old_base,&base);
697 }
698
699 void
700 BadGuy::action(float frame_ratio)
701 {
702   // Remove if it's far off the screen:
703   if (base.x < scroll_x - OFFSCREEN_DISTANCE)
704     {
705       remove_me();                                                
706       return;
707     }
708
709   // BadGuy fall below the ground
710   if (base.y > screen->h) {
711     remove_me();
712     return;
713   }
714
715   // Once it's on screen, it's activated!
716   if (base.x <= scroll_x + screen->w + OFFSCREEN_DISTANCE)
717     seen = true;
718
719   if(!seen)
720     return;
721
722   switch (kind)
723     {
724     case BAD_BSOD:
725       action_bsod(frame_ratio);
726       break;
727
728     case BAD_MRICEBLOCK:
729       action_mriceblock(frame_ratio);
730       break;
731   
732     case BAD_JUMPY:
733       action_jumpy(frame_ratio);
734       break;
735
736     case BAD_MRBOMB:
737       action_mrbomb(frame_ratio);
738       break;
739     
740     case BAD_BOMB:
741       action_bomb(frame_ratio);
742       break;
743
744     case BAD_STALACTITE:
745       action_stalactite(frame_ratio);
746       break;
747
748     case BAD_FLAME:
749       action_flame(frame_ratio);
750       break;
751
752     case BAD_FISH:
753       action_fish(frame_ratio);
754       break;
755
756     case BAD_BOUNCINGSNOWBALL:
757       action_bouncingsnowball(frame_ratio);
758       break;
759
760     case BAD_FLYINGSNOWBALL:
761       action_flyingsnowball(frame_ratio);
762       break;
763
764     case BAD_SPIKY:
765       action_spiky(frame_ratio);
766       break;
767
768     case BAD_SNOWBALL:
769       action_snowball(frame_ratio);
770       break;
771     }
772 }
773
774 void
775 BadGuy::draw()
776 {
777   // Don't try to draw stuff that is outside of the screen
778   if(base.x <= scroll_x - base.width || base.x >= scroll_x + screen->w)
779     return;
780   
781   if(sprite_left == 0 || sprite_right == 0)
782     {
783       return;
784     }
785
786   Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
787   sprite->draw(base.x - scroll_x, base.y);
788
789   if (debug_mode)
790     fillrect(base.x - scroll_x, base.y, base.width, base.height, 75,0,75, 150);
791 }
792
793 void
794 BadGuy::set_sprite(Sprite* left, Sprite* right) 
795 {
796   if (1)
797     {
798       base.width = 32;
799       base.height = 32;
800     }
801   else
802     {
803       // FIXME: Using the image size for the physics and collision is
804       // a bad idea, since images should always overlap there physical
805       // representation
806       if(left != 0) {
807         if(base.width == 0 && base.height == 0) {
808           base.width  = left->get_width();
809           base.height = left->get_height();
810         } else if(base.width != left->get_width() || base.height != left->get_height()) {
811           base.x -= (left->get_width() - base.width) / 2;
812           base.y -= left->get_height() - base.height;
813           base.width = left->get_width();
814           base.height = left->get_height();
815           old_base = base;
816         }
817       } else {
818         base.width = base.height = 0;
819       }
820     }
821
822   animation_offset = 0;
823   sprite_left  = left;
824   sprite_right = right;
825 }
826
827 void
828 BadGuy::bump()
829 {
830   // these can't be bumped
831   if(kind == BAD_FLAME || kind == BAD_BOMB || kind == BAD_FISH)
832     return;
833   
834   kill_me(25);
835 }
836
837 void
838 BadGuy::make_player_jump(Player* player)
839 {
840   player->physic.set_velocity_y(2);
841   player->base.y = base.y - player->base.height - 2;
842 }
843
844 void
845 BadGuy::squish_me(Player* player)
846 {
847   make_player_jump(player);
848     
849   World::current()->add_score(base.x - scroll_x,
850                               base.y, 50 * player_status.score_multiplier);
851   play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
852   player_status.score_multiplier++;
853
854   dying = DYING_SQUISHED;
855   timer.start(2000);
856   physic.set_velocity(0, 0);
857 }
858
859 void
860 BadGuy::squish(Player* player)
861 {
862   static const int MAX_ICEBLOCK_SQUICHES = 10;
863     
864   if(kind == BAD_MRBOMB) {
865     // mrbomb transforms into a bomb now
866     World::current()->add_bad_guy(base.x, base.y, BAD_BOMB);
867     
868     make_player_jump(player);
869     World::current()->add_score(base.x - scroll_x, base.y, 50 * player_status.score_multiplier);
870     play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
871     player_status.score_multiplier++;
872     remove_me();
873     return;
874
875   } else if(kind == BAD_BSOD) {
876     squish_me(player);
877     set_sprite(img_bsod_squished_left, img_bsod_squished_right);
878     physic.set_velocity_x(0);
879     return;
880       
881   } else if (kind == BAD_MRICEBLOCK) {
882     if (mode == NORMAL || mode == KICK)
883       {
884         /* Flatten! */
885         play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
886         mode = FLAT;
887         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
888         physic.set_velocity_x(0);
889
890         timer.start(4000);
891       } else if (mode == FLAT) {
892         /* Kick! */
893         play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
894
895         if (player->base.x < base.x + (base.width/2)) {
896           physic.set_velocity_x(5);
897           dir = RIGHT;
898         } else {
899           physic.set_velocity_x(-5);
900           dir = LEFT;
901         }
902
903         mode = KICK;
904         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
905       }
906
907     make_player_jump(player);
908
909     player_status.score_multiplier++;
910
911     // check for maximum number of squiches
912     squishcount++;
913     if(squishcount >= MAX_ICEBLOCK_SQUICHES) {
914       kill_me(50);
915       return;
916     }
917     
918     return;
919   } else if(kind == BAD_FISH) {
920     // fish can only be killed when falling down
921     if(physic.get_velocity_y() >= 0)
922       return;
923       
924     make_player_jump(player);
925               
926     World::current()->add_score(base.x - scroll_x, base.y, 25 * player_status.score_multiplier);
927     player_status.score_multiplier++;
928      
929     // simply remove the fish...
930     remove_me();
931     return;
932   } else if(kind == BAD_BOUNCINGSNOWBALL) {
933     squish_me(player);
934     set_sprite(img_bouncingsnowball_squished,img_bouncingsnowball_squished);
935     return;
936   } else if(kind == BAD_FLYINGSNOWBALL) {
937     squish_me(player);
938     set_sprite(img_flyingsnowball_squished,img_flyingsnowball_squished);
939     return;
940   } else if(kind == BAD_SNOWBALL) {
941     squish_me(player);
942     set_sprite(img_snowball_squished_left, img_snowball_squished_right);
943     return;
944   }
945 }
946
947 void
948 BadGuy::kill_me(int score)
949 {
950   if(kind == BAD_BOMB || kind == BAD_STALACTITE || kind == BAD_FLAME)
951     return;
952
953   dying = DYING_FALLING;
954   if(kind == BAD_MRICEBLOCK) {
955     set_sprite(img_mriceblock_falling_left, img_mriceblock_falling_right);
956     if(mode == HELD) {
957       mode = NORMAL;
958       Player& tux = *World::current()->get_tux();  
959       tux.holding_something = false;
960     }
961   } else if(kind == BAD_BSOD) {
962     set_sprite(img_bsod_falling_left, img_bsod_falling_right);
963   }
964   
965   physic.enable_gravity(true);
966   physic.set_velocity_y(0);
967
968   /* Gain some points: */
969 //  if (kind == BAD_BSOD)
970     World::current()->add_score(base.x - scroll_x, base.y,
971                     score * player_status.score_multiplier);
972 /*  else 
973     World::current()->add_score(base.x - scroll_x, base.y,                                 
974                     25 * player_status.score_multiplier);*/
975
976   /* Play death sound: */
977   play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
978 }
979
980 void
981 BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
982 {
983   BadGuy* pbad_c    = NULL;
984
985   if(type == COLLISION_BUMP) {
986     bump();
987     return;
988   }
989
990   if(type == COLLISION_SQUISH) {
991     Player* player = static_cast<Player*>(p_c_object);
992     squish(player);
993     return;
994   }
995
996   /* COLLISION_NORMAL */
997   switch (c_object)
998     {
999     case CO_BULLET:
1000       kill_me(10);
1001       break;
1002
1003     case CO_BADGUY:
1004       pbad_c = (BadGuy*) p_c_object;
1005
1006       /* If we're a kicked mriceblock, kill any badguys we hit */
1007       if(kind == BAD_MRICEBLOCK && mode == KICK)
1008         {
1009           pbad_c->kill_me(25);
1010         }
1011
1012       // a held mriceblock gets kills the enemy too but falls to ground then
1013       else if(kind == BAD_MRICEBLOCK && mode == HELD)
1014         {
1015           pbad_c->kill_me(25);
1016           kill_me(0);
1017         }
1018
1019       /* Kill badguys that run into exploding bomb */
1020       else if (kind == BAD_BOMB && dying == DYING_NOT)
1021       {
1022         if (pbad_c->kind == BAD_MRBOMB)
1023         {
1024           // mrbomb transforms into a bomb now
1025           World::current()->add_bad_guy(pbad_c->base.x, pbad_c->base.y,
1026                                         BAD_BOMB);
1027           pbad_c->remove_me();
1028           return;
1029         }
1030         else if (pbad_c->kind != BAD_MRBOMB)
1031         {
1032           pbad_c->kill_me(50);
1033         }
1034       }
1035
1036       /* Kill any badguys that get hit by stalactite */
1037       else if (kind == BAD_STALACTITE && dying == DYING_NOT)
1038       {
1039         pbad_c->kill_me(50);
1040       }
1041
1042       /* When enemies run into eachother, make them change directions */
1043       else
1044       {
1045         // Jumpy, fish, flame, stalactites are exceptions
1046         if (pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FLAME
1047             || pbad_c->kind == BAD_STALACTITE || pbad_c->kind == BAD_FISH)
1048           break;
1049
1050         // Bounce off of other badguy if we land on top of him
1051         if (base.y + base.height < pbad_c->base.y + pbad_c->base.height)
1052         {
1053           Direction old_dir = dir;
1054           if (pbad_c->dir == LEFT)
1055             dir = RIGHT;
1056           else if (pbad_c->dir == RIGHT)
1057             dir = LEFT;
1058
1059           if (dir != old_dir)
1060             physic.inverse_velocity_x();
1061
1062           physic.set_velocity(fabs(physic.get_velocity_x()), 2);
1063
1064           break;
1065         }
1066         else if (base.y + base.height > pbad_c->base.y + pbad_c->base.height)
1067           break;
1068
1069         if (pbad_c->kind != BAD_FLAME)
1070           {
1071           if (dir == LEFT)
1072             dir = RIGHT;
1073           else if (dir == RIGHT)
1074             dir = LEFT;
1075         
1076           physic.inverse_velocity_x();
1077           }
1078       }
1079       
1080       break;
1081
1082     case CO_PLAYER:
1083       Player* player = static_cast<Player*>(p_c_object);
1084       /* Get kicked if were flat */
1085       if (mode == FLAT && !dying)
1086       {
1087         play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
1088
1089         // Hit from left side
1090         if (player->base.x < base.x) {
1091           physic.set_velocity_x(5);
1092           dir = RIGHT;
1093         }
1094         // Hit from right side
1095         else {
1096           physic.set_velocity_x(-5);
1097           dir = LEFT;
1098         }
1099
1100         mode = KICK;
1101         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1102       }
1103       break;
1104
1105     }
1106 }
1107
1108 //---------------------------------------------------------------------------
1109
1110 void load_badguy_gfx()
1111 {
1112   img_bsod_squished_left = sprite_manager->load("bsod-squished-left");
1113   img_bsod_squished_right = sprite_manager->load("bsod-squished-right");
1114   img_bsod_falling_left = sprite_manager->load("bsod-falling-left");
1115   img_bsod_falling_right = sprite_manager->load("bsod-falling-right");
1116   img_mriceblock_flat_left = sprite_manager->load("mriceblock-flat-left");
1117   img_mriceblock_flat_right = sprite_manager->load("mriceblock-flat-right");
1118   img_mriceblock_falling_left = sprite_manager->load("mriceblock-falling-left");
1119   img_mriceblock_falling_right = sprite_manager->load("mriceblock-falling-right");
1120   img_bsod_left = sprite_manager->load("bsod-left");
1121   img_bsod_right = sprite_manager->load("bsod-right");
1122   img_mriceblock_left = sprite_manager->load("mriceblock-left");
1123   img_mriceblock_right = sprite_manager->load("mriceblock-right");
1124   img_jumpy_left_up = sprite_manager->load("jumpy-left-up");
1125   img_jumpy_left_down = sprite_manager->load("jumpy-left-down");
1126   img_jumpy_left_middle = sprite_manager->load("jumpy-left-middle");
1127   img_mrbomb_left = sprite_manager->load("mrbomb-left");
1128   img_mrbomb_right = sprite_manager->load("mrbomb-right");
1129   img_mrbomb_ticking_left = sprite_manager->load("mrbomb-ticking-left");
1130   img_mrbomb_ticking_right = sprite_manager->load("mrbomb-ticking-right");
1131   img_mrbomb_explosion = sprite_manager->load("mrbomb-explosion");
1132   img_stalactite = sprite_manager->load("stalactite");
1133   img_stalactite_broken = sprite_manager->load("stalactite-broken");
1134   img_flame = sprite_manager->load("flame");
1135   img_fish = sprite_manager->load("fish");
1136   img_fish_down = sprite_manager->load("fish-down");
1137   img_bouncingsnowball_left = sprite_manager->load("bouncingsnowball-left");
1138   img_bouncingsnowball_right = sprite_manager->load("bouncingsnowball-right");
1139   img_bouncingsnowball_squished = sprite_manager->load("bouncingsnowball-squished");
1140   img_flyingsnowball = sprite_manager->load("flyingsnowball");
1141   img_flyingsnowball_squished = sprite_manager->load("flyingsnowball-squished");
1142   img_spiky_left = sprite_manager->load("spiky-left");
1143   img_spiky_right = sprite_manager->load("spiky-right");
1144   img_snowball_left = sprite_manager->load("snowball-left");
1145   img_snowball_right = sprite_manager->load("snowball-right");
1146   img_snowball_squished_left = sprite_manager->load("snowball-squished-left");
1147   img_snowball_squished_right = sprite_manager->load("snowball-squished-right");
1148 }
1149
1150 void free_badguy_gfx()
1151 {
1152 }
1153
1154 // EOF //