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