Only show combo messages after 5 combos, as suggested by guys from IRC.
[supertux.git] / src / worldmap.cpp
index 65d45d4..9fe482f 100644 (file)
@@ -115,13 +115,12 @@ TileManager::TileManager()
           if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
             {
               int id = 0;
-              std::string filename = "<invalid>";
 
               Tile* tile = new Tile;             
               tile->north = tile->east = tile->south = tile->west = true;
               tile->stop  = true;
               tile->auto_walk = false;
-  
+
               LispReader reader(lisp_cdr(element));
               reader.read_int("id", id);
 
@@ -148,7 +147,6 @@ TileManager::TileManager()
 
               reader.read_bool("stop",  tile->stop);
               reader.read_bool("auto-walk",  tile->auto_walk);
-              reader.read_string("image", filename);
 
               reader.read_string("one-way", temp);
               tile->one_way = BOTH_WAYS;
@@ -164,9 +162,23 @@ TileManager::TileManager()
                   tile->one_way = WEST_EAST_WAY;
                 }
 
-              tile->sprite = new Surface(
-                           datadir +  "/images/worldmap/" + filename, 
-                           true);
+              std::vector<std::string> filenames;
+              reader.read_string_vector("image", filenames);
+
+              if(filenames.size() == 0)
+                std::cerr << "Warning: no image specified for tile " << id
+                          << ".\nIgnoring...\n" << std::endl;
+
+              for(int i = 0; i < filenames.size(); i++)
+                {
+                Surface* image = new Surface(
+                         datadir +  "/images/worldmap/" + filenames[i], true);
+                tile->images.push_back(image);
+                }
+
+              tile->anim_speed = 25;
+              reader.read_int("anim-speed", tile->anim_speed);
+
 
               if (id >= int(tiles.size()))
                 tiles.resize(id+1);
@@ -423,7 +435,31 @@ Tile::Tile()
 
 Tile::~Tile()
 {
-  delete sprite;
+  for(std::vector<Surface*>::iterator i = images.begin(); i != images.end(); i++)
+    delete *i;
+}
+
+
+void
+Tile::draw(DrawingContext& context, Vector pos)
+{
+  // same code as from tile_manager.cpp draw_tile()
+
+  if(!images.size())
+    return;
+
+  if(images.size() > 1)
+    {
+    size_t frame 
+      = ((global_frame_counter*25) / anim_speed) % images.size();
+
+std::cerr << "frame: " << frame << std::endl;
+    context.draw_surface(images[frame], pos, LAYER_TILES);
+    }
+  else if (images.size() == 1)
+    {
+    context.draw_surface(images[0], pos, LAYER_TILES);
+    }
 }
 
 //---------------------------------------------------------------------------
@@ -448,6 +484,11 @@ WorldMap::WorldMap()
 
   name = "<no title>";
   music = "SALCON.MOD";
+
+  global_frame_counter = 0;
+  frame_timer.init(true);
+
+  total_stats.reset();
 }
 
 WorldMap::~WorldMap()
@@ -461,6 +502,7 @@ WorldMap::~WorldMap()
   delete teleporterdot;
 }
 
