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