More -Weffc++ cleanup
[supertux.git] / src / object / magicblock.cpp
1 //  SuperTux - MagicBlock
2 //
3 //  Magic Blocks are tile-like game objects that are sensitive to
4 //  lighting conditions. They are rendered in a color and
5 //  will only be solid as long as light of the same color shines
6 //  on the block.
7 //
8 //  Copyright (C) 2006 Wolfgang Becker <uafr@gmx.de>
9 //
10 //  This program is free software: you can redistribute it and/or modify
11 //  it under the terms of the GNU General Public License as published by
12 //  the Free Software Foundation, either version 3 of the License, or
13 //  (at your option) any later version.
14 //
15 //  This program is distributed in the hope that it will be useful,
16 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 //  GNU General Public License for more details.
19 //
20 //  You should have received a copy of the GNU General Public License
21 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 #include "object/camera.hpp"
24 #include "object/magicblock.hpp"
25 #include "sprite/sprite.hpp"
26 #include "supertux/main.hpp"
27 #include "supertux/object_factory.hpp"
28 #include "supertux/sector.hpp"
29
30 namespace {
31 const float MIN_INTENSITY = 0.8f;
32 const float ALPHA_SOLID = 0.7f;
33 const float ALPHA_NONSOLID = 0.3f;
34 const float MIN_SOLIDTIME = 1.0f;
35 const float SWITCH_DELAY = 0.1f; /**< seconds to wait for stable conditions until switching solidity */
36 }
37
38 MagicBlock::MagicBlock(const Reader& lisp) :
39   MovingSprite(lisp, "images/objects/magicblock/magicblock.sprite"),
40   is_solid(false), 
41   solid_time(0), 
42   switch_delay(0), 
43   light(1.0f,1.0f,1.0f)
44 {
45   set_group(COLGROUP_STATIC);
46   //get color from lisp
47   std::vector<float> vColor;
48   lisp.get("color", vColor );
49   color = Color( vColor );
50
51   //all alpha to make the sprite still visible
52   color.alpha = ALPHA_SOLID;
53
54   //set trigger
55   if(color.red == 0 && color.green == 0 && color.blue == 0) { //is it black?
56     black = true;
57     trigger_red = MIN_INTENSITY;
58     trigger_green = MIN_INTENSITY;
59     trigger_blue = MIN_INTENSITY;
60   } else {
61     black = false;
62     trigger_red = (color.red == 1.0f ? MIN_INTENSITY : 0);
63     trigger_green = (color.green == 1.0f ? MIN_INTENSITY : 0);
64     trigger_blue = (color.blue == 1.0f ? MIN_INTENSITY : 0);
65   }
66
67   center = get_bbox().get_middle();
68 }
69
70 void
71 MagicBlock::update(float elapsed_time)
72 {
73   //Check if center of this block is on screen.
74   //Don't update if not, because there is no light off screen.
75   float screen_left = Sector::current()->camera->get_translation().x;
76   float screen_top = Sector::current()->camera->get_translation().y;
77   float screen_right = screen_left+ SCREEN_WIDTH;
78   float screen_bottom = screen_top + SCREEN_HEIGHT;
79   if((center.x > screen_right ) || ( center.y > screen_bottom) ||
80      ( center.x < screen_left) || ( center.y < screen_top)) {
81     switch_delay = SWITCH_DELAY;
82     return;
83   }
84
85   bool lighting_ok;
86   if(black) {
87     lighting_ok = (light.red >= trigger_red || light.green >= trigger_green
88                    || light.blue >= trigger_blue);
89   }else{
90     lighting_ok = (light.red >= trigger_red && light.green >= trigger_green
91                    && light.blue >= trigger_blue);
92   }
93
94   // overrule lighting_ok if switch_delay has not yet passed
95   if (lighting_ok == is_solid) {
96     switch_delay = SWITCH_DELAY;
97   } else {
98     if (switch_delay > 0) {
99       lighting_ok = is_solid;
100       switch_delay -= elapsed_time;
101     }
102   }
103
104   if (lighting_ok) {
105     // lighting suggests going solid
106
107     if (!is_solid) {
108       if (Sector::current()->is_free_of_movingstatics(get_bbox(), this)) {
109         is_solid = true;
110         solid_time = 0;
111         switch_delay = SWITCH_DELAY;
112       }
113     }
114   } else {
115     // lighting suggests going nonsolid
116
117     if( solid_time >= MIN_SOLIDTIME ){
118       is_solid = false;
119     }
120   }
121
122   //Update Sprite.
123   if(is_solid) {
124     solid_time+=elapsed_time;
125     color.alpha = ALPHA_SOLID;
126     sprite->set_action("solid");
127   } else {
128     color.alpha = ALPHA_NONSOLID;
129     sprite->set_action("normal");
130   }
131 }
132
133 void
134 MagicBlock::draw(DrawingContext& context){
135   //Ask for update about lightmap at center of this block
136   context.get_light( center, &light );
137
138   //Draw the Sprite.
139   MovingSprite::draw(context);
140   //Add the color.
141   context.draw_filled_rect( get_bbox(), color, layer);
142 }
143
144 bool
145 MagicBlock::collides(GameObject& /*other*/, const CollisionHit& /*hit*/)
146 {
147   return is_solid;
148 }
149
150 HitResponse
151 MagicBlock::collision(GameObject& /*other*/, const CollisionHit& /*hit*/)
152 {
153   return SOLID;
154 }
155
156 IMPLEMENT_FACTORY(MagicBlock, "magicblock");
157
158 /* EOF */