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