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