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