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