updating Nolok contrib templates
[supertux.git] / src / trigger / hatch.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 "hatch.h"
22 #include "gameloop.h"
23 #include "resources.h"
24 #include "object_factory.h"
25 #include "special/sprite.h"
26 #include "special/sprite_manager.h"
27 #include "video/drawing_context.h"
28 #include "app/globals.h"
29 #include "lisp/lisp.h"
30 #include "lisp/writer.h"
31
32 using namespace SuperTux;
33
34 Hatch::Hatch(const lisp::Lisp& reader)
35 {
36   reader.get("x", bbox.p1.x);
37   reader.get("y", bbox.p1.y);
38   bbox.set_size(64, 64);
39
40   reader.get("sector", target_sector);
41   reader.get("spawnpoint", target_spawnpoint);
42
43   sprite = sprite_manager->create("hatch");
44 }
45
46 Hatch::Hatch(int x, int y, std::string sector, std::string spawnpoint)
47 {
48   bbox.set_pos(Vector(x, y));
49   bbox.set_size(64, 64);
50   target_sector = sector;
51   target_spawnpoint = spawnpoint;
52
53   sprite = sprite_manager->create("hatch");
54 }
55
56 Hatch::~Hatch()
57 {
58   delete sprite;
59 }
60
61 void
62 Hatch::write(lisp::Writer& writer)
63 {
64   writer.start_list("hatch");
65
66   writer.write_float("x", bbox.p1.x);
67   writer.write_float("y", bbox.p1.y);
68   writer.write_float("width", bbox.get_width());
69   writer.write_float("height", bbox.get_height());
70   
71   writer.write_string("sector", target_sector);
72   writer.write_string("spawnpoint", target_spawnpoint);
73
74   writer.end_list("hatch");
75 }
76
77 void
78 Hatch::action(float )
79 {
80   //Check if hatch animation is complete
81   if(sprite->check_animation()) {
82     sprite->set_action("normal");
83     GameSession::current()->respawn(target_sector, target_spawnpoint);
84   }
85 }
86
87 void
88 Hatch::draw(DrawingContext& context)
89 {
90   sprite->draw(context, bbox.p1, LAYER_TILES);
91 }
92
93 void
94 Hatch::event(Player& , EventType type)
95 {
96   if(type == EVENT_ACTIVATE) {
97     sprite->set_action("open", 1);
98   }
99 }
100
101 IMPLEMENT_FACTORY(Hatch, "hatch");