2 more evil mainloops are gone
[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 {
34   reader.get("x", bbox.p1.x);
35   reader.get("y", bbox.p1.y);
36   bbox.set_size(32, 64);
37
38   reader.get("sector", target_sector);
39   reader.get("spawnpoint", target_spawnpoint);
40
41   sprite = sprite_manager->create("images/objects/door/door.sprite");
42 }
43
44 Door::Door(int x, int y, std::string sector, std::string spawnpoint)
45 {
46   bbox.set_pos(Vector(x, y));
47   bbox.set_size(32, 64);
48   target_sector = sector;
49   target_spawnpoint = spawnpoint;
50
51   sprite = sprite_manager->create("images/objects/door/door.sprite");
52 }
53
54 Door::~Door()
55 {
56   delete sprite;
57 }
58
59 void
60 Door::write(lisp::Writer& writer)
61 {
62   writer.start_list("door");
63
64   writer.write_float("x", bbox.p1.x);
65   writer.write_float("y", bbox.p1.y);
66   writer.write_float("width", bbox.get_width());
67   writer.write_float("height", bbox.get_height());
68   
69   writer.write_string("sector", target_sector);
70   writer.write_string("spawnpoint", target_spawnpoint);
71
72   writer.end_list("door");
73 }
74
75 void
76 Door::update(float )
77 {
78   //Check if door animation is complete
79   if(sprite->animation_done()) {
80     sprite->set_action("normal");
81     GameSession::current()->respawn(target_sector, target_spawnpoint);
82   }
83 }
84
85 void
86 Door::draw(DrawingContext& context)
87 {
88   sprite->draw(context, bbox.p1, LAYER_TILES);
89 }
90
91 void
92 Door::event(Player& , EventType type)
93 {
94   if(type == EVENT_ACTIVATE) {
95     sprite->set_action("open", 1);
96   }
97 }
98
99 IMPLEMENT_FACTORY(Door, "door");