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