Tuxdev's patch to fix death by falling when invincible.
[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", LAYER_OBJECTS+1, COLGROUP_MOVING)
30 {
31   grabbed = 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(!grabbed) {
50     flags |= FLAG_SOLID;
51     set_group(COLGROUP_MOVING);
52     movement = physic.get_movement(elapsed_time);
53   } else {
54     physic.set_velocity(0, 0);
55     flags &= ~FLAG_SOLID;
56     set_group(COLGROUP_DISABLED);
57   }
58   
59   grabbed = false;
60   /*
61   printf("%p - V %3.1f %3.1f - P %3.1f %3.1f\n", this,
62           physic.get_velocity().x, physic.get_velocity().y,
63           get_pos().x, get_pos().y);
64   */
65 }
66
67 HitResponse
68 Rock::collision(GameObject& object, const CollisionHit& )
69 {
70   if(grabbed) {
71     return FORCE_MOVE;
72   }
73
74   if(object.get_flags() & FLAG_SOLID) {
75     physic.set_velocity(0, 0);
76     return CONTINUE;
77   }
78
79   return FORCE_MOVE;
80 }
81
82 void
83 Rock::grab(MovingObject& , const Vector& pos, Direction)
84 {
85   movement = pos - get_pos();
86   grabbed = true;
87 }
88
89 IMPLEMENT_FACTORY(Rock, "rock");
90