Candle affects lightmap
[supertux.git] / src / object / candle.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.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 "candle.hpp"
23 #include "scripting/candle.hpp"
24 #include "scripting/squirrel_util.hpp"
25 #include "sector.hpp"
26 #include "object/sprite_particle.hpp"
27 #include "object_factory.hpp"
28 #include "random_generator.hpp"
29
30 Candle::Candle(const lisp::Lisp& lisp)
31         : MovingSprite(lisp, "images/objects/candle/candle.sprite", LAYER_BACKGROUNDTILES+1, COLGROUP_DISABLED), burning(true),
32         candle_light_1("images/objects/candle/candle-light-1.png"),
33         candle_light_2("images/objects/candle/candle-light-2.png")
34 {
35   lisp.get("name", name);
36   lisp.get("burning", burning);
37
38   if (burning) {
39     sprite->set_action("on");
40   } else {
41     sprite->set_action("off");
42   }
43
44 }
45
46 void
47 Candle::draw(DrawingContext& context)
48 {
49   // draw regular sprite
50   sprite->draw(context, get_pos(), layer);
51
52   // draw on lightmap
53   if (burning) {
54     Vector pos = get_pos() + (bbox.get_size() - candle_light_1.get_size()) / 2;
55     context.push_target();
56     context.set_target(DrawingContext::LIGHTMAP);
57     // draw approx. 1 in 10 frames darker. Makes the candle flicker
58     if (systemRandom.rand(10) != 0) {
59       context.draw_surface(&candle_light_1, pos, layer);
60     } else {
61       context.draw_surface(&candle_light_2, pos, layer);
62     }
63     context.pop_target();
64   }
65 }
66
67 HitResponse
68 Candle::collision(GameObject&, const CollisionHit& )
69 {
70   return FORCE_MOVE;
71 }
72
73 void
74 Candle::expose(HSQUIRRELVM vm, SQInteger table_idx)
75 {
76   if (name.empty()) return;
77   Scripting::Candle* interface = new Scripting::Candle(this);
78   expose_object(vm, table_idx, interface, name, true);
79 }
80
81 void
82 Candle::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
83 {
84   if (name.empty()) return;
85   Scripting::unexpose_object(vm, table_idx, name);
86 }
87
88 void
89 Candle::puff_smoke()
90 {
91   Vector ppos = bbox.get_middle();
92   Vector pspeed = Vector(0, -150);
93   Vector paccel = Vector(0,0);
94   Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_BACKGROUNDTILES+2));
95 }
96
97 bool
98 Candle::get_burning()
99 {
100   return burning;
101 }
102
103 void
104 Candle::set_burning(bool burning)
105 {
106   if (this->burning == burning) return;
107   this->burning = burning;
108   if (burning) {
109     sprite->set_action("on");
110     puff_smoke();
111   } else {
112     sprite->set_action("off");
113     puff_smoke();
114   }
115 }
116
117 IMPLEMENT_FACTORY(Candle, "candle");