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