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