src/object/candle.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
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   // draw regular sprite
00046   sprite->draw(context, get_pos(), layer);
00047 
00048   // draw on lightmap
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     // draw approx. 1 in 10 frames darker. Makes the candle flicker
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 /* EOF */

Generated on Mon Jun 9 03:38:19 2014 for SuperTux by  doxygen 1.5.1