Check return value of some lisp.get calls
[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),
28     burning(true),
29     flicker(true),
30     lightcolor(1.0f, 1.0f, 1.0f),
31     candle_light_1(SpriteManager::current()->create("images/objects/candle/candle-light-1.sprite")),
32     candle_light_2(SpriteManager::current()->create("images/objects/candle/candle-light-2.sprite"))
33 {
34
35   if(!lisp.get("name", name))
36   {
37     log_warning << "Couldn't get \"name\" property for candle." << std::endl;
38   }
39   if(!lisp.get("burning", burning))
40   {
41     log_warning << "Couldn't get \"burning\" property for candle." << std::endl;
42   }
43   if(!lisp.get("flicker", flicker))
44   {
45     log_warning << "Couldn't get \"flicker\" property for candle." << std::endl;
46   }
47   //get color from lisp
48   std::vector<float> vColor;
49   if(lisp.get("color", vColor))
50   {
51     log_warning << "Couldn't get \"color\" property for candle." << std::endl;
52   }
53
54   //change the light color if defined
55   if (vColor.size() >= 3) {
56     lightcolor = Color(vColor);
57     candle_light_1->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
58     candle_light_2->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
59     candle_light_1->set_color(lightcolor);
60     candle_light_2->set_color(lightcolor);
61     //the following allows the original candle appearance to be preserved
62     candle_light_1->set_action("white");
63     candle_light_2->set_action("white");
64   }
65
66   if (burning) {
67     sprite->set_action("on");
68   } else {
69     sprite->set_action("off");
70   }
71
72 }
73
74 void
75 Candle::draw(DrawingContext& context)
76 {
77   // draw regular sprite
78   sprite->draw(context, get_pos(), layer);
79
80   // draw on lightmap
81   if (burning) {
82     //Vector pos = get_pos() + (bbox.get_size() - candle_light_1->get_size()) / 2;
83     context.push_target();
84     context.set_target(DrawingContext::LIGHTMAP);
85     // draw approx. 1 in 10 frames darker. Makes the candle flicker
86     if (gameRandom.rand(10) != 0 || !flicker) {
87       //context.draw_surface(candle_light_1, pos, layer);
88       candle_light_1->draw(context, get_bbox().get_middle(), 0);
89     } else {
90       //context.draw_surface(candle_light_2, pos, layer);
91       candle_light_2->draw(context, get_bbox().get_middle(), 0);
92     }
93     context.pop_target();
94   }
95 }
96
97 HitResponse
98 Candle::collision(GameObject&, const CollisionHit& )
99 {
100   return FORCE_MOVE;
101 }
102
103 void
104 Candle::expose(HSQUIRRELVM vm, SQInteger table_idx)
105 {
106   if (name.empty()) return;
107   scripting::Candle* _this = new scripting::Candle(this);
108   expose_object(vm, table_idx, _this, name, true);
109 }
110
111 void
112 Candle::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
113 {
114   if (name.empty()) return;
115   scripting::unexpose_object(vm, table_idx, name);
116 }
117
118 void
119 Candle::puff_smoke()
120 {
121   Vector ppos = bbox.get_middle();
122   Vector pspeed = Vector(0, -150);
123   Vector paccel = Vector(0,0);
124   Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/smoke.sprite",
125                                                                  "default",
126                                                                  ppos, ANCHOR_MIDDLE,
127                                                                  pspeed, paccel,
128                                                                  LAYER_BACKGROUNDTILES+2));
129 }
130
131 bool
132 Candle::get_burning()
133 {
134   return burning;
135 }
136
137 void
138 Candle::set_burning(bool burning_)
139 {
140   if (this->burning == burning_) return;
141   this->burning = burning_;
142   if (burning_) {
143     sprite->set_action("on");
144   } else {
145     sprite->set_action("off");
146   }
147   //puff smoke for flickering light sources only
148   if (flicker) puff_smoke();
149 }
150
151 /* EOF */