4c338a55f9d35349477f57d0935c0bf6b4cacb63
[supertux.git] / src / trigger / door.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 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 #include <config.h>
20
21 #include "door.h"
22 #include "utils/lispreader.h"
23 #include "utils/lispwriter.h"
24 #include "gameloop.h"
25 #include "resources.h"
26 #include "special/sprite.h"
27 #include "special/sprite_manager.h"
28 #include "video/drawing_context.h"
29 #include "app/globals.h"
30
31 using namespace SuperTux;
32
33 Door::Door(LispReader& reader)
34 {
35   reader.read_float("x", bbox.p1.x);
36   reader.read_float("y", bbox.p1.y);
37   bbox.set_size(32, 64);
38
39   reader.read_string("sector", target_sector);
40   reader.read_string("spawnpoint", target_spawnpoint);
41
42   sprite = sprite_manager->create("door");
43 }
44
45 Door::Door(int x, int y, std::string sector, std::string spawnpoint)
46 {
47   bbox.set_pos(Vector(x, y));
48   bbox.set_size(32, 64);
49   target_sector = sector;
50   target_spawnpoint = spawnpoint;
51
52   sprite = sprite_manager->create("door");
53 }
54
55 Door::~Door()
56 {
57   delete sprite;
58 }
59
60 void
61 Door::write(LispWriter& writer)
62 {
63   writer.start_list("door");
64
65   writer.write_float("x", bbox.p1.x);
66   writer.write_float("y", bbox.p1.y);
67   writer.write_float("width", bbox.get_width());
68   writer.write_float("height", bbox.get_height());
69   
70   writer.write_string("sector", target_sector);
71   writer.write_string("spawnpoint", target_spawnpoint);
72
73   writer.end_list("door");
74 }
75
76 void
77 Door::action(float )
78 {
79   //Check if door animation is complete
80   if(sprite->check_animation()) {
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