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"
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;
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;
81 #define BADGUY_WALK_SPEED .8f
82 #define WINGLING_FLY_SPEED 1.6f
84 BadGuyKind badguykind_from_string(const std::string& str)
86 if (str == "money" || str == "jumpy") // was money in old maps
88 else if (str == "laptop" || str == "mriceblock") // was laptop in old maps
89 return BAD_MRICEBLOCK;
90 else if (str == "mrbomb")
92 else if (str == "stalactite")
93 return BAD_STALACTITE;
94 else if (str == "flame")
96 else if (str == "fish")
98 else if (str == "flamefish")
100 else if (str == "bouncingsnowball")
101 return BAD_BOUNCINGSNOWBALL;
102 else if (str == "flyingsnowball")
103 return BAD_FLYINGSNOWBALL;
104 else if (str == "spiky")
106 else if (str == "snowball" || str == "bsod") // was bsod in old maps
108 else if (str == "wingling")
110 else if (str == "walkingtree")
111 return BAD_WALKINGTREE;
118 std::string badguykind_to_string(BadGuyKind kind)
143 case BAD_BOUNCINGSNOWBALL:
144 return "bouncingsnowball";
146 case BAD_FLYINGSNOWBALL:
147 return "flyingsnowball";
158 case BAD_WALKINGTREE:
159 return "walkingtree";
165 BadGuy::BadGuy(BadGuyKind kind_, LispReader& lispreader)
166 : removable(false), squishcount(0)
168 lispreader.read_float("x", start_position.x);
169 lispreader.read_float("y", start_position.y);
173 stay_on_platform = false;
174 lispreader.read_bool("stay-on-platform", stay_on_platform);
179 BadGuy::BadGuy(BadGuyKind kind_, float x, float y)
180 : removable(false), squishcount(0)
182 start_position.x = x;
183 start_position.y = y;
184 stay_on_platform = false;
208 animation_offset = 0;
209 target.x = target.y = -1;
210 sprite_left = sprite_right = 0;
212 frozen_timer.init(true);
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))
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))
225 if(Sector::current()->camera) {
226 Vector scroll = Sector::current()->camera->get_translation();
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) {
235 if(start_position.x > 0 && start_position.x <= screen->w
236 && start_position.y > 0 && start_position.y <= screen->h)
242 BadGuy::write(LispWriter& writer)
244 writer.start_list(badguykind_to_string(kind));
246 writer.write_float("x", base.x);
247 writer.write_float("y", base.y);
248 writer.write_bool("stay-on-platform", stay_on_platform);
250 writer.end_list(badguykind_to_string(kind));
254 BadGuy::activate(Direction activation_dir)
257 animation_offset = 0;
258 target.x = target.y = -1;
260 frozen_timer.init(true);
263 dir = activation_dir;
264 float dirsign = activation_dir == LEFT ? -1 : 1;
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) {
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);
311 set_sprite(img_walkingtree_left, img_walkingtree_left);
316 base.x = start_position.x;
317 base.y = start_position.y;
323 BadGuy::action_mriceblock(double elapsed_time)
325 Player& tux = *Sector::current()->player;
330 /* Move left/right: */
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);
338 else if (mode == HELD)
339 { /* FIXME: The pbad object shouldn't know about pplayer objects. */
340 /* If we're holding the iceblock */
342 if(tux.size == SMALL)
345 base.x = tux.base.x + 24;
347 base.x = tux.base.x - 12;
348 base.y = tux.base.y + tux.base.height/1.5 - base.height;
353 base.x = tux.base.x + 24;
355 base.x = tux.base.x - 4;
356 base.y = tux.base.y + tux.base.height/1.5 - base.height;
359 if(collision_object_map(base))
362 base.y = tux.base.y + tux.base.height/1.5 - base.height;
365 if(tux.input.fire != DOWN) /* SHOOT! */
368 base.x = tux.base.x - base.width;
370 base.x = tux.base.x + tux.base.width;
374 tux.kick_timer.start(KICKING_TIME);
375 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
376 physic.set_velocity_x((dir == LEFT) ? -3.5 : 3.5);
377 SoundManager::get()->play_sound(IDToSound(SND_KICK), this, Sector::current()->player->get_pos());
384 check_horizontal_bump();
385 if(mode == KICK && changed != dir)
387 SoundManager::get()->play_sound(IDToSound(SND_RICOCHET), get_pos(), Sector::current()->player->get_pos());
391 /* Handle mode timer: */
397 set_sprite(img_mriceblock_left, img_mriceblock_right);
398 physic.set_velocity( (dir == LEFT) ? -.8 : .8, 0);
404 BadGuy::check_horizontal_bump(bool checkcliff)
406 float halfheight = base.height / 2;
407 if (dir == LEFT && issolid( base.x, base.y + halfheight))
409 if (kind == BAD_MRICEBLOCK && mode == KICK)
411 Sector::current()->trybreakbrick(Vector(base.x, base.y + halfheight), false);
412 Sector::current()->tryemptybox(Vector(base.x, base.y + halfheight), dir);
416 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
419 if (dir == RIGHT && issolid( base.x + base.width, base.y + halfheight))
421 if (kind == BAD_MRICEBLOCK && mode == KICK)
423 Sector::current()->trybreakbrick(
424 Vector(base.x + base.width, base.y + halfheight), false);
425 Sector::current()->tryemptybox(
426 Vector(base.x + base.width, base.y + halfheight), dir);
430 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
434 // don't check for cliffs when we're falling
437 if(!issolid(base.x + base.width/2, base.y + base.height))
440 if(dir == LEFT && !issolid(base.x, (int) base.y + base.height + halfheight))
443 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
446 if(dir == RIGHT && !issolid(base.x + base.width,
447 (int) base.y + base.height + halfheight))
450 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
458 /* Fall if we get off the ground: */
459 if (dying != DYING_FALLING)
461 if (!issolid(base.x+base.width/2, base.y + base.height))
463 // not solid below us? enable gravity
464 physic.enable_gravity(true);
469 if (physic.get_velocity_y() < 0)
471 base.y = int((base.y + base.height)/32) * 32 - base.height;
472 physic.set_velocity_y(0);
474 // no gravity anymore please
475 physic.enable_gravity(false);
477 if (stay_on_platform && mode == NORMAL)
479 if (!issolid(base.x + ((dir == LEFT) ? 0 : base.width),
480 base.y + base.height))
485 physic.set_velocity_x(fabsf(physic.get_velocity_x()));
490 physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
498 physic.enable_gravity(true);
503 BadGuy::action_jumpy(double elapsed_time)
505 if(frozen_timer.check())
507 set_sprite(img_jumpy_left_iced, img_jumpy_left_iced);
511 const float vy = physic.get_velocity_y();
513 // XXX: These tests *should* use location from ground, not velocity
514 if (fabsf(vy) > 5.6f)
515 set_sprite(img_jumpy_left_down, img_jumpy_left_down);
516 else if (fabsf(vy) > 5.3f)
517 set_sprite(img_jumpy_left_middle, img_jumpy_left_middle);
519 set_sprite(img_jumpy_left_up, img_jumpy_left_up);
521 Player& tux = *Sector::current()->player;
523 static const float JUMPV = 6;
526 // jump when on ground
527 if(dying == DYING_NOT && issolid(base.x, base.y+32))
529 physic.set_velocity_y(JUMPV);
530 physic.enable_gravity(true);
534 else if(mode == JUMPY_JUMP)
539 // set direction based on tux
540 if(tux.base.x > base.x)
546 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
547 if(dying == DYING_NOT)
548 collision_swept_object_map(&old_base, &base);
552 BadGuy::action_mrbomb(double elapsed_time)
554 if(frozen_timer.check())
556 set_sprite(img_mrbomb_iced_left, img_mrbomb_iced_right);
560 if (dying == DYING_NOT)
561 check_horizontal_bump(true);
565 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
566 if (dying != DYING_FALLING)
567 collision_swept_object_map(&old_base,&base);
571 BadGuy::action_bomb(double elapsed_time)
573 static const int TICKINGTIME = 1000;
574 static const int EXPLODETIME = 1000;
580 timer.start(TICKINGTIME);
581 } else if(!timer.check()) {
582 if(mode == BOMB_TICKING) {
584 set_sprite(img_mrbomb_explosion, img_mrbomb_explosion);
585 dying = DYING_NOT; // now the bomb hurts
586 timer.start(EXPLODETIME);
588 SoundManager::get()->play_sound(IDToSound(SND_EXPLODE), this, Sector::current()->player->get_pos());
589 } else if(mode == BOMB_EXPLODE) {
596 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
597 collision_swept_object_map(&old_base,&base);
601 BadGuy::action_stalactite(double elapsed_time)
603 Player& tux = *Sector::current()->player;
605 static const int SHAKETIME = 800;
606 static const int RANGE = 40;
609 // start shaking when tux is below the stalactite and at least 40 pixels
611 if(tux.base.x + 32 > base.x - RANGE && tux.base.x < base.x + 32 + RANGE
612 && tux.base.y + tux.base.height > base.y
613 && tux.dying == DYING_NOT) {
614 timer.start(SHAKETIME);
615 mode = STALACTITE_SHAKING;
617 } if(mode == STALACTITE_SHAKING) {
618 base.x = old_base.x + (rand() % 6) - 3; // TODO this could be done nicer...
620 mode = STALACTITE_FALL;
622 } else if(mode == STALACTITE_FALL) {
624 /* Destroy if we collides with land */
625 if(issolid(base.x+base.width/2, base.y+base.height))
628 dying = DYING_SQUISHED;
630 set_sprite(img_stalactite_broken, img_stalactite_broken);
632 } else if(mode == FLAT) {
637 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
639 if(dying == DYING_SQUISHED && !timer.check())
644 BadGuy::action_flame(double elapsed_time)
646 static const float radius = 100;
647 static const float speed = 0.02;
648 base.x = old_base.x + cos(angle) * radius;
649 base.y = old_base.y + sin(angle) * radius;
651 angle = fmodf(angle + elapsed_time * speed, 2*M_PI);
655 BadGuy::action_fish(double elapsed_time)
657 if(frozen_timer.check())
659 if(physic.get_velocity_y() < 0)
660 set_sprite(img_fish_iced_down, img_fish_iced_down);
662 set_sprite(img_fish_iced, img_fish_iced);
667 static const float JUMPV = 6;
668 static const int WAITTIME = 1000;
670 // go in wait mode when back in water
671 if(dying == DYING_NOT
672 && gettile(base.x, base.y + base.height)
673 && gettile(base.x, base.y + base.height)->attributes & Tile::WATER
674 && physic.get_velocity_y() <= 0 && mode == NORMAL)
678 physic.set_velocity(0, 0);
679 physic.enable_gravity(false);
680 timer.start(WAITTIME);
682 else if(mode == FISH_WAIT && !timer.check())
686 set_sprite(img_fish, img_fish);
687 else // BAD_FLAMEFISH
688 set_sprite(img_flamefish, img_flamefish);
690 physic.set_velocity(0, JUMPV);
691 physic.enable_gravity(true);
694 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
695 if(dying == DYING_NOT)
696 collision_swept_object_map(&old_base, &base);
698 if(physic.get_velocity_y() < 0)
701 set_sprite(img_fish_down, img_fish_down);
702 else // BAD_FLAMEFISH
703 set_sprite(img_flamefish_down, img_flamefish_down);
708 BadGuy::action_bouncingsnowball(double elapsed_time)
710 static const float JUMPV = 4.5;
714 // jump when on ground
715 if(dying == DYING_NOT && issolid(base.x, base.y+32))
717 physic.set_velocity_y(JUMPV);
718 physic.enable_gravity(true);
725 // check for right/left collisions
726 check_horizontal_bump();
728 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
729 if(dying == DYING_NOT)
730 collision_swept_object_map(&old_base, &base);
732 // Handle dying timer:
733 if (dying == DYING_SQUISHED && !timer.check())
738 BadGuy::action_flyingsnowball(double elapsed_time)
740 static const float FLYINGSPEED = 1;
741 static const int DIRCHANGETIME = 1000;
743 // go into flyup mode if none specified yet
744 if(dying == DYING_NOT && mode == NORMAL) {
746 physic.set_velocity_y(FLYINGSPEED);
747 timer.start(DIRCHANGETIME/2);
750 if(dying == DYING_NOT && !timer.check()) {
753 physic.set_velocity_y(-FLYINGSPEED);
754 } else if(mode == FLY_DOWN) {
756 physic.set_velocity_y(FLYINGSPEED);
758 timer.start(DIRCHANGETIME);
761 if(dying != DYING_NOT)
762 physic.enable_gravity(true);
764 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
765 if(dying == DYING_NOT || dying == DYING_SQUISHED)
766 collision_swept_object_map(&old_base, &base);
768 // Handle dying timer:
769 if (dying == DYING_SQUISHED && !timer.check())
774 BadGuy::action_spiky(double elapsed_time)
776 if(frozen_timer.check())
778 set_sprite(img_spiky_iced_left, img_spiky_iced_right);
782 if (dying == DYING_NOT)
783 check_horizontal_bump();
787 // jump when we're about to fall
788 if (physic.get_velocity_y() == 0 &&
789 !issolid(base.x+base.width/2, base.y + base.height)) {
790 physic.enable_gravity(true);
791 physic.set_velocity_y(2);
795 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
796 if (dying != DYING_FALLING)
797 collision_swept_object_map(&old_base,&base);
801 BadGuy::action_snowball(double elapsed_time)
803 if (dying == DYING_NOT)
804 check_horizontal_bump();
808 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
809 if (dying != DYING_FALLING)
810 collision_swept_object_map(&old_base,&base);
812 // Handle dying timer:
813 if (dying == DYING_SQUISHED && !timer.check())
818 BadGuy::action_wingling(double elapsed_time)
820 if (dying != DYING_NOT)
821 physic.enable_gravity(true);
824 Player& tux = *Sector::current()->player;
825 int dirsign = physic.get_velocity_x() < 0 ? -1 : 1;
827 if (fabsf(tux.base.x - base.x) < 150 && base.y < tux.base.y && tux.dying == DYING_NOT)
829 if (target.x < 0 && target.y < 0)
831 target.x = tux.base.x;
832 target.y = tux.base.y;
833 physic.set_velocity(dirsign * 1.5f, -2.25f);
836 else if (base.y >= target.y - 16)
837 physic.set_velocity(dirsign * WINGLING_FLY_SPEED, 0);
840 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
843 // Handle dying timer:
844 if (dying == DYING_SQUISHED && !timer.check())
847 // TODO: Winglings should be removed after flying off the screen
851 BadGuy::action_walkingtree(double elapsed_time)
853 if (dying == DYING_NOT)
854 check_horizontal_bump();
858 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
859 if (dying != DYING_FALLING)
860 collision_swept_object_map(&old_base,&base);
862 // Handle dying timer:
863 if (dying == DYING_SQUISHED && !timer.check())
868 BadGuy::action(float elapsed_time)
870 float scroll_x = Sector::current()->camera->get_translation().x;
871 float scroll_y = Sector::current()->camera->get_translation().y;
873 // BadGuy fall below the ground
874 if (base.y > Sector::current()->solids->get_height() * 32) {
879 // Kill us if we landed on spikes
880 if (dying == DYING_NOT
881 && (kind != BAD_STALACTITE && kind != BAD_FLAME && kind != BAD_BOMB)
882 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
883 || isspike(base.x, base.y + base.height)
884 || isspike(base.x + base.width, base.y + base.height)))
886 physic.set_velocity_y(3);
891 /* activate badguys if they're just inside the offscreen_distance around the
892 * screen. Don't activate them inside the screen, since that might have the
893 * effect of badguys suddenly popping up from nowhere
895 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
896 start_position.x < scroll_x - base.width)
898 else if(start_position.x > scroll_y - Y_OFFSCREEN_DISTANCE &&
899 start_position.y < scroll_y - base.height)
901 else if(start_position.x > scroll_x + screen->w &&
902 start_position.x < scroll_x + screen->w + X_OFFSCREEN_DISTANCE)
904 else if(start_position.y > scroll_y + screen->h &&
905 start_position.y < scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
908 if(base.x + base.width < scroll_x - X_OFFSCREEN_DISTANCE*4
909 || base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE*4
910 || base.y + base.height < scroll_y - Y_OFFSCREEN_DISTANCE*4
911 || base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE*4) {
913 if(dying != DYING_NOT)
924 action_mriceblock(elapsed_time);
928 action_jumpy(elapsed_time);
932 action_mrbomb(elapsed_time);
936 action_bomb(elapsed_time);
940 action_stalactite(elapsed_time);
944 action_flame(elapsed_time);
949 action_fish(elapsed_time);
952 case BAD_BOUNCINGSNOWBALL:
953 action_bouncingsnowball(elapsed_time);
956 case BAD_FLYINGSNOWBALL:
957 action_flyingsnowball(elapsed_time);
961 action_spiky(elapsed_time);
965 action_snowball(elapsed_time);
969 action_wingling(elapsed_time);
972 case BAD_WALKINGTREE:
973 action_walkingtree(elapsed_time);
982 BadGuy::draw(DrawingContext& context)
984 Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
988 if(dying == DYING_FALLING && physic.get_velocity_y() < 0)
989 sprite->draw(context, Vector(base.x, base.y), LAYER_FOREGROUNDTILES+1, VERTICAL_FLIP);
991 sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
994 context.draw_filled_rect(Vector(base.x, base.y),
995 Vector(base.width, base.height), Color(75,0,75, 150), LAYER_OBJECTS+1);
999 BadGuy::set_sprite(Sprite* left, Sprite* right)
1008 // FIXME: Using the image size for the physics and collision is
1009 // a bad idea, since images should always overlap there physical
1012 if(base.width == 0 && base.height == 0) {
1013 base.width = left->get_width();
1014 base.height = left->get_height();
1015 } else if(base.width != left->get_width() || base.height != left->get_height()) {
1016 base.x -= (left->get_width() - base.width) / 2;
1017 base.y -= left->get_height() - base.height;
1018 base.width = left->get_width();
1019 base.height = left->get_height();
1023 base.width = base.height = 0;
1027 animation_offset = 0;
1029 sprite_right = right;
1035 // these can't be bumped
1036 if(kind == BAD_FLAME || kind == BAD_BOMB || kind == BAD_FISH
1037 || kind == BAD_FLAMEFISH || kind == BAD_FLYINGSNOWBALL)
1040 physic.set_velocity_y(3);
1045 BadGuy::squish_me(Player* player)
1047 player->bounce(this);
1049 Sector::current()->add_score(Vector(base.x, base.y),
1050 25 * player_status.score_multiplier);
1052 SoundManager::get()->play_sound(IDToSound(SND_SQUISH), get_pos(), Sector::current()->player->get_pos());
1053 player_status.score_multiplier++;
1055 dying = DYING_SQUISHED;
1057 physic.set_velocity(0, 0);
1061 BadGuy::squish(Player* player)
1063 static const int MAX_ICEBLOCK_SQUICHES = 10;
1065 if(kind == BAD_MRBOMB) {
1066 // mrbomb transforms into a bomb now
1069 player->bounce(this);
1070 Sector::current()->add_score(Vector(base.x, base.y),
1071 25 * player_status.score_multiplier);
1072 SoundManager::get()->play_sound(IDToSound(SND_SQUISH), get_pos(), Sector::current()->player->get_pos());
1073 player_status.score_multiplier++;
1076 } else if (kind == BAD_MRICEBLOCK) {
1077 if (mode == NORMAL || mode == KICK)
1080 SoundManager::get()->play_sound(IDToSound(SND_STOMP), get_pos(), Sector::current()->player->get_pos());
1082 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1083 physic.set_velocity_x(0);
1086 } else if (mode == FLAT) {
1088 SoundManager::get()->play_sound(IDToSound(SND_KICK), this, Sector::current()->player->get_pos());
1090 if (player->base.x < base.x + (base.width/2)) {
1091 physic.set_velocity_x(5);
1094 physic.set_velocity_x(-5);
1099 player->kick_timer.start(KICKING_TIME);
1100 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1103 player->bounce(this);
1105 player_status.score_multiplier++;
1107 // check for maximum number of squishes
1109 if(squishcount >= MAX_ICEBLOCK_SQUICHES) {
1115 } else if(kind == BAD_FISH || kind == BAD_FLAMEFISH) {
1116 // fish can only be killed when falling down
1117 if(physic.get_velocity_y() >= 0)
1120 player->bounce(this);
1122 Sector::current()->add_score(Vector(base.x, base.y),
1123 25 * player_status.score_multiplier);
1124 player_status.score_multiplier++;
1126 // simply remove the fish...
1129 } else if(kind == BAD_BOUNCINGSNOWBALL) {
1131 set_sprite(img_bouncingsnowball_squished,img_bouncingsnowball_squished);
1133 } else if(kind == BAD_FLYINGSNOWBALL) {
1135 set_sprite(img_flyingsnowball_squished,img_flyingsnowball_squished);
1137 } else if(kind == BAD_SNOWBALL) {
1139 set_sprite(img_snowball_squished_left, img_snowball_squished_right);
1141 } else if(kind == BAD_WINGLING) {
1143 set_sprite(img_wingling_left, img_wingling_left);
1144 } else if(kind == BAD_WALKINGTREE) {
1145 if (mode == BGM_BIG)
1147 set_sprite(img_walkingtree_left_small, img_walkingtree_left_small);
1148 physic.set_velocity_x(physic.get_velocity_x() * 2.0f);
1150 /* Move to the player's direction */
1151 if(dir != Sector::current()->player->dir)
1152 physic.set_velocity_x(-physic.get_velocity_x());
1153 dir = Sector::current()->player->dir;
1155 // XXX magic number: 66 is BGM_BIG height
1157 player->bounce(this);
1158 base.y += 66 - base.height;
1160 Sector::current()->add_score(Vector(base.x, base.y),
1161 25 * player_status.score_multiplier);
1162 player_status.score_multiplier++;
1172 BadGuy::kill_me(int score)
1174 if(kind == BAD_BOMB)
1177 dying = DYING_FALLING;
1178 if(kind == BAD_MRICEBLOCK) {
1179 set_sprite(img_mriceblock_falling_left, img_mriceblock_falling_right);
1182 Player& tux = *Sector::current()->player;
1183 tux.holding_something = false;
1187 physic.enable_gravity(true);
1189 /* Gain some points: */
1191 Sector::current()->add_score(Vector(base.x, base.y),
1192 score * player_status.score_multiplier);
1194 /* Play death sound: */
1195 SoundManager::get()->play_sound(IDToSound(SND_FALL), this, Sector::current()->player->get_pos());
1199 BadGuy::explode(bool right_way)
1201 BadGuy *badguy = Sector::current()->add_bad_guy(base.x, base.y, BAD_BOMB);
1204 badguy->timer.start(0);
1205 badguy->mode = BOMB_TICKING;
1212 BadGuy::collision(const MovingObject&, int)
1218 BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
1220 BadGuy* pbad_c = NULL;
1221 Bullet* pbullet_c = NULL;
1223 if(type == COLLISION_BUMP) {
1228 if(type == COLLISION_SQUISH) {
1229 Player* player = static_cast<Player*>(p_c_object);
1234 /* COLLISION_NORMAL */
1238 pbullet_c = (Bullet*) p_c_object;
1240 if(pbullet_c->kind == FIRE_BULLET)
1242 if (kind != BAD_BOMB && kind != BAD_STALACTITE && kind != BAD_FLAME
1243 && kind != BAD_FLAMEFISH)
1246 else if(pbullet_c->kind == ICE_BULLET)
1248 if(kind == BAD_FLAME || kind == BAD_FLAMEFISH)
1251 frozen_timer.start(FROZEN_TIME);
1256 pbad_c = (BadGuy*) p_c_object;
1259 /* If we're a kicked mriceblock, kill [almost] any badguys we hit */
1260 if(kind == BAD_MRICEBLOCK && mode == KICK &&
1261 kind != BAD_FLAME && kind != BAD_BOMB && kind != BAD_STALACTITE)
1263 pbad_c->kill_me(25);
1266 // a held mriceblock kills the enemy too but falls to ground then
1267 else if(kind == BAD_MRICEBLOCK && mode == HELD)
1269 pbad_c->kill_me(25);
1273 /* Kill badguys that run into exploding bomb */
1274 else if (kind == BAD_BOMB && dying == DYING_NOT)
1276 if (pbad_c->kind == BAD_MRBOMB)
1278 // mrbomb transforms into a bomb now
1279 pbad_c->explode(true);
1284 pbad_c->kill_me(50);
1288 /* Kill any badguys that get hit by stalactite */
1289 else if (kind == BAD_STALACTITE && dying == DYING_NOT)
1291 if (pbad_c->kind == BAD_MRBOMB)
1293 // mrbomb transforms into a bomb now
1294 pbad_c->explode(false);
1298 pbad_c->kill_me(50);
1301 /* When enemies run into eachother, make them change directions */
1304 // Wingling doesn't interact with other badguys
1305 if (pbad_c->kind == BAD_WINGLING || kind == BAD_WINGLING)
1308 // Jumpy, fish, flame, stalactites, wingling are exceptions
1309 if (pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FLAME
1310 || pbad_c->kind == BAD_STALACTITE || pbad_c->kind == BAD_FISH
1311 || pbad_c->kind == BAD_FLAMEFISH)
1314 // Bounce off of other badguy if we land on top of him
1315 if (base.y + base.height < pbad_c->base.y + pbad_c->base.height)
1317 if (pbad_c->dir == LEFT)
1320 physic.set_velocity(fabsf(physic.get_velocity_x()), 2);
1322 else if (pbad_c->dir == RIGHT)
1325 physic.set_velocity(-fabsf(physic.get_velocity_x()), 2);
1330 else if (base.y + base.height > pbad_c->base.y + pbad_c->base.height)
1333 if (pbad_c->kind != BAD_FLAME)
1338 physic.set_velocity_x(fabsf(physic.get_velocity_x()));
1340 // Put bad guys a part (or they get jammed)
1341 // only needed to do to one of them
1342 if (physic.get_velocity_x() != 0)
1343 base.x = pbad_c->base.x + pbad_c->base.width + 1;
1345 else if (dir == RIGHT)
1348 physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
1357 Player* player = static_cast<Player*>(p_c_object);
1358 /* Get kicked if were flat */
1359 if (mode == FLAT && !dying)
1361 SoundManager::get()->play_sound(IDToSound(SND_KICK), this, Sector::current()->player->get_pos());
1363 // Hit from left side
1364 if (player->base.x < base.x) {
1365 physic.set_velocity_x(5);
1368 // Hit from right side
1370 physic.set_velocity_x(-5);
1375 player->kick_timer.start(KICKING_TIME);
1376 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1384 //---------------------------------------------------------------------------
1386 void load_badguy_gfx()
1388 img_mriceblock_flat_left = sprite_manager->load("mriceblock-flat-left");
1389 img_mriceblock_flat_right = sprite_manager->load("mriceblock-flat-right");
1390 img_mriceblock_falling_left = sprite_manager->load("mriceblock-falling-left");
1391 img_mriceblock_falling_right = sprite_manager->load("mriceblock-falling-right");
1392 img_mriceblock_left = sprite_manager->load("mriceblock-left");
1393 img_mriceblock_right = sprite_manager->load("mriceblock-right");
1394 img_jumpy_left_up = sprite_manager->load("jumpy-left-up");
1395 img_jumpy_left_down = sprite_manager->load("jumpy-left-down");
1396 img_jumpy_left_middle = sprite_manager->load("jumpy-left-middle");
1397 img_jumpy_left_iced = sprite_manager->load("jumpy-left-iced");
1398 img_mrbomb_left = sprite_manager->load("mrbomb-left");
1399 img_mrbomb_right = sprite_manager->load("mrbomb-right");
1400 img_mrbomb_iced_left = sprite_manager->load("mrbomb-iced-left");
1401 img_mrbomb_iced_right = sprite_manager->load("mrbomb-iced-right");
1402 img_mrbomb_ticking_left = sprite_manager->load("mrbomb-ticking-left");
1403 img_mrbomb_ticking_right = sprite_manager->load("mrbomb-ticking-right");
1404 img_mrbomb_explosion = sprite_manager->load("mrbomb-explosion");
1405 img_stalactite = sprite_manager->load("stalactite");
1406 img_stalactite_broken = sprite_manager->load("stalactite-broken");
1407 img_flame = sprite_manager->load("flame");
1408 img_fish = sprite_manager->load("fish");
1409 img_fish_down = sprite_manager->load("fish-down");
1410 img_fish_iced = sprite_manager->load("fish-iced");
1411 img_fish_iced_down = sprite_manager->load("fish-iced-down");
1412 img_flamefish = sprite_manager->load("flamefish");
1413 img_flamefish_down = sprite_manager->load("flamefish-down");
1414 img_bouncingsnowball_left = sprite_manager->load("bouncingsnowball-left");
1415 img_bouncingsnowball_right = sprite_manager->load("bouncingsnowball-right");
1416 img_bouncingsnowball_squished = sprite_manager->load("bouncingsnowball-squished");
1417 img_flyingsnowball = sprite_manager->load("flyingsnowball");
1418 img_flyingsnowball_squished = sprite_manager->load("flyingsnowball-squished");
1419 img_spiky_left = sprite_manager->load("spiky-left");
1420 img_spiky_right = sprite_manager->load("spiky-right");
1421 img_spiky_iced_left = sprite_manager->load("spiky-iced-left");
1422 img_spiky_iced_right = sprite_manager->load("spiky-iced-right");
1423 img_snowball_left = sprite_manager->load("snowball-left");
1424 img_snowball_right = sprite_manager->load("snowball-right");
1425 img_snowball_squished_left = sprite_manager->load("snowball-squished-left");
1426 img_snowball_squished_right = sprite_manager->load("snowball-squished-right");
1427 img_wingling_left = sprite_manager->load("wingling-left");
1428 img_walkingtree_left = sprite_manager->load("walkingtree-left");
1429 img_walkingtree_left_small = sprite_manager->load("walkingtree-left-small");
1432 void free_badguy_gfx()