6f7b0390b3b3318578c143f4dc3285128547c89a
[supertux.git] / src / trigger / climbable.cpp
1 //  $Id$
2 //
3 //  SuperTux - Climbable area
4 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.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 "climbable.hpp"
23
24 #include "game_session.hpp"
25 #include "lisp/lisp.hpp"
26 #include "lisp/writer.hpp"
27 #include "object_factory.hpp"
28 #include "main.hpp"
29 #include "sector.hpp"
30 #include "level.hpp"
31 #include "gettext.hpp"
32 #include "object/tilemap.hpp"
33
34 namespace {
35   const float GRACE_DX = 8; // how far off may the player's bounding-box be x-wise
36   const float GRACE_DY = 8; // how far off may the player's bounding-box be y-wise
37   const float ACTIVATE_TRY_FOR = 1; // how long to try correcting mis-alignment of player and climbable before giving up
38   const float POSITION_FIX_AX = 50; // x-wise acceleration applied to player when trying to align player and Climbable
39   const float POSITION_FIX_AY = 50; // y-wise acceleration applied to player when trying to align player and Climbable
40 }
41
42 Climbable::Climbable(const lisp::Lisp& reader)
43   : climbed_by(0)
44 {
45   reader.get("x", bbox.p1.x);
46   reader.get("y", bbox.p1.y);
47   float w = 32, h = 32;
48   reader.get("width", w);
49   reader.get("height", h);
50   bbox.set_size(w, h);
51 }
52
53 Climbable::Climbable(const Rect& area)
54   : climbed_by(0)
55 {
56   bbox = area;
57 }
58
59 Climbable::~Climbable()
60 {
61   if (climbed_by) {
62     climbed_by->stop_climbing(*this);
63     climbed_by = 0;
64   }
65 }
66
67 void
68 Climbable::write(lisp::Writer& writer)
69 {
70   writer.start_list("climbable");
71
72   writer.write("x", bbox.p1.x);
73   writer.write("y", bbox.p1.y);
74   writer.write("width", bbox.get_width());
75   writer.write("height", bbox.get_height());
76
77   writer.end_list("climbable");
78 }
79
80 void 
81 Climbable::update(float /*elapsed_time*/)
82 {
83   if (!climbed_by) return;
84
85   if (!may_climb(*climbed_by)) {
86     climbed_by->stop_climbing(*this);
87     climbed_by = 0;
88   }
89 }
90
91 void
92 Climbable::draw(DrawingContext& context)
93 {
94   if (climbed_by) {
95     context.push_transform();
96     context.set_translation(Vector(0, 0));
97     Vector pos = Vector(0, SCREEN_HEIGHT/2 - normal_font->get_height()/2);
98     context.draw_center_text(normal_font, _("Up we go..."), pos, LAYER_GUI, Climbable::text_color);
99     context.pop_transform();
100   }
101 }
102
103 void
104 Climbable::event(Player& player, EventType type)
105 {
106   if ((type == EVENT_ACTIVATE) || (activate_try_timer.started())) {
107     if(player.get_grabbed_object() == NULL){
108       if(may_climb(player)) {
109         climbed_by = &player;
110         player.start_climbing(*this);
111         activate_try_timer.stop();
112       } else {
113         if (type == EVENT_ACTIVATE) activate_try_timer.start(ACTIVATE_TRY_FOR);
114         if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) player.add_velocity(Vector(POSITION_FIX_AX,0));
115         if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) player.add_velocity(Vector(-POSITION_FIX_AX,0));
116         if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) player.add_velocity(Vector(0,POSITION_FIX_AY));
117         if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) player.add_velocity(Vector(0,-POSITION_FIX_AY));
118       }
119     }
120   }
121   if(type == EVENT_LOSETOUCH) {
122     player.stop_climbing(*this);
123     climbed_by = 0;
124   }
125 }
126
127 bool
128 Climbable::may_climb(Player& player) 
129 {
130   if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) return false;
131   if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) return false;
132   if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) return false;
133   if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) return false;
134   return true;
135 }
136
137 IMPLEMENT_FACTORY(Climbable, "climbable");
138