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