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