Fixed trailing whitespaces in all(?) source files of supertux, also fixed some svn...
[supertux.git] / src / object / magicblock.cpp
index 0e99687..9d0caf8 100644 (file)
@@ -2,7 +2,7 @@
 //
 //  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.
 #include <config.h>
 #include <vector>
 
+#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_SOLIDTIME = 1.0;
-  const float SWITCH_DELAY = 0.1; /**< seconds to wait for stable conditions until switching solidity */
+  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)
@@ -53,9 +55,17 @@ 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 = get_bbox().get_middle();
 }
@@ -63,8 +73,26 @@ MagicBlock::MagicBlock(const lisp::Lisp& lisp)
 void
 MagicBlock::update(float elapsed_time)
 {
-  bool lighting_ok = (light.red >= trigger_red && light.green >= trigger_green 
+  //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) {