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"
33 #include "lispwriter.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_bouncingsnowball_left;
63 Sprite* img_bouncingsnowball_right;
64 Sprite* img_bouncingsnowball_squished;
65 Sprite* img_flyingsnowball;
66 Sprite* img_flyingsnowball_squished;
67 Sprite* img_spiky_left;
68 Sprite* img_spiky_right;
69 Sprite* img_spiky_iced_left;
70 Sprite* img_spiky_iced_right;
71 Sprite* img_snowball_left;
72 Sprite* img_snowball_right;
73 Sprite* img_snowball_squished_left;
74 Sprite* img_snowball_squished_right;
75 Sprite* img_wingling_left;
76 Sprite* img_walkingtree_left;
77 Sprite* img_walkingtree_left_small;
79 #define BADGUY_WALK_SPEED .8f
80 #define WINGLING_FLY_SPEED 1.6f
82 BadGuyKind badguykind_from_string(const std::string& str)
84 if (str == "money" || str == "jumpy") // was money in old maps
86 else if (str == "laptop" || str == "mriceblock") // was laptop in old maps
87 return BAD_MRICEBLOCK;
88 else if (str == "mrbomb")
90 else if (str == "stalactite")
91 return BAD_STALACTITE;
92 else if (str == "flame")
94 else if (str == "fish")
96 else if (str == "bouncingsnowball")
97 return BAD_BOUNCINGSNOWBALL;
98 else if (str == "flyingsnowball")
99 return BAD_FLYINGSNOWBALL;
100 else if (str == "spiky")
102 else if (str == "snowball" || str == "bsod") // was bsod in old maps
104 else if (str == "wingling")
106 else if (str == "walkingtree")
107 return BAD_WALKINGTREE;
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(Sector::current() && Sector::current()->camera) {
218 Vector scroll = Sector::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) {
227 if(start_position.x > 0 && start_position.x <= screen->w
228 && start_position.y > 0 && start_position.y <= screen->h)
234 BadGuy::write(LispWriter& writer)
236 writer.start_list(badguykind_to_string(kind));
238 writer.write_float("x", base.x);
239 writer.write_float("y", base.y);
240 writer.write_bool("stay-on-platform", stay_on_platform);
242 writer.end_list(badguykind_to_string(kind));
246 BadGuy::activate(Direction activation_dir)
249 animation_offset = 0;
250 target.x = target.y = -1;
252 frozen_timer.init(true);
255 dir = activation_dir;
256 float dirsign = activation_dir == LEFT ? -1 : 1;
258 if(kind == BAD_MRBOMB) {
259 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
260 set_sprite(img_mrbomb_left, img_mrbomb_right);
261 } else if (kind == BAD_MRICEBLOCK) {
262 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
263 set_sprite(img_mriceblock_left, img_mriceblock_right);
264 } else if(kind == BAD_JUMPY) {
265 set_sprite(img_jumpy_left_up, img_jumpy_left_up);
266 } else if(kind == BAD_BOMB) {
267 set_sprite(img_mrbomb_ticking_left, img_mrbomb_ticking_right);
268 // hack so that the bomb doesn't hurt until it expldes...
269 dying = DYING_SQUISHED;
270 } else if(kind == BAD_FLAME) {
272 physic.enable_gravity(false);
273 set_sprite(img_flame, img_flame);
274 } else if(kind == BAD_BOUNCINGSNOWBALL) {
275 physic.set_velocity(dirsign * 1.3, 0);
276 set_sprite(img_bouncingsnowball_left, img_bouncingsnowball_right);
277 } else if(kind == BAD_STALACTITE) {
278 physic.enable_gravity(false);
279 set_sprite(img_stalactite, img_stalactite);
280 } else if(kind == BAD_FISH) {
281 set_sprite(img_fish, img_fish);
282 physic.enable_gravity(true);
283 } else if(kind == BAD_FLYINGSNOWBALL) {
284 set_sprite(img_flyingsnowball, img_flyingsnowball);
285 physic.enable_gravity(false);
286 } else if(kind == BAD_SPIKY) {
287 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
288 set_sprite(img_spiky_left, img_spiky_right);
289 } else if(kind == BAD_SNOWBALL) {
290 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
291 set_sprite(img_snowball_left, img_snowball_right);
292 } else if(kind == BAD_WINGLING) {
293 physic.set_velocity(dirsign * WINGLING_FLY_SPEED, 0);
294 physic.enable_gravity(false);
295 set_sprite(img_wingling_left, img_wingling_left);
296 } else if (kind == BAD_WALKINGTREE) {
297 // TODO: why isn't the height/width being set properly in set_sprite?
298 physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
300 set_sprite(img_walkingtree_left, img_walkingtree_left);
305 base.x = start_position.x;
306 base.y = start_position.y;
312 BadGuy::action_mriceblock(double elapsed_time)
314 Player& tux = *Sector::current()->player;
319 /* Move left/right: */
323 physic.apply(elapsed_time, base.x, base.y);
324 if (dying != DYING_FALLING)
325 collision_swept_object_map(&old_base,&base);
327 else if (mode == HELD)
328 { /* FIXME: The pbad object shouldn't know about pplayer objects. */
329 /* If we're holding the iceblock */
333 base.x = tux.base.x + 16;
334 base.y = tux.base.y + tux.base.height/1.5 - base.height;
336 else /* facing left */
338 base.x = tux.base.x - 16;
339 base.y = tux.base.y + tux.base.height/1.5 - base.height;
341 if(collision_object_map(base))
344 base.y = tux.base.y + tux.base.height/1.5 - base.height;
347 if(tux.input.fire != DOWN) /* SHOOT! */
356 tux.kick_timer.start(KICKING_TIME);
357 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
358 physic.set_velocity_x((dir == LEFT) ? -3.5 : 3.5);
359 sound_manager->play_sound(sounds[SND_KICK], this);
366 check_horizontal_bump();
367 if(mode == KICK && changed != dir)
369 sound_manager->play_sound(sounds[SND_RICOCHET], get_pos());
373 /* Handle mode timer: */
379 set_sprite(img_mriceblock_left, img_mriceblock_right);
380 physic.set_velocity( (dir == LEFT) ? -.8 : .8, 0);
386 BadGuy::check_horizontal_bump(bool checkcliff)
388 float halfheight = base.height / 2;
389 if (dir == LEFT && issolid( base.x, (int) base.y + halfheight))
391 if (kind == BAD_MRICEBLOCK && mode == KICK)
392 Sector::current()->trybreakbrick(Vector(base.x, base.y + halfheight), false);
395 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
398 if (dir == RIGHT && issolid( base.x + base.width, (int)base.y + halfheight))
400 if (kind == BAD_MRICEBLOCK && mode == KICK)
401 Sector::current()->trybreakbrick(
402 Vector(base.x + base.width, (int) base.y + halfheight), false);
405 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
409 // don't check for cliffs when we're falling
412 if(!issolid(base.x + base.width/2, base.y + base.height))
415 if(dir == LEFT && !issolid(base.x, (int) base.y + base.height + halfheight))
418 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
421 if(dir == RIGHT && !issolid(base.x + base.width,
422 (int) base.y + base.height + halfheight))
425 physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
433 /* Fall if we get off the ground: */
434 if (dying != DYING_FALLING)
436 if (!issolid(base.x+base.width/2, base.y + base.height))
438 // not solid below us? enable gravity
439 physic.enable_gravity(true);
444 if (physic.get_velocity_y() < 0)
446 base.y = int((base.y + base.height)/32) * 32 - base.height;
447 physic.set_velocity_y(0);
449 // no gravity anymore please
450 physic.enable_gravity(false);
452 if (stay_on_platform && mode == NORMAL)
454 if (!issolid(base.x + ((dir == LEFT) ? 0 : base.width),
455 base.y + base.height))
460 physic.set_velocity_x(fabsf(physic.get_velocity_x()));
465 physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
473 physic.enable_gravity(true);
478 BadGuy::action_jumpy(double elapsed_time)
480 if(frozen_timer.check())
482 set_sprite(img_jumpy_left_iced, img_jumpy_left_iced);
486 const float vy = physic.get_velocity_y();
488 // XXX: These tests *should* use location from ground, not velocity
489 if (fabsf(vy) > 5.6f)
490 set_sprite(img_jumpy_left_down, img_jumpy_left_down);
491 else if (fabsf(vy) > 5.3f)
492 set_sprite(img_jumpy_left_middle, img_jumpy_left_middle);
494 set_sprite(img_jumpy_left_up, img_jumpy_left_up);
496 Player& tux = *Sector::current()->player;
498 static const float JUMPV = 6;
501 // jump when on ground
502 if(dying == DYING_NOT && issolid(base.x, base.y+32))
504 physic.set_velocity_y(JUMPV);
505 physic.enable_gravity(true);
509 else if(mode == JUMPY_JUMP)
514 // set direction based on tux
515 if(tux.base.x > base.x)
521 physic.apply(elapsed_time, base.x, base.y);
522 if(dying == DYING_NOT)
523 collision_swept_object_map(&old_base, &base);
527 BadGuy::action_mrbomb(double elapsed_time)
529 if(frozen_timer.check())
531 set_sprite(img_mrbomb_iced_left, img_mrbomb_iced_right);
535 if (dying == DYING_NOT)
536 check_horizontal_bump(true);
540 physic.apply(elapsed_time, base.x, base.y);
541 if (dying != DYING_FALLING)
542 collision_swept_object_map(&old_base,&base);
546 BadGuy::action_bomb(double elapsed_time)
548 static const int TICKINGTIME = 1000;
549 static const int EXPLODETIME = 1000;
555 timer.start(TICKINGTIME);
556 } else if(!timer.check()) {
557 if(mode == BOMB_TICKING) {
559 set_sprite(img_mrbomb_explosion, img_mrbomb_explosion);
560 dying = DYING_NOT; // now the bomb hurts
561 timer.start(EXPLODETIME);
563 sound_manager->play_sound(sounds[SND_EXPLODE], this);
564 } else if(mode == BOMB_EXPLODE) {
571 physic.apply(elapsed_time, base.x, base.y);
572 collision_swept_object_map(&old_base,&base);
576 BadGuy::action_stalactite(double elapsed_time)
578 Player& tux = *Sector::current()->player;
580 static const int SHAKETIME = 800;
581 static const int RANGE = 40;
584 // start shaking when tux is below the stalactite and at least 40 pixels
586 if(tux.base.x + 32 > base.x - RANGE && tux.base.x < base.x + 32 + RANGE
587 && tux.base.y + tux.base.height > base.y
588 && tux.dying == DYING_NOT) {
589 timer.start(SHAKETIME);
590 mode = STALACTITE_SHAKING;
592 } if(mode == STALACTITE_SHAKING) {
593 base.x = old_base.x + (rand() % 6) - 3; // TODO this could be done nicer...
595 mode = STALACTITE_FALL;
597 } else if(mode == STALACTITE_FALL) {
599 /* Destroy if we collides with land */
600 if(issolid(base.x+base.width/2, base.y+base.height))
603 dying = DYING_SQUISHED;
605 set_sprite(img_stalactite_broken, img_stalactite_broken);
607 } else if(mode == FLAT) {
612 physic.apply(elapsed_time, base.x, base.y);
614 if(dying == DYING_SQUISHED && !timer.check())
619 BadGuy::action_flame(double elapsed_time)
621 static const float radius = 100;
622 static const float speed = 0.02;
623 base.x = old_base.x + cos(angle) * radius;
624 base.y = old_base.y + sin(angle) * radius;
626 angle = fmodf(angle + elapsed_time * speed, 2*M_PI);
630 BadGuy::action_fish(double elapsed_time)
632 if(frozen_timer.check())
634 if(physic.get_velocity_y() < 0)
635 set_sprite(img_fish_iced_down, img_fish_iced_down);
637 set_sprite(img_fish_iced, img_fish_iced);
642 static const float JUMPV = 6;
643 static const int WAITTIME = 1000;
645 // go in wait mode when back in water
646 if(dying == DYING_NOT
647 && gettile(base.x, base.y+ base.height)->attributes & Tile::WATER
648 && physic.get_velocity_y() <= 0 && mode == NORMAL)
652 physic.set_velocity(0, 0);
653 physic.enable_gravity(false);
654 timer.start(WAITTIME);
656 else if(mode == FISH_WAIT && !timer.check())
659 set_sprite(img_fish, img_fish);
661 physic.set_velocity(0, JUMPV);
662 physic.enable_gravity(true);
665 physic.apply(elapsed_time, base.x, base.y);
666 if(dying == DYING_NOT)
667 collision_swept_object_map(&old_base, &base);
669 if(physic.get_velocity_y() < 0)
670 set_sprite(img_fish_down, img_fish_down);
674 BadGuy::action_bouncingsnowball(double elapsed_time)
676 static const float JUMPV = 4.5;
680 // jump when on ground
681 if(dying == DYING_NOT && issolid(base.x, base.y+32))
683 physic.set_velocity_y(JUMPV);
684 physic.enable_gravity(true);
691 // check for right/left collisions
692 check_horizontal_bump();
694 physic.apply(elapsed_time, base.x, base.y);
695 if(dying == DYING_NOT)
696 collision_swept_object_map(&old_base, &base);
698 // Handle dying timer:
699 if (dying == DYING_SQUISHED && !timer.check())
704 BadGuy::action_flyingsnowball(double elapsed_time)
706 static const float FLYINGSPEED = 1;
707 static const int DIRCHANGETIME = 1000;
709 // go into flyup mode if none specified yet
710 if(dying == DYING_NOT && mode == NORMAL) {
712 physic.set_velocity_y(FLYINGSPEED);
713 timer.start(DIRCHANGETIME/2);
716 if(dying == DYING_NOT && !timer.check()) {
719 physic.set_velocity_y(-FLYINGSPEED);
720 } else if(mode == FLY_DOWN) {
722 physic.set_velocity_y(FLYINGSPEED);
724 timer.start(DIRCHANGETIME);
727 if(dying != DYING_NOT)
728 physic.enable_gravity(true);
730 physic.apply(elapsed_time, base.x, base.y);
731 if(dying == DYING_NOT || dying == DYING_SQUISHED)
732 collision_swept_object_map(&old_base, &base);
734 // Handle dying timer:
735 if (dying == DYING_SQUISHED && !timer.check())
740 BadGuy::action_spiky(double elapsed_time)
742 if(frozen_timer.check())
744 set_sprite(img_spiky_iced_left, img_spiky_iced_right);
748 if (dying == DYING_NOT)
749 check_horizontal_bump();
753 // jump when we're about to fall
754 if (physic.get_velocity_y() == 0 &&
755 !issolid(base.x+base.width/2, base.y + base.height)) {
756 physic.enable_gravity(true);
757 physic.set_velocity_y(2);
761 physic.apply(elapsed_time, base.x, base.y);
762 if (dying != DYING_FALLING)
763 collision_swept_object_map(&old_base,&base);
767 BadGuy::action_snowball(double elapsed_time)
769 if (dying == DYING_NOT)
770 check_horizontal_bump();
774 physic.apply(elapsed_time, base.x, base.y);
775 if (dying != DYING_FALLING)
776 collision_swept_object_map(&old_base,&base);
778 // Handle dying timer:
779 if (dying == DYING_SQUISHED && !timer.check())
784 BadGuy::action_wingling(double elapsed_time)
786 if (dying != DYING_NOT)
787 physic.enable_gravity(true);
790 Player& tux = *Sector::current()->player;
791 int dirsign = physic.get_velocity_x() < 0 ? -1 : 1;
793 if (fabsf(tux.base.x - base.x) < 150 && base.y < tux.base.y && tux.dying == DYING_NOT)
795 if (target.x < 0 && target.y < 0)
797 target.x = tux.base.x;
798 target.y = tux.base.y;
799 physic.set_velocity(dirsign * 1.5f, -2.25f);
802 else if (base.y >= target.y - 16)
803 physic.set_velocity(dirsign * WINGLING_FLY_SPEED, 0);
806 physic.apply(elapsed_time, base.x, base.y);
809 // Handle dying timer:
810 if (dying == DYING_SQUISHED && !timer.check())
813 // TODO: Winglings should be removed after flying off the screen
817 BadGuy::action_walkingtree(double elapsed_time)
819 Player& tux = *Sector::current()->player;
820 Direction v_dir = physic.get_velocity_x() < 0 ? LEFT : RIGHT;
822 if (dying == DYING_NOT)
823 check_horizontal_bump();
827 if (mode == BGM_BIG && tux.dying == DYING_NOT)
829 if ((tux.base.x + tux.base.width/2 > base.x + base.width/2) && v_dir == LEFT)
832 physic.set_velocity_x(-physic.get_velocity_x());
834 else if ((tux.base.x + tux.base.width/2 < base.x + base.width/2) && v_dir == RIGHT)
837 physic.set_velocity_x(-physic.get_velocity_x());
842 physic.apply(elapsed_time, base.x, base.y);
843 if (dying != DYING_FALLING)
844 collision_swept_object_map(&old_base,&base);
846 // Handle dying timer:
847 if (dying == DYING_SQUISHED && !timer.check())
852 BadGuy::action(float elapsed_time)
854 float scroll_x = Sector::current()->camera->get_translation().x;
855 float scroll_y = Sector::current()->camera->get_translation().y;
857 // BadGuy fall below the ground
858 if (base.y > Sector::current()->solids->get_height() * 32) {
863 // Kill us if we landed on spikes
864 if (dying == DYING_NOT
865 && (kind != BAD_STALACTITE && kind != BAD_FLAME && kind != BAD_BOMB)
866 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
867 || isspike(base.x, base.y + base.height)
868 || isspike(base.x + base.width, base.y + base.height)))
870 physic.set_velocity_y(3);
875 /* activate badguys if they're just inside the offscreen_distance around the
876 * screen. Don't activate them inside the screen, since that might have the
877 * effect of badguys suddenly popping up from nowhere
879 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
880 start_position.x < scroll_x - base.width)
882 else if(start_position.x > scroll_y - Y_OFFSCREEN_DISTANCE &&
883 start_position.y < scroll_y - base.height)
885 else if(start_position.x > scroll_x + screen->w &&
886 start_position.x < scroll_x + screen->w + X_OFFSCREEN_DISTANCE)
888 else if(start_position.y > scroll_y + screen->h &&
889 start_position.y < scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
892 if(base.x + base.width < scroll_x - X_OFFSCREEN_DISTANCE*4
893 || base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE*4
894 || base.y + base.height < scroll_y - Y_OFFSCREEN_DISTANCE*4
895 || base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE*4) {
897 if(dying != DYING_NOT)
908 action_mriceblock(elapsed_time);
912 action_jumpy(elapsed_time);
916 action_mrbomb(elapsed_time);
920 action_bomb(elapsed_time);
924 action_stalactite(elapsed_time);
928 action_flame(elapsed_time);
932 action_fish(elapsed_time);
935 case BAD_BOUNCINGSNOWBALL:
936 action_bouncingsnowball(elapsed_time);
939 case BAD_FLYINGSNOWBALL:
940 action_flyingsnowball(elapsed_time);
944 action_spiky(elapsed_time);
948 action_snowball(elapsed_time);
952 action_wingling(elapsed_time);
955 case BAD_WALKINGTREE:
956 action_walkingtree(elapsed_time);
965 BadGuy::draw(DrawingContext& context)
967 Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
971 if(dying == DYING_FALLING && physic.get_velocity_y() < 0)
972 sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS, VERTICAL_FLIP);
974 sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
977 context.draw_filled_rect(Vector(base.x, base.y),
978 Vector(base.width, base.height), Color(75,0,75, 150), LAYER_OBJECTS+1);
982 BadGuy::set_sprite(Sprite* left, Sprite* right)
991 // FIXME: Using the image size for the physics and collision is
992 // a bad idea, since images should always overlap there physical
995 if(base.width == 0 && base.height == 0) {
996 base.width = left->get_width();
997 base.height = left->get_height();
998 } else if(base.width != left->get_width() || base.height != left->get_height()) {
999 base.x -= (left->get_width() - base.width) / 2;
1000 base.y -= left->get_height() - base.height;
1001 base.width = left->get_width();
1002 base.height = left->get_height();
1006 base.width = base.height = 0;
1010 animation_offset = 0;
1012 sprite_right = right;
1018 // these can't be bumped
1019 if(kind == BAD_FLAME || kind == BAD_BOMB || kind == BAD_FISH
1020 || kind == BAD_FLYINGSNOWBALL)
1023 physic.set_velocity_y(3);
1028 BadGuy::make_player_jump(Player* player)
1030 player->physic.set_velocity_y(2);
1031 player->base.y = base.y - player->base.height - 2;
1035 BadGuy::squish_me(Player* player)
1037 make_player_jump(player);
1039 Sector::current()->add_score(Vector(base.x, base.y),
1040 50 * player_status.score_multiplier);
1042 sound_manager->play_sound(sounds[SND_SQUISH], get_pos());
1043 player_status.score_multiplier++;
1045 dying = DYING_SQUISHED;
1047 physic.set_velocity(0, 0);
1051 BadGuy::squish(Player* player)
1053 static const int MAX_ICEBLOCK_SQUICHES = 10;
1055 if(kind == BAD_MRBOMB) {
1056 // mrbomb transforms into a bomb now
1059 make_player_jump(player);
1060 Sector::current()->add_score(Vector(base.x, base.y),
1061 50 * player_status.score_multiplier);
1062 sound_manager->play_sound(sounds[SND_SQUISH], get_pos());
1063 player_status.score_multiplier++;
1066 } else if (kind == BAD_MRICEBLOCK) {
1067 if (mode == NORMAL || mode == KICK)
1070 sound_manager->play_sound(sounds[SND_STOMP], get_pos());
1072 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1073 physic.set_velocity_x(0);
1076 } else if (mode == FLAT) {
1078 sound_manager->play_sound(sounds[SND_KICK], this);
1080 if (player->base.x < base.x + (base.width/2)) {
1081 physic.set_velocity_x(5);
1084 physic.set_velocity_x(-5);
1089 player->kick_timer.start(KICKING_TIME);
1090 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1093 make_player_jump(player);
1095 player_status.score_multiplier++;
1097 // check for maximum number of squishes
1099 if(squishcount >= MAX_ICEBLOCK_SQUICHES) {
1105 } else if(kind == BAD_FISH) {
1106 // fish can only be killed when falling down
1107 if(physic.get_velocity_y() >= 0)
1110 make_player_jump(player);
1112 Sector::current()->add_score(Vector(base.x, base.y),
1113 25 * player_status.score_multiplier);
1114 player_status.score_multiplier++;
1116 // simply remove the fish...
1119 } else if(kind == BAD_BOUNCINGSNOWBALL) {
1121 set_sprite(img_bouncingsnowball_squished,img_bouncingsnowball_squished);
1123 } else if(kind == BAD_FLYINGSNOWBALL) {
1125 set_sprite(img_flyingsnowball_squished,img_flyingsnowball_squished);
1127 } else if(kind == BAD_SNOWBALL) {
1129 set_sprite(img_snowball_squished_left, img_snowball_squished_right);
1131 } else if(kind == BAD_WINGLING) {
1133 set_sprite(img_wingling_left, img_wingling_left);
1134 } else if(kind == BAD_WALKINGTREE) {
1135 if (mode == BGM_BIG)
1137 set_sprite(img_walkingtree_left_small, img_walkingtree_left_small);
1138 physic.set_velocity_x(physic.get_velocity_x() * 2.0f);
1139 // XXX magic number: 66 is BGM_BIG height
1141 make_player_jump(player);
1142 base.y += 66 - base.height;
1144 Sector::current()->add_score(Vector(base.x, base.y),
1145 25 * player_status.score_multiplier);
1146 player_status.score_multiplier++;
1156 BadGuy::kill_me(int score)
1158 if(kind == BAD_BOMB)
1161 dying = DYING_FALLING;
1162 if(kind == BAD_MRICEBLOCK) {
1163 set_sprite(img_mriceblock_falling_left, img_mriceblock_falling_right);
1166 Player& tux = *Sector::current()->player;
1167 tux.holding_something = false;
1171 physic.enable_gravity(true);
1173 /* Gain some points: */
1175 Sector::current()->add_score(Vector(base.x, base.y),
1176 score * player_status.score_multiplier);
1178 /* Play death sound: */
1179 sound_manager->play_sound(sounds[SND_FALL], this);
1183 BadGuy::explode(bool right_way)
1185 BadGuy *badguy = Sector::current()->add_bad_guy(base.x, base.y, BAD_BOMB);
1188 badguy->timer.start(0);
1189 badguy->mode = BOMB_TICKING;
1196 BadGuy::collision(const MovingObject&, int)
1202 BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
1204 BadGuy* pbad_c = NULL;
1205 Bullet* pbullet_c = NULL;
1207 if(type == COLLISION_BUMP) {
1212 if(type == COLLISION_SQUISH) {
1213 Player* player = static_cast<Player*>(p_c_object);
1218 /* COLLISION_NORMAL */
1222 pbullet_c = (Bullet*) p_c_object;
1224 if(pbullet_c->kind == FIRE_BULLET)
1226 if (kind != BAD_BOMB && kind != BAD_STALACTITE && kind != BAD_FLAME)
1229 else if(pbullet_c->kind == ICE_BULLET)
1231 //if(kind == BAD_FLAME)
1234 frozen_timer.start(FROZEN_TIME);
1239 pbad_c = (BadGuy*) p_c_object;
1242 /* If we're a kicked mriceblock, kill [almost] any badguys we hit */
1243 if(kind == BAD_MRICEBLOCK && mode == KICK &&
1244 kind != BAD_FLAME && kind != BAD_BOMB && kind != BAD_STALACTITE)
1246 pbad_c->kill_me(25);
1249 // a held mriceblock kills the enemy too but falls to ground then
1250 else if(kind == BAD_MRICEBLOCK && mode == HELD)
1252 pbad_c->kill_me(25);
1256 /* Kill badguys that run into exploding bomb */
1257 else if (kind == BAD_BOMB && dying == DYING_NOT)
1259 if (pbad_c->kind == BAD_MRBOMB)
1261 // mrbomb transforms into a bomb now
1262 pbad_c->explode(true);
1265 else if (pbad_c->kind != BAD_MRBOMB)
1267 pbad_c->kill_me(50);
1271 /* Kill any badguys that get hit by stalactite */
1272 else if (kind == BAD_STALACTITE && dying == DYING_NOT)
1274 if (pbad_c->kind == BAD_MRBOMB)
1276 // mrbomb transforms into a bomb now
1277 pbad_c->explode(false);
1281 pbad_c->kill_me(50);
1284 /* When enemies run into eachother, make them change directions */
1287 // Wingling doesn't interact with other badguys
1288 if (pbad_c->kind == BAD_WINGLING || kind == BAD_WINGLING)
1291 // Jumpy, fish, flame, stalactites, wingling are exceptions
1292 if (pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FLAME
1293 || pbad_c->kind == BAD_STALACTITE || pbad_c->kind == BAD_FISH)
1296 // Bounce off of other badguy if we land on top of him
1297 if (base.y + base.height < pbad_c->base.y + pbad_c->base.height)
1299 if (pbad_c->dir == LEFT)
1302 physic.set_velocity(fabsf(physic.get_velocity_x()), 2);
1304 else if (pbad_c->dir == RIGHT)
1307 physic.set_velocity(-fabsf(physic.get_velocity_x()), 2);
1312 else if (base.y + base.height > pbad_c->base.y + pbad_c->base.height)
1315 if (pbad_c->kind != BAD_FLAME)
1320 physic.set_velocity_x(fabsf(physic.get_velocity_x()));
1322 // in case badguys get "jammed"
1323 if (physic.get_velocity_x() != 0)
1324 base.x = pbad_c->base.x + pbad_c->base.width;
1326 else if (dir == RIGHT)
1329 physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
1338 Player* player = static_cast<Player*>(p_c_object);
1339 /* Get kicked if were flat */
1340 if (mode == FLAT && !dying)
1342 sound_manager->play_sound(sounds[SND_KICK], this);
1344 // Hit from left side
1345 if (player->base.x < base.x) {
1346 physic.set_velocity_x(5);
1349 // Hit from right side
1351 physic.set_velocity_x(-5);
1356 player->kick_timer.start(KICKING_TIME);
1357 set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right);
1365 //---------------------------------------------------------------------------
1367 void load_badguy_gfx()
1369 img_mriceblock_flat_left = sprite_manager->load("mriceblock-flat-left");
1370 img_mriceblock_flat_right = sprite_manager->load("mriceblock-flat-right");
1371 img_mriceblock_falling_left = sprite_manager->load("mriceblock-falling-left");
1372 img_mriceblock_falling_right = sprite_manager->load("mriceblock-falling-right");
1373 img_mriceblock_left = sprite_manager->load("mriceblock-left");
1374 img_mriceblock_right = sprite_manager->load("mriceblock-right");
1375 img_jumpy_left_up = sprite_manager->load("jumpy-left-up");
1376 img_jumpy_left_down = sprite_manager->load("jumpy-left-down");
1377 img_jumpy_left_middle = sprite_manager->load("jumpy-left-middle");
1378 img_jumpy_left_iced = sprite_manager->load("jumpy-left-iced");
1379 img_mrbomb_left = sprite_manager->load("mrbomb-left");
1380 img_mrbomb_right = sprite_manager->load("mrbomb-right");
1381 img_mrbomb_iced_left = sprite_manager->load("mrbomb-iced-left");
1382 img_mrbomb_iced_right = sprite_manager->load("mrbomb-iced-right");
1383 img_mrbomb_ticking_left = sprite_manager->load("mrbomb-ticking-left");
1384 img_mrbomb_ticking_right = sprite_manager->load("mrbomb-ticking-right");
1385 img_mrbomb_explosion = sprite_manager->load("mrbomb-explosion");
1386 img_stalactite = sprite_manager->load("stalactite");
1387 img_stalactite_broken = sprite_manager->load("stalactite-broken");
1388 img_flame = sprite_manager->load("flame");
1389 img_fish = sprite_manager->load("fish");
1390 img_fish_down = sprite_manager->load("fish-down");
1391 img_fish_iced = sprite_manager->load("fish-iced");
1392 img_fish_iced_down = sprite_manager->load("fish-iced-down");
1393 img_bouncingsnowball_left = sprite_manager->load("bouncingsnowball-left");
1394 img_bouncingsnowball_right = sprite_manager->load("bouncingsnowball-right");
1395 img_bouncingsnowball_squished = sprite_manager->load("bouncingsnowball-squished");
1396 img_flyingsnowball = sprite_manager->load("flyingsnowball");
1397 img_flyingsnowball_squished = sprite_manager->load("flyingsnowball-squished");
1398 img_spiky_left = sprite_manager->load("spiky-left");
1399 img_spiky_right = sprite_manager->load("spiky-right");
1400 img_spiky_iced_left = sprite_manager->load("spiky-iced-left");
1401 img_spiky_iced_right = sprite_manager->load("spiky-iced-right");
1402 img_snowball_left = sprite_manager->load("snowball-left");
1403 img_snowball_right = sprite_manager->load("snowball-right");
1404 img_snowball_squished_left = sprite_manager->load("snowball-squished-left");
1405 img_snowball_squished_right = sprite_manager->load("snowball-squished-right");
1406 img_wingling_left = sprite_manager->load("wingling-left");
1407 img_walkingtree_left = sprite_manager->load("walkingtree-left");
1408 img_walkingtree_left_small = sprite_manager->load("walkingtree-left-small");
1411 void free_badguy_gfx()