Fix music not properly fading in again
[supertux.git] / src / object / tilemap.cpp
index 6f20195..9136092 100644 (file)
 #include "util/reader.hpp"
 
 TileMap::TileMap(const TileSet *new_tileset) :
-  tileset(new_tileset), 
+  tileset(new_tileset),
   tiles(),
-  solid(false), 
-  speed_x(1), 
-  speed_y(1), 
+  real_solid(false),
+  effective_solid(false),
+  speed_x(1),
+  speed_y(1),
   width(0),
-  height(0), 
-  z_pos(0), 
+  height(0),
+  z_pos(0),
   offset(Vector(0,0)),
   movement(0,0),
   drawing_effect(NO_EFFECT),
-  alpha(1.0), 
+  alpha(1.0),
   current_alpha(1.0),
   remaining_fade_time(0),
   path(),
@@ -49,17 +50,18 @@ TileMap::TileMap(const TileSet *new_tileset) :
 TileMap::TileMap(const Reader& reader) :
   tileset(),
   tiles(),
-  solid(false), 
-  speed_x(1), 
-  speed_y(1), 
+  real_solid(false),
+  effective_solid(false),
+  speed_x(1),
+  speed_y(1),
   width(-1),
-  height(-1), 
-  z_pos(0), 
+  height(-1),
+  z_pos(0),
   offset(Vector(0,0)),
-  movement(Vector(0,0)), 
+  movement(Vector(0,0)),
   drawing_effect(NO_EFFECT),
-  alpha(1.0), 
-  current_alpha(1.0), 
+  alpha(1.0),
+  current_alpha(1.0),
   remaining_fade_time(0),
   path(),
   walker(),
@@ -69,25 +71,18 @@ TileMap::TileMap(const Reader& reader) :
   assert(tileset != NULL);
 
   reader.get("name",   name);
-  reader.get("z-pos",  z_pos);
-  reader.get("solid",  solid);
+  reader.get("solid",  real_solid);
   reader.get("speed",  speed_x);
   reader.get("speed-y", speed_y);
-  
-  if(solid && ((speed_x != 1) || (speed_y != 1))) {
+
+  z_pos = reader_get_layer (reader, /* default = */ 0);
+
+  if(real_solid && ((speed_x != 1) || (speed_y != 1))) {
     log_warning << "Speed of solid tilemap is not 1. fixing" << std::endl;
     speed_x = 1;
     speed_y = 1;
   }
 
-  if (z_pos > (LAYER_GUI - 100)) {
-    log_warning << "z-pos of "
-      << ((name == "") ? "unnamed tilemap" : name) << " (" << z_pos << ") "
-      << "is too large. "
-      << "Clipping to " << (LAYER_GUI - 100) << "." << std::endl;
-    z_pos = LAYER_GUI - 100;
-  }
-
   const lisp::Lisp* pathLisp = reader.get_lisp("path");
   if (pathLisp) {
     path.reset(new Path());
@@ -106,6 +101,10 @@ TileMap::TileMap(const Reader& reader) :
     current_alpha = alpha;
   }
 
+  /* Initialize effective_solid based on real_solid and current_alpha. */
+  effective_solid = real_solid;
+  update_effective_solid ();
+
   reader.get("width", width);
   reader.get("height", height);
   if(width < 0 || height < 0)
@@ -130,35 +129,38 @@ TileMap::TileMap(const Reader& reader) :
   }
 
   if(empty)
+  {
     log_info << "Tilemap '" << name << "', z-pos '" << z_pos << "' is empty." << std::endl;
+  }
 }
 
-TileMap::TileMap(const TileSet *new_tileset, std::string name, int z_pos,
-                 bool solid, size_t width, size_t height) :
-  tileset(new_tileset), 
+TileMap::TileMap(const TileSet *new_tileset, std::string name_, int z_pos_,
+                 bool solid, size_t width_, size_t height_) :
+  tileset(new_tileset),
   tiles(),
-  solid(solid), 
-  speed_x(1), 
-  speed_y(1), 
+  real_solid(solid),
+  effective_solid(solid),
+  speed_x(1),
+  speed_y(1),
   width(0),
-  height(0), 
-  z_pos(z_pos), 
+  height(0),
+  z_pos(z_pos_),
   offset(Vector(0,0)),
   movement(Vector(0,0)),
-  drawing_effect(NO_EFFECT), 
-  alpha(1.0), 
+  drawing_effect(NO_EFFECT),
+  alpha(1.0),
   current_alpha(1.0),
-  remaining_fade_time(0), 
+  remaining_fade_time(0),
   path(),
   walker(),
   draw_target(DrawingContext::NORMAL)
 {
-  this->name = name;
+  this->name = name_;
 
   if (this->z_pos > (LAYER_GUI - 100))
     this->z_pos = LAYER_GUI - 100;
 
-  resize(width, height);
+  resize(width_, height_);
 }
 
 TileMap::~TileMap()
@@ -178,15 +180,14 @@ TileMap::update(float elapsed_time)
       if (amt > 0) current_alpha = std::min(current_alpha + amt, alpha);
       if (amt < 0) current_alpha = std::max(current_alpha + amt, alpha);
     }
-    if ((alpha < 0.25) && (current_alpha < 0.25)) set_solid(false);
-    if ((alpha > 0.75) && (current_alpha > 0.75)) set_solid(true);
+    update_effective_solid ();
   }
 
   movement = Vector(0,0);
   // if we have a path to follow, follow it
   if (walker.get()) {
     Vector v = walker->advance(elapsed_time);
-    movement = Vector(v.x-get_offset().x, std::max(0.0f,v.y-get_offset().y));
+    movement = v - get_offset();
     set_offset(v);
   }
 }
@@ -295,7 +296,8 @@ TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
     z_pos = LAYER_GUI - 100;
   else
     z_pos  = new_z_pos;
-  solid  = newsolid;
+  real_solid  = newsolid;
+  update_effective_solid ();
 
   // make sure all tiles are loaded
   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
@@ -350,7 +352,8 @@ TileMap::get_tiles_overlapping(const Rectf &rect) const
 void
 TileMap::set_solid(bool solid)
 {
-  this->solid = solid;
+  this->real_solid = solid;
+  update_effective_solid ();
 }
 
 uint32_t
@@ -413,27 +416,39 @@ TileMap::change_all(uint32_t oldtile, uint32_t newtile)
 }
 
 void
-TileMap::fade(float alpha, float seconds)
+TileMap::fade(float alpha_, float seconds)
 {
-  this->alpha = alpha;
+  this->alpha = alpha_;
   this->remaining_fade_time = seconds;
 }
 
-void 
-TileMap::set_alpha(float alpha)
+void
+TileMap::set_alpha(float alpha_)
 {
-  this->alpha = alpha;
+  this->alpha = alpha_;
   this->current_alpha = alpha;
   this->remaining_fade_time = 0;
-  if (current_alpha < 0.25) set_solid(false);
-  if (current_alpha > 0.75) set_solid(true);
+  update_effective_solid ();
 }
 
-float 
+float
 TileMap::get_alpha()
 {
   return this->current_alpha;
 }
+
+/*
+ * Private methods
+ */
+void
+TileMap::update_effective_solid (void)
+{
+  if (!real_solid)
+    effective_solid = false;
+  else if (effective_solid && (current_alpha < .25))
+    effective_solid = false;
+  else if (!effective_solid && (current_alpha >= .75))
+    effective_solid = true;
+}
 
 /* EOF */