Improved frozen behavior.
[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_mriceblock_flat_left;
37 Sprite* img_mriceblock_flat_right;
38 Sprite* img_mriceblock_falling_left;
39 Sprite* img_mriceblock_falling_right;
40 Sprite* img_mriceblock_left;
41 Sprite* img_mriceblock_right;
42 Sprite* img_jumpy_left_up;
43 Sprite* img_jumpy_left_down;
44 Sprite* img_jumpy_left_middle;
45 Sprite* img_jumpy_left_iced;
46 Sprite* img_mrbomb_left;
47 Sprite* img_mrbomb_right;
48 Sprite* img_mrbomb_iced_left;
49 Sprite* img_mrbomb_iced_right;
50 Sprite* img_mrbomb_ticking_left;
51 Sprite* img_mrbomb_ticking_right;
52 Sprite* img_mrbomb_explosion;
53 Sprite* img_stalactite;
54 Sprite* img_stalactite_broken;
55 Sprite* img_flame;
56 Sprite* img_fish;
57 Sprite* img_fish_down;
58 Sprite* img_fish_iced;
59 Sprite* img_fish_iced_down;
60 Sprite* img_bouncingsnowball_left;
61 Sprite* img_bouncingsnowball_right;
62 Sprite* img_bouncingsnowball_squished;
63 Sprite* img_flyingsnowball;
64 Sprite* img_flyingsnowball_squished;
65 Sprite* img_spiky_left;
66 Sprite* img_spiky_right;
67 Sprite* img_spiky_iced_left;
68 Sprite* img_spiky_iced_right;
69 Sprite* img_snowball_left;
70 Sprite* img_snowball_right;
71 Sprite* img_snowball_squished_left;
72 Sprite* img_snowball_squished_right;
73
74 #define BADGUY_WALK_SPEED .8f
75
76 BadGuyKind  badguykind_from_string(const std::string& str)
77 {
78   if (str == "money" || str == "jumpy") // was money in old maps
79     return BAD_JUMPY;
80   else if (str == "laptop" || str == "mriceblock") // was laptop in old maps
81     return BAD_MRICEBLOCK;
82   else if (str == "mrbomb")
83     return BAD_MRBOMB;
84   else if (str == "stalactite")
85     return BAD_STALACTITE;
86   else if (str == "flame")
87     return BAD_FLAME;
88   else if (str == "fish")
89     return BAD_FISH;
90   else if (str == "bouncingsnowball")
91     return BAD_BOUNCINGSNOWBALL;
92   else if (str == "flyingsnowball")
93     return BAD_FLYINGSNOWBALL;
94   else if (str == "spiky")
95     return BAD_SPIKY;
96   else if (str == "snowball" || str == "bsod") // was bsod in old maps
97     return BAD_SNOWBALL;
98   else
99     {
100       printf("Couldn't convert badguy: '%s'\n", str.c_str());
101       return BAD_SNOWBALL;
102     }
103 }
104
105 std::string badguykind_to_string(BadGuyKind kind)
106 {
107   switch(kind)
108     {
109     case BAD_JUMPY:
110       return "jumpy";
111       break;
112     case BAD_MRICEBLOCK:
113       return "mriceblock";
114       break;
115     case BAD_MRBOMB:
116       return "mrbomb";
117       break;
118     case BAD_STALACTITE:
119       return "stalactite";
120       break;
121     case BAD_FLAME:
122       return "flame";
123       break;
124     case BAD_FISH:
125       return "fish";
126       break;
127     case BAD_BOUNCINGSNOWBALL:
128       return "bouncingsnowball";
129       break;
130     case BAD_FLYINGSNOWBALL:
131       return "flyingsnowball";
132       break;
133     case BAD_SPIKY:
134       return "spiky";
135       break;
136     case BAD_SNOWBALL:
137       return "snowball";
138       break;
139     default:
140       return "snowball";
141     }
142 }
143
144 BadGuy::BadGuy(float x, float y, BadGuyKind kind_, bool stay_on_platform_)
145   : removable(false), squishcount(0)
146 {
147   base.x   = x;
148   base.y   = y;    
149   base.width  = 0;
150   base.height = 0;
151   base.xm  = 0;
152   base.ym  = 0;
153
154   stay_on_platform = stay_on_platform_;
155   mode     = NORMAL;
156   dying    = DYING_NOT;
157   kind     = kind_;
158   old_base = base;
159   dir      = LEFT;
160   seen     = false;
161   frozen_timer.init(true);
162   animation_offset = 0;
163   sprite_left = sprite_right = 0;
164   physic.reset();
165   timer.init(true);
166
167   if(kind == BAD_MRBOMB) {
168     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
169     set_sprite(img_mrbomb_left, img_mrbomb_right);
170   } else if (kind == BAD_MRICEBLOCK) {
171     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
172     set_sprite(img_mriceblock_left, img_mriceblock_right);
173   } else if(kind == BAD_JUMPY) {
174     set_sprite(img_jumpy_left_up, img_jumpy_left_up);
175   } else if(kind == BAD_BOMB) {
176     set_sprite(img_mrbomb_ticking_left, img_mrbomb_ticking_right);
177     // hack so that the bomb doesn't hurt until it expldes...
178     dying = DYING_SQUISHED;
179   } else if(kind == BAD_FLAME) {
180     base.ym = 0; // we misuse base.ym as angle for the flame
181     physic.enable_gravity(false);
182     set_sprite(img_flame, img_flame);
183   } else if(kind == BAD_BOUNCINGSNOWBALL) {
184     physic.set_velocity(-1.3, 0);
185     set_sprite(img_bouncingsnowball_left, img_bouncingsnowball_right);
186   } else if(kind == BAD_STALACTITE) {
187     physic.enable_gravity(false);
188     set_sprite(img_stalactite, img_stalactite);
189   } else if(kind == BAD_FISH) {
190     set_sprite(img_fish, img_fish);
191     physic.enable_gravity(true);
192   } else if(kind == BAD_FLYINGSNOWBALL) {
193     set_sprite(img_flyingsnowball, img_flyingsnowball);
194     physic.enable_gravity(false);
195   } else if(kind == BAD_SPIKY) {
196     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
197     set_sprite(img_spiky_left, img_spiky_right);
198   } else if(kind == BAD_SNOWBALL) {
199     physic.set_velocity(-BADGUY_WALK_SPEED, 0);
200     set_sprite(img_snowball_left, img_snowball_right);
201   }
202
203   // if we're in a solid tile at start correct that now
204   if(kind != BAD_FLAME && kind != BAD_FISH && collision_object_map(base)) 
205     {
206       std::cout << "Warning: badguy started in wall: kind: " << badguykind_to_string(kind) 
207                 << " pos: (" << base.x << ", " << base.y << ")" << std::endl;
208       while(collision_object_map(base))
209         --base.y;
210     }
211 }
212
213 void
214 BadGuy::action_mriceblock(double frame_ratio)
215 {
216   Player& tux = *World::current()->get_tux();
217
218   if(mode != HELD)
219     fall();
220   
221   /* Move left/right: */
222   if (mode != HELD)
223     {
224       // move
225       physic.apply(frame_ratio, base.x, base.y);
226       if (dying != DYING_FALLING)
227         collision_swept_object_map(&old_base,&base);
228     }
229   else if (mode == HELD)
230     { /* FIXME: The pbad object shouldn't know about pplayer objects. */
231       /* If we're holding the iceblock */
232       dir = tux.dir;
233       if(dir==RIGHT)
234         {
235           base.x = tux.base.x + 16;
236           base.y = tux.base.y + tux.base.height/1.5 - base.height;
237         }
238       else /* facing left */
239         {
240           base.x = tux.base.x - 16;
241           base.y = tux.base.y + tux.base.height/1.5 - base.height;
242         }
243       if(collision_object_map(base))
244         {
245           base.x = tux.base.x;
246           base.y = tux.base.y + tux.base.height/1.5 - base.height;
247         }
248
249       if(tux.input.fire != DOWN) /* SHOOT! */
250         {
251           if(dir == LEFT)
252             base.x -= 24;
253           else
254             base.x += 24;
255           old_base = base;
256
257           mode=KICK;
258           tux.kick_timer.start(KICKING_TIME);
259           set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
260           physic.set_velocity_x((dir == LEFT) ? -3.5 : 3.5);
261           play_sound(sounds[SND_KICK],SOUND_CENTER_SPEAKER);
262         }
263     }
264
265   if (!dying)
266     {
267       int changed = dir;
268       check_horizontal_bump();
269       if(mode == KICK && changed != dir)
270         {
271           /* handle stereo sound (number 10 should be tweaked...)*/
272           if (base.x < scroll_x + screen->w/2 - 10)
273             play_sound(sounds[SND_RICOCHET], SOUND_LEFT_SPEAKER);
274           else if (base.x > scroll_x + screen->w/2 + 10)
275             play_sound(sounds[SND_RICOCHET], SOUND_RIGHT_SPEAKER);
276           else
277             play_sound(sounds[SND_RICOCHET], SOUND_CENTER_SPEAKER);
278         }
279     }
280
281   /* Handle mode timer: */
282   if (mode == FLAT)
283     {
284       if(!timer.check())
285         {
286           mode = NORMAL;
287           set_sprite(img_mriceblock_left, img_mriceblock_right);
288           physic.set_velocity( (dir == LEFT) ? -.8 : .8, 0);
289         }
290     }
291 }
292
293 void
294 BadGuy::check_horizontal_bump(bool checkcliff)
295 {
296     float halfheight = base.height / 2;
297     if (dir == LEFT && issolid( base.x, (int) base.y + halfheight))
298     {
299         dir = RIGHT;
300         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
301         return;
302     }
303     if (dir == RIGHT && issolid( base.x + base.width, (int)base.y + halfheight))
304     {
305         dir = LEFT;
306         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
307         return;
308     }
309
310     // don't check for cliffs when we're falling
311     if(!checkcliff)
312         return;
313     if(!issolid(base.x + base.width/2, base.y + base.height))
314         return;
315     
316     if(dir == LEFT && !issolid(base.x, (int) base.y + base.height + halfheight))
317     {
318         dir = RIGHT;
319         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
320         return;
321     }
322     if(dir == RIGHT && !issolid(base.x + base.width,
323                 (int) base.y + base.height + halfheight))
324     {
325         dir = LEFT;
326         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
327         return;
328     }
329 }
330
331 void
332 BadGuy::fall()
333 {
334   /* Fall if we get off the ground: */
335   if (dying != DYING_FALLING)
336     {
337       if (!issolid(base.x+base.width/2, base.y + base.height))
338         {
339           // not solid below us? enable gravity
340           physic.enable_gravity(true);
341         }
342       else
343         {
344           /* Land: */
345           if (physic.get_velocity_y() < 0)
346             {
347               base.y = int((base.y + base.height)/32) * 32 - base.height;
348               physic.set_velocity_y(0);
349             }
350           // no gravity anymore please
351           physic.enable_gravity(false);
352
353           if (stay_on_platform && mode == NORMAL)
354             {
355               if (!issolid(base.x + ((dir == LEFT) ? 0 : base.width),
356                            base.y + base.height))
357                 {
358                   if (dir == LEFT)
359                   {
360                     dir = RIGHT;
361                     physic.set_velocity_x(fabsf(physic.get_velocity_x()));
362                   } 
363                   else
364                   {
365                     dir = LEFT;
366                     physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
367                   }
368                 }
369             }
370         }
371     }
372   else
373     {
374       physic.enable_gravity(true);
375     }
376 }
377
378 void
379 BadGuy::remove_me()
380 {
381   removable = true;
382 }
383
384 void
385 BadGuy::action_jumpy(double frame_ratio)
386 {
387   if(frozen_timer.check())
388     {
389     set_sprite(img_jumpy_left_iced, img_jumpy_left_iced);
390     return;
391     }
392
393   const float vy = physic.get_velocity_y();
394
395   // XXX: These tests *should* use location from ground, not velocity
396   if (fabsf(vy) > 5.6f)
397     set_sprite(img_jumpy_left_down, img_jumpy_left_down);
398   else if (fabsf(vy) > 5.3f)
399     set_sprite(img_jumpy_left_middle, img_jumpy_left_middle);
400   else
401     set_sprite(img_jumpy_left_up, img_jumpy_left_up);
402
403   Player& tux = *World::current()->get_tux();
404
405   static const float JUMPV = 6;
406     
407   fall();
408   // jump when on ground
409   if(dying == DYING_NOT && issolid(base.x, base.y+32))
410     {
411       physic.set_velocity_y(JUMPV);
412       physic.enable_gravity(true);
413
414       mode = JUMPY_JUMP;
415     }
416   else if(mode == JUMPY_JUMP)
417     {
418       mode = NORMAL;
419     }
420
421   // set direction based on tux
422   if(tux.base.x > base.x)
423     dir = RIGHT;
424   else
425     dir = LEFT;
426
427   // move
428   physic.apply(frame_ratio, base.x, base.y);
429   if(dying == DYING_NOT)
430     collision_swept_object_map(&old_base, &base);
431 }
432
433 void
434 BadGuy::action_mrbomb(double frame_ratio)
435 {
436   if(frozen_timer.check())
437     {
438     set_sprite(img_mrbomb_iced_left, img_mrbomb_iced_right);
439     return;
440     }
441
442   if (dying == DYING_NOT)
443     check_horizontal_bump(true);
444
445   fall();
446
447   physic.apply(frame_ratio, base.x, base.y);
448   if (dying != DYING_FALLING)
449     collision_swept_object_map(&old_base,&base); 
450 }
451
452 void
453 BadGuy::action_bomb(double frame_ratio)
454 {
455   static const int TICKINGTIME = 1000;
456   static const int EXPLODETIME = 1000;
457     
458   fall();
459
460   if(mode == NORMAL) {
461     mode = BOMB_TICKING;
462     timer.start(TICKINGTIME);
463   } else if(!timer.check()) {
464     if(mode == BOMB_TICKING) {
465       mode = BOMB_EXPLODE;
466       set_sprite(img_mrbomb_explosion, img_mrbomb_explosion);
467       dying = DYING_NOT; // now the bomb hurts
468       timer.start(EXPLODETIME);
469
470       /* play explosion sound */  // FIXME: is the stereo all right? maybe we should use player cordinates...
471       if (base.x < scroll_x + screen->w/2 - 10)
472         play_sound(sounds[SND_EXPLODE], SOUND_LEFT_SPEAKER);
473       else if (base.x > scroll_x + screen->w/2 + 10)
474         play_sound(sounds[SND_EXPLODE], SOUND_RIGHT_SPEAKER);
475       else
476         play_sound(sounds[SND_EXPLODE], SOUND_CENTER_SPEAKER);
477
478     } else if(mode == BOMB_EXPLODE) {
479       remove_me();
480       return;
481     }
482   }
483
484   // move
485   physic.apply(frame_ratio, base.x, base.y);                 
486   collision_swept_object_map(&old_base,&base);
487 }
488
489 void
490 BadGuy::action_stalactite(double frame_ratio)
491 {
492   Player& tux = *World::current()->get_tux();
493
494   static const int SHAKETIME = 800;
495   static const int RANGE = 40;
496     
497   if(mode == NORMAL) {
498     // start shaking when tux is below the stalactite and at least 40 pixels
499     // near
500     if(tux.base.x + 32 > base.x - RANGE && tux.base.x < base.x + 32 + RANGE
501             && tux.base.y + tux.base.height > base.y) {
502       timer.start(SHAKETIME);
503       mode = STALACTITE_SHAKING;
504     }
505   } if(mode == STALACTITE_SHAKING) {
506     base.x = old_base.x + (rand() % 6) - 3; // TODO this could be done nicer...
507     if(!timer.check()) {
508       mode = STALACTITE_FALL;
509     }
510   } else if(mode == STALACTITE_FALL) {
511     fall();
512     /* Destroy if we collides with land */
513     if(issolid(base.x+base.width/2, base.y+base.height))
514     {
515       timer.start(2000);
516       dying = DYING_SQUISHED;
517       mode = FLAT;
518       set_sprite(img_stalactite_broken, img_stalactite_broken);
519     }
520   } else if(mode == FLAT) {
521     fall();
522   }
523
524   // move
525   physic.apply(frame_ratio, base.x, base.y);
526
527   if(dying == DYING_SQUISHED && !timer.check())
528     remove_me();
529 }
530
531 void
532 BadGuy::action_flame(double frame_ratio)
533 {
534     static const float radius = 100;
535     static const float speed = 0.02;
536     base.x = old_base.x + cos(base.ym) * radius;
537     base.y = old_base.y + sin(base.ym) * radius;
538
539     base.ym = fmodf(base.ym + frame_ratio * speed, 2*M_PI);
540 }
541
542 void
543 BadGuy::action_fish(double frame_ratio)
544 {
545   if(frozen_timer.check())
546     {
547     if(physic.get_velocity_y() < 0)
548       set_sprite(img_fish_iced_down, img_fish_iced_down);
549     else
550       set_sprite(img_fish_iced, img_fish_iced);
551
552     return;
553     }
554
555   static const float JUMPV = 6;
556   static const int WAITTIME = 1000;
557     
558   // go in wait mode when back in water
559   if(dying == DYING_NOT && gettile(base.x, base.y+ base.height)->water
560         && physic.get_velocity_y() <= 0 && mode == NORMAL)
561     {
562       mode = FISH_WAIT;
563       set_sprite(0, 0);
564       physic.set_velocity(0, 0);
565       physic.enable_gravity(false);
566       timer.start(WAITTIME);
567     }
568   else if(mode == FISH_WAIT && !timer.check())
569     {
570       // jump again
571       set_sprite(img_fish, img_fish);
572       mode = NORMAL;
573       physic.set_velocity(0, JUMPV);
574       physic.enable_gravity(true);
575     }
576
577   physic.apply(frame_ratio, base.x, base.y);
578   if(dying == DYING_NOT)
579     collision_swept_object_map(&old_base, &base);
580
581   if(physic.get_velocity_y() < 0)
582     set_sprite(img_fish_down, img_fish_down);
583 }
584
585 void
586 BadGuy::action_bouncingsnowball(double frame_ratio)
587 {
588   static const float JUMPV = 4.5;
589     
590   fall();
591
592   // jump when on ground
593   if(dying == DYING_NOT && issolid(base.x, base.y+32))
594     {
595       physic.set_velocity_y(JUMPV);
596       physic.enable_gravity(true);
597     }                                                     
598   else
599     {
600       mode = NORMAL;
601     }
602
603   // check for right/left collisions
604   check_horizontal_bump();
605
606   physic.apply(frame_ratio, base.x, base.y);
607   if(dying == DYING_NOT)
608     collision_swept_object_map(&old_base, &base);
609
610   // Handle dying timer:
611   if (dying == DYING_SQUISHED && !timer.check())
612     {
613       /* Remove it if time's up: */
614       remove_me();
615       return;
616     }
617 }
618
619 void
620 BadGuy::action_flyingsnowball(double frame_ratio)
621 {
622   static const float FLYINGSPEED = 1;
623   static const int DIRCHANGETIME = 1000;
624     
625   // go into flyup mode if none specified yet
626   if(dying == DYING_NOT && mode == NORMAL) {
627     mode = FLY_UP;
628     physic.set_velocity_y(FLYINGSPEED);
629     timer.start(DIRCHANGETIME/2);
630   }
631
632   if(dying == DYING_NOT && !timer.check()) {
633     if(mode == FLY_UP) {
634       mode = FLY_DOWN;
635       physic.set_velocity_y(-FLYINGSPEED);
636     } else if(mode == FLY_DOWN) {
637       mode = FLY_UP;
638       physic.set_velocity_y(FLYINGSPEED);
639     }
640     timer.start(DIRCHANGETIME);
641   }
642
643   if(dying != DYING_NOT)
644     physic.enable_gravity(true);
645
646   physic.apply(frame_ratio, base.x, base.y);
647   if(dying == DYING_NOT || dying == DYING_SQUISHED)
648     collision_swept_object_map(&old_base, &base);
649
650   // Handle dying timer:
651   if (dying == DYING_SQUISHED && !timer.check())
652     {
653       /* Remove it if time's up: */
654       remove_me();
655       return;
656     }                                                          
657 }
658
659 void
660 BadGuy::action_spiky(double frame_ratio)
661 {
662   if(frozen_timer.check())
663     {
664     set_sprite(img_spiky_iced_left, img_spiky_iced_right);
665     return;
666     }
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(double 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(double 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_MRICEBLOCK:
725       action_mriceblock(frame_ratio);
726       break;
727   
728     case BAD_JUMPY:
729       action_jumpy(frame_ratio);
730       break;
731
732     case BAD_MRBOMB:
733       action_mrbomb(frame_ratio);
734       break;
735     
736     case BAD_BOMB:
737       action_bomb(frame_ratio);
738       break;
739
740     case BAD_STALACTITE:
741       action_stalactite(frame_ratio);
742       break;
743
744     case BAD_FLAME:
745       action_flame(frame_ratio);
746       break;
747
748     case BAD_FISH:
749       action_fish(frame_ratio);
750       break;
751
752     case BAD_BOUNCINGSNOWBALL:
753       action_bouncingsnowball(frame_ratio);
754       break;
755
756     case BAD_FLYINGSNOWBALL:
757       action_flyingsnowball(frame_ratio);
758       break;
759
760     case BAD_SPIKY:
761       action_spiky(frame_ratio);
762       break;
763
764     case BAD_SNOWBALL:
765       action_snowball(frame_ratio);
766       break;
767     default:
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       return;
782     }
783
784   Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
785   sprite->draw(base.x, base.y);
786
787   if (debug_mode)
788     fillrect(base.x - scroll_x, base.y - scroll_y, base.width, base.height, 75,0,75, 150);
789 }
790
791 void
792 BadGuy::set_sprite(Sprite* left, Sprite* right) 
793 {
794   if (1)
795     {
796       base.width = 32;
797       base.height = 32;
798     }
799   else
800     {
801       // FIXME: Using the image size for the physics and collision is
802       // a bad idea, since images should always overlap there physical
803       // representation
804       if(left != 0) {
805         if(base.width == 0 && base.height == 0) {
806           base.width  = left->get_width();
807           base.height = left->get_height();
808         } else if(base.width != left->get_width() || base.height != left->get_height()) {
809           base.x -= (left->get_width() - base.width) / 2;
810           base.y -= left->get_height() - base.height;
811           base.width = left->get_width();
812           base.height = left->get_height();
813           old_base = base;
814         }
815       } else {
816         base.width = base.height = 0;
817       }
818     }
819
820   animation_offset = 0;
821   sprite_left  = left;
822   sprite_right = right;
823 }
824
825 void
826 BadGuy::bump()
827 {
828   // these can't be bumped
829   if(kind == BAD_FLAME || kind == BAD_BOMB || kind == BAD_FISH
830       || kind == BAD_FLYINGSNOWBALL)
831     return;
832
833   physic.set_velocity_y(3);
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,
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, 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_MRICEBLOCK) {
876     if (mode == NORMAL || mode == KICK)
877       {
878         /* Flatten! */
879         play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
880         mode = FLAT;
881         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
882         physic.set_velocity_x(0);
883
884         timer.start(4000);
885       } else if (mode == FLAT) {
886         /* Kick! */
887         play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
888
889         if (player->base.x < base.x + (base.width/2)) {
890           physic.set_velocity_x(5);
891           dir = RIGHT;
892         } else {
893           physic.set_velocity_x(-5);
894           dir = LEFT;
895         }
896
897         mode = KICK;
898         player->kick_timer.start(KICKING_TIME);
899         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
900       }
901
902     make_player_jump(player);
903
904     player_status.score_multiplier++;
905
906     // check for maximum number of squiches
907     squishcount++;
908     if(squishcount >= MAX_ICEBLOCK_SQUICHES) {
909       kill_me(50);
910       return;
911     }
912     
913     return;
914   } else if(kind == BAD_FISH) {
915     // fish can only be killed when falling down
916     if(physic.get_velocity_y() >= 0)
917       return;
918       
919     make_player_jump(player);
920               
921     World::current()->add_score(base.x, base.y, 25 * player_status.score_multiplier);
922     player_status.score_multiplier++;
923      
924     // simply remove the fish...
925     remove_me();
926     return;
927   } else if(kind == BAD_BOUNCINGSNOWBALL) {
928     squish_me(player);
929     set_sprite(img_bouncingsnowball_squished,img_bouncingsnowball_squished);
930     return;
931   } else if(kind == BAD_FLYINGSNOWBALL) {
932     squish_me(player);
933     set_sprite(img_flyingsnowball_squished,img_flyingsnowball_squished);
934     return;
935   } else if(kind == BAD_SNOWBALL) {
936     squish_me(player);
937     set_sprite(img_snowball_squished_left, img_snowball_squished_right);
938     return;
939   }
940 }
941
942 void
943 BadGuy::kill_me(int score)
944 {
945   if(kind == BAD_BOMB)
946     return;
947
948   dying = DYING_FALLING;
949   if(kind == BAD_MRICEBLOCK) {
950     set_sprite(img_mriceblock_falling_left, img_mriceblock_falling_right);
951     if(mode == HELD) {
952       mode = NORMAL;
953       Player& tux = *World::current()->get_tux();  
954       tux.holding_something = false;
955     }
956   }
957   
958   physic.enable_gravity(true);
959
960   /* Gain some points: */
961     World::current()->add_score(base.x, base.y,
962                     score * player_status.score_multiplier);
963
964   /* Play death sound: */
965   play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
966 }
967
968 void BadGuy::explode(BadGuy *badguy)
969 {
970 World::current()->add_bad_guy(badguy->base.x, badguy->base.y, BAD_BOMB);
971 badguy->remove_me();
972 }
973
974 void
975 BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
976 {
977   BadGuy* pbad_c    = NULL;
978   Bullet* pbullet_c = NULL;
979
980   if(type == COLLISION_BUMP) {
981     bump();
982     return;
983   }
984
985   if(type == COLLISION_SQUISH) {
986     Player* player = static_cast<Player*>(p_c_object);
987     squish(player);
988     return;
989   }
990
991   /* COLLISION_NORMAL */
992   switch (c_object)
993     {
994     case CO_BULLET:
995       pbullet_c = (Bullet*) p_c_object;
996
997       if(pbullet_c->kind == FIRE_BULLET)
998         {
999         if(kind == BAD_MRICEBLOCK || kind == BAD_STALACTITE ||
1000             kind == BAD_FISH || kind == BAD_BOUNCINGSNOWBALL ||
1001             kind == BAD_FLYINGSNOWBALL || kind == BAD_FLYINGSNOWBALL)
1002         kill_me(10);
1003         }
1004       else if(pbullet_c->kind == ICE_BULLET)
1005         {
1006         if(kind == BAD_FLAME)
1007           kill_me(10);
1008         else
1009           frozen_timer.start(FROZEN_TIME);
1010         }
1011       break;
1012
1013     case CO_BADGUY:
1014       pbad_c = (BadGuy*) p_c_object;
1015
1016       /* If we're a kicked mriceblock, kill any badguys we hit */
1017       if(kind == BAD_MRICEBLOCK && mode == KICK)
1018         {
1019           pbad_c->kill_me(25);
1020         }
1021
1022       // a held mriceblock gets kills the enemy too but falls to ground then
1023       else if(kind == BAD_MRICEBLOCK && mode == HELD)
1024         {
1025           pbad_c->kill_me(25);
1026           kill_me(0);
1027         }
1028
1029       /* Kill badguys that run into exploding bomb */
1030       else if (kind == BAD_BOMB && dying == DYING_NOT)
1031       {
1032         if (pbad_c->kind == BAD_MRBOMB)
1033         {
1034           // mrbomb transforms into a bomb now
1035           explode(pbad_c);
1036           return;
1037         }
1038         else if (pbad_c->kind != BAD_MRBOMB)
1039         {
1040           pbad_c->kill_me(50);
1041         }
1042       }
1043
1044       /* Kill any badguys that get hit by stalactite */
1045       else if (kind == BAD_STALACTITE && dying == DYING_NOT)
1046       {
1047         if (pbad_c->kind == BAD_MRBOMB)
1048         {
1049           // mrbomb transforms into a bomb now
1050           explode(pbad_c);
1051           return;
1052         }
1053         else
1054           pbad_c->kill_me(50);
1055       }
1056
1057       /* When enemies run into eachother, make them change directions */
1058       else
1059       {
1060         // Jumpy, fish, flame, stalactites are exceptions
1061         if (pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FLAME
1062             || pbad_c->kind == BAD_STALACTITE || pbad_c->kind == BAD_FISH)
1063           break;
1064
1065         // Bounce off of other badguy if we land on top of him
1066         if (base.y + base.height < pbad_c->base.y + pbad_c->base.height)
1067         {
1068           if (pbad_c->dir == LEFT)
1069           {
1070             dir = RIGHT;
1071             physic.set_velocity(fabsf(physic.get_velocity_x()), 2);
1072           }
1073           else if (pbad_c->dir == RIGHT)
1074           {
1075             dir = LEFT;
1076             physic.set_velocity(-fabsf(physic.get_velocity_x()), 2);
1077           }
1078
1079
1080
1081           break;
1082         }
1083         else if (base.y + base.height > pbad_c->base.y + pbad_c->base.height)
1084           break;
1085
1086         if (pbad_c->kind != BAD_FLAME)
1087           {
1088             if (dir == LEFT)
1089             {
1090               dir = RIGHT;
1091               physic.set_velocity_x(fabsf(physic.get_velocity_x()));
1092             }
1093             else if (dir == RIGHT)
1094             {
1095               dir = LEFT;
1096               physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
1097             }
1098
1099           }
1100       }
1101       
1102       break;
1103
1104     case CO_PLAYER:
1105       Player* player = static_cast<Player*>(p_c_object);
1106       /* Get kicked if were flat */
1107       if (mode == FLAT && !dying)
1108       {
1109         play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
1110
1111         // Hit from left side
1112         if (player->base.x < base.x) {
1113           physic.set_velocity_x(5);
1114           dir = RIGHT;
1115         }
1116         // Hit from right side
1117         else {
1118           physic.set_velocity_x(-5);
1119           dir = LEFT;
1120         }
1121
1122         mode = KICK;
1123         player->kick_timer.start(KICKING_TIME);
1124         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1125       }
1126       break;
1127
1128     }
1129 }
1130
1131
1132 //---------------------------------------------------------------------------
1133
1134 void load_badguy_gfx()
1135 {
1136   img_mriceblock_flat_left = sprite_manager->load("mriceblock-flat-left");
1137   img_mriceblock_flat_right = sprite_manager->load("mriceblock-flat-right");
1138   img_mriceblock_falling_left = sprite_manager->load("mriceblock-falling-left");
1139   img_mriceblock_falling_right = sprite_manager->load("mriceblock-falling-right");
1140   img_mriceblock_left = sprite_manager->load("mriceblock-left");
1141   img_mriceblock_right = sprite_manager->load("mriceblock-right");
1142   img_jumpy_left_up = sprite_manager->load("jumpy-left-up");
1143   img_jumpy_left_down = sprite_manager->load("jumpy-left-down");
1144   img_jumpy_left_middle = sprite_manager->load("jumpy-left-middle");
1145   img_jumpy_left_iced = sprite_manager->load("jumpy-left-iced");
1146   img_mrbomb_left = sprite_manager->load("mrbomb-left");
1147   img_mrbomb_right = sprite_manager->load("mrbomb-right");
1148   img_mrbomb_iced_left = sprite_manager->load("mrbomb-iced-left");
1149   img_mrbomb_iced_right = sprite_manager->load("mrbomb-iced-right");
1150   img_mrbomb_ticking_left = sprite_manager->load("mrbomb-ticking-left");
1151   img_mrbomb_ticking_right = sprite_manager->load("mrbomb-ticking-right");
1152   img_mrbomb_explosion = sprite_manager->load("mrbomb-explosion");
1153   img_stalactite = sprite_manager->load("stalactite");
1154   img_stalactite_broken = sprite_manager->load("stalactite-broken");
1155   img_flame = sprite_manager->load("flame");
1156   img_fish = sprite_manager->load("fish");
1157   img_fish_down = sprite_manager->load("fish-down");
1158   img_fish_iced = sprite_manager->load("fish-iced");
1159   img_fish_iced_down = sprite_manager->load("fish-iced-down");
1160   img_bouncingsnowball_left = sprite_manager->load("bouncingsnowball-left");
1161   img_bouncingsnowball_right = sprite_manager->load("bouncingsnowball-right");
1162   img_bouncingsnowball_squished = sprite_manager->load("bouncingsnowball-squished");
1163   img_flyingsnowball = sprite_manager->load("flyingsnowball");
1164   img_flyingsnowball_squished = sprite_manager->load("flyingsnowball-squished");
1165   img_spiky_left = sprite_manager->load("spiky-left");
1166   img_spiky_right = sprite_manager->load("spiky-right");
1167   img_spiky_iced_left = sprite_manager->load("spiky-iced-left");
1168   img_spiky_iced_right = sprite_manager->load("spiky-iced-right");
1169   img_snowball_left = sprite_manager->load("snowball-left");
1170   img_snowball_right = sprite_manager->load("snowball-right");
1171   img_snowball_squished_left = sprite_manager->load("snowball-squished-left");
1172   img_snowball_squished_right = sprite_manager->load("snowball-squished-right");
1173 }
1174
1175 void free_badguy_gfx()
1176 {
1177 }
1178
1179 // EOF //