Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / rock.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "audio/sound_manager.hpp"
18 #include "object/rock.hpp"
19 #include "supertux/object_factory.hpp"
20 #include "supertux/tile.hpp"
21
22 namespace {
23 const std::string ROCK_SOUND = "sounds/brick.wav"; //TODO use own sound.
24 }
25
26 Rock::Rock(const Vector& pos, std::string spritename) :
27   MovingSprite(pos, spritename),
28   on_ground(),
29   grabbed(),
30   last_movement()
31 {
32   sound_manager->preload(ROCK_SOUND);
33   on_ground = false;
34   grabbed = false;
35   set_group(COLGROUP_MOVING_STATIC);
36 }
37
38 Rock::Rock(const Reader& reader) :
39   MovingSprite(reader, "images/objects/rock/rock.sprite"),
40   on_ground(),
41   grabbed(),
42   last_movement()
43 {
44   sound_manager->preload(ROCK_SOUND);
45   on_ground = false;
46   grabbed = false;
47   set_group(COLGROUP_MOVING_STATIC);
48 }
49
50 Rock::Rock(const Reader& reader, std::string spritename)
51   : MovingSprite(reader, spritename)
52 {
53   sound_manager->preload(ROCK_SOUND);
54   on_ground = false;
55   grabbed = false;
56   set_group(COLGROUP_MOVING_STATIC);
57 }
58
59 void
60 Rock::update(float elapsed_time)
61 {
62   if( grabbed )
63     return;
64
65   if (on_ground) physic.set_velocity_x(0);
66
67   movement = physic.get_movement(elapsed_time);
68 }
69
70 void
71 Rock::collision_solid(const CollisionHit& hit)
72 {
73   if(grabbed) {
74     return;
75   }
76   if(hit.top || hit.bottom)
77     physic.set_velocity_y(0);
78   if(hit.left || hit.right)
79     physic.set_velocity_x(0);
80   if(hit.crush)
81     physic.set_velocity(0, 0);
82
83   if(hit.bottom  && !on_ground && !grabbed) {
84     sound_manager->play(ROCK_SOUND, get_pos());
85     on_ground = true;
86   }
87 }
88
89 HitResponse
90 Rock::collision(GameObject& other, const CollisionHit& hit)
91 {
92   if(grabbed) {
93     return PASSTHROUGH;
94   }
95   if(!on_ground) {
96     if(hit.bottom && physic.get_velocity_y() > 200) {
97       MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
98       if(moving_object) {
99         //Getting a rock on the head hurts. A lot.
100         moving_object->collision_tile(Tile::HURTS);
101       }
102     }
103     return FORCE_MOVE;
104   }
105
106   return FORCE_MOVE;
107 }
108
109 void
110 Rock::grab(MovingObject& , const Vector& pos, Direction)
111 {
112   movement = pos - get_pos();
113   last_movement = movement;
114   set_group(COLGROUP_TOUCHABLE);
115   on_ground = false;
116   grabbed = true;
117 }
118
119 void
120 Rock::ungrab(MovingObject& , Direction dir)
121 {
122   set_group(COLGROUP_MOVING_STATIC);
123   on_ground = false;
124   if(dir == UP) {
125     physic.set_velocity(0, -500);
126   } else if (last_movement.norm() > 1) {
127     physic.set_velocity((dir == RIGHT) ? 200 : -200, -200);
128   } else {
129     physic.set_velocity(0, 0);
130   }
131   grabbed = false;
132 }
133
134 IMPLEMENT_FACTORY(Rock, "rock");
135
136 /* EOF */