[ Patch #1793 ] Turn physic into POD
[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   set_group(COLGROUP_MOVING_STATIC);
39 }
40
41 Rock::Rock(const lisp::Lisp& reader, std::string spritename)
42   : MovingSprite(reader, spritename)
43 {
44   sound_manager->preload(ROCK_SOUND);
45   on_ground = false;
46   grabbed = false;
47   set_group(COLGROUP_MOVING_STATIC);
48 }
49
50 void
51 Rock::write(lisp::Writer& writer)
52 {
53   writer.start_list("rock");
54
55   writer.write_float("x", bbox.p1.x);
56   writer.write_float("y", bbox.p1.y);
57
58   writer.end_list("rock");
59 }
60
61 void
62 Rock::update(float elapsed_time)
63 {
64   if( grabbed )
65     return;
66
67   if (on_ground) physic.vx = 0;
68
69   movement = physic.get_movement(elapsed_time);
70 }
71
72 void
73 Rock::collision_solid(const CollisionHit& hit)
74 {
75   if(hit.top || hit.bottom)
76     physic.vy = 0;
77   if(hit.left || hit.right)
78     physic.vx = 0;
79   if(hit.crush)
80     physic.vx = 0;
81     physic.vy = 0;
82
83   if(hit.bottom  && !on_ground) {
84     sound_manager->play(ROCK_SOUND, get_pos());
85     on_ground = true;
86   }
87 }
88
89 HitResponse
90 Rock::collision(GameObject& other, const CollisionHit& hit)
91 {
92   if(!on_ground) {
93     if(hit.bottom && physic.vy > 200) {
94       MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
95       if(moving_object) {
96         //Getting a rock on the head hurts. A lot.
97         moving_object->collision_tile(Tile::HURTS);
98       }
99     }
100     return FORCE_MOVE;
101   }
102
103   return FORCE_MOVE;
104 }
105
106 void
107 Rock::grab(MovingObject& , const Vector& pos, Direction)
108 {
109   movement = pos - get_pos();
110   last_movement = movement;
111   set_group(COLGROUP_DISABLED);
112   on_ground = true;
113   grabbed = true;
114 }
115
116 void
117 Rock::ungrab(MovingObject& , Direction dir)
118 {
119   set_group(COLGROUP_MOVING_STATIC);
120   on_ground = false;
121   if (last_movement.norm() > 1) {
122     physic.vx = ((dir == RIGHT) ? 200 : -200);
123     physic.vy = -200;
124   } else {
125     physic.vx = 0;
126     physic.vy = 0;
127   }
128   grabbed = false;
129 }
130
131 IMPLEMENT_FACTORY(Rock, "rock");