src/object/magicblock.cpp

Go to the documentation of this file.
00001 //  SuperTux - MagicBlock
00002 //
00003 //  Magic Blocks are tile-like game objects that are sensitive to
00004 //  lighting conditions. They are rendered in a color and
00005 //  will only be solid as long as light of the same color shines
00006 //  on the block.
00007 //
00008 //  Copyright (C) 2006 Wolfgang Becker <uafr@gmx.de>
00009 //
00010 //  This program is free software: you can redistribute it and/or modify
00011 //  it under the terms of the GNU General Public License as published by
00012 //  the Free Software Foundation, either version 3 of the License, or
00013 //  (at your option) any later version.
00014 //
00015 //  This program is distributed in the hope that it will be useful,
00016 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //  GNU General Public License for more details.
00019 //
00020 //  You should have received a copy of the GNU General Public License
00021 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00022 
00023 #include "object/camera.hpp"
00024 #include "object/magicblock.hpp"
00025 #include "sprite/sprite.hpp"
00026 #include "supertux/globals.hpp"
00027 #include "supertux/object_factory.hpp"
00028 #include "supertux/sector.hpp"
00029 #include "util/reader.hpp"
00030 
00031 namespace {
00032 const float MIN_INTENSITY = 0.8f;
00033 const float ALPHA_SOLID = 0.7f;
00034 const float ALPHA_NONSOLID = 0.3f;
00035 const float MIN_SOLIDTIME = 1.0f;
00036 const float SWITCH_DELAY = 0.1f; 
00037 }
00038 
00039 MagicBlock::MagicBlock(const Reader& lisp) :
00040   MovingSprite(lisp, "images/objects/magicblock/magicblock.sprite"),
00041   is_solid(false), 
00042   trigger_red(),
00043   trigger_green(),
00044   trigger_blue(),
00045   solid_time(0), 
00046   switch_delay(0),
00047   color(),
00048   light(1.0f,1.0f,1.0f),
00049   center(),
00050   black()
00051 {
00052   set_group(COLGROUP_STATIC);
00053   //get color from lisp
00054   std::vector<float> vColor;
00055   lisp.get("color", vColor );
00056   color = Color( vColor );
00057 
00058   //all alpha to make the sprite still visible
00059   color.alpha = ALPHA_SOLID;
00060 
00061   //set trigger
00062   if(color.red == 0 && color.green == 0 && color.blue == 0) { //is it black?
00063     black = true;
00064     trigger_red = MIN_INTENSITY;
00065     trigger_green = MIN_INTENSITY;
00066     trigger_blue = MIN_INTENSITY;
00067   } else {
00068     black = false;
00069     trigger_red = (color.red == 1.0f ? MIN_INTENSITY : 0);
00070     trigger_green = (color.green == 1.0f ? MIN_INTENSITY : 0);
00071     trigger_blue = (color.blue == 1.0f ? MIN_INTENSITY : 0);
00072   }
00073 
00074   center = get_bbox().get_middle();
00075 }
00076 
00077 void
00078 MagicBlock::update(float elapsed_time)
00079 {
00080   //Check if center of this block is on screen.
00081   //Don't update if not, because there is no light off screen.
00082   float screen_left = Sector::current()->camera->get_translation().x;
00083   float screen_top = Sector::current()->camera->get_translation().y;
00084   float screen_right = screen_left+ SCREEN_WIDTH;
00085   float screen_bottom = screen_top + SCREEN_HEIGHT;
00086   if((center.x > screen_right ) || ( center.y > screen_bottom) ||
00087      ( center.x < screen_left) || ( center.y < screen_top)) {
00088     switch_delay = SWITCH_DELAY;
00089     return;
00090   }
00091 
00092   bool lighting_ok;
00093   if(black) {
00094     lighting_ok = (light.red >= trigger_red || light.green >= trigger_green
00095                    || light.blue >= trigger_blue);
00096   }else{
00097     lighting_ok = (light.red >= trigger_red && light.green >= trigger_green
00098                    && light.blue >= trigger_blue);
00099   }
00100 
00101   // overrule lighting_ok if switch_delay has not yet passed
00102   if (lighting_ok == is_solid) {
00103     switch_delay = SWITCH_DELAY;
00104   } else {
00105     if (switch_delay > 0) {
00106       lighting_ok = is_solid;
00107       switch_delay -= elapsed_time;
00108     }
00109   }
00110 
00111   if (lighting_ok) {
00112     // lighting suggests going solid
00113 
00114     if (!is_solid) {
00115       if (Sector::current()->is_free_of_movingstatics(get_bbox(), this)) {
00116         is_solid = true;
00117         solid_time = 0;
00118         switch_delay = SWITCH_DELAY;
00119       }
00120     }
00121   } else {
00122     // lighting suggests going nonsolid
00123 
00124     if( solid_time >= MIN_SOLIDTIME ){
00125       is_solid = false;
00126     }
00127   }
00128 
00129   //Update Sprite.
00130   if(is_solid) {
00131     solid_time+=elapsed_time;
00132     color.alpha = ALPHA_SOLID;
00133     sprite->set_action("solid");
00134     set_group(COLGROUP_STATIC);
00135   } else {
00136     color.alpha = ALPHA_NONSOLID;
00137     sprite->set_action("normal");
00138     set_group(COLGROUP_DISABLED);
00139   }
00140 }
00141 
00142 void
00143 MagicBlock::draw(DrawingContext& context){
00144   //Ask for update about lightmap at center of this block
00145   context.get_light( center, &light );
00146 
00147   //Draw the Sprite.
00148   MovingSprite::draw(context);
00149   //Add the color.
00150   context.draw_filled_rect( get_bbox(), color, layer);
00151 }
00152 
00153 bool
00154 MagicBlock::collides(GameObject& /*other*/, const CollisionHit& /*hit*/)
00155 {
00156   return is_solid;
00157 }
00158 
00159 HitResponse
00160 MagicBlock::collision(GameObject& /*other*/, const CollisionHit& /*hit*/)
00161 {
00162   return FORCE_MOVE;
00163 }
00164 
00165 /* EOF */

Generated on Mon Jun 9 03:38:19 2014 for SuperTux by  doxygen 1.5.1