Getting a rock on the head hurts.
[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 lisp::Lisp& reader)
33   : MovingSprite(reader, "images/objects/rock/rock.sprite")
34 {
35   sound_manager->preload( ROCK_SOUND );
36   on_ground = false;
37   grabbed = false;
38 }
39
40 void
41 Rock::write(lisp::Writer& writer)
42 {
43   writer.start_list("rock");
44
45   writer.write_float("x", bbox.p1.x);
46   writer.write_float("y", bbox.p1.y);
47
48   writer.end_list("rock");
49 }
50
51 void
52 Rock::update(float elapsed_time)
53 {
54   if( grabbed )
55     return;
56
57   movement = physic.get_movement(elapsed_time);
58 }
59
60 void
61 Rock::collision_solid(const CollisionHit& hit)
62 {
63   if( hit.top || hit.bottom )
64     physic.set_velocity_y( 0 );
65   if( hit.left || hit.right )
66     physic.set_velocity_x( 0 );
67   if( hit.crush )
68     physic.set_velocity(0, 0);
69
70   if( hit.bottom  && !on_ground ){
71     sound_manager->play( ROCK_SOUND, get_pos() );
72     on_ground = true;
73   }
74 }
75
76 HitResponse
77 Rock::collision(GameObject& other, const CollisionHit& hit)
78 {
79   if( !on_ground ){
80     if( hit.bottom && physic.get_velocity_y() > 200  ){
81       MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
82       if( moving_object ){
83         //Getting a rock on the head hurts. A lot.
84         moving_object->collision_tile( Tile::HURTS );
85       }
86     }
87     return FORCE_MOVE;
88   }
89
90   //Fake being solid for moving_object.
91   MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
92   if( moving_object ){
93     if( hit.top ){
94       float inside = moving_object->get_bbox().get_bottom() - get_bbox().get_top();
95       if( inside > 0 ){
96         Vector pos = moving_object->get_pos();
97         pos.y -= inside;
98         moving_object->set_pos( pos );
99       }
100     }
101     CollisionHit hit_other = hit;
102     std::swap(hit_other.left, hit_other.right);
103     std::swap(hit_other.top, hit_other.bottom);
104     moving_object->collision_solid( hit_other );
105   }
106   return FORCE_MOVE;
107 }
108
109 void
110 Rock::grab(MovingObject& , const Vector& pos, Direction)
111 {
112   movement = pos - get_pos();
113   set_group( COLGROUP_DISABLED );
114   on_ground = true;
115   grabbed = true;
116 }
117
118 void
119 Rock::ungrab(MovingObject& , Direction ){
120   set_group( COLGROUP_MOVING );
121   on_ground = false;
122   physic.set_velocity(0, 0);
123   grabbed = false;
124 }
125
126 IMPLEMENT_FACTORY(Rock, "rock");