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