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