src/object/rock.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
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"; //TODO use own sound.
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         //Getting a rock on the head hurts. A lot.
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); //needed for lanterns catching willowisps
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 /* EOF */

Generated on Mon Jun 9 03:38:20 2014 for SuperTux by  doxygen 1.5.1