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