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>
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.
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.
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
26 #include "app/globals.h"
28 #include "special/sprite_manager.h"
29 #include "utils/lispwriter.h"
32 #include "resources.h"
37 #include "statistics.h"
39 Sprite* img_mriceblock_flat_left;
40 Sprite* img_mriceblock_flat_right;
41 Sprite* img_mriceblock_falling_left;
42 Sprite* img_mriceblock_falling_right;
43 Sprite* img_mriceblock_left;
44 Sprite* img_mriceblock_right;
45 Sprite* img_jumpy_left_up;
46 Sprite* img_jumpy_left_down;
47 Sprite* img_jumpy_left_middle;
48 Sprite* img_jumpy_left_iced;
49 Sprite* img_mrbomb_left;
50 Sprite* img_mrbomb_right;
51 Sprite* img_mrbomb_iced_left;
52 Sprite* img_mrbomb_iced_right;
53 Sprite* img_mrbomb_ticking_left;
54 Sprite* img_mrbomb_ticking_right;
55 Sprite* img_mrbomb_explosion;
56 Sprite* img_stalactite;
57 Sprite* img_stalactite_broken;
60 Sprite* img_fish_down;
61 Sprite* img_fish_iced;
62 Sprite* img_fish_iced_down;
63 Sprite* img_flamefish;
64 Sprite* img_flamefish_down;
65 Sprite* img_bouncingsnowball_left;
66 Sprite* img_bouncingsnowball_right;
67 Sprite* img_bouncingsnowball_squished;
68 Sprite* img_flyingsnowball;
69 Sprite* img_flyingsnowball_squished;
70 Sprite* img_spiky_left;
71 Sprite* img_spiky_right;
72 Sprite* img_spiky_iced_left;
73 Sprite* img_spiky_iced_right;
74 Sprite* img_snowball_left;
75 Sprite* img_snowball_right;
76 Sprite* img_snowball_squished_left;
77 Sprite* img_snowball_squished_right;
78 Sprite* img_wingling_left;
79 Sprite* img_walkingtree_left;
80 Sprite* img_walkingtree_left_small;
82 #define BADGUY_WALK_SPEED .8f
83 #define WINGLING_FLY_SPEED 1.6f
85 BadGuyKind badguykind_from_string(const std::string& str)
87 if (str == "money" || str == "jumpy") // was money in old maps
89 else if (str == "laptop" || str == "mriceblock") // was laptop in old maps
90 return BAD_MRICEBLOCK;
91 else if (str == "mrbomb")
93 else if (str == "stalactite")
94 return BAD_STALACTITE;
95 else if (str == "flame")
97 else if (str == "fish")
99 else if (str == "flamefish")
100 return BAD_FLAMEFISH;
101 else if (str == "bouncingsnowball")
102 return BAD_BOUNCINGSNOWBALL;
103 else if (str == "flyingsnowball")
104 return BAD_FLYINGSNOWBALL;
105 else if (str == "spiky")
107 else if (str == "snowball" || str == "bsod") // was bsod in old maps
109 else if (str == "wingling")
111 else if (str == "walkingtree")
112 return BAD_WALKINGTREE;
119 std::string badguykind_to_string(BadGuyKind kind)
144 case BAD_BOUNCINGSNOWBALL:
145 return "bouncingsnowball";
147 case BAD_FLYINGSNOWBALL:
148 return "flyingsnowball";
159 case BAD_WALKINGTREE:
160 return "walkingtree";
166 BadGuy::BadGuy(BadGuyKind kind_, LispReader& lispreader)
167 : removable(false), squishcount(0)
169 lispreader.read_float("x", start_position.x);
170 lispreader.read_float("y", start_position.y);
174 stay_on_platform = false;
175 lispreader.read_bool("stay-on-platform", stay_on_platform);
180 BadGuy::BadGuy(BadGuyKind kind_, float x, float y)
181 : removable(false), squishcount(0)
183 start_position.x = x;
184 start_position.y = y;
185 stay_on_platform = false;
209 animation_offset = 0;
210 target.x = target.y = -1;
211 sprite_left = sprite_right = 0;
213 frozen_timer.init(true);
216 // if we're in a solid tile at start correct that now
217 if(Sector::current()) {
218 if(kind != BAD_FLAME && kind != BAD_FISH && kind != BAD_FLAMEFISH && collision_object_map(base))
220 std::cout << "Warning: badguy started in wall: kind: " << badguykind_to_string(kind)
221 << " pos: (" << base.x << ", " << base.y << ")" << std::endl;
222 while(collision_object_map(base))
226 if(Sector::current()->camera) {
227 Vector scroll = Sector::current()->camera->get_translation();
229 if(start_position.x > scroll.x - X_OFFSCREEN_DISTANCE &&
230 start_position.x < scroll.x + screen->w + X_OFFSCREEN_DISTANCE &&
231 start_position.y > scroll.y - Y_OFFSCREEN_DISTANCE &&
232 start_position.y < scroll.y + screen->h + Y_OFFSCREEN_DISTANCE) {
236 if(start_position.x > 0 && start_position.x <= screen->w
237 && start_position.y > 0 && start_position.y <= screen->h)
243 BadGuy::write(LispWriter& writer)
245 writer.start_list(badguykind_to_string(kind));
247 writer.write_float("x", base.x);
248 writer.write_float("y", base.y);
249 writer.write_bool("stay-on-platform", stay_on_platform);
251 writer.end_list(badguykind_to_string(kind));
255 BadGuy::activate(Direction activation_dir)
258 animation_offset = 0;
259 target.x = target.y = -1;
261 frozen_timer.init(true);
264 dir = activation_dir;
265 float dirsign = activation_dir == LEFT ? -1 : 1;
267 if(kind == BAD_MRBOMB) {
268 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
269 set_sprite(img_mrbomb_left, img_mrbomb_right);
270 } else if (kind == BAD_MRICEBLOCK) {
271 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
272 set_sprite(img_mriceblock_left, img_mriceblock_right);
273 } else if(kind == BAD_JUMPY) {
274 set_sprite(img_jumpy_left_up, img_jumpy_left_up);
275 } else if(kind == BAD_BOMB) {
276 set_sprite(img_mrbomb_ticking_left, img_mrbomb_ticking_right);
277 // hack so that the bomb doesn't hurt until it expldes...
278 dying = DYING_SQUISHED;
279 } else if(kind == BAD_FLAME) {
281 physic.enable_gravity(false);
282 set_sprite(img_flame, img_flame);
283 } else if(kind == BAD_BOUNCINGSNOWBALL) {
284 physic.set_velocity(dirsign * 1.3, 0);
285 set_sprite(img_bouncingsnowball_left, img_bouncingsnowball_right);
286 } else if(kind == BAD_STALACTITE) {
287 physic.enable_gravity(false);
288 set_sprite(img_stalactite, img_stalactite);
289 } else if(kind == BAD_FISH) {
290 set_sprite(img_fish, img_fish);
291 physic.enable_gravity(true);
292 } else if(kind == BAD_FLAMEFISH) {
293 set_sprite(img_flamefish, img_flamefish);
294 physic.enable_gravity(true);
295 } else if(kind == BAD_FLYINGSNOWBALL) {
296 set_sprite(img_flyingsnowball, img_flyingsnowball);
297 physic.enable_gravity(false);
298 } else if(kind == BAD_SPIKY) {
299 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
300 set_sprite(img_spiky_left, img_spiky_right);
301 } else if(kind == BAD_SNOWBALL) {
302 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
303 set_sprite(img_snowball_left, img_snowball_right);
304 } else if(kind == BAD_WINGLING) {
305 physic.set_velocity(dirsign * WINGLING_FLY_SPEED, 0);
306 physic.enable_gravity(false);
307 set_sprite(img_wingling_left, img_wingling_left);
308 } else if (kind == BAD_WALKINGTREE) {
309 // TODO: why isn't the height/width being set properly in set_sprite?
310 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
312 set_sprite(img_walkingtree_left, img_walkingtree_left);
317 base.x = start_position.x;
318 base.y = start_position.y;
324 BadGuy::action_mriceblock(double elapsed_time)
326 Player& tux = *Sector::current()->player;
331 /* Move left/right: */
335 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
336 if (dying != DYING_FALLING)
337 collision_swept_object_map(&old_base,&base);
339 else if (mode == HELD)
340 { /* FIXME: The pbad object shouldn't know about pplayer objects. */
341 /* If we're holding the iceblock */
343 if(tux.size == SMALL)
346 base.x = tux.base.x + 24;
348 base.x = tux.base.x - 12;
349 base.y = tux.base.y + tux.base.height/1.5 - base.height;
354 base.x = tux.base.x + 24;
356 base.x = tux.base.x - 4;
357 base.y = tux.base.y + tux.base.height/1.5 - base.height;
360 if(collision_object_map(base))
363 base.y = tux.base.y + tux.base.height/1.5 - base.height;
366 if(tux.input.fire != DOWN) /* SHOOT! */
369 base.x = tux.base.x - base.width;
371 base.x = tux.base.x + tux.base.width;
375 tux.kick_timer.start(KICKING_TIME);
376 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
377 physic.set_velocity_x((dir == LEFT) ? -3.5 : 3.5);
378 SoundManager::get()->play_sound(IDToSound(SND_KICK), this, Sector::current()->player->get_pos());
385 check_horizontal_bump();
386 if(mode == KICK && changed != dir)
388 SoundManager::get()->play_sound(IDToSound(SND_RICOCHET), get_pos(), Sector::current()->player->get_pos());
392 /* Handle mode timer: */
398 set_sprite(img_mriceblock_left, img_mriceblock_right);
399 physic.set_velocity( (dir == LEFT) ? -.8 : .8, 0);
405 BadGuy::check_horizontal_bump(bool checkcliff)
407 float halfheight = base.height / 2;
408 if (dir == LEFT && issolid( base.x, base.y + halfheight))
410 if (kind == BAD_MRICEBLOCK && mode == KICK)
412 Sector::current()->trybreakbrick(Vector(base.x, base.y + halfheight), false);
413 Sector::current()->tryemptybox(Vector(base.x, base.y + halfheight), dir);
417 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
420 if (dir == RIGHT && issolid( base.x + base.width, base.y + halfheight))
422 if (kind == BAD_MRICEBLOCK && mode == KICK)
424 Sector::current()->trybreakbrick(
425 Vector(base.x + base.width, base.y + halfheight), false);
426 Sector::current()->tryemptybox(
427 Vector(base.x + base.width, base.y + halfheight), dir);
431 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
435 // don't check for cliffs when we're falling
438 if(!issolid(base.x + base.width/2, base.y + base.height))
441 if(dir == LEFT && !issolid(base.x, (int) base.y + base.height + halfheight))
444 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
447 if(dir == RIGHT && !issolid(base.x + base.width,
448 (int) base.y + base.height + halfheight))
451 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
459 /* Fall if we get off the ground: */
460 if (dying != DYING_FALLING)
462 if (!issolid(base.x+base.width/2, base.y + base.height))
464 // not solid below us? enable gravity
465 physic.enable_gravity(true);
470 if (physic.get_velocity_y() < 0)
472 base.y = int((base.y + base.height)/32) * 32 - base.height;
473 physic.set_velocity_y(0);
475 // no gravity anymore please
476 physic.enable_gravity(false);
478 if (stay_on_platform && mode == NORMAL)
480 if (!issolid(base.x + ((dir == LEFT) ? 0 : base.width),
481 base.y + base.height))
486 physic.set_velocity_x(fabsf(physic.get_velocity_x()));
491 physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
499 physic.enable_gravity(true);
504 BadGuy::action_jumpy(double elapsed_time)
506 if(frozen_timer.check())
508 set_sprite(img_jumpy_left_iced, img_jumpy_left_iced);
512 const float vy = physic.get_velocity_y();
514 // XXX: These tests *should* use location from ground, not velocity
515 if (fabsf(vy) > 5.6f)
516 set_sprite(img_jumpy_left_down, img_jumpy_left_down);
517 else if (fabsf(vy) > 5.3f)
518 set_sprite(img_jumpy_left_middle, img_jumpy_left_middle);
520 set_sprite(img_jumpy_left_up, img_jumpy_left_up);
522 Player& tux = *Sector::current()->player;
524 static const float JUMPV = 6;
527 // jump when on ground
528 if(dying == DYING_NOT && issolid(base.x, base.y+32))
530 physic.set_velocity_y(JUMPV);
531 physic.enable_gravity(true);
535 else if(mode == JUMPY_JUMP)
540 // set direction based on tux
541 if(tux.base.x > base.x)
547 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
548 if(dying == DYING_NOT)
549 collision_swept_object_map(&old_base, &base);
553 BadGuy::action_mrbomb(double elapsed_time)
555 if(frozen_timer.check())
557 set_sprite(img_mrbomb_iced_left, img_mrbomb_iced_right);
561 if (dying == DYING_NOT)
562 check_horizontal_bump(true);
566 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
567 if (dying != DYING_FALLING)
568 collision_swept_object_map(&old_base,&base);
572 BadGuy::action_bomb(double elapsed_time)
574 static const int TICKINGTIME = 1000;
575 static const int EXPLODETIME = 1000;
581 timer.start(TICKINGTIME);
582 } else if(!timer.check()) {
583 if(mode == BOMB_TICKING) {
585 set_sprite(img_mrbomb_explosion, img_mrbomb_explosion);
586 dying = DYING_NOT; // now the bomb hurts
587 timer.start(EXPLODETIME);
589 SoundManager::get()->play_sound(IDToSound(SND_EXPLODE), this, Sector::current()->player->get_pos());
590 } else if(mode == BOMB_EXPLODE) {
597 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
598 collision_swept_object_map(&old_base,&base);
602 BadGuy::action_stalactite(double elapsed_time)
604 Player& tux = *Sector::current()->player;
606 static const int SHAKETIME = 800;
607 static const int RANGE = 40;
610 // start shaking when tux is below the stalactite and at least 40 pixels
612 if(tux.base.x + 32 > base.x - RANGE && tux.base.x < base.x + 32 + RANGE
613 && tux.base.y + tux.base.height > base.y
614 && tux.dying == DYING_NOT) {
615 timer.start(SHAKETIME);
616 mode = STALACTITE_SHAKING;
618 } if(mode == STALACTITE_SHAKING) {
619 base.x = old_base.x + (rand() % 6) - 3; // TODO this could be done nicer...
621 mode = STALACTITE_FALL;
623 } else if(mode == STALACTITE_FALL) {
625 /* Destroy if we collides with land */
626 if(issolid(base.x+base.width/2, base.y+base.height))
629 dying = DYING_SQUISHED;
631 set_sprite(img_stalactite_broken, img_stalactite_broken);
633 } else if(mode == FLAT) {
638 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
640 if(dying == DYING_SQUISHED && !timer.check())
645 BadGuy::action_flame(double elapsed_time)
647 static const float radius = 100;
648 static const float speed = 0.02;
649 base.x = old_base.x + cos(angle) * radius;
650 base.y = old_base.y + sin(angle) * radius;
652 angle = fmodf(angle + elapsed_time * speed, 2*M_PI);
656 BadGuy::action_fish(double elapsed_time)
658 if(frozen_timer.check())
660 if(physic.get_velocity_y() < 0)
661 set_sprite(img_fish_iced_down, img_fish_iced_down);
663 set_sprite(img_fish_iced, img_fish_iced);
668 static const float JUMPV = 6;
669 static const int WAITTIME = 1000;
671 // go in wait mode when back in water
672 if(dying == DYING_NOT
673 && gettile(base.x, base.y + base.height)
674 && gettile(base.x, base.y + base.height)->attributes & Tile::WATER
675 && physic.get_velocity_y() <= 0 && mode == NORMAL)
679 physic.set_velocity(0, 0);
680 physic.enable_gravity(false);
681 timer.start(WAITTIME);
683 else if(mode == FISH_WAIT && !timer.check())
687 set_sprite(img_fish, img_fish);
688 else // BAD_FLAMEFISH
689 set_sprite(img_flamefish, img_flamefish);
691 physic.set_velocity(0, JUMPV);
692 physic.enable_gravity(true);
695 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
696 if(dying == DYING_NOT)
697 collision_swept_object_map(&old_base, &base);
699 if(physic.get_velocity_y() < 0)
702 set_sprite(img_fish_down, img_fish_down);
703 else // BAD_FLAMEFISH
704 set_sprite(img_flamefish_down, img_flamefish_down);
709 BadGuy::action_bouncingsnowball(double elapsed_time)
711 static const float JUMPV = 4.5;
715 // jump when on ground
716 if(dying == DYING_NOT && issolid(base.x, base.y+32))
718 physic.set_velocity_y(JUMPV);
719 physic.enable_gravity(true);
726 // check for right/left collisions
727 check_horizontal_bump();
729 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
730 if(dying == DYING_NOT)
731 collision_swept_object_map(&old_base, &base);
733 // Handle dying timer:
734 if (dying == DYING_SQUISHED && !timer.check())
739 BadGuy::action_flyingsnowball(double elapsed_time)
741 static const float FLYINGSPEED = 1;
742 static const int DIRCHANGETIME = 1000;
744 // go into flyup mode if none specified yet
745 if(dying == DYING_NOT && mode == NORMAL) {
747 physic.set_velocity_y(FLYINGSPEED);
748 timer.start(DIRCHANGETIME/2);
751 if(dying == DYING_NOT && !timer.check()) {
754 physic.set_velocity_y(-FLYINGSPEED);
755 } else if(mode == FLY_DOWN) {
757 physic.set_velocity_y(FLYINGSPEED);
759 timer.start(DIRCHANGETIME);
762 if(dying != DYING_NOT)
763 physic.enable_gravity(true);
765 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
766 if(dying == DYING_NOT || dying == DYING_SQUISHED)
767 collision_swept_object_map(&old_base, &base);
769 // Handle dying timer:
770 if (dying == DYING_SQUISHED && !timer.check())
775 BadGuy::action_spiky(double elapsed_time)
777 if(frozen_timer.check())
779 set_sprite(img_spiky_iced_left, img_spiky_iced_right);
783 if (dying == DYING_NOT)
784 check_horizontal_bump();
788 // jump when we're about to fall
789 if (physic.get_velocity_y() == 0 &&
790 !issolid(base.x+base.width/2, base.y + base.height)) {
791 physic.enable_gravity(true);
792 physic.set_velocity_y(2);
796 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
797 if (dying != DYING_FALLING)
798 collision_swept_object_map(&old_base,&base);
802 BadGuy::action_snowball(double elapsed_time)
804 if (dying == DYING_NOT)
805 check_horizontal_bump();
809 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
810 if (dying != DYING_FALLING)
811 collision_swept_object_map(&old_base,&base);
813 // Handle dying timer:
814 if (dying == DYING_SQUISHED && !timer.check())
819 BadGuy::action_wingling(double elapsed_time)
821 if (dying != DYING_NOT)
822 physic.enable_gravity(true);
825 Player& tux = *Sector::current()->player;
826 int dirsign = physic.get_velocity_x() < 0 ? -1 : 1;
828 if (fabsf(tux.base.x - base.x) < 150 && base.y < tux.base.y && tux.dying == DYING_NOT)
830 if (target.x < 0 && target.y < 0)
832 target.x = tux.base.x;
833 target.y = tux.base.y;
834 physic.set_velocity(dirsign * 1.5f, -2.25f);
837 else if (base.y >= target.y - 16)
838 physic.set_velocity(dirsign * WINGLING_FLY_SPEED, 0);
841 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
844 // Handle dying timer:
845 if (dying == DYING_SQUISHED && !timer.check())
848 // TODO: Winglings should be removed after flying off the screen
852 BadGuy::action_walkingtree(double elapsed_time)
854 if (dying == DYING_NOT)
855 check_horizontal_bump();
859 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
860 if (dying != DYING_FALLING)
861 collision_swept_object_map(&old_base,&base);
863 // Handle dying timer:
864 if (dying == DYING_SQUISHED && !timer.check())
869 BadGuy::action(float elapsed_time)
871 float scroll_x = Sector::current()->camera->get_translation().x;
872 float scroll_y = Sector::current()->camera->get_translation().y;
874 // BadGuy fall below the ground
875 if (base.y > Sector::current()->solids->get_height() * 32) {
880 // Kill us if we landed on spikes
881 if (dying == DYING_NOT
882 && (kind != BAD_STALACTITE && kind != BAD_FLAME && kind != BAD_BOMB)
883 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
884 || isspike(base.x, base.y + base.height)
885 || isspike(base.x + base.width, base.y + base.height)))
887 physic.set_velocity_y(3);
892 /* activate badguys if they're just inside the offscreen_distance around the
893 * screen. Don't activate them inside the screen, since that might have the
894 * effect of badguys suddenly popping up from nowhere
896 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
897 start_position.x < scroll_x - base.width)
899 else if(start_position.x > scroll_y - Y_OFFSCREEN_DISTANCE &&
900 start_position.y < scroll_y - base.height)
902 else if(start_position.x > scroll_x + screen->w &&
903 start_position.x < scroll_x + screen->w + X_OFFSCREEN_DISTANCE)
905 else if(start_position.y > scroll_y + screen->h &&
906 start_position.y < scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
909 if(base.x + base.width < scroll_x - X_OFFSCREEN_DISTANCE*4
910 || base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE*4
911 || base.y + base.height < scroll_y - Y_OFFSCREEN_DISTANCE*4
912 || base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE*4) {
914 if(dying != DYING_NOT)
925 action_mriceblock(elapsed_time);
929 action_jumpy(elapsed_time);
933 action_mrbomb(elapsed_time);
937 action_bomb(elapsed_time);
941 action_stalactite(elapsed_time);
945 action_flame(elapsed_time);
950 action_fish(elapsed_time);
953 case BAD_BOUNCINGSNOWBALL:
954 action_bouncingsnowball(elapsed_time);
957 case BAD_FLYINGSNOWBALL:
958 action_flyingsnowball(elapsed_time);
962 action_spiky(elapsed_time);
966 action_snowball(elapsed_time);
970 action_wingling(elapsed_time);
973 case BAD_WALKINGTREE:
974 action_walkingtree(elapsed_time);
983 BadGuy::draw(DrawingContext& context)
985 Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
989 if(dying == DYING_FALLING && physic.get_velocity_y() < 0)
990 sprite->draw(context, Vector(base.x, base.y), LAYER_FOREGROUNDTILES+1, VERTICAL_FLIP);
992 sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
995 context.draw_filled_rect(Vector(base.x, base.y),
996 Vector(base.width, base.height), Color(75,0,75, 150), LAYER_OBJECTS+1);
1000 BadGuy::set_sprite(Sprite* left, Sprite* right)
1009 // FIXME: Using the image size for the physics and collision is
1010 // a bad idea, since images should always overlap there physical
1013 if(base.width == 0 && base.height == 0) {
1014 base.width = left->get_width();
1015 base.height = left->get_height();
1016 } else if(base.width != left->get_width() || base.height != left->get_height()) {
1017 base.x -= (left->get_width() - base.width) / 2;
1018 base.y -= left->get_height() - base.height;
1019 base.width = left->get_width();
1020 base.height = left->get_height();
1024 base.width = base.height = 0;
1028 animation_offset = 0;
1030 sprite_right = right;
1036 // these can't be bumped
1037 if(kind == BAD_FLAME || kind == BAD_BOMB || kind == BAD_FISH
1038 || kind == BAD_FLAMEFISH || kind == BAD_FLYINGSNOWBALL)
1041 physic.set_velocity_y(3);
1046 BadGuy::squish_me(Player* player)
1048 player->bounce(this);
1050 Sector::current()->add_score(Vector(base.x, base.y),
1051 25 * player_status.score_multiplier);
1053 SoundManager::get()->play_sound(IDToSound(SND_SQUISH), get_pos(), Sector::current()->player->get_pos());
1054 player_status.score_multiplier++;
1056 dying = DYING_SQUISHED;
1058 physic.set_velocity(0, 0);
1062 BadGuy::squish(Player* player)
1064 static const int MAX_ICEBLOCK_SQUICHES = 10;
1066 if(kind == BAD_MRBOMB) {
1067 // mrbomb transforms into a bomb now
1070 player->bounce(this);
1071 Sector::current()->add_score(Vector(base.x, base.y),
1072 25 * player_status.score_multiplier);
1073 SoundManager::get()->play_sound(IDToSound(SND_SQUISH), get_pos(), Sector::current()->player->get_pos());
1075 player_status.score_multiplier++;
1078 } else if (kind == BAD_MRICEBLOCK) {
1079 if (mode == NORMAL || mode == KICK)
1082 SoundManager::get()->play_sound(IDToSound(SND_STOMP), get_pos(), Sector::current()->player->get_pos());
1084 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1085 physic.set_velocity_x(0);
1088 } else if (mode == FLAT) {
1090 SoundManager::get()->play_sound(IDToSound(SND_KICK), this, Sector::current()->player->get_pos());
1092 if (player->base.x < base.x + (base.width/2)) {
1093 physic.set_velocity_x(5);
1096 physic.set_velocity_x(-5);
1101 player->kick_timer.start(KICKING_TIME);
1102 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1105 player->bounce(this);
1107 player_status.score_multiplier++;
1109 // check for maximum number of squishes
1111 if(squishcount >= MAX_ICEBLOCK_SQUICHES) {
1117 } else if(kind == BAD_FISH || kind == BAD_FLAMEFISH) {
1118 // fish can only be killed when falling down
1119 if(physic.get_velocity_y() >= 0)
1122 player->bounce(this);
1124 Sector::current()->add_score(Vector(base.x, base.y),
1125 25 * player_status.score_multiplier);
1127 player_status.score_multiplier++;
1129 // simply remove the fish...
1132 } else if(kind == BAD_BOUNCINGSNOWBALL) {
1134 set_sprite(img_bouncingsnowball_squished,img_bouncingsnowball_squished);
1136 } else if(kind == BAD_FLYINGSNOWBALL) {
1138 set_sprite(img_flyingsnowball_squished,img_flyingsnowball_squished);
1140 } else if(kind == BAD_SNOWBALL) {
1142 set_sprite(img_snowball_squished_left, img_snowball_squished_right);
1144 } else if(kind == BAD_WINGLING) {
1146 set_sprite(img_wingling_left, img_wingling_left);
1147 } else if(kind == BAD_WALKINGTREE) {
1148 if (mode == BGM_BIG)
1150 set_sprite(img_walkingtree_left_small, img_walkingtree_left_small);
1151 physic.set_velocity_x(physic.get_velocity_x() * 2.0f);
1153 /* Move to the player's direction */
1154 if(dir != Sector::current()->player->dir)
1155 physic.set_velocity_x(-physic.get_velocity_x());
1156 dir = Sector::current()->player->dir;
1158 // XXX magic number: 66 is BGM_BIG height
1160 player->bounce(this);
1161 base.y += 66 - base.height;
1163 Sector::current()->add_score(Vector(base.x, base.y),
1164 25 * player_status.score_multiplier);
1165 player_status.score_multiplier++;
1175 BadGuy::kill_me(int score)
1177 if(kind == BAD_BOMB)
1181 global_stats.add_points(BADGUYS_KILLED_STAT, 1);
1183 dying = DYING_FALLING;
1184 if(kind == BAD_MRICEBLOCK) {
1185 set_sprite(img_mriceblock_falling_left, img_mriceblock_falling_right);
1188 Player& tux = *Sector::current()->player;
1189 tux.holding_something = false;
1193 physic.enable_gravity(true);
1195 /* Gain some points: */
1197 Sector::current()->add_score(Vector(base.x, base.y),
1198 score * player_status.score_multiplier);
1200 /* Play death sound: */
1201 SoundManager::get()->play_sound(IDToSound(SND_FALL), this, Sector::current()->player->get_pos());
1205 BadGuy::explode(bool right_way)
1207 BadGuy *badguy = Sector::current()->add_bad_guy(base.x, base.y, BAD_BOMB);
1210 badguy->timer.start(0);
1211 badguy->mode = BOMB_TICKING;
1218 BadGuy::collision(const MovingObject&, int)
1224 BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
1226 BadGuy* pbad_c = NULL;
1227 Bullet* pbullet_c = NULL;
1229 if(type == COLLISION_BUMP) {
1234 if(type == COLLISION_SQUISH) {
1235 Player* player = static_cast<Player*>(p_c_object);
1240 /* COLLISION_NORMAL */
1244 pbullet_c = (Bullet*) p_c_object;
1246 if(pbullet_c->kind == FIRE_BULLET)
1248 if (kind != BAD_BOMB && kind != BAD_STALACTITE && kind != BAD_FLAME
1249 && kind != BAD_FLAMEFISH)
1252 else if(pbullet_c->kind == ICE_BULLET)
1254 if(kind == BAD_FLAME || kind == BAD_FLAMEFISH)
1257 frozen_timer.start(FROZEN_TIME);
1262 pbad_c = (BadGuy*) p_c_object;
1265 /* If we're a kicked mriceblock, kill [almost] any badguys we hit */
1266 if(kind == BAD_MRICEBLOCK && mode == KICK &&
1267 kind != BAD_FLAME && kind != BAD_BOMB && kind != BAD_STALACTITE)
1269 pbad_c->kill_me(25);
1272 // a held mriceblock kills the enemy too but falls to ground then
1273 else if(kind == BAD_MRICEBLOCK && mode == HELD)
1275 pbad_c->kill_me(25);
1279 /* Kill badguys that run into exploding bomb */
1280 else if (kind == BAD_BOMB && dying == DYING_NOT)
1282 if (pbad_c->kind == BAD_MRBOMB)
1284 // mrbomb transforms into a bomb now
1285 pbad_c->explode(true);
1290 pbad_c->kill_me(50);
1294 /* Kill any badguys that get hit by stalactite */
1295 else if (kind == BAD_STALACTITE && dying == DYING_NOT)
1297 if (pbad_c->kind == BAD_MRBOMB)
1299 // mrbomb transforms into a bomb now
1300 pbad_c->explode(false);
1304 pbad_c->kill_me(50);
1307 /* When enemies run into eachother, make them change directions */
1310 // Wingling doesn't interact with other badguys
1311 if (pbad_c->kind == BAD_WINGLING || kind == BAD_WINGLING)
1314 // Jumpy, fish, flame, stalactites, wingling are exceptions
1315 if (pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FLAME
1316 || pbad_c->kind == BAD_STALACTITE || pbad_c->kind == BAD_FISH
1317 || pbad_c->kind == BAD_FLAMEFISH)
1320 // Bounce off of other badguy if we land on top of him
1321 if (base.y + base.height < pbad_c->base.y + pbad_c->base.height)
1323 if (pbad_c->dir == LEFT)
1326 physic.set_velocity(fabsf(physic.get_velocity_x()), 2);
1328 else if (pbad_c->dir == RIGHT)
1331 physic.set_velocity(-fabsf(physic.get_velocity_x()), 2);
1336 else if (base.y + base.height > pbad_c->base.y + pbad_c->base.height)
1339 if (pbad_c->kind != BAD_FLAME)
1344 physic.set_velocity_x(fabsf(physic.get_velocity_x()));
1346 // Put bad guys a part (or they get jammed)
1347 // only needed to do to one of them
1348 if (physic.get_velocity_x() != 0)
1349 base.x = pbad_c->base.x + pbad_c->base.width + 1;
1351 else if (dir == RIGHT)
1354 physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
1363 Player* player = static_cast<Player*>(p_c_object);
1364 /* Get kicked if were flat */
1365 if (mode == FLAT && !dying)
1367 SoundManager::get()->play_sound(IDToSound(SND_KICK), this, Sector::current()->player->get_pos());
1369 // Hit from left side
1370 if (player->base.x < base.x) {
1371 physic.set_velocity_x(5);
1374 // Hit from right side
1376 physic.set_velocity_x(-5);
1381 player->kick_timer.start(KICKING_TIME);
1382 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1390 //---------------------------------------------------------------------------
1392 void load_badguy_gfx()
1394 img_mriceblock_flat_left = sprite_manager->load("mriceblock-flat-left");
1395 img_mriceblock_flat_right = sprite_manager->load("mriceblock-flat-right");
1396 img_mriceblock_falling_left = sprite_manager->load("mriceblock-falling-left");
1397 img_mriceblock_falling_right = sprite_manager->load("mriceblock-falling-right");
1398 img_mriceblock_left = sprite_manager->load("mriceblock-left");
1399 img_mriceblock_right = sprite_manager->load("mriceblock-right");
1400 img_jumpy_left_up = sprite_manager->load("jumpy-left-up");
1401 img_jumpy_left_down = sprite_manager->load("jumpy-left-down");
1402 img_jumpy_left_middle = sprite_manager->load("jumpy-left-middle");
1403 img_jumpy_left_iced = sprite_manager->load("jumpy-left-iced");
1404 img_mrbomb_left = sprite_manager->load("mrbomb-left");
1405 img_mrbomb_right = sprite_manager->load("mrbomb-right");
1406 img_mrbomb_iced_left = sprite_manager->load("mrbomb-iced-left");
1407 img_mrbomb_iced_right = sprite_manager->load("mrbomb-iced-right");
1408 img_mrbomb_ticking_left = sprite_manager->load("mrbomb-ticking-left");
1409 img_mrbomb_ticking_right = sprite_manager->load("mrbomb-ticking-right");
1410 img_mrbomb_explosion = sprite_manager->load("mrbomb-explosion");
1411 img_stalactite = sprite_manager->load("stalactite");
1412 img_stalactite_broken = sprite_manager->load("stalactite-broken");
1413 img_flame = sprite_manager->load("flame");
1414 img_fish = sprite_manager->load("fish");
1415 img_fish_down = sprite_manager->load("fish-down");
1416 img_fish_iced = sprite_manager->load("fish-iced");
1417 img_fish_iced_down = sprite_manager->load("fish-iced-down");
1418 img_flamefish = sprite_manager->load("flamefish");
1419 img_flamefish_down = sprite_manager->load("flamefish-down");
1420 img_bouncingsnowball_left = sprite_manager->load("bouncingsnowball-left");
1421 img_bouncingsnowball_right = sprite_manager->load("bouncingsnowball-right");
1422 img_bouncingsnowball_squished = sprite_manager->load("bouncingsnowball-squished");
1423 img_flyingsnowball = sprite_manager->load("flyingsnowball");
1424 img_flyingsnowball_squished = sprite_manager->load("flyingsnowball-squished");
1425 img_spiky_left = sprite_manager->load("spiky-left");
1426 img_spiky_right = sprite_manager->load("spiky-right");
1427 img_spiky_iced_left = sprite_manager->load("spiky-iced-left");
1428 img_spiky_iced_right = sprite_manager->load("spiky-iced-right");
1429 img_snowball_left = sprite_manager->load("snowball-left");
1430 img_snowball_right = sprite_manager->load("snowball-right");
1431 img_snowball_squished_left = sprite_manager->load("snowball-squished-left");
1432 img_snowball_squished_right = sprite_manager->load("snowball-squished-right");
1433 img_wingling_left = sprite_manager->load("wingling-left");
1434 img_walkingtree_left = sprite_manager->load("walkingtree-left");
1435 img_walkingtree_left_small = sprite_manager->load("walkingtree-left-small");
1438 void free_badguy_gfx()