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
30 #include "resources.h"
31 #include "sprite_manager.h"
34 #include "lispwriter.h"
37 Sprite* img_mriceblock_flat_left;
38 Sprite* img_mriceblock_flat_right;
39 Sprite* img_mriceblock_falling_left;
40 Sprite* img_mriceblock_falling_right;
41 Sprite* img_mriceblock_left;
42 Sprite* img_mriceblock_right;
43 Sprite* img_jumpy_left_up;
44 Sprite* img_jumpy_left_down;
45 Sprite* img_jumpy_left_middle;
46 Sprite* img_jumpy_left_iced;
47 Sprite* img_mrbomb_left;
48 Sprite* img_mrbomb_right;
49 Sprite* img_mrbomb_iced_left;
50 Sprite* img_mrbomb_iced_right;
51 Sprite* img_mrbomb_ticking_left;
52 Sprite* img_mrbomb_ticking_right;
53 Sprite* img_mrbomb_explosion;
54 Sprite* img_stalactite;
55 Sprite* img_stalactite_broken;
58 Sprite* img_fish_down;
59 Sprite* img_fish_iced;
60 Sprite* img_fish_iced_down;
61 Sprite* img_bouncingsnowball_left;
62 Sprite* img_bouncingsnowball_right;
63 Sprite* img_bouncingsnowball_squished;
64 Sprite* img_flyingsnowball;
65 Sprite* img_flyingsnowball_squished;
66 Sprite* img_spiky_left;
67 Sprite* img_spiky_right;
68 Sprite* img_spiky_iced_left;
69 Sprite* img_spiky_iced_right;
70 Sprite* img_snowball_left;
71 Sprite* img_snowball_right;
72 Sprite* img_snowball_squished_left;
73 Sprite* img_snowball_squished_right;
74 Sprite* img_wingling_left;
75 Sprite* img_walkingtree_left;
76 Sprite* img_walkingtree_left_small;
78 #define BADGUY_WALK_SPEED .8f
79 #define WINGLING_FLY_SPEED 1.6f
81 BadGuyKind badguykind_from_string(const std::string& str)
83 if (str == "money" || str == "jumpy") // was money in old maps
85 else if (str == "laptop" || str == "mriceblock") // was laptop in old maps
86 return BAD_MRICEBLOCK;
87 else if (str == "mrbomb")
89 else if (str == "stalactite")
90 return BAD_STALACTITE;
91 else if (str == "flame")
93 else if (str == "fish")
95 else if (str == "bouncingsnowball")
96 return BAD_BOUNCINGSNOWBALL;
97 else if (str == "flyingsnowball")
98 return BAD_FLYINGSNOWBALL;
99 else if (str == "spiky")
101 else if (str == "snowball" || str == "bsod") // was bsod in old maps
103 else if (str == "wingling")
105 else if (str == "walkingtree")
106 return BAD_WALKINGTREE;
109 printf("Couldn't convert badguy: '%s'\n", str.c_str());
114 std::string badguykind_to_string(BadGuyKind kind)
136 case BAD_BOUNCINGSNOWBALL:
137 return "bouncingsnowball";
139 case BAD_FLYINGSNOWBALL:
140 return "flyingsnowball";
151 case BAD_WALKINGTREE:
152 return "walkingtree";
158 BadGuy::BadGuy(BadGuyKind kind_, LispReader& lispreader)
159 : removable(false), squishcount(0)
161 lispreader.read_float("x", &start_position.x);
162 lispreader.read_float("y", &start_position.y);
166 stay_on_platform = false;
167 lispreader.read_bool("stay-on-platform", &stay_on_platform);
172 BadGuy::BadGuy(BadGuyKind kind_, float x, float y)
173 : removable(false), squishcount(0)
175 start_position.x = x;
176 start_position.y = y;
177 stay_on_platform = false;
201 animation_offset = 0;
202 target.x = target.y = -1;
203 sprite_left = sprite_right = 0;
205 frozen_timer.init(true);
208 // if we're in a solid tile at start correct that now
209 if(kind != BAD_FLAME && kind != BAD_FISH && collision_object_map(base))
211 std::cout << "Warning: badguy started in wall: kind: " << badguykind_to_string(kind)
212 << " pos: (" << base.x << ", " << base.y << ")" << std::endl;
213 while(collision_object_map(base))
217 if(World::current()->camera) {
218 Vector scroll = World::current()->camera->get_translation();
220 if(start_position.x > scroll.x - X_OFFSCREEN_DISTANCE &&
221 start_position.x < scroll.x + screen->w + X_OFFSCREEN_DISTANCE &&
222 start_position.y > scroll.y - Y_OFFSCREEN_DISTANCE &&
223 start_position.y < scroll.y + screen->h + Y_OFFSCREEN_DISTANCE) {
230 BadGuy::write(LispWriter& writer)
232 writer.start_list(badguykind_to_string(kind));
234 writer.write_float("x", base.x);
235 writer.write_float("y", base.y);
236 writer.write_bool("stay-on-platform", stay_on_platform);
238 writer.end_list(badguykind_to_string(kind));
242 BadGuy::activate(Direction activation_dir)
245 animation_offset = 0;
246 target.x = target.y = -1;
248 frozen_timer.init(true);
251 dir = activation_dir;
252 float dirsign = activation_dir == LEFT ? -1 : 1;
254 if(kind == BAD_MRBOMB) {
255 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
256 set_sprite(img_mrbomb_left, img_mrbomb_right);
257 } else if (kind == BAD_MRICEBLOCK) {
258 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
259 set_sprite(img_mriceblock_left, img_mriceblock_right);
260 } else if(kind == BAD_JUMPY) {
261 set_sprite(img_jumpy_left_up, img_jumpy_left_up);
262 } else if(kind == BAD_BOMB) {
263 set_sprite(img_mrbomb_ticking_left, img_mrbomb_ticking_right);
264 // hack so that the bomb doesn't hurt until it expldes...
265 dying = DYING_SQUISHED;
266 } else if(kind == BAD_FLAME) {
268 physic.enable_gravity(false);
269 set_sprite(img_flame, img_flame);
270 } else if(kind == BAD_BOUNCINGSNOWBALL) {
271 physic.set_velocity(dirsign * 1.3, 0);
272 set_sprite(img_bouncingsnowball_left, img_bouncingsnowball_right);
273 } else if(kind == BAD_STALACTITE) {
274 physic.enable_gravity(false);
275 set_sprite(img_stalactite, img_stalactite);
276 } else if(kind == BAD_FISH) {
277 set_sprite(img_fish, img_fish);
278 physic.enable_gravity(true);
279 } else if(kind == BAD_FLYINGSNOWBALL) {
280 set_sprite(img_flyingsnowball, img_flyingsnowball);
281 physic.enable_gravity(false);
282 } else if(kind == BAD_SPIKY) {
283 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
284 set_sprite(img_spiky_left, img_spiky_right);
285 } else if(kind == BAD_SNOWBALL) {
286 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
287 set_sprite(img_snowball_left, img_snowball_right);
288 } else if(kind == BAD_WINGLING) {
289 physic.set_velocity(dirsign * WINGLING_FLY_SPEED, 0);
290 physic.enable_gravity(false);
291 set_sprite(img_wingling_left, img_wingling_left);
292 } else if (kind == BAD_WALKINGTREE) {
293 // TODO: why isn't the height/width being set properly in set_sprite?
294 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
296 set_sprite(img_walkingtree_left, img_walkingtree_left);
301 base.x = start_position.x;
302 base.y = start_position.y;
308 BadGuy::action_mriceblock(double elapsed_time)
310 Player& tux = *World::current()->get_tux();
315 /* Move left/right: */
319 physic.apply(elapsed_time, base.x, base.y);
320 if (dying != DYING_FALLING)
321 collision_swept_object_map(&old_base,&base);
323 else if (mode == HELD)
324 { /* FIXME: The pbad object shouldn't know about pplayer objects. */
325 /* If we're holding the iceblock */
329 base.x = tux.base.x + 16;
330 base.y = tux.base.y + tux.base.height/1.5 - base.height;
332 else /* facing left */
334 base.x = tux.base.x - 16;
335 base.y = tux.base.y + tux.base.height/1.5 - base.height;
337 if(collision_object_map(base))
340 base.y = tux.base.y + tux.base.height/1.5 - base.height;
343 if(tux.input.fire != DOWN) /* SHOOT! */
352 tux.kick_timer.start(KICKING_TIME);
353 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
354 physic.set_velocity_x((dir == LEFT) ? -3.5 : 3.5);
355 play_sound(sounds[SND_KICK],SOUND_CENTER_SPEAKER);
362 check_horizontal_bump();
363 if(mode == KICK && changed != dir)
365 float scroll_x = World::current()->camera->get_translation().x;
367 /* handle stereo sound (number 10 should be tweaked...)*/
368 if (base.x < scroll_x + screen->w/2 - 10)
369 play_sound(sounds[SND_RICOCHET], SOUND_LEFT_SPEAKER);
370 else if (base.x > scroll_x + screen->w/2 + 10)
371 play_sound(sounds[SND_RICOCHET], SOUND_RIGHT_SPEAKER);
373 play_sound(sounds[SND_RICOCHET], SOUND_CENTER_SPEAKER);
377 /* Handle mode timer: */
383 set_sprite(img_mriceblock_left, img_mriceblock_right);
384 physic.set_velocity( (dir == LEFT) ? -.8 : .8, 0);
390 BadGuy::check_horizontal_bump(bool checkcliff)
392 float halfheight = base.height / 2;
393 if (dir == LEFT && issolid( base.x, (int) base.y + halfheight))
395 if (kind == BAD_MRICEBLOCK && mode == KICK)
396 World::current()->trybreakbrick(base.x, (int) base.y + halfheight, false);
399 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
402 if (dir == RIGHT && issolid( base.x + base.width, (int)base.y + halfheight))
404 if (kind == BAD_MRICEBLOCK && mode == KICK)
405 World::current()->trybreakbrick(base.x + base.width, (int) base.y + halfheight, false);
408 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
412 // don't check for cliffs when we're falling
415 if(!issolid(base.x + base.width/2, base.y + base.height))
418 if(dir == LEFT && !issolid(base.x, (int) base.y + base.height + halfheight))
421 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
424 if(dir == RIGHT && !issolid(base.x + base.width,
425 (int) base.y + base.height + halfheight))
428 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
436 /* Fall if we get off the ground: */
437 if (dying != DYING_FALLING)
439 if (!issolid(base.x+base.width/2, base.y + base.height))
441 // not solid below us? enable gravity
442 physic.enable_gravity(true);
447 if (physic.get_velocity_y() < 0)
449 base.y = int((base.y + base.height)/32) * 32 - base.height;
450 physic.set_velocity_y(0);
452 // no gravity anymore please
453 physic.enable_gravity(false);
455 if (stay_on_platform && mode == NORMAL)
457 if (!issolid(base.x + ((dir == LEFT) ? 0 : base.width),
458 base.y + base.height))
463 physic.set_velocity_x(fabsf(physic.get_velocity_x()));
468 physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
476 physic.enable_gravity(true);
481 BadGuy::action_jumpy(double elapsed_time)
483 if(frozen_timer.check())
485 set_sprite(img_jumpy_left_iced, img_jumpy_left_iced);
489 const float vy = physic.get_velocity_y();
491 // XXX: These tests *should* use location from ground, not velocity
492 if (fabsf(vy) > 5.6f)
493 set_sprite(img_jumpy_left_down, img_jumpy_left_down);
494 else if (fabsf(vy) > 5.3f)
495 set_sprite(img_jumpy_left_middle, img_jumpy_left_middle);
497 set_sprite(img_jumpy_left_up, img_jumpy_left_up);
499 Player& tux = *World::current()->get_tux();
501 static const float JUMPV = 6;
504 // jump when on ground
505 if(dying == DYING_NOT && issolid(base.x, base.y+32))
507 physic.set_velocity_y(JUMPV);
508 physic.enable_gravity(true);
512 else if(mode == JUMPY_JUMP)
517 // set direction based on tux
518 if(tux.base.x > base.x)
524 physic.apply(elapsed_time, base.x, base.y);
525 if(dying == DYING_NOT)
526 collision_swept_object_map(&old_base, &base);
530 BadGuy::action_mrbomb(double elapsed_time)
532 if(frozen_timer.check())
534 set_sprite(img_mrbomb_iced_left, img_mrbomb_iced_right);
538 if (dying == DYING_NOT)
539 check_horizontal_bump(true);
543 physic.apply(elapsed_time, base.x, base.y);
544 if (dying != DYING_FALLING)
545 collision_swept_object_map(&old_base,&base);
549 BadGuy::action_bomb(double elapsed_time)
551 static const int TICKINGTIME = 1000;
552 static const int EXPLODETIME = 1000;
558 timer.start(TICKINGTIME);
559 } else if(!timer.check()) {
560 if(mode == BOMB_TICKING) {
562 set_sprite(img_mrbomb_explosion, img_mrbomb_explosion);
563 dying = DYING_NOT; // now the bomb hurts
564 timer.start(EXPLODETIME);
566 float scroll_x = World::current()->camera->get_translation().x;
568 /* play explosion sound */ // FIXME: is the stereo all right? maybe we should use player cordinates...
569 if (base.x < scroll_x + screen->w/2 - 10)
570 play_sound(sounds[SND_EXPLODE], SOUND_LEFT_SPEAKER);
571 else if (base.x > scroll_x + screen->w/2 + 10)
572 play_sound(sounds[SND_EXPLODE], SOUND_RIGHT_SPEAKER);
574 play_sound(sounds[SND_EXPLODE], SOUND_CENTER_SPEAKER);
576 } else if(mode == BOMB_EXPLODE) {
583 physic.apply(elapsed_time, base.x, base.y);
584 collision_swept_object_map(&old_base,&base);
588 BadGuy::action_stalactite(double elapsed_time)
590 Player& tux = *World::current()->get_tux();
592 static const int SHAKETIME = 800;
593 static const int RANGE = 40;
596 // start shaking when tux is below the stalactite and at least 40 pixels
598 if(tux.base.x + 32 > base.x - RANGE && tux.base.x < base.x + 32 + RANGE
599 && tux.base.y + tux.base.height > base.y) {
600 timer.start(SHAKETIME);
601 mode = STALACTITE_SHAKING;
603 } if(mode == STALACTITE_SHAKING) {
604 base.x = old_base.x + (rand() % 6) - 3; // TODO this could be done nicer...
606 mode = STALACTITE_FALL;
608 } else if(mode == STALACTITE_FALL) {
610 /* Destroy if we collides with land */
611 if(issolid(base.x+base.width/2, base.y+base.height))
614 dying = DYING_SQUISHED;
616 set_sprite(img_stalactite_broken, img_stalactite_broken);
618 } else if(mode == FLAT) {
623 physic.apply(elapsed_time, base.x, base.y);
625 if(dying == DYING_SQUISHED && !timer.check())
630 BadGuy::action_flame(double elapsed_time)
632 static const float radius = 100;
633 static const float speed = 0.02;
634 base.x = old_base.x + cos(angle) * radius;
635 base.y = old_base.y + sin(angle) * radius;
637 angle = fmodf(angle + elapsed_time * speed, 2*M_PI);
641 BadGuy::action_fish(double elapsed_time)
643 if(frozen_timer.check())
645 if(physic.get_velocity_y() < 0)
646 set_sprite(img_fish_iced_down, img_fish_iced_down);
648 set_sprite(img_fish_iced, img_fish_iced);
653 static const float JUMPV = 6;
654 static const int WAITTIME = 1000;
656 // go in wait mode when back in water
657 if(dying == DYING_NOT
658 && gettile(base.x, base.y+ base.height)->attributes & Tile::WATER
659 && physic.get_velocity_y() <= 0 && mode == NORMAL)
663 physic.set_velocity(0, 0);
664 physic.enable_gravity(false);
665 timer.start(WAITTIME);
667 else if(mode == FISH_WAIT && !timer.check())
670 set_sprite(img_fish, img_fish);
672 physic.set_velocity(0, JUMPV);
673 physic.enable_gravity(true);
676 physic.apply(elapsed_time, base.x, base.y);
677 if(dying == DYING_NOT)
678 collision_swept_object_map(&old_base, &base);
680 if(physic.get_velocity_y() < 0)
681 set_sprite(img_fish_down, img_fish_down);
685 BadGuy::action_bouncingsnowball(double elapsed_time)
687 static const float JUMPV = 4.5;
691 // jump when on ground
692 if(dying == DYING_NOT && issolid(base.x, base.y+32))
694 physic.set_velocity_y(JUMPV);
695 physic.enable_gravity(true);
702 // check for right/left collisions
703 check_horizontal_bump();
705 physic.apply(elapsed_time, base.x, base.y);
706 if(dying == DYING_NOT)
707 collision_swept_object_map(&old_base, &base);
709 // Handle dying timer:
710 if (dying == DYING_SQUISHED && !timer.check())
715 BadGuy::action_flyingsnowball(double elapsed_time)
717 static const float FLYINGSPEED = 1;
718 static const int DIRCHANGETIME = 1000;
720 // go into flyup mode if none specified yet
721 if(dying == DYING_NOT && mode == NORMAL) {
723 physic.set_velocity_y(FLYINGSPEED);
724 timer.start(DIRCHANGETIME/2);
727 if(dying == DYING_NOT && !timer.check()) {
730 physic.set_velocity_y(-FLYINGSPEED);
731 } else if(mode == FLY_DOWN) {
733 physic.set_velocity_y(FLYINGSPEED);
735 timer.start(DIRCHANGETIME);
738 if(dying != DYING_NOT)
739 physic.enable_gravity(true);
741 physic.apply(elapsed_time, base.x, base.y);
742 if(dying == DYING_NOT || dying == DYING_SQUISHED)
743 collision_swept_object_map(&old_base, &base);
745 // Handle dying timer:
746 if (dying == DYING_SQUISHED && !timer.check())
751 BadGuy::action_spiky(double elapsed_time)
753 if(frozen_timer.check())
755 set_sprite(img_spiky_iced_left, img_spiky_iced_right);
759 if (dying == DYING_NOT)
760 check_horizontal_bump();
764 // jump when we're about to fall
765 if (physic.get_velocity_y() == 0 &&
766 !issolid(base.x+base.width/2, base.y + base.height)) {
767 physic.enable_gravity(true);
768 physic.set_velocity_y(2);
772 physic.apply(elapsed_time, base.x, base.y);
773 if (dying != DYING_FALLING)
774 collision_swept_object_map(&old_base,&base);
778 BadGuy::action_snowball(double elapsed_time)
780 if (dying == DYING_NOT)
781 check_horizontal_bump();
785 physic.apply(elapsed_time, base.x, base.y);
786 if (dying != DYING_FALLING)
787 collision_swept_object_map(&old_base,&base);
789 // Handle dying timer:
790 if (dying == DYING_SQUISHED && !timer.check())
795 BadGuy::action_wingling(double elapsed_time)
797 if (dying != DYING_NOT)
798 physic.enable_gravity(true);
801 Player& tux = *World::current()->get_tux();
802 int dirsign = physic.get_velocity_x() < 0 ? -1 : 1;
804 if (fabsf(tux.base.x - base.x) < 150 && base.y < tux.base.y && tux.dying == DYING_NOT)
806 if (target.x < 0 && target.y < 0)
808 target.x = tux.base.x;
809 target.y = tux.base.y;
810 physic.set_velocity(dirsign * 1.5f, -2.25f);
813 else if (base.y >= target.y - 16)
814 physic.set_velocity(dirsign * WINGLING_FLY_SPEED, 0);
817 physic.apply(elapsed_time, base.x, base.y);
820 // Handle dying timer:
821 if (dying == DYING_SQUISHED && !timer.check())
824 // TODO: Winglings should be removed after flying off the screen
828 BadGuy::action_walkingtree(double elapsed_time)
830 Player& tux = *World::current()->get_tux();
831 Direction v_dir = physic.get_velocity_x() < 0 ? LEFT : RIGHT;
833 if (dying == DYING_NOT)
834 check_horizontal_bump();
840 if ((tux.base.x + tux.base.width/2 > base.x + base.width/2) && v_dir == LEFT)
843 physic.set_velocity_x(-physic.get_velocity_x());
845 else if ((tux.base.x + tux.base.width/2 < base.x + base.width/2) && v_dir == RIGHT)
848 physic.set_velocity_x(-physic.get_velocity_x());
853 physic.apply(elapsed_time, base.x, base.y);
854 if (dying != DYING_FALLING)
855 collision_swept_object_map(&old_base,&base);
857 // Handle dying timer:
858 if (dying == DYING_SQUISHED && !timer.check())
863 BadGuy::action(float elapsed_time)
865 float scroll_x = World::current()->camera->get_translation().x;
866 float scroll_y = World::current()->camera->get_translation().y;
868 // BadGuy fall below the ground
869 if (base.y > World::current()->get_level()->height * 32) {
874 // Kill us if we landed on spikes
875 if (dying == DYING_NOT
876 && (kind != BAD_STALACTITE && kind != BAD_FLAME && kind != BAD_BOMB)
877 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
878 || isspike(base.x, base.y + base.height)
879 || isspike(base.x + base.width, base.y + base.height)))
881 physic.set_velocity_y(3);
886 /* activate badguys if they're just inside the offscreen_distance around the
887 * screen. Don't activate them inside the screen, since that might have the
888 * effect of badguys suddenly popping up from nowhere
890 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
891 start_position.x < scroll_x - base.width)
893 else if(start_position.x > scroll_y - Y_OFFSCREEN_DISTANCE &&
894 start_position.y < scroll_y - base.height)
896 else if(start_position.x > scroll_x + screen->w &&
897 start_position.x < scroll_x + screen->w + X_OFFSCREEN_DISTANCE)
899 else if(start_position.y > scroll_y + screen->h &&
900 start_position.y < scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
903 if(base.x + base.width < scroll_x - X_OFFSCREEN_DISTANCE*4
904 || base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE*4
905 || base.y + base.height < scroll_y - Y_OFFSCREEN_DISTANCE*4
906 || base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE*4) {
908 if(dying != DYING_NOT)
919 action_mriceblock(elapsed_time);
923 action_jumpy(elapsed_time);
927 action_mrbomb(elapsed_time);
931 action_bomb(elapsed_time);
935 action_stalactite(elapsed_time);
939 action_flame(elapsed_time);
943 action_fish(elapsed_time);
946 case BAD_BOUNCINGSNOWBALL:
947 action_bouncingsnowball(elapsed_time);
950 case BAD_FLYINGSNOWBALL:
951 action_flyingsnowball(elapsed_time);
955 action_spiky(elapsed_time);
959 action_snowball(elapsed_time);
963 action_wingling(elapsed_time);
966 case BAD_WALKINGTREE:
967 action_walkingtree(elapsed_time);
976 BadGuy::draw(DrawingContext& context)
978 Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
982 sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
984 Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
985 if(dying == DYING_FALLING && physic.get_velocity_y() < 0)
986 sprite->draw(viewport.world2screen(Vector(base.x, base.y)), SD_VERTICAL_FLIP);
988 sprite->draw(viewport.world2screen(Vector(base.x, base.y)));
991 float scroll_x = context.get_translation().x;
992 float scroll_y = context.get_translation().y;
994 fillrect(base.x - scroll_x, base.y - scroll_y, base.width, base.height, 75,0,75, 150);
998 BadGuy::set_sprite(Sprite* left, Sprite* right)
1007 // FIXME: Using the image size for the physics and collision is
1008 // a bad idea, since images should always overlap there physical
1011 if(base.width == 0 && base.height == 0) {
1012 base.width = left->get_width();
1013 base.height = left->get_height();
1014 } else if(base.width != left->get_width() || base.height != left->get_height()) {
1015 base.x -= (left->get_width() - base.width) / 2;
1016 base.y -= left->get_height() - base.height;
1017 base.width = left->get_width();
1018 base.height = left->get_height();
1022 base.width = base.height = 0;
1026 animation_offset = 0;
1028 sprite_right = right;
1034 // these can't be bumped
1035 if(kind == BAD_FLAME || kind == BAD_BOMB || kind == BAD_FISH
1036 || kind == BAD_FLYINGSNOWBALL)
1039 physic.set_velocity_y(3);
1044 BadGuy::make_player_jump(Player* player)
1046 player->physic.set_velocity_y(2);
1047 player->base.y = base.y - player->base.height - 2;
1051 BadGuy::squish_me(Player* player)
1053 make_player_jump(player);
1055 World::current()->add_score(Vector(base.x, base.y),
1056 50 * player_status.score_multiplier);
1057 play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
1058 player_status.score_multiplier++;
1060 dying = DYING_SQUISHED;
1062 physic.set_velocity(0, 0);
1066 BadGuy::squish(Player* player)
1068 static const int MAX_ICEBLOCK_SQUICHES = 10;
1070 if(kind == BAD_MRBOMB) {
1071 // mrbomb transforms into a bomb now
1074 make_player_jump(player);
1075 World::current()->add_score(Vector(base.x, base.y),
1076 50 * player_status.score_multiplier);
1077 play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
1078 player_status.score_multiplier++;
1081 } else if (kind == BAD_MRICEBLOCK) {
1082 if (mode == NORMAL || mode == KICK)
1085 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
1087 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1088 physic.set_velocity_x(0);
1091 } else if (mode == FLAT) {
1093 play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
1095 if (player->base.x < base.x + (base.width/2)) {
1096 physic.set_velocity_x(5);
1099 physic.set_velocity_x(-5);
1104 player->kick_timer.start(KICKING_TIME);
1105 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1108 make_player_jump(player);
1110 player_status.score_multiplier++;
1112 // check for maximum number of squishes
1114 if(squishcount >= MAX_ICEBLOCK_SQUICHES) {
1120 } else if(kind == BAD_FISH) {
1121 // fish can only be killed when falling down
1122 if(physic.get_velocity_y() >= 0)
1125 make_player_jump(player);
1127 World::current()->add_score(Vector(base.x, base.y),
1128 25 * player_status.score_multiplier);
1129 player_status.score_multiplier++;
1131 // simply remove the fish...
1134 } else if(kind == BAD_BOUNCINGSNOWBALL) {
1136 set_sprite(img_bouncingsnowball_squished,img_bouncingsnowball_squished);
1138 } else if(kind == BAD_FLYINGSNOWBALL) {
1140 set_sprite(img_flyingsnowball_squished,img_flyingsnowball_squished);
1142 } else if(kind == BAD_SNOWBALL) {
1144 set_sprite(img_snowball_squished_left, img_snowball_squished_right);
1146 } else if(kind == BAD_WINGLING) {
1148 set_sprite(img_wingling_left, img_wingling_left);
1149 } else if(kind == BAD_WALKINGTREE) {
1150 if (mode == BGM_BIG)
1152 set_sprite(img_walkingtree_left_small, img_walkingtree_left_small);
1153 physic.set_velocity_x(physic.get_velocity_x() * 2.0f);
1154 // XXX magic number: 66 is BGM_BIG height
1156 make_player_jump(player);
1157 base.y += 66 - base.height;
1159 World::current()->add_score(Vector(base.x, base.y),
1160 25 * player_status.score_multiplier);
1161 player_status.score_multiplier++;
1171 BadGuy::kill_me(int score)
1173 if(kind == BAD_BOMB)
1176 dying = DYING_FALLING;
1177 if(kind == BAD_MRICEBLOCK) {
1178 set_sprite(img_mriceblock_falling_left, img_mriceblock_falling_right);
1181 Player& tux = *World::current()->get_tux();
1182 tux.holding_something = false;
1186 physic.enable_gravity(true);
1188 /* Gain some points: */
1190 World::current()->add_score(Vector(base.x, base.y),
1191 score * player_status.score_multiplier);
1193 /* Play death sound: */
1194 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
1198 BadGuy::explode(bool right_way)
1200 BadGuy *badguy = World::current()->add_bad_guy(base.x, base.y, BAD_BOMB);
1203 badguy->timer.start(0);
1204 badguy->mode = BOMB_TICKING;
1211 BadGuy::collision(const MovingObject&, int)
1217 BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
1219 BadGuy* pbad_c = NULL;
1220 Bullet* pbullet_c = NULL;
1222 if(type == COLLISION_BUMP) {
1227 if(type == COLLISION_SQUISH) {
1228 Player* player = static_cast<Player*>(p_c_object);
1233 /* COLLISION_NORMAL */
1237 pbullet_c = (Bullet*) p_c_object;
1239 if(pbullet_c->kind == FIRE_BULLET)
1241 if (kind != BAD_BOMB && kind != BAD_STALACTITE && kind != BAD_FLAME)
1244 else if(pbullet_c->kind == ICE_BULLET)
1246 //if(kind == BAD_FLAME)
1249 frozen_timer.start(FROZEN_TIME);
1254 pbad_c = (BadGuy*) p_c_object;
1257 /* If we're a kicked mriceblock, kill [almost] any badguys we hit */
1258 if(kind == BAD_MRICEBLOCK && mode == KICK &&
1259 kind != BAD_FLAME && kind != BAD_BOMB && kind != BAD_STALACTITE)
1261 pbad_c->kill_me(25);
1264 // a held mriceblock kills the enemy too but falls to ground then
1265 else if(kind == BAD_MRICEBLOCK && mode == HELD)
1267 pbad_c->kill_me(25);
1271 /* Kill badguys that run into exploding bomb */
1272 else if (kind == BAD_BOMB && dying == DYING_NOT)
1274 if (pbad_c->kind == BAD_MRBOMB)
1276 // mrbomb transforms into a bomb now
1277 pbad_c->explode(true);
1280 else if (pbad_c->kind != BAD_MRBOMB)
1282 pbad_c->kill_me(50);
1286 /* Kill any badguys that get hit by stalactite */
1287 else if (kind == BAD_STALACTITE && dying == DYING_NOT)
1289 if (pbad_c->kind == BAD_MRBOMB)
1291 // mrbomb transforms into a bomb now
1292 pbad_c->explode(false);
1296 pbad_c->kill_me(50);
1299 /* When enemies run into eachother, make them change directions */
1302 // Wingling doesn't interact with other badguys
1303 if (pbad_c->kind == BAD_WINGLING || kind == BAD_WINGLING)
1306 // Jumpy, fish, flame, stalactites, wingling are exceptions
1307 if (pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FLAME
1308 || pbad_c->kind == BAD_STALACTITE || pbad_c->kind == BAD_FISH)
1311 // Bounce off of other badguy if we land on top of him
1312 if (base.y + base.height < pbad_c->base.y + pbad_c->base.height)
1314 if (pbad_c->dir == LEFT)
1317 physic.set_velocity(fabsf(physic.get_velocity_x()), 2);
1319 else if (pbad_c->dir == RIGHT)
1322 physic.set_velocity(-fabsf(physic.get_velocity_x()), 2);
1327 else if (base.y + base.height > pbad_c->base.y + pbad_c->base.height)
1330 if (pbad_c->kind != BAD_FLAME)
1335 physic.set_velocity_x(fabsf(physic.get_velocity_x()));
1337 // in case badguys get "jammed"
1338 if (physic.get_velocity_x() != 0)
1339 base.x = pbad_c->base.x + pbad_c->base.width;
1341 else if (dir == RIGHT)
1344 physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
1353 Player* player = static_cast<Player*>(p_c_object);
1354 /* Get kicked if were flat */
1355 if (mode == FLAT && !dying)
1357 play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
1359 // Hit from left side
1360 if (player->base.x < base.x) {
1361 physic.set_velocity_x(5);
1364 // Hit from right side
1366 physic.set_velocity_x(-5);
1371 player->kick_timer.start(KICKING_TIME);
1372 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1380 //---------------------------------------------------------------------------
1382 void load_badguy_gfx()
1384 img_mriceblock_flat_left = sprite_manager->load("mriceblock-flat-left");
1385 img_mriceblock_flat_right = sprite_manager->load("mriceblock-flat-right");
1386 img_mriceblock_falling_left = sprite_manager->load("mriceblock-falling-left");
1387 img_mriceblock_falling_right = sprite_manager->load("mriceblock-falling-right");
1388 img_mriceblock_left = sprite_manager->load("mriceblock-left");
1389 img_mriceblock_right = sprite_manager->load("mriceblock-right");
1390 img_jumpy_left_up = sprite_manager->load("jumpy-left-up");
1391 img_jumpy_left_down = sprite_manager->load("jumpy-left-down");
1392 img_jumpy_left_middle = sprite_manager->load("jumpy-left-middle");
1393 img_jumpy_left_iced = sprite_manager->load("jumpy-left-iced");
1394 img_mrbomb_left = sprite_manager->load("mrbomb-left");
1395 img_mrbomb_right = sprite_manager->load("mrbomb-right");
1396 img_mrbomb_iced_left = sprite_manager->load("mrbomb-iced-left");
1397 img_mrbomb_iced_right = sprite_manager->load("mrbomb-iced-right");
1398 img_mrbomb_ticking_left = sprite_manager->load("mrbomb-ticking-left");
1399 img_mrbomb_ticking_right = sprite_manager->load("mrbomb-ticking-right");
1400 img_mrbomb_explosion = sprite_manager->load("mrbomb-explosion");
1401 img_stalactite = sprite_manager->load("stalactite");
1402 img_stalactite_broken = sprite_manager->load("stalactite-broken");
1403 img_flame = sprite_manager->load("flame");
1404 img_fish = sprite_manager->load("fish");
1405 img_fish_down = sprite_manager->load("fish-down");
1406 img_fish_iced = sprite_manager->load("fish-iced");
1407 img_fish_iced_down = sprite_manager->load("fish-iced-down");
1408 img_bouncingsnowball_left = sprite_manager->load("bouncingsnowball-left");
1409 img_bouncingsnowball_right = sprite_manager->load("bouncingsnowball-right");
1410 img_bouncingsnowball_squished = sprite_manager->load("bouncingsnowball-squished");
1411 img_flyingsnowball = sprite_manager->load("flyingsnowball");
1412 img_flyingsnowball_squished = sprite_manager->load("flyingsnowball-squished");
1413 img_spiky_left = sprite_manager->load("spiky-left");
1414 img_spiky_right = sprite_manager->load("spiky-right");
1415 img_spiky_iced_left = sprite_manager->load("spiky-iced-left");
1416 img_spiky_iced_right = sprite_manager->load("spiky-iced-right");
1417 img_snowball_left = sprite_manager->load("snowball-left");
1418 img_snowball_right = sprite_manager->load("snowball-right");
1419 img_snowball_squished_left = sprite_manager->load("snowball-squished-left");
1420 img_snowball_squished_right = sprite_manager->load("snowball-squished-right");
1421 img_wingling_left = sprite_manager->load("wingling-left");
1422 img_walkingtree_left = sprite_manager->load("walkingtree-left");
1423 img_walkingtree_left_small = sprite_manager->load("walkingtree-left-small");
1426 void free_badguy_gfx()