Replaced <c*> headers with <*.h>
[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   trigger_red(),
42   trigger_green(),
43   trigger_blue(),
44   solid_time(0), 
45   switch_delay(0),
46   color(),
47   light(1.0f,1.0f,1.0f),
48   center(),
49   black()
50 {
51   set_group(COLGROUP_STATIC);
52   //get color from lisp
53   std::vector<float> vColor;
54   lisp.get("color", vColor );
55   color = Color( vColor );
56
57   //all alpha to make the sprite still visible
58   color.alpha = ALPHA_SOLID;
59
60   //set trigger
61   if(color.red == 0 && color.green == 0 && color.blue == 0) { //is it black?
62     black = true;
63     trigger_red = MIN_INTENSITY;
64     trigger_green = MIN_INTENSITY;
65     trigger_blue = MIN_INTENSITY;
66   } else {
67     black = false;
68     trigger_red = (color.red == 1.0f ? MIN_INTENSITY : 0);
69     trigger_green = (color.green == 1.0f ? MIN_INTENSITY : 0);
70     trigger_blue = (color.blue == 1.0f ? MIN_INTENSITY : 0);
71   }
72
73   center = get_bbox().get_middle();
74 }
75
76 void
77 MagicBlock::update(float elapsed_time)
78 {
79   //Check if center of this block is on screen.
80   //Don't update if not, because there is no light off screen.
81   float screen_left = Sector::current()->camera->get_translation().x;
82   float screen_top = Sector::current()->camera->get_translation().y;
83   float screen_right = screen_left+ SCREEN_WIDTH;
84   float screen_bottom = screen_top + SCREEN_HEIGHT;
85   if((center.x > screen_right ) || ( center.y > screen_bottom) ||
86      ( center.x < screen_left) || ( center.y < screen_top)) {
87     switch_delay = SWITCH_DELAY;
88     return;
89   }
90
91   bool lighting_ok;
92   if(black) {
93     lighting_ok = (light.red >= trigger_red || light.green >= trigger_green
94                    || light.blue >= trigger_blue);
95   }else{
96     lighting_ok = (light.red >= trigger_red && light.green >= trigger_green
97                    && light.blue >= trigger_blue);
98   }
99
100   // overrule lighting_ok if switch_delay has not yet passed
101   if (lighting_ok == is_solid) {
102     switch_delay = SWITCH_DELAY;
103   } else {
104     if (switch_delay > 0) {
105       lighting_ok = is_solid;
106       switch_delay -= elapsed_time;
107     }
108   }
109
110   if (lighting_ok) {
111     // lighting suggests going solid
112
113     if (!is_solid) {
114       if (Sector::current()->is_free_of_movingstatics(get_bbox(), this)) {
115         is_solid = true;
116         solid_time = 0;
117         switch_delay = SWITCH_DELAY;
118       }
119     }
120   } else {
121     // lighting suggests going nonsolid
122
123     if( solid_time >= MIN_SOLIDTIME ){
124       is_solid = false;
125     }
126   }
127
128   //Update Sprite.
129   if(is_solid) {
130     solid_time+=elapsed_time;
131     color.alpha = ALPHA_SOLID;
132     sprite->set_action("solid");
133   } else {
134     color.alpha = ALPHA_NONSOLID;
135     sprite->set_action("normal");
136   }
137 }
138
139 void
140 MagicBlock::draw(DrawingContext& context){
141   //Ask for update about lightmap at center of this block
142   context.get_light( center, &light );
143
144   //Draw the Sprite.
145   MovingSprite::draw(context);
146   //Add the color.
147   context.draw_filled_rect( get_bbox(), color, layer);
148 }
149
150 bool
151 MagicBlock::collides(GameObject& /*other*/, const CollisionHit& /*hit*/)
152 {
153   return is_solid;
154 }
155
156 HitResponse
157 MagicBlock::collision(GameObject& /*other*/, const CollisionHit& /*hit*/)
158 {
159   return SOLID;
160 }
161
162 IMPLEMENT_FACTORY(MagicBlock, "magicblock");
163
164 /* EOF */