More -Weffc++ cleanup
[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   physic(),
29   on_ground(),
30   grabbed(),
31   last_movement()
32 {
33   sound_manager->preload(ROCK_SOUND);
34   on_ground = false;
35   grabbed = false;
36   set_group(COLGROUP_MOVING_STATIC);
37 }
38
39 Rock::Rock(const Reader& reader) :
40   MovingSprite(reader, "images/objects/rock/rock.sprite"),
41   on_ground(),
42   grabbed(),
43   last_movement()
44 {
45   sound_manager->preload(ROCK_SOUND);
46   on_ground = false;
47   grabbed = false;
48   set_group(COLGROUP_MOVING_STATIC);
49 }
50
51 Rock::Rock(const Reader& reader, std::string spritename)
52   : MovingSprite(reader, spritename)
53 {
54   sound_manager->preload(ROCK_SOUND);
55   on_ground = false;
56   grabbed = false;
57   set_group(COLGROUP_MOVING_STATIC);
58 }
59
60 void
61 Rock::update(float elapsed_time)
62 {
63   if( grabbed )
64     return;
65
66   if (on_ground) physic.set_velocity_x(0);
67
68   movement = physic.get_movement(elapsed_time);
69 }
70
71 void
72 Rock::collision_solid(const CollisionHit& hit)
73 {
74   if(grabbed) {
75     return;
76   }
77   if(hit.top || hit.bottom)
78     physic.set_velocity_y(0);
79   if(hit.left || hit.right)
80     physic.set_velocity_x(0);
81   if(hit.crush)
82     physic.set_velocity(0, 0);
83
84   if(hit.bottom  && !on_ground && !grabbed) {
85     sound_manager->play(ROCK_SOUND, get_pos());
86     on_ground = true;
87   }
88 }
89
90 HitResponse
91 Rock::collision(GameObject& other, const CollisionHit& hit)
92 {
93   if(grabbed) {
94     return PASSTHROUGH;
95   }
96   if(!on_ground) {
97     if(hit.bottom && physic.get_velocity_y() > 200) {
98       MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
99       if(moving_object) {
100         //Getting a rock on the head hurts. A lot.
101         moving_object->collision_tile(Tile::HURTS);
102       }
103     }
104     return FORCE_MOVE;
105   }
106
107   return FORCE_MOVE;
108 }
109
110 void
111 Rock::grab(MovingObject& , const Vector& pos, Direction)
112 {
113   movement = pos - get_pos();
114   last_movement = movement;
115   set_group(COLGROUP_TOUCHABLE);
116   on_ground = false;
117   grabbed = true;
118 }
119
120 void
121 Rock::ungrab(MovingObject& , Direction dir)
122 {
123   set_group(COLGROUP_MOVING_STATIC);
124   on_ground = false;
125   if(dir == UP) {
126     physic.set_velocity(0, -500);
127   } else if (last_movement.norm() > 1) {
128     physic.set_velocity((dir == RIGHT) ? 200 : -200, -200);
129   } else {
130     physic.set_velocity(0, 0);
131   }
132   grabbed = false;
133 }
134
135 IMPLEMENT_FACTORY(Rock, "rock");
136
137 /* EOF */