f4b28a1e773a56e6ebf1bc39401d56a66d736ff5
[supertux.git] / src / object / candle.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "math/random_generator.hpp"
18 #include "object/candle.hpp"
19 #include "object/sprite_particle.hpp"
20 #include "scripting/candle.hpp"
21 #include "scripting/squirrel_util.hpp"
22 #include "supertux/object_factory.hpp"
23 #include "supertux/sector.hpp"
24 #include "util/reader.hpp"
25
26 Candle::Candle(const Reader& lisp)
27   : MovingSprite(lisp, "images/objects/candle/candle.sprite", LAYER_BACKGROUNDTILES+1, COLGROUP_DISABLED), burning(true),
28     candle_light_1("images/objects/candle/candle-light-1.png"),
29     candle_light_2("images/objects/candle/candle-light-2.png")
30 {
31   lisp.get("name", name);
32   lisp.get("burning", burning);
33
34   if (burning) {
35     sprite->set_action("on");
36   } else {
37     sprite->set_action("off");
38   }
39
40 }
41
42 void
43 Candle::draw(DrawingContext& context)
44 {
45   // draw regular sprite
46   sprite->draw(context, get_pos(), layer);
47
48   // draw on lightmap
49   if (burning) {
50     Vector pos = get_pos() + (bbox.get_size() - candle_light_1.get_size()) / 2;
51     context.push_target();
52     context.set_target(DrawingContext::LIGHTMAP);
53     // draw approx. 1 in 10 frames darker. Makes the candle flicker
54     if (systemRandom.rand(10) != 0) {
55       context.draw_surface(&candle_light_1, pos, layer);
56     } else {
57       context.draw_surface(&candle_light_2, pos, layer);
58     }
59     context.pop_target();
60   }
61 }
62
63 HitResponse
64 Candle::collision(GameObject&, const CollisionHit& )
65 {
66   return FORCE_MOVE;
67 }
68
69 void
70 Candle::expose(HSQUIRRELVM vm, SQInteger table_idx)
71 {
72   if (name.empty()) return;
73   Scripting::Candle* interface = new Scripting::Candle(this);
74   expose_object(vm, table_idx, interface, name, true);
75 }
76
77 void
78 Candle::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
79 {
80   if (name.empty()) return;
81   Scripting::unexpose_object(vm, table_idx, name);
82 }
83
84 void
85 Candle::puff_smoke()
86 {
87   Vector ppos = bbox.get_middle();
88   Vector pspeed = Vector(0, -150);
89   Vector paccel = Vector(0,0);
90   Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_BACKGROUNDTILES+2));
91 }
92
93 bool
94 Candle::get_burning()
95 {
96   return burning;
97 }
98
99 void
100 Candle::set_burning(bool burning)
101 {
102   if (this->burning == burning) return;
103   this->burning = burning;
104   if (burning) {
105     sprite->set_action("on");
106     puff_smoke();
107   } else {
108     sprite->set_action("off");
109     puff_smoke();
110   }
111 }
112
113 IMPLEMENT_FACTORY(Candle, "candle");
114
115 /* EOF */