00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00054 std::vector<float> vColor;
00055 lisp.get("color", vColor );
00056 color = Color( vColor );
00057
00058
00059 color.alpha = ALPHA_SOLID;
00060
00061
00062 if(color.red == 0 && color.green == 0 && color.blue == 0) {
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
00081
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
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
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
00123
00124 if( solid_time >= MIN_SOLIDTIME ){
00125 is_solid = false;
00126 }
00127 }
00128
00129
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
00145 context.get_light( center, &light );
00146
00147
00148 MovingSprite::draw(context);
00149
00150 context.draw_filled_rect( get_bbox(), color, layer);
00151 }
00152
00153 bool
00154 MagicBlock::collides(GameObject& , const CollisionHit& )
00155 {
00156 return is_solid;
00157 }
00158
00159 HitResponse
00160 MagicBlock::collision(GameObject& , const CollisionHit& )
00161 {
00162 return FORCE_MOVE;
00163 }
00164
00165