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