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