+// Don't forget to set map_filename before calling this
 void
 WorldMap::load_map()
 {
@@ -810,6 +852,12 @@ std::cerr << "one way only\n";
 void
 WorldMap::update(float delta)
 {
+  if(!frame_timer.check())
+    {
+    frame_timer.start(25);
+    global_frame_counter++;
+    }
+
   if (enter_level && !tux->is_moving())
     {
       bool level_finished = true;
@@ -834,7 +882,8 @@ WorldMap::update(float delta)
               shrink_fade(Vector((special_tile->x*32 + 16 + offset.x),(special_tile->y*32 + 16
                       + offset.y)), 500);
               GameSession session(datadir +  "/levels/" + special_tile->level_name,
-                                  ST_GL_LOAD_LEVEL_FILE, special_tile->vertical_flip);
+                                  ST_GL_LOAD_LEVEL_FILE, special_tile->vertical_flip,
+                                  &special_tile->statistics);
 
               switch (session.run())
                 {
@@ -905,21 +954,19 @@ WorldMap::update(float delta)
                   context.draw_gradient(Color (200,240,220), Color(200,200,220),
                       LAYER_BACKGROUND0);
 
-                  context.draw_text_center(blue_text, _("GAMEOVER"), 
-                      Vector(0, 200), LAYER_FOREGROUND1);
-
-                  sprintf(str, _("SCORE: %d"), total_stats.get_points(SCORE_STAT));
-                  context.draw_text_center(gold_text, str,
-                      Vector(0, 230), LAYER_FOREGROUND1);
+                  context.draw_text(blue_text, _("GAMEOVER"), 
+                      Vector(screen->w/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
 
                   sprintf(str, _("COINS: %d"), player_status.distros);
-                  context.draw_text_center(gold_text, str,
-                      Vector(0, screen->w - 32), LAYER_FOREGROUND1);
+                  context.draw_text(gold_text, str,
+                      Vector(screen->w/2, screen->w - 32), CENTER_ALLIGN, LAYER_FOREGROUND1);
+
+                  total_stats.draw_message_info(context, _("Total Statistics"));
 
                   context.do_drawing();
   
                   SDL_Event event;
-                  wait_for_event(event,2000,5000,true);
+                  wait_for_event(event,2000,6000,true);
 
                   quit = true;
                   player_status.reset();
@@ -1026,8 +1073,7 @@ WorldMap::draw(DrawingContext& context, const Vector& offset)
     for(int x = 0; x < width; ++x)
       {
         Tile* tile = at(Vector(x, y));
-        context.draw_surface(tile->sprite,
-            Vector(x*32 + offset.x, y*32 + offset.y), LAYER_TILES);
+        tile->draw(context, Vector(x*32 + offset.x, y*32 + offset.y));
       }
   
   for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
@@ -1065,23 +1111,23 @@ WorldMap::draw_status(DrawingContext& context)
   char str[80];
   sprintf(str, " %d", total_stats.get_points(SCORE_STAT));
 
-  context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
-  context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
+  context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
+  context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
 
   sprintf(str, "%d", player_status.distros);
   context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0),
-      LAYER_FOREGROUND1);
+      LEFT_ALLIGN, LAYER_FOREGROUND1);
   context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
-        LAYER_FOREGROUND1);
+        LEFT_ALLIGN, LAYER_FOREGROUND1);
 
   if (player_status.lives >= 5)
     {
       sprintf(str, "%dx", player_status.lives);
       context.draw_text(gold_text, str, 
           Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
-          LAYER_FOREGROUND1);
+          LEFT_ALLIGN, LAYER_FOREGROUND1);
       context.draw_surface(tux_life, Vector(screen->w -
-            gold_text->get_text_width("9"), 0), LAYER_FOREGROUND1);
+            gold_text->get_text_width("9"), 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
     }
   else
     {
@@ -1092,7 +1138,7 @@ WorldMap::draw_status(DrawingContext& context)
     }
   context.draw_text(white_text, _("LIVES"),
       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 0),
-      LAYER_FOREGROUND1);
+      LEFT_ALLIGN, LAYER_FOREGROUND1);
 
   if (!tux->is_moving())
     {
@@ -1106,27 +1152,27 @@ WorldMap::draw_status(DrawingContext& context)
                 if(i->title == "")
                   get_level_title(*i);
 
-                context.draw_text_center(white_text, i->title, 
-                    Vector(0, screen->h - white_text->get_height() - 30),
-                    LAYER_FOREGROUND1);
+                context.draw_text(white_text, i->title, 
+                    Vector(screen->w/2, screen->h - white_text->get_height() - 30),
+                    CENTER_ALLIGN, LAYER_FOREGROUND1);
 
                 i->statistics.draw_worldmap_info(context);
                 }
 
               /* Display an in-map message in the map, if any as been selected */
               if(!i->map_message.empty() && !i->passive_message)
-                context.draw_text_center(gold_text, i->map_message, 
-                    Vector(0, screen->h - white_text->get_height() - 60),
-                    LAYER_FOREGROUND1);
+                context.draw_text(gold_text, i->map_message, 
+                    Vector(screen->w/2, screen->h - white_text->get_height() - 60),
+                    CENTER_ALLIGN, LAYER_FOREGROUND1);
               break;
             }
         }
     }
   /* Display a passive message in the map, if needed */
   if(passive_message_timer.check())
-    context.draw_text_center(gold_text, passive_message, 
-            Vector(0, screen->h - white_text->get_height() - 60),
-            LAYER_FOREGROUND1);
+    context.draw_text(gold_text, passive_message, 
+            Vector(screen->w/2, screen->h - white_text->get_height() - 60),
+            CENTER_ALLIGN, LAYER_FOREGROUND1);
 }
 
 void
@@ -1253,11 +1299,13 @@ WorldMap::loadgame(const std::string& filename)
 {
   std::cout << "loadgame: " << filename << std::endl;
   savegame_file = filename;
-  map_filename = "icyisland.stwm";
 
   if (access(filename.c_str(), F_OK) != 0)
     {
     load_map();
+
+    player_status.reset();
+
     return;
     }
   
@@ -1281,8 +1329,10 @@ WorldMap::loadgame(const std::string& filename)
   LispReader reader(cur);
 
   /* Get the Map filename and then load it before setting special_tile settings */
+  std::string cur_map_filename = map_filename;
   reader.read_string("map", map_filename);
-  load_map(); 
+  if(cur_map_filename != map_filename)
+    load_map(); 
 
   reader.read_int("lives", player_status.lives);
   reader.read_int("distros", player_status.distros);