changed worldmap format a bit to be more consistent with level format
[supertux.git] / src / object / rock.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 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
19 //  02111-1307, USA.
20
21 #include <config.h>
22
23 #include "rock.h"
24 #include "sprite/sprite.h"
25 #include "sprite/sprite_manager.h"
26 #include "lisp/writer.h"
27 #include "video/drawing_context.h"
28 #include "resources.h"
29 #include "object_factory.h"
30
31 Rock::Rock(const lisp::Lisp& reader)
32 {
33   reader.get("x", bbox.p1.x);
34   reader.get("y", bbox.p1.y);
35   bbox.set_size(31.8, 31.8);
36   sprite = sprite_manager->create("rock");
37   grabbed = false;
38   flags |= FLAG_SOLID;
39 }
40
41 Rock::~Rock()
42 {
43   delete sprite;
44 }
45
46 void
47 Rock::write(lisp::Writer& writer)
48 {
49   writer.start_list("rock");
50
51   writer.write_float("x", bbox.p1.x);
52   writer.write_float("y", bbox.p1.y);
53
54   writer.end_list("rock");
55 }
56
57 void
58 Rock::draw(DrawingContext& context)
59 {
60
61   sprite->draw(context, get_pos(), LAYER_OBJECTS);
62 }
63
64 void
65 Rock::update(float elapsed_time)
66 {
67   if(!grabbed) {
68     flags |= FLAG_SOLID;
69     flags &= ~FLAG_NO_COLLDET;
70     movement = physic.get_movement(elapsed_time);
71   } else {
72     physic.set_velocity(0, 0);
73     flags &= ~FLAG_SOLID;
74     flags |= FLAG_NO_COLLDET;
75   }
76   
77   grabbed = false;
78 }
79
80 HitResponse
81 Rock::collision(GameObject& , const CollisionHit& )
82 {
83   if(grabbed)
84     return FORCE_MOVE;
85
86   physic.set_velocity(0, 0);
87   return CONTINUE;
88 }
89
90 void
91 Rock::grab(MovingObject& , const Vector& pos)
92 {
93   movement = pos - get_pos();
94   grabbed = true;
95 }
96
97 IMPLEMENT_FACTORY(Rock, "rock")
98