Let's Rock
[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 "video/drawing_context.hpp"
25 #include "resources.hpp"
26 #include "object_factory.hpp"
27
28 Rock::Rock(const lisp::Lisp& reader)
29         : MovingSprite(reader, "images/objects/rock/rock.sprite")
30 {
31   on_ground = false;
32   flags |= FLAG_SOLID | FLAG_PORTABLE;
33 }
34
35 void
36 Rock::write(lisp::Writer& writer)
37 {
38   writer.start_list("rock");
39
40   writer.write_float("x", bbox.p1.x);
41   writer.write_float("y", bbox.p1.y);
42
43   writer.end_list("rock");
44 }
45
46 void
47 Rock::update(float elapsed_time)
48 {
49   if( !on_ground ) {
50     movement = physic.get_movement(elapsed_time);
51   }
52 }
53
54 void
55 Rock::collision_solid(const CollisionHit& hit)
56 {
57   physic.set_velocity(0, 0);
58   if( hit.bottom ){
59      on_ground = true;
60   }
61 }
62
63 HitResponse
64 Rock::collision(GameObject& other, const CollisionHit& hit)
65 {
66   if( !on_ground ){
67       return FORCE_MOVE;
68   }
69   
70   //Fake being solid for moving_object. 
71   MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
72   if( moving_object ){
73       if( hit.top ){
74         float inside = moving_object->get_bbox().get_bottom() - get_bbox().get_top();
75         if( inside > 0 ){
76           Vector pos = moving_object->get_pos();
77           pos.y -= inside; 
78           moving_object->set_pos( pos );    
79         }
80       }
81       CollisionHit hit_other = hit;
82       std::swap(hit_other.left, hit_other.right);
83       std::swap(hit_other.top, hit_other.bottom);
84       moving_object->collision_solid( hit_other );
85   }
86   return FORCE_MOVE;
87 }
88
89 void
90 Rock::grab(MovingObject& , const Vector& pos, Direction)
91 {
92   movement = pos - get_pos();
93   set_group( COLGROUP_DISABLED );
94   on_ground = true;
95  
96 }
97
98 void
99 Rock::ungrab(MovingObject& , Direction ){
100   set_group( COLGROUP_MOVING );
101   on_ground = false;
102   physic.set_velocity(0, 0);
103 }
104
105 IMPLEMENT_FACTORY(Rock, "rock");
106