2b17713af5dfb0af3e5fc587eaa7491aeb509032
[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 #include <config.h>
21
22 #include "rock.hpp"
23 #include "sprite/sprite.hpp"
24 #include "sprite/sprite_manager.hpp"
25 #include "lisp/writer.hpp"
26 #include "video/drawing_context.hpp"
27 #include "resources.hpp"
28 #include "object_factory.hpp"
29
30 Rock::Rock(const lisp::Lisp& reader)
31 {
32   reader.get("x", bbox.p1.x);
33   reader.get("y", bbox.p1.y);
34   bbox.set_size(31.8, 31.8);
35   sprite = sprite_manager->create("images/objects/rock/rock.sprite");
36   grabbed = false;
37   flags |= FLAG_SOLID | FLAG_PORTABLE;
38   set_group(COLGROUP_MOVING);
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   sprite->draw(context, get_pos(), LAYER_OBJECTS);
61 }
62
63 void
64 Rock::update(float elapsed_time)
65 {
66   if(!grabbed) {
67     flags |= FLAG_SOLID;
68     set_group(COLGROUP_MOVING);
69     movement = physic.get_movement(elapsed_time);
70   } else {
71     physic.set_velocity(0, 0);
72     flags &= ~FLAG_SOLID;
73     set_group(COLGROUP_DISABLED);
74   }
75   
76   grabbed = false;
77   printf("%p - V %3.1f %3.1f - P %3.1f %3.1f\n", this,
78           physic.get_velocity().x, physic.get_velocity().y,
79           get_pos().x, get_pos().y);
80 }
81
82 HitResponse
83 Rock::collision(GameObject& object, const CollisionHit& hit)
84 {
85   if(grabbed) {
86     return FORCE_MOVE;
87   }
88
89   if(object.get_flags() & FLAG_SOLID) {
90     printf("%p vs %p - %3.1f %3.1f D %3.1f\n", this, &object,
91         hit.normal.x, hit.normal.y, hit.depth);
92     physic.set_velocity(0, 0);
93     return CONTINUE;
94   }
95
96   return FORCE_MOVE;
97 }
98
99 void
100 Rock::grab(MovingObject& , const Vector& pos, Direction)
101 {
102   movement = pos - get_pos();
103   grabbed = true;
104 }
105
106 IMPLEMENT_FACTORY(Rock, "rock");
107