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