Changed door to only teleport Tux when he is standing in the doorway
[supertux.git] / src / trigger / door.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 "door.hpp"
23 #include "game_session.hpp"
24 #include "resources.hpp"
25 #include "object_factory.hpp"
26 #include "sprite/sprite.hpp"
27 #include "sprite/sprite_manager.hpp"
28 #include "video/drawing_context.hpp"
29 #include "lisp/lisp.hpp"
30 #include "lisp/writer.hpp"
31
32 Door::Door(const lisp::Lisp& reader)
33         : state(CLOSED)
34 {
35   reader.get("x", bbox.p1.x);
36   reader.get("y", bbox.p1.y);
37   reader.get("sector", target_sector);
38   reader.get("spawnpoint", target_spawnpoint);
39
40   sprite = sprite_manager->create("images/objects/door/door.sprite");
41   sprite->set_action("closed");
42   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());  
43 }
44
45 Door::Door(int x, int y, std::string sector, std::string spawnpoint)
46         : state(CLOSED)
47 {
48   bbox.set_pos(Vector(x, y));
49   target_sector = sector;
50   target_spawnpoint = spawnpoint;
51
52   sprite = sprite_manager->create("images/objects/door/door.sprite");
53   sprite->set_action("closed");
54   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());  
55 }
56
57 Door::~Door()
58 {
59   delete sprite;
60 }
61
62 void
63 Door::write(lisp::Writer& writer)
64 {
65   writer.start_list("door");
66
67   writer.write_float("x", bbox.p1.x);
68   writer.write_float("y", bbox.p1.y);
69   writer.write_float("width", bbox.get_width());
70   writer.write_float("height", bbox.get_height());
71   
72   writer.write_string("sector", target_sector);
73   writer.write_string("spawnpoint", target_spawnpoint);
74
75   writer.end_list("door");
76 }
77
78 void
79 Door::update(float )
80 {
81   switch (state) {
82     case CLOSED:
83       break;
84     case OPENING:
85       if(sprite->animation_done()) {
86         state = OPEN;
87         sprite->set_action("open");
88         stay_open_timer.start(1.0);
89       }
90       break;
91     case OPEN:
92       if (stay_open_timer.check()) {
93         state = CLOSING;
94         sprite->set_action("closing", 1);
95       }
96       break;
97     case CLOSING:
98       if(sprite->animation_done()) {
99         state = CLOSED;
100         sprite->set_action("closed");
101       }
102       break;
103   }
104 }
105
106 void
107 Door::draw(DrawingContext& context)
108 {
109   sprite->draw(context, bbox.p1, LAYER_TILES);
110 }
111
112 void
113 Door::event(Player& who, EventType type)
114 {
115   switch (state) {
116     case CLOSED:
117       if (type == EVENT_ACTIVATE) {
118         state = OPENING;
119         sprite->set_action("opening", 1);
120       }
121       break;
122     case OPENING:
123       break;
124     case OPEN:
125       break;
126     case CLOSING:
127       break;
128   }
129 }
130
131 HitResponse
132 Door::collision(GameObject& other, const CollisionHit& hit)
133 {
134   switch (state) {
135     case CLOSED:
136       break;
137     case OPENING:
138       break;
139     case OPEN:
140       {
141         Player* player = dynamic_cast<Player*> (&other);
142         if (player) {
143           state = CLOSING;
144           sprite->set_action("closing", 1);
145           GameSession::current()->respawn(target_sector, target_spawnpoint);
146         }
147       }
148       break;
149     case CLOSING:
150       break;
151   }
152
153   return TriggerBase::collision(other, hit);
154 }
155
156 IMPLEMENT_FACTORY(Door, "door");