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