00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "audio/sound_manager.hpp"
00018 #include "object/rock.hpp"
00019 #include "supertux/object_factory.hpp"
00020 #include "supertux/tile.hpp"
00021
00022 namespace {
00023 const std::string ROCK_SOUND = "sounds/brick.wav";
00024 }
00025
00026 Rock::Rock(const Vector& pos, std::string spritename) :
00027 MovingSprite(pos, spritename),
00028 physic(),
00029 on_ground(),
00030 grabbed(),
00031 last_movement()
00032 {
00033 sound_manager->preload(ROCK_SOUND);
00034 on_ground = false;
00035 grabbed = false;
00036 set_group(COLGROUP_MOVING_STATIC);
00037 }
00038
00039 Rock::Rock(const Reader& reader) :
00040 MovingSprite(reader, "images/objects/rock/rock.sprite"),
00041 physic(),
00042 on_ground(),
00043 grabbed(),
00044 last_movement()
00045 {
00046 sound_manager->preload(ROCK_SOUND);
00047 on_ground = false;
00048 grabbed = false;
00049 set_group(COLGROUP_MOVING_STATIC);
00050 }
00051
00052 Rock::Rock(const Reader& reader, std::string spritename) :
00053 MovingSprite(reader, spritename),
00054 physic(),
00055 on_ground(),
00056 grabbed(),
00057 last_movement()
00058 {
00059 sound_manager->preload(ROCK_SOUND);
00060 on_ground = false;
00061 grabbed = false;
00062 set_group(COLGROUP_MOVING_STATIC);
00063 }
00064
00065 void
00066 Rock::update(float elapsed_time)
00067 {
00068 if( grabbed )
00069 return;
00070
00071 if (on_ground) physic.set_velocity_x(0);
00072
00073 movement = physic.get_movement(elapsed_time);
00074 }
00075
00076 void
00077 Rock::collision_solid(const CollisionHit& hit)
00078 {
00079 if(grabbed) {
00080 return;
00081 }
00082 if(hit.top || hit.bottom)
00083 physic.set_velocity_y(0);
00084 if(hit.left || hit.right)
00085 physic.set_velocity_x(0);
00086 if(hit.crush)
00087 physic.set_velocity(0, 0);
00088
00089 if(hit.bottom && !on_ground && !grabbed) {
00090 sound_manager->play(ROCK_SOUND, get_pos());
00091 on_ground = true;
00092 }
00093 }
00094
00095 HitResponse
00096 Rock::collision(GameObject& other, const CollisionHit& hit)
00097 {
00098 if(grabbed) {
00099 return ABORT_MOVE;
00100 }
00101 if(!on_ground) {
00102 if(hit.bottom && physic.get_velocity_y() > 200) {
00103 MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
00104 if(moving_object) {
00105
00106 moving_object->collision_tile(Tile::HURTS);
00107 }
00108 }
00109 return FORCE_MOVE;
00110 }
00111
00112 return FORCE_MOVE;
00113 }
00114
00115 void
00116 Rock::grab(MovingObject& , const Vector& pos, Direction)
00117 {
00118 movement = pos - get_pos();
00119 last_movement = movement;
00120 set_group(COLGROUP_TOUCHABLE);
00121 on_ground = false;
00122 grabbed = true;
00123 }
00124
00125 void
00126 Rock::ungrab(MovingObject& , Direction dir)
00127 {
00128 set_group(COLGROUP_MOVING_STATIC);
00129 on_ground = false;
00130 if(dir == UP) {
00131 physic.set_velocity(0, -500);
00132 } else if (last_movement.norm() > 1) {
00133 physic.set_velocity((dir == RIGHT) ? 200 : -200, -200);
00134 } else {
00135 physic.set_velocity(0, 0);
00136 }
00137 grabbed = false;
00138 }
00139
00140
00141