Implemented Tux growing animation.
[supertux.git] / src / badguy.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de>
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU General Public License
10 //  as published by the Free Software Foundation; either version 2
11 //  of the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU General Public License for more details.
17 // 
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 //  02111-1307, USA.
22
23 #include <iostream>
24 #include <math.h>
25
26 #include "globals.h"
27 #include "defines.h"
28 #include "badguy.h"
29 #include "scene.h"
30 #include "screen.h"
31 #include "world.h"
32 #include "tile.h"
33 #include "resources.h"
34 #include "sprite_manager.h"
35 #include "gameloop.h"
36 #include "display_manager.h"
37 #include "lispwriter.h"
38 #include "camera.h"
39
40 Sprite* img_mriceblock_flat_left;
41 Sprite* img_mriceblock_flat_right;
42 Sprite* img_mriceblock_falling_left;
43 Sprite* img_mriceblock_falling_right;
44 Sprite* img_mriceblock_left;
45 Sprite* img_mriceblock_right;
46 Sprite* img_jumpy_left_up;
47 Sprite* img_jumpy_left_down;
48 Sprite* img_jumpy_left_middle;
49 Sprite* img_jumpy_left_iced;
50 Sprite* img_mrbomb_left;
51 Sprite* img_mrbomb_right;
52 Sprite* img_mrbomb_iced_left;
53 Sprite* img_mrbomb_iced_right;
54 Sprite* img_mrbomb_ticking_left;
55 Sprite* img_mrbomb_ticking_right;
56 Sprite* img_mrbomb_explosion;
57 Sprite* img_stalactite;
58 Sprite* img_stalactite_broken;
59 Sprite* img_flame;
60 Sprite* img_fish;
61 Sprite* img_fish_down;
62 Sprite* img_fish_iced;
63 Sprite* img_fish_iced_down;
64 Sprite* img_bouncingsnowball_left;
65 Sprite* img_bouncingsnowball_right;
66 Sprite* img_bouncingsnowball_squished;
67 Sprite* img_flyingsnowball;
68 Sprite* img_flyingsnowball_squished;
69 Sprite* img_spiky_left;
70 Sprite* img_spiky_right;
71 Sprite* img_spiky_iced_left;
72 Sprite* img_spiky_iced_right;
73 Sprite* img_snowball_left;
74 Sprite* img_snowball_right;
75 Sprite* img_snowball_squished_left;
76 Sprite* img_snowball_squished_right;
77 Sprite* img_wingling_left;
78 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 == "bouncingsnowball")
99     return BAD_BOUNCINGSNOWBALL;
100   else if (str == "flyingsnowball")
101     return BAD_FLYINGSNOWBALL;
102   else if (str == "spiky")
103     return BAD_SPIKY;
104   else if (str == "snowball" || str == "bsod") // was bsod in old maps
105     return BAD_SNOWBALL;
106   else if (str == "wingling")
107     return BAD_WINGLING;
108   else if (str == "walkingtree")
109     return BAD_WALKINGTREE;
110   else
111     {
112       printf("Couldn't convert badguy: '%s'\n", str.c_str());
113       return BAD_SNOWBALL;
114     }
115 }
116
117 std::string badguykind_to_string(BadGuyKind kind)
118 {
119   switch(kind)
120     {
121     case BAD_JUMPY:
122       return "jumpy";
123       break;
124     case BAD_MRICEBLOCK:
125       return "mriceblock";
126       break;
127     case BAD_MRBOMB:
128       return "mrbomb";
129       break;
130     case BAD_STALACTITE:
131       return "stalactite";
132       break;
133     case BAD_FLAME:
134       return "flame";
135       break;
136     case BAD_FISH:
137       return "fish";
138       break;
139     case BAD_BOUNCINGSNOWBALL:
140       return "bouncingsnowball";
141       break;
142     case BAD_FLYINGSNOWBALL:
143       return "flyingsnowball";
144       break;
145     case BAD_SPIKY:
146       return "spiky";
147       break;
148     case BAD_SNOWBALL:
149       return "snowball";
150       break;
151     case BAD_WINGLING:
152       return "wingling";
153       break;
154     case BAD_WALKINGTREE:
155       return "walkingtree";
156     default:
157       return "snowball";
158     }
159 }
160
161 BadGuy::BadGuy(DisplayManager& display_manager, BadGuyKind kind_,
162     LispReader& lispreader)
163   : removable(false), squishcount(0)
164 {
165   display_manager.add_drawable(this, LAYER_OBJECTS);
166
167   lispreader.read_float("x", &start_position.x);
168   lispreader.read_float("y", &start_position.y);
169
170   kind     = kind_;
171
172   stay_on_platform = false;
173   lispreader.read_bool("stay-on-platform", &stay_on_platform);  
174
175   init();
176 }
177
178 BadGuy::BadGuy(DisplayManager& display_manager, BadGuyKind kind_,
179     float x, float y)
180 {
181   display_manager.add_drawable(this, LAYER_OBJECTS);
182
183   start_position.x = x;
184   start_position.y = y;
185   stay_on_platform = false;
186
187   kind     = kind_;
188   
189   init();
190 }
191
192 BadGuy::~BadGuy()
193 {
194 }
195
196 void
197 BadGuy::init()
198 {
199   base.x = 0;
200   base.y = 0;
201   base.width  = 0;
202   base.height = 0;
203   
204   mode     = NORMAL;
205   dying    = DYING_NOT;
206   old_base = base;
207   dir      = LEFT;
208   seen     = false;
209   animation_offset = 0;
210   target.x = target.y = -1;
211   sprite_left = sprite_right = 0;
212   physic.reset();
213   frozen_timer.init(true);
214   timer.init(true);
215
216   // if we're in a solid tile at start correct that now
217   if(kind != BAD_FLAME && kind != BAD_FISH && 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(World::current()->camera) {
226     Vector scroll = World::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   }
235 }
236
237 void
238 BadGuy::write(LispWriter& writer)
239 {
240   writer.start_list(badguykind_to_string(kind));
241
242   writer.write_float("x", base.x);
243   writer.write_float("y", base.y);
244   writer.write_bool("stay-on-platform", stay_on_platform);  
245
246   writer.end_list(badguykind_to_string(kind));
247 }
248
249 void
250 BadGuy::activate(Direction activation_dir)
251 {
252   mode     = NORMAL;
253   animation_offset = 0;
254   target.x = target.y = -1;
255   physic.reset();
256   frozen_timer.init(true);
257   timer.init(true);
258
259   dir = activation_dir;
260   float dirsign = activation_dir == LEFT ? -1 : 1;
261   
262   if(kind == BAD_MRBOMB) {
263     physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
264     set_sprite(img_mrbomb_left, img_mrbomb_right);
265   } else if (kind == BAD_MRICEBLOCK) {
266     physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
267     set_sprite(img_mriceblock_left, img_mriceblock_right);
268   } else if(kind == BAD_JUMPY) {
269     set_sprite(img_jumpy_left_up, img_jumpy_left_up);
270   } else if(kind == BAD_BOMB) {
271     set_sprite(img_mrbomb_ticking_left, img_mrbomb_ticking_right);
272     // hack so that the bomb doesn't hurt until it expldes...           
273     dying = DYING_SQUISHED;
274   } else if(kind == BAD_FLAME) {
275     angle = 0;
276     physic.enable_gravity(false);
277     set_sprite(img_flame, img_flame);
278   } else if(kind == BAD_BOUNCINGSNOWBALL) {
279     physic.set_velocity(dirsign * 1.3, 0);
280     set_sprite(img_bouncingsnowball_left, img_bouncingsnowball_right);
281   } else if(kind == BAD_STALACTITE) {
282     physic.enable_gravity(false);
283     set_sprite(img_stalactite, img_stalactite);
284   } else if(kind == BAD_FISH) {
285     set_sprite(img_fish, img_fish);
286     physic.enable_gravity(true);
287   } else if(kind == BAD_FLYINGSNOWBALL) {
288     set_sprite(img_flyingsnowball, img_flyingsnowball);
289     physic.enable_gravity(false);
290   } else if(kind == BAD_SPIKY) {
291     physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
292     set_sprite(img_spiky_left, img_spiky_right);
293   } else if(kind == BAD_SNOWBALL) {
294     physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
295     set_sprite(img_snowball_left, img_snowball_right);
296   } else if(kind == BAD_WINGLING) {
297     physic.set_velocity(dirsign * WINGLING_FLY_SPEED, 0);
298     physic.enable_gravity(false);
299     set_sprite(img_wingling_left, img_wingling_left);
300   } else if (kind == BAD_WALKINGTREE) {
301     // TODO: why isn't the height/width being set properly in set_sprite?
302     physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
303     mode = BGM_BIG;
304     set_sprite(img_walkingtree_left, img_walkingtree_left);
305     base.width = 66;
306     base.height = 66;
307   }
308
309   base.x = start_position.x;
310   base.y = start_position.y;  
311   old_base = base;
312   seen = true;
313 }
314
315 void
316 BadGuy::action_mriceblock(double elapsed_time)
317 {
318   Player& tux = *World::current()->get_tux();
319
320   if(mode != HELD)
321     fall();
322   
323   /* Move left/right: */
324   if (mode != HELD)
325     {
326       // move
327       physic.apply(elapsed_time, base.x, base.y);
328       if (dying != DYING_FALLING)
329         collision_swept_object_map(&old_base,&base);
330     }
331   else if (mode == HELD)
332     { /* FIXME: The pbad object shouldn't know about pplayer objects. */
333       /* If we're holding the iceblock */
334       dir = tux.dir;
335       if(dir==RIGHT)
336         {
337           base.x = tux.base.x + 16;
338           base.y = tux.base.y + tux.base.height/1.5 - base.height;
339         }
340       else /* facing left */
341         {
342           base.x = tux.base.x - 16;
343           base.y = tux.base.y + tux.base.height/1.5 - base.height;
344         }
345       if(collision_object_map(base))
346         {
347           base.x = tux.base.x;
348           base.y = tux.base.y + tux.base.height/1.5 - base.height;
349         }
350
351       if(tux.input.fire != DOWN) /* SHOOT! */
352         {
353           if(dir == LEFT)
354             base.x -= 24;
355           else
356             base.x += 24;
357           old_base = base;
358
359           mode=KICK;
360           tux.kick_timer.start(KICKING_TIME);
361           set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
362           physic.set_velocity_x((dir == LEFT) ? -3.5 : 3.5);
363           play_sound(sounds[SND_KICK],SOUND_CENTER_SPEAKER);
364         }
365     }
366
367   if (!dying)
368     {
369       int changed = dir;
370       check_horizontal_bump();
371       if(mode == KICK && changed != dir)
372         {
373           float scroll_x = World::current()->camera->get_translation().x;
374           
375           /* handle stereo sound (number 10 should be tweaked...)*/
376           if (base.x < scroll_x + screen->w/2 - 10)
377             play_sound(sounds[SND_RICOCHET], SOUND_LEFT_SPEAKER);
378           else if (base.x > scroll_x + screen->w/2 + 10)
379             play_sound(sounds[SND_RICOCHET], SOUND_RIGHT_SPEAKER);
380           else
381             play_sound(sounds[SND_RICOCHET], SOUND_CENTER_SPEAKER);
382         }
383     }
384
385   /* Handle mode timer: */
386   if (mode == FLAT)
387     {
388       if(!timer.check())
389         {
390           mode = NORMAL;
391           set_sprite(img_mriceblock_left, img_mriceblock_right);
392           physic.set_velocity( (dir == LEFT) ? -.8 : .8, 0);
393         }
394     }
395 }
396
397 void
398 BadGuy::check_horizontal_bump(bool checkcliff)
399 {
400     float halfheight = base.height / 2;
401     if (dir == LEFT && issolid( base.x, (int) base.y + halfheight))
402     {
403         if (kind == BAD_MRICEBLOCK && mode == KICK)
404             World::current()->trybreakbrick(base.x, (int) base.y + halfheight, false);
405             
406         dir = RIGHT;
407         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
408         return;
409     }
410     if (dir == RIGHT && issolid( base.x + base.width, (int)base.y + halfheight))
411     {
412         if (kind == BAD_MRICEBLOCK && mode == KICK)
413             World::current()->trybreakbrick(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 = *World::current()->get_tux();
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);
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);
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       float scroll_x = World::current()->camera->get_translation().x;                 
575
576       /* play explosion sound */  // FIXME: is the stereo all right? maybe we should use player cordinates...
577       if (base.x < scroll_x + screen->w/2 - 10)
578         play_sound(sounds[SND_EXPLODE], SOUND_LEFT_SPEAKER);
579       else if (base.x > scroll_x + screen->w/2 + 10)
580         play_sound(sounds[SND_EXPLODE], SOUND_RIGHT_SPEAKER);
581       else
582         play_sound(sounds[SND_EXPLODE], SOUND_CENTER_SPEAKER);
583
584     } else if(mode == BOMB_EXPLODE) {
585       remove_me();
586       return;
587     }
588   }
589
590   // move
591   physic.apply(elapsed_time, base.x, base.y);                 
592   collision_swept_object_map(&old_base,&base);
593 }
594
595 void
596 BadGuy::action_stalactite(double elapsed_time)
597 {
598   Player& tux = *World::current()->get_tux();
599
600   static const int SHAKETIME = 800;
601   static const int RANGE = 40;
602     
603   if(mode == NORMAL) {
604     // start shaking when tux is below the stalactite and at least 40 pixels
605     // near
606     if(tux.base.x + 32 > base.x - RANGE && tux.base.x < base.x + 32 + RANGE
607             && tux.base.y + tux.base.height > base.y) {
608       timer.start(SHAKETIME);
609       mode = STALACTITE_SHAKING;
610     }
611   } if(mode == STALACTITE_SHAKING) {
612     base.x = old_base.x + (rand() % 6) - 3; // TODO this could be done nicer...
613     if(!timer.check()) {
614       mode = STALACTITE_FALL;
615     }
616   } else if(mode == STALACTITE_FALL) {
617     fall();
618     /* Destroy if we collides with land */
619     if(issolid(base.x+base.width/2, base.y+base.height))
620     {
621       timer.start(2000);
622       dying = DYING_SQUISHED;
623       mode = FLAT;
624       set_sprite(img_stalactite_broken, img_stalactite_broken);
625     }
626   } else if(mode == FLAT) {
627     fall();
628   }
629
630   // move
631   physic.apply(elapsed_time, base.x, base.y);
632
633   if(dying == DYING_SQUISHED && !timer.check())
634     remove_me();
635 }
636
637 void
638 BadGuy::action_flame(double elapsed_time)
639 {
640     static const float radius = 100;
641     static const float speed = 0.02;
642     base.x = old_base.x + cos(angle) * radius;
643     base.y = old_base.y + sin(angle) * radius;
644
645     angle = fmodf(angle + elapsed_time * speed, 2*M_PI);
646 }
647
648 void
649 BadGuy::action_fish(double elapsed_time)
650 {
651   if(frozen_timer.check())
652     {
653     if(physic.get_velocity_y() < 0)
654       set_sprite(img_fish_iced_down, img_fish_iced_down);
655     else
656       set_sprite(img_fish_iced, img_fish_iced);
657
658     return;
659     }
660
661   static const float JUMPV = 6;
662   static const int WAITTIME = 1000;
663     
664   // go in wait mode when back in water
665   if(dying == DYING_NOT && gettile(base.x, base.y+ base.height)->water
666         && physic.get_velocity_y() <= 0 && mode == NORMAL)
667     {
668       mode = FISH_WAIT;
669       set_sprite(0, 0);
670       physic.set_velocity(0, 0);
671       physic.enable_gravity(false);
672       timer.start(WAITTIME);
673     }
674   else if(mode == FISH_WAIT && !timer.check())
675     {
676       // jump again
677       set_sprite(img_fish, img_fish);
678       mode = NORMAL;
679       physic.set_velocity(0, JUMPV);
680       physic.enable_gravity(true);
681     }
682
683   physic.apply(elapsed_time, base.x, base.y);
684   if(dying == DYING_NOT)
685     collision_swept_object_map(&old_base, &base);
686
687   if(physic.get_velocity_y() < 0)
688     set_sprite(img_fish_down, img_fish_down);
689 }
690
691 void
692 BadGuy::action_bouncingsnowball(double elapsed_time)
693 {
694   static const float JUMPV = 4.5;
695     
696   fall();
697
698   // jump when on ground
699   if(dying == DYING_NOT && issolid(base.x, base.y+32))
700     {
701       physic.set_velocity_y(JUMPV);
702       physic.enable_gravity(true);
703     }                                                     
704   else
705     {
706       mode = NORMAL;
707     }
708
709   // check for right/left collisions
710   check_horizontal_bump();
711
712   physic.apply(elapsed_time, base.x, base.y);
713   if(dying == DYING_NOT)
714     collision_swept_object_map(&old_base, &base);
715
716   // Handle dying timer:
717   if (dying == DYING_SQUISHED && !timer.check())
718     remove_me();
719 }
720
721 void
722 BadGuy::action_flyingsnowball(double elapsed_time)
723 {
724   static const float FLYINGSPEED = 1;
725   static const int DIRCHANGETIME = 1000;
726     
727   // go into flyup mode if none specified yet
728   if(dying == DYING_NOT && mode == NORMAL) {
729     mode = FLY_UP;
730     physic.set_velocity_y(FLYINGSPEED);
731     timer.start(DIRCHANGETIME/2);
732   }
733
734   if(dying == DYING_NOT && !timer.check()) {
735     if(mode == FLY_UP) {
736       mode = FLY_DOWN;
737       physic.set_velocity_y(-FLYINGSPEED);
738     } else if(mode == FLY_DOWN) {
739       mode = FLY_UP;
740       physic.set_velocity_y(FLYINGSPEED);
741     }
742     timer.start(DIRCHANGETIME);
743   }
744
745   if(dying != DYING_NOT)
746     physic.enable_gravity(true);
747
748   physic.apply(elapsed_time, base.x, base.y);
749   if(dying == DYING_NOT || dying == DYING_SQUISHED)
750     collision_swept_object_map(&old_base, &base);
751
752   // Handle dying timer:
753   if (dying == DYING_SQUISHED && !timer.check())
754     remove_me();
755 }
756
757 void
758 BadGuy::action_spiky(double elapsed_time)
759 {
760   if(frozen_timer.check())
761     {
762     set_sprite(img_spiky_iced_left, img_spiky_iced_right);
763     return;
764     }
765
766   if (dying == DYING_NOT)
767     check_horizontal_bump();
768
769   fall();
770 #if 0
771   // jump when we're about to fall
772   if (physic.get_velocity_y() == 0 && 
773           !issolid(base.x+base.width/2, base.y + base.height)) {
774     physic.enable_gravity(true);
775     physic.set_velocity_y(2);
776   }
777 #endif
778
779   physic.apply(elapsed_time, base.x, base.y);
780   if (dying != DYING_FALLING)
781     collision_swept_object_map(&old_base,&base);   
782 }
783
784 void
785 BadGuy::action_snowball(double elapsed_time)
786 {
787   if (dying == DYING_NOT)
788     check_horizontal_bump();
789
790   fall();
791
792   physic.apply(elapsed_time, base.x, base.y);
793   if (dying != DYING_FALLING)
794     collision_swept_object_map(&old_base,&base);
795
796   // Handle dying timer:
797   if (dying == DYING_SQUISHED && !timer.check())
798     remove_me();                                  
799 }
800
801 void
802 BadGuy::action_wingling(double elapsed_time)
803 {
804   if (dying != DYING_NOT)
805     physic.enable_gravity(true);
806   else
807   {
808     Player& tux = *World::current()->get_tux();
809     int dirsign = physic.get_velocity_x() < 0 ? -1 : 1;
810
811     if (fabsf(tux.base.x - base.x) < 150 && base.y < tux.base.y && tux.dying == DYING_NOT)
812     {
813       if (target.x < 0 && target.y < 0)
814       {
815         target.x = tux.base.x;
816         target.y = tux.base.y;
817         physic.set_velocity(dirsign * 1.5f, -2.25f);
818       }
819     }
820     else if (base.y >= target.y - 16)
821       physic.set_velocity(dirsign * WINGLING_FLY_SPEED, 0);
822   }
823
824   physic.apply(elapsed_time, base.x, base.y);
825
826
827   // Handle dying timer:
828   if (dying == DYING_SQUISHED && !timer.check())
829     remove_me();
830
831   // TODO: Winglings should be removed after flying off the screen
832 }
833
834 void
835 BadGuy::action_walkingtree(double elapsed_time)
836 {
837   if (dying == DYING_NOT)
838     check_horizontal_bump();
839
840   fall();
841
842   physic.apply(elapsed_time, base.x, base.y);
843   if (dying != DYING_FALLING)
844     collision_swept_object_map(&old_base,&base);
845
846   // Handle dying timer:
847   if (dying == DYING_SQUISHED && !timer.check())
848     remove_me();
849 }
850
851
852 void
853 BadGuy::action(float elapsed_time)
854 {
855   float scroll_x = World::current()->camera->get_translation().x;
856   float scroll_y = World::current()->camera->get_translation().y;
857   
858   // BadGuy fall below the ground
859   if (base.y > World::current()->get_level()->height * 32) {
860     remove_me();
861     return;
862   }
863
864   // Kill us if we landed on spikes
865   if (dying == DYING_NOT
866       && (kind != BAD_STALACTITE && kind != BAD_FLAME && kind != BAD_BOMB)
867       && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
868       ||  isspike(base.x, base.y + base.height)
869       ||  isspike(base.x + base.width, base.y + base.height)))
870       {
871          physic.set_velocity_y(3);
872          kill_me(0);
873       }
874
875   if(!seen) {
876     /* activate badguys if they're just inside the offscreen_distance around the
877      * screen. Don't activate them inside the screen, since that might have the
878      * effect of badguys suddenly popping up from nowhere
879      */
880     if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
881         start_position.x < scroll_x - base.width)
882       activate(RIGHT);
883     else if(start_position.x > scroll_y - Y_OFFSCREEN_DISTANCE &&
884         start_position.y < scroll_y - base.height)
885       activate(LEFT);
886     else if(start_position.x > scroll_x + screen->w &&
887         start_position.x < scroll_x + screen->w + X_OFFSCREEN_DISTANCE)
888       activate(LEFT);
889     else if(start_position.y > scroll_y + screen->h &&
890         start_position.y < scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
891       activate(LEFT);
892   } else {
893     if(base.x + base.width < scroll_x - X_OFFSCREEN_DISTANCE*4
894       || base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE*4
895       || base.y + base.height < scroll_y - Y_OFFSCREEN_DISTANCE*4
896       || base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE*4) {
897       seen = false;
898       if(dying != DYING_NOT)
899         remove_me();
900     }
901   }
902   
903   if(!seen)
904     return;
905   
906   switch (kind)
907     {
908     case BAD_MRICEBLOCK:
909       action_mriceblock(elapsed_time);
910       break;
911   
912     case BAD_JUMPY:
913       action_jumpy(elapsed_time);
914       break;
915
916     case BAD_MRBOMB:
917       action_mrbomb(elapsed_time);
918       break;
919     
920     case BAD_BOMB:
921       action_bomb(elapsed_time);
922       break;
923
924     case BAD_STALACTITE:
925       action_stalactite(elapsed_time);
926       break;
927
928     case BAD_FLAME:
929       action_flame(elapsed_time);
930       break;
931
932     case BAD_FISH:
933       action_fish(elapsed_time);
934       break;
935
936     case BAD_BOUNCINGSNOWBALL:
937       action_bouncingsnowball(elapsed_time);
938       break;
939
940     case BAD_FLYINGSNOWBALL:
941       action_flyingsnowball(elapsed_time);
942       break;
943
944     case BAD_SPIKY:
945       action_spiky(elapsed_time);
946       break;
947
948     case BAD_SNOWBALL:
949       action_snowball(elapsed_time);
950       break;
951
952     case BAD_WINGLING:
953       action_wingling(elapsed_time);
954       break;
955
956     case BAD_WALKINGTREE:
957       action_walkingtree(elapsed_time);
958       break;
959
960     default:
961       break;
962     }
963 }
964
965 void
966 BadGuy::draw(Camera& viewport, int)
967 {
968   float scroll_x = viewport.get_translation().x;
969   float scroll_y = viewport.get_translation().y;
970
971   // Don't try to draw stuff that is outside of the screen
972   if(base.x <= scroll_x - base.width || base.x >= scroll_x + screen->w)
973     return;
974   
975   if(sprite_left == 0 || sprite_right == 0)
976     {
977       return;
978     }
979
980   Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
981   if(dying == DYING_FALLING)
982     sprite->draw(viewport.world2screen(Vector(base.x, base.y)), SD_VERTICAL_FLIP);
983   else
984     sprite->draw(viewport.world2screen(Vector(base.x, base.y)));
985
986   if (debug_mode)
987     fillrect(base.x - scroll_x, base.y - scroll_y, base.width, base.height, 75,0,75, 150);
988 }
989
990 void
991 BadGuy::set_sprite(Sprite* left, Sprite* right) 
992 {
993   if (1)
994     {
995       base.width = 32;
996       base.height = 32;
997     }
998   else
999     {
1000       // FIXME: Using the image size for the physics and collision is
1001       // a bad idea, since images should always overlap there physical
1002       // representation
1003       if(left != 0) {
1004         if(base.width == 0 && base.height == 0) {
1005           base.width  = left->get_width();
1006           base.height = left->get_height();
1007         } else if(base.width != left->get_width() || base.height != left->get_height()) {
1008           base.x -= (left->get_width() - base.width) / 2;
1009           base.y -= left->get_height() - base.height;
1010           base.width = left->get_width();
1011           base.height = left->get_height();
1012           old_base = base;
1013         }
1014       } else {
1015         base.width = base.height = 0;
1016       }
1017     }
1018
1019   animation_offset = 0;
1020   sprite_left  = left;
1021   sprite_right = right;
1022 }
1023
1024 void
1025 BadGuy::bump()
1026 {
1027   // these can't be bumped
1028   if(kind == BAD_FLAME || kind == BAD_BOMB || kind == BAD_FISH
1029       || kind == BAD_FLYINGSNOWBALL)
1030     return;
1031
1032   physic.set_velocity_y(3);
1033   kill_me(25);
1034 }
1035
1036 void
1037 BadGuy::make_player_jump(Player* player)
1038 {
1039   player->physic.set_velocity_y(2);
1040   player->base.y = base.y - player->base.height - 2;
1041 }
1042
1043 void
1044 BadGuy::squish_me(Player* player)
1045 {
1046   make_player_jump(player);
1047     
1048   World::current()->add_score(Vector(base.x, base.y),
1049                               50 * player_status.score_multiplier);
1050   play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
1051   player_status.score_multiplier++;
1052
1053   dying = DYING_SQUISHED;
1054   timer.start(2000);
1055   physic.set_velocity(0, 0);
1056 }
1057
1058 void
1059 BadGuy::squish(Player* player)
1060 {
1061   static const int MAX_ICEBLOCK_SQUICHES = 10;
1062     
1063   if(kind == BAD_MRBOMB) {
1064     // mrbomb transforms into a bomb now
1065     explode();
1066     
1067     make_player_jump(player);
1068     World::current()->add_score(Vector(base.x, base.y),
1069                                 50 * player_status.score_multiplier);
1070     play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
1071     player_status.score_multiplier++;
1072     return;
1073
1074   } else if (kind == BAD_MRICEBLOCK) {
1075     if (mode == NORMAL || mode == KICK)
1076       {
1077         /* Flatten! */
1078         play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
1079         mode = FLAT;
1080         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1081         physic.set_velocity_x(0);
1082
1083         timer.start(4000);
1084       } else if (mode == FLAT) {
1085         /* Kick! */
1086         play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
1087
1088         if (player->base.x < base.x + (base.width/2)) {
1089           physic.set_velocity_x(5);
1090           dir = RIGHT;
1091         } else {
1092           physic.set_velocity_x(-5);
1093           dir = LEFT;
1094         }
1095
1096         mode = KICK;
1097         player->kick_timer.start(KICKING_TIME);
1098         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1099       }
1100
1101     make_player_jump(player);
1102
1103     player_status.score_multiplier++;
1104
1105     // check for maximum number of squishes
1106     squishcount++;
1107     if(squishcount >= MAX_ICEBLOCK_SQUICHES) {
1108       kill_me(50);
1109       return;
1110     }
1111     
1112     return;
1113   } else if(kind == BAD_FISH) {
1114     // fish can only be killed when falling down
1115     if(physic.get_velocity_y() >= 0)
1116       return;
1117       
1118     make_player_jump(player);
1119               
1120     World::current()->add_score(Vector(base.x, base.y),
1121                                 25 * player_status.score_multiplier);
1122     player_status.score_multiplier++;
1123      
1124     // simply remove the fish...
1125     remove_me();
1126     return;
1127   } else if(kind == BAD_BOUNCINGSNOWBALL) {
1128     squish_me(player);
1129     set_sprite(img_bouncingsnowball_squished,img_bouncingsnowball_squished);
1130     return;
1131   } else if(kind == BAD_FLYINGSNOWBALL) {
1132     squish_me(player);
1133     set_sprite(img_flyingsnowball_squished,img_flyingsnowball_squished);
1134     return;
1135   } else if(kind == BAD_SNOWBALL) {
1136     squish_me(player);
1137     set_sprite(img_snowball_squished_left, img_snowball_squished_right);
1138     return;
1139   } else if(kind == BAD_WINGLING) {
1140     squish_me(player);
1141     set_sprite(img_wingling_left, img_wingling_left);
1142   } else if(kind == BAD_WALKINGTREE) {
1143     if (mode == BGM_BIG)
1144     {
1145       set_sprite(img_walkingtree_left_small, img_walkingtree_left_small);
1146       physic.set_velocity_x(physic.get_velocity_x() * 2.0f);
1147       // XXX magic number: 66 is BGM_BIG height
1148
1149       make_player_jump(player);
1150       base.y += 66 - base.height;
1151               
1152       World::current()->add_score(Vector(base.x, base.y),
1153                                 25 * player_status.score_multiplier);
1154       player_status.score_multiplier++;
1155
1156       mode = BGM_SMALL;
1157     }
1158     else
1159       squish_me(player);
1160   }
1161 }
1162
1163 void
1164 BadGuy::kill_me(int score)
1165 {
1166   if(kind == BAD_BOMB)
1167     return;
1168
1169   dying = DYING_FALLING;
1170   if(kind == BAD_MRICEBLOCK) {
1171     set_sprite(img_mriceblock_falling_left, img_mriceblock_falling_right);
1172     if(mode == HELD) {
1173       mode = NORMAL;
1174       Player& tux = *World::current()->get_tux();  
1175       tux.holding_something = false;
1176     }
1177   }
1178
1179   physic.enable_gravity(true);
1180
1181   /* Gain some points: */
1182   if (score != 0)
1183     World::current()->add_score(Vector(base.x, base.y),
1184                                 score * player_status.score_multiplier);
1185
1186   /* Play death sound: */
1187   play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
1188 }
1189
1190 void
1191 BadGuy::explode()
1192 {
1193   World::current()->add_bad_guy(base.x, base.y, BAD_BOMB);
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           kill_me(10);
1230         }
1231       else if(pbullet_c->kind == ICE_BULLET)
1232         {
1233         //if(kind == BAD_FLAME)
1234         //  kill_me(10);
1235         //else
1236           frozen_timer.start(FROZEN_TIME);
1237         }
1238       break;
1239
1240     case CO_BADGUY:
1241       pbad_c = (BadGuy*) p_c_object;
1242
1243
1244       /* If we're a kicked mriceblock, kill [almost] any badguys we hit */
1245       if(kind == BAD_MRICEBLOCK && mode == KICK &&
1246          kind != BAD_FLAME && kind != BAD_BOMB && kind != BAD_STALACTITE)
1247         {
1248           pbad_c->kill_me(25);
1249         }
1250
1251       // a held mriceblock kills the enemy too but falls to ground then
1252       else if(kind == BAD_MRICEBLOCK && mode == HELD)
1253         {
1254           pbad_c->kill_me(25);
1255           kill_me(0);
1256         }
1257
1258       /* Kill badguys that run into exploding bomb */
1259       else if (kind == BAD_BOMB && dying == DYING_NOT)
1260       {
1261         if (pbad_c->kind == BAD_MRBOMB)
1262         {
1263           // mrbomb transforms into a bomb now
1264           pbad_c->explode();
1265           return;
1266         }
1267         else if (pbad_c->kind != BAD_MRBOMB)
1268         {
1269           pbad_c->kill_me(50);
1270         }
1271       }
1272
1273       /* Kill any badguys that get hit by stalactite */
1274       else if (kind == BAD_STALACTITE && dying == DYING_NOT)
1275       {
1276         if (pbad_c->kind == BAD_MRBOMB)
1277         {
1278           // mrbomb transforms into a bomb now
1279           pbad_c->explode();
1280           return;
1281         }
1282         else
1283           pbad_c->kill_me(50);
1284       }
1285
1286       /* When enemies run into eachother, make them change directions */
1287       else
1288       {
1289         // Wingling doesn't interact with other badguys
1290         if (pbad_c->kind == BAD_WINGLING || kind == BAD_WINGLING)
1291           break;
1292
1293         // Jumpy, fish, flame, stalactites, wingling are exceptions
1294         if (pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FLAME
1295             || pbad_c->kind == BAD_STALACTITE || pbad_c->kind == BAD_FISH)
1296           break;
1297
1298         // Bounce off of other badguy if we land on top of him
1299         if (base.y + base.height < pbad_c->base.y + pbad_c->base.height)
1300         {
1301           if (pbad_c->dir == LEFT)
1302           {
1303             dir = RIGHT;
1304             physic.set_velocity(fabsf(physic.get_velocity_x()), 2);
1305           }
1306           else if (pbad_c->dir == RIGHT)
1307           {
1308             dir = LEFT;
1309             physic.set_velocity(-fabsf(physic.get_velocity_x()), 2);
1310           }
1311
1312           break;
1313         }
1314         else if (base.y + base.height > pbad_c->base.y + pbad_c->base.height)
1315           break;
1316
1317         if (pbad_c->kind != BAD_FLAME)
1318           {
1319             if (dir == LEFT)
1320             {
1321               dir = RIGHT;
1322               physic.set_velocity_x(fabsf(physic.get_velocity_x()));
1323
1324               // in case badguys get "jammed"
1325               if (physic.get_velocity_x() != 0)
1326                 base.x = pbad_c->base.x + pbad_c->base.width;
1327             }
1328             else if (dir == RIGHT)
1329             {
1330               dir = LEFT;
1331               physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
1332             }
1333
1334           }
1335       }
1336       
1337       break;
1338
1339     case CO_PLAYER:
1340       Player* player = static_cast<Player*>(p_c_object);
1341       /* Get kicked if were flat */
1342       if (mode == FLAT && !dying)
1343       {
1344         play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
1345
1346         // Hit from left side
1347         if (player->base.x < base.x) {
1348           physic.set_velocity_x(5);
1349           dir = RIGHT;
1350         }
1351         // Hit from right side
1352         else {
1353           physic.set_velocity_x(-5);
1354           dir = LEFT;
1355         }
1356
1357         mode = KICK;
1358         player->kick_timer.start(KICKING_TIME);
1359         set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1360       }
1361       break;
1362
1363     }
1364 }
1365
1366
1367 //---------------------------------------------------------------------------
1368
1369 void load_badguy_gfx()
1370 {
1371   img_mriceblock_flat_left = sprite_manager->load("mriceblock-flat-left");
1372   img_mriceblock_flat_right = sprite_manager->load("mriceblock-flat-right");
1373   img_mriceblock_falling_left = sprite_manager->load("mriceblock-falling-left");
1374   img_mriceblock_falling_right = sprite_manager->load("mriceblock-falling-right");
1375   img_mriceblock_left = sprite_manager->load("mriceblock-left");
1376   img_mriceblock_right = sprite_manager->load("mriceblock-right");
1377   img_jumpy_left_up = sprite_manager->load("jumpy-left-up");
1378   img_jumpy_left_down = sprite_manager->load("jumpy-left-down");
1379   img_jumpy_left_middle = sprite_manager->load("jumpy-left-middle");
1380   img_jumpy_left_iced = sprite_manager->load("jumpy-left-iced");
1381   img_mrbomb_left = sprite_manager->load("mrbomb-left");
1382   img_mrbomb_right = sprite_manager->load("mrbomb-right");
1383   img_mrbomb_iced_left = sprite_manager->load("mrbomb-iced-left");
1384   img_mrbomb_iced_right = sprite_manager->load("mrbomb-iced-right");
1385   img_mrbomb_ticking_left = sprite_manager->load("mrbomb-ticking-left");
1386   img_mrbomb_ticking_right = sprite_manager->load("mrbomb-ticking-right");
1387   img_mrbomb_explosion = sprite_manager->load("mrbomb-explosion");
1388   img_stalactite = sprite_manager->load("stalactite");
1389   img_stalactite_broken = sprite_manager->load("stalactite-broken");
1390   img_flame = sprite_manager->load("flame");
1391   img_fish = sprite_manager->load("fish");
1392   img_fish_down = sprite_manager->load("fish-down");
1393   img_fish_iced = sprite_manager->load("fish-iced");
1394   img_fish_iced_down = sprite_manager->load("fish-iced-down");
1395   img_bouncingsnowball_left = sprite_manager->load("bouncingsnowball-left");
1396   img_bouncingsnowball_right = sprite_manager->load("bouncingsnowball-right");
1397   img_bouncingsnowball_squished = sprite_manager->load("bouncingsnowball-squished");
1398   img_flyingsnowball = sprite_manager->load("flyingsnowball");
1399   img_flyingsnowball_squished = sprite_manager->load("flyingsnowball-squished");
1400   img_spiky_left = sprite_manager->load("spiky-left");
1401   img_spiky_right = sprite_manager->load("spiky-right");
1402   img_spiky_iced_left = sprite_manager->load("spiky-iced-left");
1403   img_spiky_iced_right = sprite_manager->load("spiky-iced-right");
1404   img_snowball_left = sprite_manager->load("snowball-left");
1405   img_snowball_right = sprite_manager->load("snowball-right");
1406   img_snowball_squished_left = sprite_manager->load("snowball-squished-left");
1407   img_snowball_squished_right = sprite_manager->load("snowball-squished-right");
1408   img_wingling_left = sprite_manager->load("wingling-left");
1409   img_walkingtree_left = sprite_manager->load("walkingtree-left");
1410   img_walkingtree_left_small = sprite_manager->load("walkingtree-left-small");
1411 }
1412
1413 void free_badguy_gfx()
1414 {
1415 }
1416
1417 // EOF //