00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "badguy/toad.hpp"
00018
00019 #include "audio/sound_manager.hpp"
00020 #include "object/player.hpp"
00021 #include "sprite/sprite.hpp"
00022 #include "supertux/object_factory.hpp"
00023
00024 namespace {
00025 const float VERTICAL_SPEED = -450;
00026 const float HORIZONTAL_SPEED = 320;
00027 const float TOAD_RECOVER_TIME = 0.5;
00028 static const std::string HOP_SOUND = "sounds/hop.ogg";
00029 }
00030
00031 Toad::Toad(const Reader& reader) :
00032 BadGuy(reader, "images/creatures/toad/toad.sprite"),
00033 recover_timer(),
00034 state()
00035 {
00036 sound_manager->preload(HOP_SOUND);
00037 }
00038
00039 Toad::Toad(const Vector& pos, Direction d) :
00040 BadGuy(pos, d, "images/creatures/toad/toad.sprite"),
00041 recover_timer(),
00042 state()
00043 {
00044 sound_manager->preload(HOP_SOUND);
00045 }
00046
00047 void
00048 Toad::initialize()
00049 {
00050
00051 state = JUMPING;
00052 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
00053 }
00054
00055 void
00056 Toad::set_state(ToadState newState)
00057 {
00058 if (newState == IDLE) {
00059 physic.set_velocity_x(0);
00060 physic.set_velocity_y(0);
00061 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
00062
00063 recover_timer.start(TOAD_RECOVER_TIME);
00064 } else
00065 if (newState == JUMPING) {
00066 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
00067 physic.set_velocity_x(dir == LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
00068 physic.set_velocity_y(VERTICAL_SPEED);
00069 sound_manager->play( HOP_SOUND, get_pos());
00070 } else
00071 if (newState == FALLING) {
00072 Player* player = get_nearest_player();
00073
00074 if (player && (player->get_bbox().p2.x < get_bbox().p1.x) && (dir == RIGHT)) dir = LEFT;
00075 if (player && (player->get_bbox().p1.x > get_bbox().p2.x) && (dir == LEFT)) dir = RIGHT;
00076 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
00077 }
00078
00079 state = newState;
00080 }
00081
00082 bool
00083 Toad::collision_squished(GameObject& object)
00084 {
00085 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
00086 kill_squished(object);
00087 return true;
00088 }
00089
00090 void
00091 Toad::collision_solid(const CollisionHit& hit)
00092 {
00093
00094 if (BadGuy::get_state() == STATE_SQUISHED) {
00095 BadGuy::collision_solid(hit);
00096 return;
00097 }
00098
00099
00100 if(state == IDLE) {
00101 return;
00102 }
00103
00104
00105 if(((physic.get_velocity_x() < 0) && hit.left) || ((physic.get_velocity_x() > 0) && hit.right)) {
00106
00107
00108
00109
00110
00111
00112
00113
00114 physic.set_velocity_x(-0.25*physic.get_velocity_x());
00115 }
00116
00117
00118 if ((state == FALLING) && hit.bottom) {
00119 set_state(IDLE);
00120 return;
00121 }
00122
00123
00124 if ((state == JUMPING) && hit.top) {
00125 physic.set_velocity_y(0);
00126 }
00127
00128 }
00129
00130 HitResponse
00131 Toad::collision_badguy(BadGuy& , const CollisionHit& hit)
00132 {
00133
00134 collision_solid(hit);
00135
00136 return CONTINUE;
00137 }
00138
00139 void
00140 Toad::active_update(float elapsed_time)
00141 {
00142 BadGuy::active_update(elapsed_time);
00143
00144
00145 if ((state == JUMPING) && (physic.get_velocity_y() > 0)) {
00146 set_state(FALLING);
00147 return;
00148 }
00149
00150
00151 if ((state == IDLE) && (recover_timer.check())) {
00152 set_state(JUMPING);
00153 return;
00154 }
00155
00156 }
00157
00158