00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "math/random_generator.hpp"
00018 #include "object/candle.hpp"
00019 #include "object/sprite_particle.hpp"
00020 #include "scripting/candle.hpp"
00021 #include "scripting/squirrel_util.hpp"
00022 #include "supertux/object_factory.hpp"
00023 #include "supertux/sector.hpp"
00024 #include "util/reader.hpp"
00025
00026 Candle::Candle(const Reader& lisp)
00027 : MovingSprite(lisp, "images/objects/candle/candle.sprite", LAYER_BACKGROUNDTILES+1, COLGROUP_DISABLED), burning(true),
00028 candle_light_1(Surface::create("images/objects/candle/candle-light-1.png")),
00029 candle_light_2(Surface::create("images/objects/candle/candle-light-2.png"))
00030 {
00031 lisp.get("name", name);
00032 lisp.get("burning", burning);
00033
00034 if (burning) {
00035 sprite->set_action("on");
00036 } else {
00037 sprite->set_action("off");
00038 }
00039
00040 }
00041
00042 void
00043 Candle::draw(DrawingContext& context)
00044 {
00045
00046 sprite->draw(context, get_pos(), layer);
00047
00048
00049 if (burning) {
00050 Vector pos = get_pos() + (bbox.get_size() - candle_light_1->get_size()) / 2;
00051 context.push_target();
00052 context.set_target(DrawingContext::LIGHTMAP);
00053
00054 if (gameRandom.rand(10) != 0) {
00055 context.draw_surface(candle_light_1, pos, layer);
00056 } else {
00057 context.draw_surface(candle_light_2, pos, layer);
00058 }
00059 context.pop_target();
00060 }
00061 }
00062
00063 HitResponse
00064 Candle::collision(GameObject&, const CollisionHit& )
00065 {
00066 return FORCE_MOVE;
00067 }
00068
00069 void
00070 Candle::expose(HSQUIRRELVM vm, SQInteger table_idx)
00071 {
00072 if (name.empty()) return;
00073 scripting::Candle* _this = new scripting::Candle(this);
00074 expose_object(vm, table_idx, _this, name, true);
00075 }
00076
00077 void
00078 Candle::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
00079 {
00080 if (name.empty()) return;
00081 scripting::unexpose_object(vm, table_idx, name);
00082 }
00083
00084 void
00085 Candle::puff_smoke()
00086 {
00087 Vector ppos = bbox.get_middle();
00088 Vector pspeed = Vector(0, -150);
00089 Vector paccel = Vector(0,0);
00090 Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_BACKGROUNDTILES+2));
00091 }
00092
00093 bool
00094 Candle::get_burning()
00095 {
00096 return burning;
00097 }
00098
00099 void
00100 Candle::set_burning(bool burning)
00101 {
00102 if (this->burning == burning) return;
00103 this->burning = burning;
00104 if (burning) {
00105 sprite->set_action("on");
00106 puff_smoke();
00107 } else {
00108 sprite->set_action("off");
00109 puff_smoke();
00110 }
00111 }
00112
00113