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