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