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