X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fobject%2Fmagicblock.cpp;h=99135de94b0623324e634d3372c8cd5295a66659;hb=555d1b7bebb45326d82d934e07463209837309b0;hp=ef8d5b346a7ed71281e37ea42cc65cae906c7791;hpb=ab9eab4c870195c2b60ce76b77044c35b31e8806;p=supertux.git diff --git a/src/object/magicblock.cpp b/src/object/magicblock.cpp index ef8d5b346..99135de94 100644 --- a/src/object/magicblock.cpp +++ b/src/object/magicblock.cpp @@ -1,8 +1,8 @@ -// $Id:$ +// $Id$ // // SuperTux - MagicBlock // -// Magic Blocks are tile-like game objects that are sensitive to +// Magic Blocks are tile-like game objects that are sensitive to // lighting conditions. They are rendered in a color and // will only be solid as long as light of the same color shines // on the block. @@ -26,19 +26,24 @@ #include #include +#include "object/camera.hpp" #include "magicblock.hpp" #include "object_factory.hpp" #include "sprite/sprite_manager.hpp" +#include "sector.hpp" +#include "main.hpp" namespace { - const float MIN_INTENSITY = 0.8; - const float ALPHA_SOLID = 0.7; - const float ALPHA_NONSOLID = 0.3; + const float MIN_INTENSITY = 0.8f; + const float ALPHA_SOLID = 0.7f; + const float ALPHA_NONSOLID = 0.3f; + const float MIN_SOLIDTIME = 1.0f; + const float SWITCH_DELAY = 0.1f; /**< seconds to wait for stable conditions until switching solidity */ } MagicBlock::MagicBlock(const lisp::Lisp& lisp) - : MovingSprite(lisp, "images/objects/magicblock/magicblock.sprite"), - is_solid(false), light(1.0f,1.0f,1.0f) + : MovingSprite(lisp, "images/objects/magicblock/magicblock.sprite"), + is_solid(false), solid_time(0), switch_delay(0), light(1.0f,1.0f,1.0f) { set_group(COLGROUP_STATIC); //get color from lisp @@ -50,26 +55,76 @@ MagicBlock::MagicBlock(const lisp::Lisp& lisp) color.alpha = ALPHA_SOLID; //set trigger - trigger_red = (color.red == 1.0f ? MIN_INTENSITY : 0); - trigger_green = (color.green == 1.0f ? MIN_INTENSITY : 0); - trigger_blue = (color.blue == 1.0f ? MIN_INTENSITY : 0); + if(color.red == 0 && color.green == 0 && color.blue == 0) { //is it black? + black = true; + trigger_red = MIN_INTENSITY; + trigger_green = MIN_INTENSITY; + trigger_blue = MIN_INTENSITY; + } else { + black = false; + trigger_red = (color.red == 1.0f ? MIN_INTENSITY : 0); + trigger_green = (color.green == 1.0f ? MIN_INTENSITY : 0); + trigger_blue = (color.blue == 1.0f ? MIN_INTENSITY : 0); + } - center = Vector((get_bbox().p1.x+get_bbox().p2.x)/2, - (get_bbox().p1.y+get_bbox().p2.y)/2); + center = get_bbox().get_middle(); } void -MagicBlock::update(float /*elapsed_time*/) +MagicBlock::update(float elapsed_time) { - if(light.red >= trigger_red && light.green >= trigger_green - && light.blue >= trigger_blue) { - is_solid = true; + //Check if center of this block is on screen. + //Don't update if not, because there is no light off screen. + float screen_left = Sector::current()->camera->get_translation().x; + float screen_top = Sector::current()->camera->get_translation().y; + float screen_right = screen_left+ SCREEN_WIDTH; + float screen_bottom = screen_top + SCREEN_HEIGHT; + if((center.x > screen_right ) || ( center.y > screen_bottom) || + ( center.x < screen_left) || ( center.y < screen_top)) { + switch_delay = SWITCH_DELAY; + return; + } + + bool lighting_ok; + if(black) { + lighting_ok = (light.red >= trigger_red || light.green >= trigger_green + || light.blue >= trigger_blue); + }else{ + lighting_ok = (light.red >= trigger_red && light.green >= trigger_green + && light.blue >= trigger_blue); + } + + // overrule lighting_ok if switch_delay has not yet passed + if (lighting_ok == is_solid) { + switch_delay = SWITCH_DELAY; } else { - is_solid = false; + if (switch_delay > 0) { + lighting_ok = is_solid; + switch_delay -= elapsed_time; + } + } + + if (lighting_ok) { + // lighting suggests going solid + + if (!is_solid) { + if (Sector::current()->is_free_of_movingstatics(get_bbox(), this)) { + is_solid = true; + solid_time = 0; + switch_delay = SWITCH_DELAY; + } + } + } else { + // lighting suggests going nonsolid + + if( solid_time >= MIN_SOLIDTIME ){ + is_solid = false; + } } //Update Sprite. if(is_solid) { + solid_time+=elapsed_time; color.alpha = ALPHA_SOLID; sprite->set_action("solid"); } else { @@ -89,14 +144,16 @@ MagicBlock::draw(DrawingContext& context){ context.draw_filled_rect( get_bbox(), color, layer); } +bool +MagicBlock::collides(GameObject& /*other*/, const CollisionHit& /*hit*/) +{ + return is_solid; +} + HitResponse MagicBlock::collision(GameObject& /*other*/, const CollisionHit& /*hit*/) { - if(is_solid) { - return SOLID; - } else { - return PASSTHROUGH; - } + return SOLID; } IMPLEMENT_FACTORY(MagicBlock, "magicblock");