- draw dead badguys/Tux above foreground tiles
[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           sound_manager->play_sound(sounds[SND_KICK], this);
360         }
361     }
362
363   if (!dying)
364     {
365       int changed = dir;
366       check_horizontal_bump();
367       if(mode == KICK && changed != dir)
368         {
369           sound_manager->play_sound(sounds[SND_RICOCHET], get_pos());
370         }
371     }
372
373   /* Handle mode timer: */
374   if (mode == FLAT)
375     {
376       if(!timer.check())
377         {
378           mode = NORMAL;
379           set_sprite(img_mriceblock_left, img_mriceblock_right);
380           physic.set_velocity( (dir == LEFT) ? -.8 : .8, 0);
381         }
382     }
383 }
384
385 void
386 BadGuy::check_horizontal_bump(bool checkcliff)
387 {
388     float halfheight = base.height / 2;
389     if (dir == LEFT && issolid( base.x, (int) base.y + halfheight))
390     {
391         if (kind == BAD_MRICEBLOCK && mode == KICK)
392             Sector::current()->trybreakbrick(Vector(base.x, base.y + halfheight), false);
393             
394         dir = RIGHT;
395         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
396         return;
397     }
398     if (dir == RIGHT && issolid( base.x + base.width, (int)base.y + halfheight))
399     {
400         if (kind == BAD_MRICEBLOCK && mode == KICK)
401             Sector::current()->trybreakbrick(
402                 Vector(base.x + base.width, (int) base.y + halfheight), false);
403             
404         dir = LEFT;
405         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
406         return;
407     }
408
409     // don't check for cliffs when we're falling
410     if(!checkcliff)
411         return;
412     if(!issolid(base.x + base.width/2, base.y + base.height))
413         return;
414     
415     if(dir == LEFT && !issolid(base.x, (int) base.y + base.height + halfheight))
416     {
417         dir = RIGHT;
418         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
419         return;
420     }
421     if(dir == RIGHT && !issolid(base.x + base.width,
422                 (int) base.y + base.height + halfheight))
423     {
424         dir = LEFT;
425         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
426         return;
427     }
428 }
429
430 void
431 BadGuy::fall()
432 {
433   /* Fall if we get off the ground: */
434   if (dying != DYING_FALLING)
435     {
436       if (!issolid(base.x+base.width/2, base.y + base.height))
437         {
438           // not solid below us? enable gravity
439           physic.enable_gravity(true);
440         }
441       else
442         {
443           /* Land: */
444           if (physic.get_velocity_y() < 0)
445             {
446               base.y = int((base.y + base.height)/32) * 32 - base.height;
447               physic.set_velocity_y(0);
448             }
449           // no gravity anymore please
450           physic.enable_gravity(false);
451
452           if (stay_on_platform && mode == NORMAL)
453             {
454               if (!issolid(base.x + ((dir == LEFT) ? 0 : base.width),
455                            base.y + base.height))
456                 {
457                   if (dir == LEFT)
458                   {
459                     dir = RIGHT;
460                     physic.set_velocity_x(fabsf(physic.get_velocity_x()));
461                   } 
462                   else
463                   {
464                     dir = LEFT;
465                     physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
466                   }
467                 }
468             }
469         }
470     }
471   else
472     {
473       physic.enable_gravity(true);
474     }
475 }
476
477 void
478 BadGuy::action_jumpy(double elapsed_time)
479 {
480   if(frozen_timer.check())
481     {
482     set_sprite(img_jumpy_left_iced, img_jumpy_left_iced);
483     return;
484     }
485
486   const float vy = physic.get_velocity_y();
487
488   // XXX: These tests *should* use location from ground, not velocity
489   if (fabsf(vy) > 5.6f)
490     set_sprite(img_jumpy_left_down, img_jumpy_left_down);
491   else if (fabsf(vy) > 5.3f)
492     set_sprite(img_jumpy_left_middle, img_jumpy_left_middle);
493   else
494     set_sprite(img_jumpy_left_up, img_jumpy_left_up);
495
496   Player& tux = *Sector::current()->player;
497
498   static const float JUMPV = 6;
499     
500   fall();
501   // jump when on ground
502   if(dying == DYING_NOT && issolid(base.x, base.y+32))
503     {
504       physic.set_velocity_y(JUMPV);
505       physic.enable_gravity(true);
506
507       mode = JUMPY_JUMP;
508     }
509   else if(mode == JUMPY_JUMP)
510     {
511       mode = NORMAL;
512     }
513
514   // set direction based on tux
515   if(tux.base.x > base.x)
516     dir = RIGHT;
517   else
518     dir = LEFT;
519
520   // move
521   physic.apply(elapsed_time, base.x, base.y);
522   if(dying == DYING_NOT)
523     collision_swept_object_map(&old_base, &base);
524 }
525
526 void
527 BadGuy::action_mrbomb(double elapsed_time)
528 {
529   if(frozen_timer.check())
530     {
531     set_sprite(img_mrbomb_iced_left, img_mrbomb_iced_right);
532     return;
533     }
534
535   if (dying == DYING_NOT)
536     check_horizontal_bump(true);
537
538   fall();
539
540   physic.apply(elapsed_time, base.x, base.y);
541   if (dying != DYING_FALLING)
542     collision_swept_object_map(&old_base,&base); 
543 }
544
545 void
546 BadGuy::action_bomb(double elapsed_time)
547 {
548   static const int TICKINGTIME = 1000;
549   static const int EXPLODETIME = 1000;
550     
551   fall();
552
553   if(mode == NORMAL) {
554     mode = BOMB_TICKING;
555     timer.start(TICKINGTIME);
556   } else if(!timer.check()) {
557     if(mode == BOMB_TICKING) {
558       mode = BOMB_EXPLODE;
559       set_sprite(img_mrbomb_explosion, img_mrbomb_explosion);
560       dying = DYING_NOT; // now the bomb hurts
561       timer.start(EXPLODETIME);
562
563       sound_manager->play_sound(sounds[SND_EXPLODE], this);
564     } else if(mode == BOMB_EXPLODE) {
565       remove_me();
566       return;
567     }
568   }
569
570   // move
571   physic.apply(elapsed_time, base.x, base.y);                 
572   collision_swept_object_map(&old_base,&base);
573 }
574
575 void
576 BadGuy::action_stalactite(double elapsed_time)
577 {
578   Player& tux = *Sector::current()->player;
579
580   static const int SHAKETIME = 800;
581   static const int RANGE = 40;
582     
583   if(mode == NORMAL) {
584     // start shaking when tux is below the stalactite and at least 40 pixels
585     // near
586     if(tux.base.x + 32 > base.x - RANGE && tux.base.x < base.x + 32 + RANGE
587             && tux.base.y + tux.base.height > base.y
588             && tux.dying == DYING_NOT) {
589       timer.start(SHAKETIME);
590       mode = STALACTITE_SHAKING;
591     }
592   } if(mode == STALACTITE_SHAKING) {
593     base.x = old_base.x + (rand() % 6) - 3; // TODO this could be done nicer...
594     if(!timer.check()) {
595       mode = STALACTITE_FALL;
596     }
597   } else if(mode == STALACTITE_FALL) {
598     fall();
599     /* Destroy if we collides with land */
600     if(issolid(base.x+base.width/2, base.y+base.height))
601     {
602       timer.start(2000);
603       dying = DYING_SQUISHED;
604       mode = FLAT;
605       set_sprite(img_stalactite_broken, img_stalactite_broken);
606     }
607   } else if(mode == FLAT) {
608     fall();
609   }
610
611   // move
612   physic.apply(elapsed_time, base.x, base.y);
613
614   if(dying == DYING_SQUISHED && !timer.check())
615     remove_me();
616 }
617
618 void
619 BadGuy::action_flame(double elapsed_time)
620 {
621     static const float radius = 100;
622     static const float speed = 0.02;
623     base.x = old_base.x + cos(angle) * radius;
624     base.y = old_base.y + sin(angle) * radius;
625
626     angle = fmodf(angle + elapsed_time * speed, 2*M_PI);
627 }
628
629 void
630 BadGuy::action_fish(double elapsed_time)
631 {
632   if(frozen_timer.check())
633     {
634     if(physic.get_velocity_y() < 0)
635       set_sprite(img_fish_iced_down, img_fish_iced_down);
636     else
637       set_sprite(img_fish_iced, img_fish_iced);
638
639     return;
640     }
641
642   static const float JUMPV = 6;
643   static const int WAITTIME = 1000;
644     
645   // go in wait mode when back in water
646   if(dying == DYING_NOT 
647       && gettile(base.x, base.y + base.height)
648       && gettile(base.x, base.y + base.height)->attributes & Tile::WATER
649       && physic.get_velocity_y() <= 0 && mode == NORMAL)
650     {
651       mode = FISH_WAIT;
652       set_sprite(0, 0);
653       physic.set_velocity(0, 0);
654       physic.enable_gravity(false);
655       timer.start(WAITTIME);
656     }
657   else if(mode == FISH_WAIT && !timer.check())
658     {
659       // jump again
660       set_sprite(img_fish, img_fish);
661       mode = NORMAL;
662       physic.set_velocity(0, JUMPV);
663       physic.enable_gravity(true);
664     }
665
666   physic.apply(elapsed_time, base.x, base.y);
667   if(dying == DYING_NOT)
668     collision_swept_object_map(&old_base, &base);
669
670   if(physic.get_velocity_y() < 0)
671     set_sprite(img_fish_down, img_fish_down);
672 }
673
674 void
675 BadGuy::action_bouncingsnowball(double elapsed_time)
676 {
677   static const float JUMPV = 4.5;
678     
679   fall();
680
681   // jump when on ground
682   if(dying == DYING_NOT && issolid(base.x, base.y+32))
683     {
684       physic.set_velocity_y(JUMPV);
685       physic.enable_gravity(true);
686     }                                                     
687   else
688     {
689       mode = NORMAL;
690     }
691
692   // check for right/left collisions
693   check_horizontal_bump();
694
695   physic.apply(elapsed_time, base.x, base.y);
696   if(dying == DYING_NOT)
697     collision_swept_object_map(&old_base, &base);
698
699   // Handle dying timer:
700   if (dying == DYING_SQUISHED && !timer.check())
701     remove_me();
702 }
703
704 void
705 BadGuy::action_flyingsnowball(double elapsed_time)
706 {
707   static const float FLYINGSPEED = 1;
708   static const int DIRCHANGETIME = 1000;
709     
710   // go into flyup mode if none specified yet
711   if(dying == DYING_NOT && mode == NORMAL) {
712     mode = FLY_UP;
713     physic.set_velocity_y(FLYINGSPEED);
714     timer.start(DIRCHANGETIME/2);
715   }
716
717   if(dying == DYING_NOT && !timer.check()) {
718     if(mode == FLY_UP) {
719       mode = FLY_DOWN;
720       physic.set_velocity_y(-FLYINGSPEED);
721     } else if(mode == FLY_DOWN) {
722       mode = FLY_UP;
723       physic.set_velocity_y(FLYINGSPEED);
724     }
725     timer.start(DIRCHANGETIME);
726   }
727
728   if(dying != DYING_NOT)
729     physic.enable_gravity(true);
730
731   physic.apply(elapsed_time, base.x, base.y);
732   if(dying == DYING_NOT || dying == DYING_SQUISHED)
733     collision_swept_object_map(&old_base, &base);
734
735   // Handle dying timer:
736   if (dying == DYING_SQUISHED && !timer.check())
737     remove_me();
738 }
739
740 void
741 BadGuy::action_spiky(double elapsed_time)
742 {
743   if(frozen_timer.check())
744     {
745     set_sprite(img_spiky_iced_left, img_spiky_iced_right);
746     return;
747     }
748
749   if (dying == DYING_NOT)
750     check_horizontal_bump();
751
752   fall();
753 #if 0
754   // jump when we're about to fall
755   if (physic.get_velocity_y() == 0 && 
756           !issolid(base.x+base.width/2, base.y + base.height)) {
757     physic.enable_gravity(true);
758     physic.set_velocity_y(2);
759   }
760 #endif
761
762   physic.apply(elapsed_time, base.x, base.y);
763   if (dying != DYING_FALLING)
764     collision_swept_object_map(&old_base,&base);   
765 }
766
767 void
768 BadGuy::action_snowball(double elapsed_time)
769 {
770   if (dying == DYING_NOT)
771     check_horizontal_bump();
772
773   fall();
774
775   physic.apply(elapsed_time, base.x, base.y);
776   if (dying != DYING_FALLING)
777     collision_swept_object_map(&old_base,&base);
778
779   // Handle dying timer:
780   if (dying == DYING_SQUISHED && !timer.check())
781     remove_me();                                  
782 }
783
784 void
785 BadGuy::action_wingling(double elapsed_time)
786 {
787   if (dying != DYING_NOT)
788     physic.enable_gravity(true);
789   else
790   {
791     Player& tux = *Sector::current()->player;
792     int dirsign = physic.get_velocity_x() < 0 ? -1 : 1;
793
794     if (fabsf(tux.base.x - base.x) < 150 && base.y < tux.base.y && tux.dying == DYING_NOT)
795     {
796       if (target.x < 0 && target.y < 0)
797       {
798         target.x = tux.base.x;
799         target.y = tux.base.y;
800         physic.set_velocity(dirsign * 1.5f, -2.25f);
801       }
802     }
803     else if (base.y >= target.y - 16)
804       physic.set_velocity(dirsign * WINGLING_FLY_SPEED, 0);
805   }
806
807   physic.apply(elapsed_time, base.x, base.y);
808
809
810   // Handle dying timer:
811   if (dying == DYING_SQUISHED && !timer.check())
812     remove_me();
813
814   // TODO: Winglings should be removed after flying off the screen
815 }
816
817 void
818 BadGuy::action_walkingtree(double elapsed_time)
819 {
820   Player& tux = *Sector::current()->player;
821   Direction v_dir = physic.get_velocity_x() < 0 ? LEFT : RIGHT;
822
823   if (dying == DYING_NOT)
824     check_horizontal_bump();
825
826   fall();
827
828   if (mode == BGM_BIG && tux.dying == DYING_NOT)
829   {
830     if ((tux.base.x + tux.base.width/2 > base.x + base.width/2) && v_dir == LEFT)
831     {
832       dir = RIGHT;
833       physic.set_velocity_x(-physic.get_velocity_x());
834     }
835     else if ((tux.base.x + tux.base.width/2 < base.x + base.width/2) && v_dir == RIGHT)
836     {
837       dir = LEFT;
838       physic.set_velocity_x(-physic.get_velocity_x());
839     }
840   }
841   
842
843   physic.apply(elapsed_time, base.x, base.y);
844   if (dying != DYING_FALLING)
845     collision_swept_object_map(&old_base,&base);
846
847   // Handle dying timer:
848   if (dying == DYING_SQUISHED && !timer.check())
849     remove_me();
850 }
851
852 void
853 BadGuy::action(float elapsed_time)
854 {
855   float scroll_x = Sector::current()->camera->get_translation().x;
856   float scroll_y = Sector::current()->camera->get_translation().y;
857   
858   // BadGuy fall below the ground
859   if (base.y > Sector::current()->solids->get_height() * 32) {
860     remove_me();
861     return;
862   }
863
864   // Kill us if we landed on spikes
865   if (dying == DYING_NOT
866       && (kind != BAD_STALACTITE && kind != BAD_FLAME && kind != BAD_BOMB)
867       && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
868       ||  isspike(base.x, base.y + base.height)
869       ||  isspike(base.x + base.width, base.y + base.height)))
870       {
871          physic.set_velocity_y(3);
872          kill_me(0);
873       }
874
875   if(!seen) {
876     /* activate badguys if they're just inside the offscreen_distance around the
877      * screen. Don't activate them inside the screen, since that might have the
878      * effect of badguys suddenly popping up from nowhere
879      */
880     if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
881         start_position.x < scroll_x - base.width)
882       activate(RIGHT);
883     else if(start_position.x > scroll_y - Y_OFFSCREEN_DISTANCE &&
884         start_position.y < scroll_y - base.height)
885       activate(LEFT);
886     else if(start_position.x > scroll_x + screen->w &&
887         start_position.x < scroll_x + screen->w + X_OFFSCREEN_DISTANCE)
888       activate(LEFT);
889     else if(start_position.y > scroll_y + screen->h &&
890         start_position.y < scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
891       activate(LEFT);
892   } else {
893     if(base.x + base.width < scroll_x - X_OFFSCREEN_DISTANCE*4
894       || base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE*4
895       || base.y + base.height < scroll_y - Y_OFFSCREEN_DISTANCE*4
896       || base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE*4) {
897       seen = false;
898       if(dying != DYING_NOT)
899         remove_me();
900     }
901   }
902   
903   if(!seen)
904     return;
905   
906   switch (kind)
907     {
908     case BAD_MRICEBLOCK:
909       action_mriceblock(elapsed_time);
910       break;
911   
912     case BAD_JUMPY:
913       action_jumpy(elapsed_time);
914       break;
915
916     case BAD_MRBOMB:
917       action_mrbomb(elapsed_time);
918       break;
919     
920     case BAD_BOMB:
921       action_bomb(elapsed_time);
922       break;
923
924     case BAD_STALACTITE:
925       action_stalactite(elapsed_time);
926       break;
927
928     case BAD_FLAME:
929       action_flame(elapsed_time);
930       break;
931
932     case BAD_FISH:
933       action_fish(elapsed_time);
934       break;
935
936     case BAD_BOUNCINGSNOWBALL:
937       action_bouncingsnowball(elapsed_time);
938       break;
939
940     case BAD_FLYINGSNOWBALL:
941       action_flyingsnowball(elapsed_time);
942       break;
943
944     case BAD_SPIKY:
945       action_spiky(elapsed_time);
946       break;
947
948     case BAD_SNOWBALL:
949       action_snowball(elapsed_time);
950       break;
951
952     case BAD_WINGLING:
953       action_wingling(elapsed_time);
954       break;
955
956     case BAD_WALKINGTREE:
957       action_walkingtree(elapsed_time);
958       break;
959
960     default:
961       break;
962     }
963 }
964
965 void
966 BadGuy::draw(DrawingContext& context)
967 {
968   Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
969   if(sprite == 0)
970     return;
971
972   if(dying == DYING_FALLING && physic.get_velocity_y() < 0)
973     sprite->draw(context, Vector(base.x, base.y), LAYER_FOREGROUNDTILES+1, VERTICAL_FLIP);
974   else
975     sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
976
977   if(debug_mode)
978     context.draw_filled_rect(Vector(base.x, base.y),
979         Vector(base.width, base.height), Color(75,0,75, 150), LAYER_OBJECTS+1);
980 }
981
982 void
983 BadGuy::set_sprite(Sprite* left, Sprite* right) 
984 {
985   if (1)
986     {
987       base.width = 32;
988       base.height = 32;
989     }
990   else
991     {
992       // FIXME: Using the image size for the physics and collision is
993       // a bad idea, since images should always overlap there physical
994       // representation
995       if(left != 0) {
996         if(base.width == 0 && base.height == 0) {
997           base.width  = left->get_width();
998           base.height = left->get_height();
999         } else if(base.width != left->get_width() || base.height != left->get_height()) {
1000           base.x -= (left->get_width() - base.width) / 2;
1001           base.y -= left->get_height() - base.height;
1002           base.width = left->get_width();
1003           base.height = left->get_height();
1004           old_base = base;
1005         }
1006       } else {
1007         base.width = base.height = 0;
1008       }
1009     }
1010
1011   animation_offset = 0;
1012   sprite_left  = left;
1013   sprite_right = right;
1014 }
1015
1016 void
1017 BadGuy::bump()
1018 {
1019   // these can't be bumped
1020   if(kind == BAD_FLAME || kind == BAD_BOMB || kind == BAD_FISH
1021       || kind == BAD_FLYINGSNOWBALL)
1022     return;
1023
1024   physic.set_velocity_y(3);
1025   kill_me(25);
1026 }
1027
1028 void
1029 BadGuy::make_player_jump(Player* player)
1030 {
1031   player->physic.set_velocity_y(2);
1032   player->base.y = base.y - player->base.height - 2;
1033 }
1034
1035 void
1036 BadGuy::squish_me(Player* player)
1037 {
1038   make_player_jump(player);
1039     
1040   Sector::current()->add_score(Vector(base.x, base.y),
1041                               50 * player_status.score_multiplier);
1042
1043   sound_manager->play_sound(sounds[SND_SQUISH], get_pos());
1044   player_status.score_multiplier++;
1045
1046   dying = DYING_SQUISHED;
1047   timer.start(2000);
1048   physic.set_velocity(0, 0);
1049 }
1050
1051 void
1052 BadGuy::squish(Player* player)
1053 {
1054   static const int MAX_ICEBLOCK_SQUICHES = 10;
1055     
1056   if(kind == BAD_MRBOMB) {
1057     // mrbomb transforms into a bomb now
1058     explode(false);
1059     
1060     make_player_jump(player);
1061     Sector::current()->add_score(Vector(base.x, base.y),
1062                                 50 * player_status.score_multiplier);
1063     sound_manager->play_sound(sounds[SND_SQUISH], get_pos());
1064     player_status.score_multiplier++;
1065     return;
1066
1067   } else if (kind == BAD_MRICEBLOCK) {
1068     if (mode == NORMAL || mode == KICK)
1069       {
1070         /* Flatten! */
1071         sound_manager->play_sound(sounds[SND_STOMP], get_pos());
1072         mode = FLAT;
1073         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1074         physic.set_velocity_x(0);
1075
1076         timer.start(4000);
1077       } else if (mode == FLAT) {
1078         /* Kick! */
1079         sound_manager->play_sound(sounds[SND_KICK], this);
1080
1081         if (player->base.x < base.x + (base.width/2)) {
1082           physic.set_velocity_x(5);
1083           dir = RIGHT;
1084         } else {
1085           physic.set_velocity_x(-5);
1086           dir = LEFT;
1087         }
1088
1089         mode = KICK;
1090         player->kick_timer.start(KICKING_TIME);
1091         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1092       }
1093
1094     make_player_jump(player);
1095
1096     player_status.score_multiplier++;
1097
1098     // check for maximum number of squishes
1099     squishcount++;
1100     if(squishcount >= MAX_ICEBLOCK_SQUICHES) {
1101       kill_me(50);
1102       return;
1103     }
1104     
1105     return;
1106   } else if(kind == BAD_FISH) {
1107     // fish can only be killed when falling down
1108     if(physic.get_velocity_y() >= 0)
1109       return;
1110       
1111     make_player_jump(player);
1112               
1113     Sector::current()->add_score(Vector(base.x, base.y),
1114                                 25 * player_status.score_multiplier);
1115     player_status.score_multiplier++;
1116      
1117     // simply remove the fish...
1118     remove_me();
1119     return;
1120   } else if(kind == BAD_BOUNCINGSNOWBALL) {
1121     squish_me(player);
1122     set_sprite(img_bouncingsnowball_squished,img_bouncingsnowball_squished);
1123     return;
1124   } else if(kind == BAD_FLYINGSNOWBALL) {
1125     squish_me(player);
1126     set_sprite(img_flyingsnowball_squished,img_flyingsnowball_squished);
1127     return;
1128   } else if(kind == BAD_SNOWBALL) {
1129     squish_me(player);
1130     set_sprite(img_snowball_squished_left, img_snowball_squished_right);
1131     return;
1132   } else if(kind == BAD_WINGLING) {
1133     squish_me(player);
1134     set_sprite(img_wingling_left, img_wingling_left);
1135   } else if(kind == BAD_WALKINGTREE) {
1136     if (mode == BGM_BIG)
1137     {
1138       set_sprite(img_walkingtree_left_small, img_walkingtree_left_small);
1139       physic.set_velocity_x(physic.get_velocity_x() * 2.0f);
1140       // XXX magic number: 66 is BGM_BIG height
1141
1142       make_player_jump(player);
1143       base.y += 66 - base.height;
1144               
1145       Sector::current()->add_score(Vector(base.x, base.y),
1146                                 25 * player_status.score_multiplier);
1147       player_status.score_multiplier++;
1148
1149       mode = BGM_SMALL;
1150     }
1151     else
1152       squish_me(player);
1153   }
1154 }
1155
1156 void
1157 BadGuy::kill_me(int score)
1158 {
1159   if(kind == BAD_BOMB)
1160     return;
1161
1162   dying = DYING_FALLING;
1163   if(kind == BAD_MRICEBLOCK) {
1164     set_sprite(img_mriceblock_falling_left, img_mriceblock_falling_right);
1165     if(mode == HELD) {
1166       mode = NORMAL;
1167       Player& tux = *Sector::current()->player;
1168       tux.holding_something = false;
1169     }
1170   }
1171
1172   physic.enable_gravity(true);
1173
1174   /* Gain some points: */
1175   if (score != 0)
1176     Sector::current()->add_score(Vector(base.x, base.y),
1177                                 score * player_status.score_multiplier);
1178
1179   /* Play death sound: */
1180   sound_manager->play_sound(sounds[SND_FALL], this);
1181 }
1182
1183 void
1184 BadGuy::explode(bool right_way)
1185 {
1186   BadGuy *badguy = Sector::current()->add_bad_guy(base.x, base.y, BAD_BOMB);
1187   if(right_way)
1188     {
1189     badguy->timer.start(0);
1190     badguy->mode = BOMB_TICKING;
1191     }
1192
1193   remove_me();
1194 }
1195
1196 void
1197 BadGuy::collision(const MovingObject&, int)
1198 {
1199   // later
1200 }
1201
1202 void
1203 BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
1204 {
1205   BadGuy* pbad_c    = NULL;
1206   Bullet* pbullet_c = NULL;
1207
1208   if(type == COLLISION_BUMP) {
1209     bump();
1210     return;
1211   }
1212
1213   if(type == COLLISION_SQUISH) {
1214     Player* player = static_cast<Player*>(p_c_object);
1215     squish(player);
1216     return;
1217   }
1218
1219   /* COLLISION_NORMAL */
1220   switch (c_object)
1221     {
1222     case CO_BULLET:
1223       pbullet_c = (Bullet*) p_c_object;
1224
1225       if(pbullet_c->kind == FIRE_BULLET)
1226         {
1227         if (kind != BAD_BOMB && kind != BAD_STALACTITE && kind != BAD_FLAME)
1228           kill_me(10);
1229         }
1230       else if(pbullet_c->kind == ICE_BULLET)
1231         {
1232         //if(kind == BAD_FLAME)
1233         //  kill_me(10);
1234         //else
1235           frozen_timer.start(FROZEN_TIME);
1236         }
1237       break;
1238
1239     case CO_BADGUY:
1240       pbad_c = (BadGuy*) p_c_object;
1241
1242
1243       /* If we're a kicked mriceblock, kill [almost] any badguys we hit */
1244       if(kind == BAD_MRICEBLOCK && mode == KICK &&
1245          kind != BAD_FLAME && kind != BAD_BOMB && kind != BAD_STALACTITE)
1246         {
1247           pbad_c->kill_me(25);
1248         }
1249
1250       // a held mriceblock kills the enemy too but falls to ground then
1251       else if(kind == BAD_MRICEBLOCK && mode == HELD)
1252         {
1253           pbad_c->kill_me(25);
1254           kill_me(0);
1255         }
1256
1257       /* Kill badguys that run into exploding bomb */
1258       else if (kind == BAD_BOMB && dying == DYING_NOT)
1259       {
1260         if (pbad_c->kind == BAD_MRBOMB)
1261         {
1262           // mrbomb transforms into a bomb now
1263           pbad_c->explode(true);
1264           return;
1265         }
1266         else if (pbad_c->kind != BAD_MRBOMB)
1267         {
1268           pbad_c->kill_me(50);
1269         }
1270       }
1271
1272       /* Kill any badguys that get hit by stalactite */
1273       else if (kind == BAD_STALACTITE && dying == DYING_NOT)
1274       {
1275         if (pbad_c->kind == BAD_MRBOMB)
1276         {
1277           // mrbomb transforms into a bomb now
1278           pbad_c->explode(false);
1279           return;
1280         }
1281         else
1282           pbad_c->kill_me(50);
1283       }
1284
1285       /* When enemies run into eachother, make them change directions */
1286       else
1287       {
1288         // Wingling doesn't interact with other badguys
1289         if (pbad_c->kind == BAD_WINGLING || kind == BAD_WINGLING)
1290           break;
1291
1292         // Jumpy, fish, flame, stalactites, wingling are exceptions
1293         if (pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FLAME
1294             || pbad_c->kind == BAD_STALACTITE || pbad_c->kind == BAD_FISH)
1295           break;
1296
1297         // Bounce off of other badguy if we land on top of him
1298         if (base.y + base.height < pbad_c->base.y + pbad_c->base.height)
1299         {
1300           if (pbad_c->dir == LEFT)
1301           {
1302             dir = RIGHT;
1303             physic.set_velocity(fabsf(physic.get_velocity_x()), 2);
1304           }
1305           else if (pbad_c->dir == RIGHT)
1306           {
1307             dir = LEFT;
1308             physic.set_velocity(-fabsf(physic.get_velocity_x()), 2);
1309           }
1310
1311           break;
1312         }
1313         else if (base.y + base.height > pbad_c->base.y + pbad_c->base.height)
1314           break;
1315
1316         if (pbad_c->kind != BAD_FLAME)
1317           {
1318             if (dir == LEFT)
1319             {
1320               dir = RIGHT;
1321               physic.set_velocity_x(fabsf(physic.get_velocity_x()));
1322
1323               // in case badguys get "jammed"
1324               if (physic.get_velocity_x() != 0)
1325                 base.x = pbad_c->base.x + pbad_c->base.width;
1326             }
1327             else if (dir == RIGHT)
1328             {
1329               dir = LEFT;
1330               physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
1331             }
1332
1333           }
1334       }
1335       
1336       break;
1337
1338     case CO_PLAYER:
1339       Player* player = static_cast<Player*>(p_c_object);
1340       /* Get kicked if were flat */
1341       if (mode == FLAT && !dying)
1342       {
1343         sound_manager->play_sound(sounds[SND_KICK], this);
1344
1345         // Hit from left side
1346         if (player->base.x < base.x) {
1347           physic.set_velocity_x(5);
1348           dir = RIGHT;
1349         }
1350         // Hit from right side
1351         else {
1352           physic.set_velocity_x(-5);
1353           dir = LEFT;
1354         }
1355
1356         mode = KICK;
1357         player->kick_timer.start(KICKING_TIME);
1358         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1359       }
1360       break;
1361
1362     }
1363 }
1364
1365
1366 //---------------------------------------------------------------------------
1367
1368 void load_badguy_gfx()
1369 {
1370   img_mriceblock_flat_left = sprite_manager->load("mriceblock-flat-left");
1371   img_mriceblock_flat_right = sprite_manager->load("mriceblock-flat-right");
1372   img_mriceblock_falling_left = sprite_manager->load("mriceblock-falling-left");
1373   img_mriceblock_falling_right = sprite_manager->load("mriceblock-falling-right");
1374   img_mriceblock_left = sprite_manager->load("mriceblock-left");
1375   img_mriceblock_right = sprite_manager->load("mriceblock-right");
1376   img_jumpy_left_up = sprite_manager->load("jumpy-left-up");
1377   img_jumpy_left_down = sprite_manager->load("jumpy-left-down");
1378   img_jumpy_left_middle = sprite_manager->load("jumpy-left-middle");
1379   img_jumpy_left_iced = sprite_manager->load("jumpy-left-iced");
1380   img_mrbomb_left = sprite_manager->load("mrbomb-left");
1381   img_mrbomb_right = sprite_manager->load("mrbomb-right");
1382   img_mrbomb_iced_left = sprite_manager->load("mrbomb-iced-left");
1383   img_mrbomb_iced_right = sprite_manager->load("mrbomb-iced-right");
1384   img_mrbomb_ticking_left = sprite_manager->load("mrbomb-ticking-left");
1385   img_mrbomb_ticking_right = sprite_manager->load("mrbomb-ticking-right");
1386   img_mrbomb_explosion = sprite_manager->load("mrbomb-explosion");
1387   img_stalactite = sprite_manager->load("stalactite");
1388   img_stalactite_broken = sprite_manager->load("stalactite-broken");
1389   img_flame = sprite_manager->load("flame");
1390   img_fish = sprite_manager->load("fish");
1391   img_fish_down = sprite_manager->load("fish-down");
1392   img_fish_iced = sprite_manager->load("fish-iced");
1393   img_fish_iced_down = sprite_manager->load("fish-iced-down");
1394   img_bouncingsnowball_left = sprite_manager->load("bouncingsnowball-left");
1395   img_bouncingsnowball_right = sprite_manager->load("bouncingsnowball-right");
1396   img_bouncingsnowball_squished = sprite_manager->load("bouncingsnowball-squished");
1397   img_flyingsnowball = sprite_manager->load("flyingsnowball");
1398   img_flyingsnowball_squished = sprite_manager->load("flyingsnowball-squished");
1399   img_spiky_left = sprite_manager->load("spiky-left");
1400   img_spiky_right = sprite_manager->load("spiky-right");
1401   img_spiky_iced_left = sprite_manager->load("spiky-iced-left");
1402   img_spiky_iced_right = sprite_manager->load("spiky-iced-right");
1403   img_snowball_left = sprite_manager->load("snowball-left");
1404   img_snowball_right = sprite_manager->load("snowball-right");
1405   img_snowball_squished_left = sprite_manager->load("snowball-squished-left");
1406   img_snowball_squished_right = sprite_manager->load("snowball-squished-right");
1407   img_wingling_left = sprite_manager->load("wingling-left");
1408   img_walkingtree_left = sprite_manager->load("walkingtree-left");
1409   img_walkingtree_left_small = sprite_manager->load("walkingtree-left-small");
1410 }
1411
1412 void free_badguy_gfx()
1413 {
1414 }
1415
1416 // EOF //