88bd9d935307c3cad453fc4453ecc79313f5da5a
[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 "magicblock.hpp"
30 #include "object_factory.hpp"
31 #include "sprite/sprite_manager.hpp"
32 #include "sector.hpp"
33
34 namespace {
35   const float MIN_INTENSITY = 0.8;
36   const float ALPHA_SOLID = 0.7;
37   const float ALPHA_NONSOLID = 0.3;
38   const float MIN_SOLIDTIME = 1.0;
39   const float SWITCH_DELAY = 0.1; /**< seconds to wait for stable conditions until switching solidity */
40 }
41
42 MagicBlock::MagicBlock(const lisp::Lisp& lisp)
43         : MovingSprite(lisp, "images/objects/magicblock/magicblock.sprite"),
44         is_solid(false), solid_time(0), switch_delay(0), light(1.0f,1.0f,1.0f)
45 {
46   set_group(COLGROUP_STATIC);
47   //get color from lisp
48   std::vector<float> vColor;
49   lisp.get_vector("color", vColor );
50   color = Color( vColor );
51
52   //all alpha to make the sprite still visible
53   color.alpha = ALPHA_SOLID;
54
55   //set trigger
56   trigger_red = (color.red == 1.0f ? MIN_INTENSITY : 0);
57   trigger_green = (color.green == 1.0f ? MIN_INTENSITY : 0);
58   trigger_blue = (color.blue == 1.0f ? MIN_INTENSITY : 0);
59
60   center = get_bbox().get_middle();
61 }
62
63 void
64 MagicBlock::update(float elapsed_time)
65 {
66   bool lighting_ok = (light.red >= trigger_red && light.green >= trigger_green 
67       && light.blue >= trigger_blue);
68
69   // overrule lighting_ok if switch_delay has not yet passed
70   if (lighting_ok == is_solid) {
71     switch_delay = SWITCH_DELAY;
72   } else {
73     if (switch_delay > 0) {
74       lighting_ok = is_solid;
75       switch_delay -= elapsed_time;
76     }
77   }
78
79   if (lighting_ok) {
80     // lighting suggests going solid
81
82     if (!is_solid) {
83       //if (Sector::current()->is_free_space(get_bbox(), this)) {
84       is_solid = true;
85       solid_time = 0;
86       switch_delay = SWITCH_DELAY;
87       //}
88     }
89   } else {
90     // lighting suggests going nonsolid
91
92     if( solid_time >= MIN_SOLIDTIME ){
93       is_solid = false;
94     }
95   }
96
97   //Update Sprite.
98   if(is_solid) {
99     solid_time+=elapsed_time;
100     color.alpha = ALPHA_SOLID;
101     sprite->set_action("solid");
102   } else {
103     color.alpha = ALPHA_NONSOLID;
104     sprite->set_action("normal");
105   }
106 }
107
108 void
109 MagicBlock::draw(DrawingContext& context){
110   //Ask for update about lightmap at center of this block
111   context.get_light( center, &light );
112
113   //Draw the Sprite.
114   MovingSprite::draw(context);
115   //Add the color.
116   context.draw_filled_rect( get_bbox(), color, layer);
117 }
118
119 HitResponse
120 MagicBlock::collision(GameObject& /*other*/, const CollisionHit& /*hit*/)
121 {
122   if(is_solid) {
123     return SOLID;
124   } else {
125     return PASSTHROUGH;
126   }
127 }
128
129 IMPLEMENT_FACTORY(MagicBlock, "magicblock");