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