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
33 #include "resources.h"
34 #include "sprite_manager.h"
36 #include "display_manager.h"
37 #include "lispwriter.h"
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;
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;
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 == "bouncingsnowball")
99 return BAD_BOUNCINGSNOWBALL;
100 else if (str == "flyingsnowball")
101 return BAD_FLYINGSNOWBALL;
102 else if (str == "spiky")
104 else if (str == "snowball" || str == "bsod") // was bsod in old maps
106 else if (str == "wingling")
108 else if (str == "walkingtree")
109 return BAD_WALKINGTREE;
112 printf("Couldn't convert badguy: '%s'\n", str.c_str());
117 std::string badguykind_to_string(BadGuyKind kind)
139 case BAD_BOUNCINGSNOWBALL:
140 return "bouncingsnowball";
142 case BAD_FLYINGSNOWBALL:
143 return "flyingsnowball";
154 case BAD_WALKINGTREE:
155 return "walkingtree";
161 BadGuy::BadGuy(DisplayManager& display_manager, BadGuyKind kind_,
162 LispReader& lispreader)
163 : removable(false), squishcount(0)
165 display_manager.add_drawable(this, LAYER_OBJECTS);
167 lispreader.read_float("x", &start_position.x);
168 lispreader.read_float("y", &start_position.y);
172 stay_on_platform = false;
173 lispreader.read_bool("stay-on-platform", &stay_on_platform);
178 BadGuy::BadGuy(DisplayManager& display_manager, BadGuyKind kind_,
181 display_manager.add_drawable(this, LAYER_OBJECTS);
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(kind != BAD_FLAME && kind != BAD_FISH && 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(World::current()->camera) {
226 Vector scroll = World::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) {
238 BadGuy::write(LispWriter& writer)
240 writer.start_list(badguykind_to_string(kind));
242 writer.write_float("x", base.x);
243 writer.write_float("y", base.y);
244 writer.write_bool("stay-on-platform", stay_on_platform);
246 writer.end_list(badguykind_to_string(kind));
250 BadGuy::activate(Direction activation_dir)
253 animation_offset = 0;
254 target.x = target.y = -1;
256 frozen_timer.init(true);
259 dir = activation_dir;
260 float dirsign = activation_dir == LEFT ? -1 : 1;
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) {
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);
304 set_sprite(img_walkingtree_left, img_walkingtree_left);
309 base.x = start_position.x;
310 base.y = start_position.y;
316 BadGuy::action_mriceblock(double elapsed_time)
318 Player& tux = *World::current()->get_tux();
323 /* Move left/right: */
327 physic.apply(elapsed_time, base.x, base.y);
328 if (dying != DYING_FALLING)
329 collision_swept_object_map(&old_base,&base);
331 else if (mode == HELD)
332 { /* FIXME: The pbad object shouldn't know about pplayer objects. */
333 /* If we're holding the iceblock */
337 base.x = tux.base.x + 16;
338 base.y = tux.base.y + tux.base.height/1.5 - base.height;
340 else /* facing left */
342 base.x = tux.base.x - 16;
343 base.y = tux.base.y + tux.base.height/1.5 - base.height;
345 if(collision_object_map(base))
348 base.y = tux.base.y + tux.base.height/1.5 - base.height;
351 if(tux.input.fire != DOWN) /* SHOOT! */
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);
370 check_horizontal_bump();
371 if(mode == KICK && changed != dir)
373 float scroll_x = World::current()->camera->get_translation().x;
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);
381 play_sound(sounds[SND_RICOCHET], SOUND_CENTER_SPEAKER);
385 /* Handle mode timer: */
391 set_sprite(img_mriceblock_left, img_mriceblock_right);
392 physic.set_velocity( (dir == LEFT) ? -.8 : .8, 0);
398 BadGuy::check_horizontal_bump(bool checkcliff)
400 float halfheight = base.height / 2;
401 if (dir == LEFT && issolid( base.x, (int) base.y + halfheight))
403 if (kind == BAD_MRICEBLOCK && mode == KICK)
404 World::current()->trybreakbrick(base.x, (int) base.y + halfheight, false);
407 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
410 if (dir == RIGHT && issolid( base.x + base.width, (int)base.y + halfheight))
412 if (kind == BAD_MRICEBLOCK && mode == KICK)
413 World::current()->trybreakbrick(base.x + base.width, (int) base.y + halfheight, false);
416 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
420 // don't check for cliffs when we're falling
423 if(!issolid(base.x + base.width/2, base.y + base.height))
426 if(dir == LEFT && !issolid(base.x, (int) base.y + base.height + halfheight))
429 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
432 if(dir == RIGHT && !issolid(base.x + base.width,
433 (int) base.y + base.height + halfheight))
436 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
444 /* Fall if we get off the ground: */
445 if (dying != DYING_FALLING)
447 if (!issolid(base.x+base.width/2, base.y + base.height))
449 // not solid below us? enable gravity
450 physic.enable_gravity(true);
455 if (physic.get_velocity_y() < 0)
457 base.y = int((base.y + base.height)/32) * 32 - base.height;
458 physic.set_velocity_y(0);
460 // no gravity anymore please
461 physic.enable_gravity(false);
463 if (stay_on_platform && mode == NORMAL)
465 if (!issolid(base.x + ((dir == LEFT) ? 0 : base.width),
466 base.y + base.height))
471 physic.set_velocity_x(fabsf(physic.get_velocity_x()));
476 physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
484 physic.enable_gravity(true);
489 BadGuy::action_jumpy(double elapsed_time)
491 if(frozen_timer.check())
493 set_sprite(img_jumpy_left_iced, img_jumpy_left_iced);
497 const float vy = physic.get_velocity_y();
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);
505 set_sprite(img_jumpy_left_up, img_jumpy_left_up);
507 Player& tux = *World::current()->get_tux();
509 static const float JUMPV = 6;
512 // jump when on ground
513 if(dying == DYING_NOT && issolid(base.x, base.y+32))
515 physic.set_velocity_y(JUMPV);
516 physic.enable_gravity(true);
520 else if(mode == JUMPY_JUMP)
525 // set direction based on tux
526 if(tux.base.x > base.x)
532 physic.apply(elapsed_time, base.x, base.y);
533 if(dying == DYING_NOT)
534 collision_swept_object_map(&old_base, &base);
538 BadGuy::action_mrbomb(double elapsed_time)
540 if(frozen_timer.check())
542 set_sprite(img_mrbomb_iced_left, img_mrbomb_iced_right);
546 if (dying == DYING_NOT)
547 check_horizontal_bump(true);
551 physic.apply(elapsed_time, base.x, base.y);
552 if (dying != DYING_FALLING)
553 collision_swept_object_map(&old_base,&base);
557 BadGuy::action_bomb(double elapsed_time)
559 static const int TICKINGTIME = 1000;
560 static const int EXPLODETIME = 1000;
566 timer.start(TICKINGTIME);
567 } else if(!timer.check()) {
568 if(mode == BOMB_TICKING) {
570 set_sprite(img_mrbomb_explosion, img_mrbomb_explosion);
571 dying = DYING_NOT; // now the bomb hurts
572 timer.start(EXPLODETIME);
574 float scroll_x = World::current()->camera->get_translation().x;
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);
582 play_sound(sounds[SND_EXPLODE], SOUND_CENTER_SPEAKER);
584 } else if(mode == BOMB_EXPLODE) {
591 physic.apply(elapsed_time, base.x, base.y);
592 collision_swept_object_map(&old_base,&base);
596 BadGuy::action_stalactite(double elapsed_time)
598 Player& tux = *World::current()->get_tux();
600 static const int SHAKETIME = 800;
601 static const int RANGE = 40;
604 // start shaking when tux is below the stalactite and at least 40 pixels
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;
611 } if(mode == STALACTITE_SHAKING) {
612 base.x = old_base.x + (rand() % 6) - 3; // TODO this could be done nicer...
614 mode = STALACTITE_FALL;
616 } else if(mode == STALACTITE_FALL) {
618 /* Destroy if we collides with land */
619 if(issolid(base.x+base.width/2, base.y+base.height))
622 dying = DYING_SQUISHED;
624 set_sprite(img_stalactite_broken, img_stalactite_broken);
626 } else if(mode == FLAT) {
631 physic.apply(elapsed_time, base.x, base.y);
633 if(dying == DYING_SQUISHED && !timer.check())
638 BadGuy::action_flame(double elapsed_time)
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;
645 angle = fmodf(angle + elapsed_time * speed, 2*M_PI);
649 BadGuy::action_fish(double elapsed_time)
651 if(frozen_timer.check())
653 if(physic.get_velocity_y() < 0)
654 set_sprite(img_fish_iced_down, img_fish_iced_down);
656 set_sprite(img_fish_iced, img_fish_iced);
661 static const float JUMPV = 6;
662 static const int WAITTIME = 1000;
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)
670 physic.set_velocity(0, 0);
671 physic.enable_gravity(false);
672 timer.start(WAITTIME);
674 else if(mode == FISH_WAIT && !timer.check())
677 set_sprite(img_fish, img_fish);
679 physic.set_velocity(0, JUMPV);
680 physic.enable_gravity(true);
683 physic.apply(elapsed_time, base.x, base.y);
684 if(dying == DYING_NOT)
685 collision_swept_object_map(&old_base, &base);
687 if(physic.get_velocity_y() < 0)
688 set_sprite(img_fish_down, img_fish_down);
692 BadGuy::action_bouncingsnowball(double elapsed_time)
694 static const float JUMPV = 4.5;
698 // jump when on ground
699 if(dying == DYING_NOT && issolid(base.x, base.y+32))
701 physic.set_velocity_y(JUMPV);
702 physic.enable_gravity(true);
709 // check for right/left collisions
710 check_horizontal_bump();
712 physic.apply(elapsed_time, base.x, base.y);
713 if(dying == DYING_NOT)
714 collision_swept_object_map(&old_base, &base);
716 // Handle dying timer:
717 if (dying == DYING_SQUISHED && !timer.check())
722 BadGuy::action_flyingsnowball(double elapsed_time)
724 static const float FLYINGSPEED = 1;
725 static const int DIRCHANGETIME = 1000;
727 // go into flyup mode if none specified yet
728 if(dying == DYING_NOT && mode == NORMAL) {
730 physic.set_velocity_y(FLYINGSPEED);
731 timer.start(DIRCHANGETIME/2);
734 if(dying == DYING_NOT && !timer.check()) {
737 physic.set_velocity_y(-FLYINGSPEED);
738 } else if(mode == FLY_DOWN) {
740 physic.set_velocity_y(FLYINGSPEED);
742 timer.start(DIRCHANGETIME);
745 if(dying != DYING_NOT)
746 physic.enable_gravity(true);
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);
752 // Handle dying timer:
753 if (dying == DYING_SQUISHED && !timer.check())
758 BadGuy::action_spiky(double elapsed_time)
760 if(frozen_timer.check())
762 set_sprite(img_spiky_iced_left, img_spiky_iced_right);
766 if (dying == DYING_NOT)
767 check_horizontal_bump();
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);
779 physic.apply(elapsed_time, base.x, base.y);
780 if (dying != DYING_FALLING)
781 collision_swept_object_map(&old_base,&base);
785 BadGuy::action_snowball(double elapsed_time)
787 if (dying == DYING_NOT)
788 check_horizontal_bump();
792 physic.apply(elapsed_time, base.x, base.y);
793 if (dying != DYING_FALLING)
794 collision_swept_object_map(&old_base,&base);
796 // Handle dying timer:
797 if (dying == DYING_SQUISHED && !timer.check())
802 BadGuy::action_wingling(double elapsed_time)
804 if (dying != DYING_NOT)
805 physic.enable_gravity(true);
808 Player& tux = *World::current()->get_tux();
809 int dirsign = physic.get_velocity_x() < 0 ? -1 : 1;
811 if (fabsf(tux.base.x - base.x) < 150 && base.y < tux.base.y && tux.dying == DYING_NOT)
813 if (target.x < 0 && target.y < 0)
815 target.x = tux.base.x;
816 target.y = tux.base.y;
817 physic.set_velocity(dirsign * 1.5f, -2.25f);
820 else if (base.y >= target.y - 16)
821 physic.set_velocity(dirsign * WINGLING_FLY_SPEED, 0);
824 physic.apply(elapsed_time, base.x, base.y);
827 // Handle dying timer:
828 if (dying == DYING_SQUISHED && !timer.check())
831 // TODO: Winglings should be removed after flying off the screen
835 BadGuy::action_walkingtree(double elapsed_time)
837 Player& tux = *World::current()->get_tux();
838 Direction v_dir = physic.get_velocity_x() < 0 ? LEFT : RIGHT;
840 if (dying == DYING_NOT)
841 check_horizontal_bump();
847 if ((tux.base.x + tux.base.width/2 > base.x + base.width/2) && v_dir == LEFT)
850 physic.set_velocity_x(-physic.get_velocity_x());
852 else if ((tux.base.x + tux.base.width/2 < base.x + base.width/2) && v_dir == RIGHT)
855 physic.set_velocity_x(-physic.get_velocity_x());
860 physic.apply(elapsed_time, base.x, base.y);
861 if (dying != DYING_FALLING)
862 collision_swept_object_map(&old_base,&base);
864 // Handle dying timer:
865 if (dying == DYING_SQUISHED && !timer.check())
871 BadGuy::action(float elapsed_time)
873 float scroll_x = World::current()->camera->get_translation().x;
874 float scroll_y = World::current()->camera->get_translation().y;
876 // BadGuy fall below the ground
877 if (base.y > World::current()->get_level()->height * 32) {
882 // Kill us if we landed on spikes
883 if (dying == DYING_NOT
884 && (kind != BAD_STALACTITE && kind != BAD_FLAME && kind != BAD_BOMB)
885 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
886 || isspike(base.x, base.y + base.height)
887 || isspike(base.x + base.width, base.y + base.height)))
889 physic.set_velocity_y(3);
894 /* activate badguys if they're just inside the offscreen_distance around the
895 * screen. Don't activate them inside the screen, since that might have the
896 * effect of badguys suddenly popping up from nowhere
898 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
899 start_position.x < scroll_x - base.width)
901 else if(start_position.x > scroll_y - Y_OFFSCREEN_DISTANCE &&
902 start_position.y < scroll_y - base.height)
904 else if(start_position.x > scroll_x + screen->w &&
905 start_position.x < scroll_x + screen->w + X_OFFSCREEN_DISTANCE)
907 else if(start_position.y > scroll_y + screen->h &&
908 start_position.y < scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
911 if(base.x + base.width < scroll_x - X_OFFSCREEN_DISTANCE*4
912 || base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE*4
913 || base.y + base.height < scroll_y - Y_OFFSCREEN_DISTANCE*4
914 || base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE*4) {
916 if(dying != DYING_NOT)
927 action_mriceblock(elapsed_time);
931 action_jumpy(elapsed_time);
935 action_mrbomb(elapsed_time);
939 action_bomb(elapsed_time);
943 action_stalactite(elapsed_time);
947 action_flame(elapsed_time);
951 action_fish(elapsed_time);
954 case BAD_BOUNCINGSNOWBALL:
955 action_bouncingsnowball(elapsed_time);
958 case BAD_FLYINGSNOWBALL:
959 action_flyingsnowball(elapsed_time);
963 action_spiky(elapsed_time);
967 action_snowball(elapsed_time);
971 action_wingling(elapsed_time);
974 case BAD_WALKINGTREE:
975 action_walkingtree(elapsed_time);
984 BadGuy::draw(Camera& viewport, int)
986 float scroll_x = viewport.get_translation().x;
987 float scroll_y = viewport.get_translation().y;
989 // Don't try to draw stuff that is outside of the screen
990 if(base.x <= scroll_x - base.width || base.x >= scroll_x + screen->w)
993 if(sprite_left == 0 || sprite_right == 0)
998 Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
999 if(dying == DYING_FALLING)
1000 sprite->draw(viewport.world2screen(Vector(base.x, base.y)), SD_VERTICAL_FLIP);
1002 sprite->draw(viewport.world2screen(Vector(base.x, base.y)));
1005 fillrect(base.x - scroll_x, base.y - scroll_y, base.width, base.height, 75,0,75, 150);
1009 BadGuy::set_sprite(Sprite* left, Sprite* right)
1018 // FIXME: Using the image size for the physics and collision is
1019 // a bad idea, since images should always overlap there physical
1022 if(base.width == 0 && base.height == 0) {
1023 base.width = left->get_width();
1024 base.height = left->get_height();
1025 } else if(base.width != left->get_width() || base.height != left->get_height()) {
1026 base.x -= (left->get_width() - base.width) / 2;
1027 base.y -= left->get_height() - base.height;
1028 base.width = left->get_width();
1029 base.height = left->get_height();
1033 base.width = base.height = 0;
1037 animation_offset = 0;
1039 sprite_right = right;
1045 // these can't be bumped
1046 if(kind == BAD_FLAME || kind == BAD_BOMB || kind == BAD_FISH
1047 || kind == BAD_FLYINGSNOWBALL)
1050 physic.set_velocity_y(3);
1055 BadGuy::make_player_jump(Player* player)
1057 player->physic.set_velocity_y(2);
1058 player->base.y = base.y - player->base.height - 2;
1062 BadGuy::squish_me(Player* player)
1064 make_player_jump(player);
1066 World::current()->add_score(Vector(base.x, base.y),
1067 50 * player_status.score_multiplier);
1068 play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
1069 player_status.score_multiplier++;
1071 dying = DYING_SQUISHED;
1073 physic.set_velocity(0, 0);
1077 BadGuy::squish(Player* player)
1079 static const int MAX_ICEBLOCK_SQUICHES = 10;
1081 if(kind == BAD_MRBOMB) {
1082 // mrbomb transforms into a bomb now
1085 make_player_jump(player);
1086 World::current()->add_score(Vector(base.x, base.y),
1087 50 * player_status.score_multiplier);
1088 play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
1089 player_status.score_multiplier++;
1092 } else if (kind == BAD_MRICEBLOCK) {
1093 if (mode == NORMAL || mode == KICK)
1096 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
1098 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1099 physic.set_velocity_x(0);
1102 } else if (mode == FLAT) {
1104 play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
1106 if (player->base.x < base.x + (base.width/2)) {
1107 physic.set_velocity_x(5);
1110 physic.set_velocity_x(-5);
1115 player->kick_timer.start(KICKING_TIME);
1116 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1119 make_player_jump(player);
1121 player_status.score_multiplier++;
1123 // check for maximum number of squishes
1125 if(squishcount >= MAX_ICEBLOCK_SQUICHES) {
1131 } else if(kind == BAD_FISH) {
1132 // fish can only be killed when falling down
1133 if(physic.get_velocity_y() >= 0)
1136 make_player_jump(player);
1138 World::current()->add_score(Vector(base.x, base.y),
1139 25 * player_status.score_multiplier);
1140 player_status.score_multiplier++;
1142 // simply remove the fish...
1145 } else if(kind == BAD_BOUNCINGSNOWBALL) {
1147 set_sprite(img_bouncingsnowball_squished,img_bouncingsnowball_squished);
1149 } else if(kind == BAD_FLYINGSNOWBALL) {
1151 set_sprite(img_flyingsnowball_squished,img_flyingsnowball_squished);
1153 } else if(kind == BAD_SNOWBALL) {
1155 set_sprite(img_snowball_squished_left, img_snowball_squished_right);
1157 } else if(kind == BAD_WINGLING) {
1159 set_sprite(img_wingling_left, img_wingling_left);
1160 } else if(kind == BAD_WALKINGTREE) {
1161 if (mode == BGM_BIG)
1163 set_sprite(img_walkingtree_left_small, img_walkingtree_left_small);
1164 physic.set_velocity_x(physic.get_velocity_x() * 2.0f);
1165 // XXX magic number: 66 is BGM_BIG height
1167 make_player_jump(player);
1168 base.y += 66 - base.height;
1170 World::current()->add_score(Vector(base.x, base.y),
1171 25 * player_status.score_multiplier);
1172 player_status.score_multiplier++;
1182 BadGuy::kill_me(int score)
1184 if(kind == BAD_BOMB)
1187 dying = DYING_FALLING;
1188 if(kind == BAD_MRICEBLOCK) {
1189 set_sprite(img_mriceblock_falling_left, img_mriceblock_falling_right);
1192 Player& tux = *World::current()->get_tux();
1193 tux.holding_something = false;
1197 physic.enable_gravity(true);
1199 /* Gain some points: */
1201 World::current()->add_score(Vector(base.x, base.y),
1202 score * player_status.score_multiplier);
1204 /* Play death sound: */
1205 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
1211 World::current()->add_bad_guy(base.x, base.y, BAD_BOMB);
1216 BadGuy::collision(const MovingObject&, int)
1222 BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
1224 BadGuy* pbad_c = NULL;
1225 Bullet* pbullet_c = NULL;
1227 if(type == COLLISION_BUMP) {
1232 if(type == COLLISION_SQUISH) {
1233 Player* player = static_cast<Player*>(p_c_object);
1238 /* COLLISION_NORMAL */
1242 pbullet_c = (Bullet*) p_c_object;
1244 if(pbullet_c->kind == FIRE_BULLET)
1246 if (kind != BAD_BOMB && kind != BAD_STALACTITE && kind != BAD_FLAME)
1249 else if(pbullet_c->kind == ICE_BULLET)
1251 //if(kind == BAD_FLAME)
1254 frozen_timer.start(FROZEN_TIME);
1259 pbad_c = (BadGuy*) p_c_object;
1262 /* If we're a kicked mriceblock, kill [almost] any badguys we hit */
1263 if(kind == BAD_MRICEBLOCK && mode == KICK &&
1264 kind != BAD_FLAME && kind != BAD_BOMB && kind != BAD_STALACTITE)
1266 pbad_c->kill_me(25);
1269 // a held mriceblock kills the enemy too but falls to ground then
1270 else if(kind == BAD_MRICEBLOCK && mode == HELD)
1272 pbad_c->kill_me(25);
1276 /* Kill badguys that run into exploding bomb */
1277 else if (kind == BAD_BOMB && dying == DYING_NOT)
1279 if (pbad_c->kind == BAD_MRBOMB)
1281 // mrbomb transforms into a bomb now
1285 else if (pbad_c->kind != BAD_MRBOMB)
1287 pbad_c->kill_me(50);
1291 /* Kill any badguys that get hit by stalactite */
1292 else if (kind == BAD_STALACTITE && dying == DYING_NOT)
1294 if (pbad_c->kind == BAD_MRBOMB)
1296 // mrbomb transforms into a bomb now
1301 pbad_c->kill_me(50);
1304 /* When enemies run into eachother, make them change directions */
1307 // Wingling doesn't interact with other badguys
1308 if (pbad_c->kind == BAD_WINGLING || kind == BAD_WINGLING)
1311 // Jumpy, fish, flame, stalactites, wingling are exceptions
1312 if (pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FLAME
1313 || pbad_c->kind == BAD_STALACTITE || pbad_c->kind == BAD_FISH)
1316 // Bounce off of other badguy if we land on top of him
1317 if (base.y + base.height < pbad_c->base.y + pbad_c->base.height)
1319 if (pbad_c->dir == LEFT)
1322 physic.set_velocity(fabsf(physic.get_velocity_x()), 2);
1324 else if (pbad_c->dir == RIGHT)
1327 physic.set_velocity(-fabsf(physic.get_velocity_x()), 2);
1332 else if (base.y + base.height > pbad_c->base.y + pbad_c->base.height)
1335 if (pbad_c->kind != BAD_FLAME)
1340 physic.set_velocity_x(fabsf(physic.get_velocity_x()));
1342 // in case badguys get "jammed"
1343 if (physic.get_velocity_x() != 0)
1344 base.x = pbad_c->base.x + pbad_c->base.width;
1346 else if (dir == RIGHT)
1349 physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
1358 Player* player = static_cast<Player*>(p_c_object);
1359 /* Get kicked if were flat */
1360 if (mode == FLAT && !dying)
1362 play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
1364 // Hit from left side
1365 if (player->base.x < base.x) {
1366 physic.set_velocity_x(5);
1369 // Hit from right side
1371 physic.set_velocity_x(-5);
1376 player->kick_timer.start(KICKING_TIME);
1377 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1385 //---------------------------------------------------------------------------
1387 void load_badguy_gfx()
1389 img_mriceblock_flat_left = sprite_manager->load("mriceblock-flat-left");
1390 img_mriceblock_flat_right = sprite_manager->load("mriceblock-flat-right");
1391 img_mriceblock_falling_left = sprite_manager->load("mriceblock-falling-left");
1392 img_mriceblock_falling_right = sprite_manager->load("mriceblock-falling-right");
1393 img_mriceblock_left = sprite_manager->load("mriceblock-left");
1394 img_mriceblock_right = sprite_manager->load("mriceblock-right");
1395 img_jumpy_left_up = sprite_manager->load("jumpy-left-up");
1396 img_jumpy_left_down = sprite_manager->load("jumpy-left-down");
1397 img_jumpy_left_middle = sprite_manager->load("jumpy-left-middle");
1398 img_jumpy_left_iced = sprite_manager->load("jumpy-left-iced");
1399 img_mrbomb_left = sprite_manager->load("mrbomb-left");
1400 img_mrbomb_right = sprite_manager->load("mrbomb-right");
1401 img_mrbomb_iced_left = sprite_manager->load("mrbomb-iced-left");
1402 img_mrbomb_iced_right = sprite_manager->load("mrbomb-iced-right");
1403 img_mrbomb_ticking_left = sprite_manager->load("mrbomb-ticking-left");
1404 img_mrbomb_ticking_right = sprite_manager->load("mrbomb-ticking-right");
1405 img_mrbomb_explosion = sprite_manager->load("mrbomb-explosion");
1406 img_stalactite = sprite_manager->load("stalactite");
1407 img_stalactite_broken = sprite_manager->load("stalactite-broken");
1408 img_flame = sprite_manager->load("flame");
1409 img_fish = sprite_manager->load("fish");
1410 img_fish_down = sprite_manager->load("fish-down");
1411 img_fish_iced = sprite_manager->load("fish-iced");
1412 img_fish_iced_down = sprite_manager->load("fish-iced-down");
1413 img_bouncingsnowball_left = sprite_manager->load("bouncingsnowball-left");
1414 img_bouncingsnowball_right = sprite_manager->load("bouncingsnowball-right");
1415 img_bouncingsnowball_squished = sprite_manager->load("bouncingsnowball-squished");
1416 img_flyingsnowball = sprite_manager->load("flyingsnowball");
1417 img_flyingsnowball_squished = sprite_manager->load("flyingsnowball-squished");
1418 img_spiky_left = sprite_manager->load("spiky-left");
1419 img_spiky_right = sprite_manager->load("spiky-right");
1420 img_spiky_iced_left = sprite_manager->load("spiky-iced-left");
1421 img_spiky_iced_right = sprite_manager->load("spiky-iced-right");
1422 img_snowball_left = sprite_manager->load("snowball-left");
1423 img_snowball_right = sprite_manager->load("snowball-right");
1424 img_snowball_squished_left = sprite_manager->load("snowball-squished-left");
1425 img_snowball_squished_right = sprite_manager->load("snowball-squished-right");
1426 img_wingling_left = sprite_manager->load("wingling-left");
1427 img_walkingtree_left = sprite_manager->load("walkingtree-left");
1428 img_walkingtree_left_small = sprite_manager->load("walkingtree-left-small");
1431 void free_badguy_gfx()