Magic Blocks. (DrawingContext::get_light is not working yet.)
[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
33 namespace {
34   const float MIN_INTENSITY = 0.8;
35   const float ALPHA_SOLID = 0.7;
36   const float ALPHA_NONSOLID = 0.3;
37 }
38
39 MagicBlock::MagicBlock(const lisp::Lisp& lisp)
40         : MovingSprite(lisp, "images/objects/magicblock/magicblock.sprite"),
41         is_solid(false), light(1.0f,1.0f,1.0f)
42 {
43   set_group(COLGROUP_STATIC);
44   //get color from lisp
45   std::vector<float> vColor;
46   lisp.get_vector("color", vColor );
47   color = Color( vColor );
48
49   //all alpha to make the sprite still visible
50   color.alpha = ALPHA_SOLID;
51
52   //set trigger
53   trigger_red = (color.red == 1.0f ? MIN_INTENSITY : 0);
54   trigger_green = (color.green == 1.0f ? MIN_INTENSITY : 0);
55   trigger_blue = (color.blue == 1.0f ? MIN_INTENSITY : 0);
56
57   center =  Vector((get_bbox().p1.x+get_bbox().p2.x)/2,  
58                    (get_bbox().p1.y+get_bbox().p2.y)/2);
59 }
60
61 void
62 MagicBlock::update(float /*elapsed_time*/)
63 {
64   if(light.red >= trigger_red && light.green >= trigger_green 
65       && light.blue >= trigger_blue) {
66     is_solid = true;
67   } else {
68     is_solid = false;
69   }
70
71   //Update Sprite.
72   if(is_solid) {
73     color.alpha = ALPHA_SOLID;
74     sprite->set_action("solid");
75   } else {
76     color.alpha = ALPHA_NONSOLID;
77     sprite->set_action("normal");
78   }
79 }
80
81 void
82 MagicBlock::draw(DrawingContext& context){
83   //Ask for update about lightmap at center of this block
84   context.get_light( center, &light );
85
86   //Draw the Sprite.
87   MovingSprite::draw(context);
88   //Add the color.
89   context.draw_filled_rect( get_bbox(), color, layer);
90 }
91
92 HitResponse
93 MagicBlock::collision(GameObject& /*other*/, const CollisionHit& /*hit*/)
94 {
95   if(is_solid) {
96     return SOLID;
97   } else {
98     return PASSTHROUGH;
99   }
100 }
101
102 IMPLEMENT_FACTORY(MagicBlock, "magicblock");