Added drawing of statistics on levels messages and game over.
authorRicardo Cruz <rick2@aeiou.pt>
Wed, 15 Sep 2004 21:59:30 +0000 (21:59 +0000)
committerRicardo Cruz <rick2@aeiou.pt>
Wed, 15 Sep 2004 21:59:30 +0000 (21:59 +0000)
Also fixed a couple of things on statistics.

SVN-Revision: 1921

src/gameloop.cpp
src/gameloop.h
src/statistics.cpp
src/statistics.h
src/worldmap.cpp

index 697d9f5..25054ad 100644 (file)
@@ -78,9 +78,10 @@ if(haystack.compare(haystack_size-needle_size, needle_size, needle) == 0)
 return false;
 }
 
-GameSession::GameSession(const std::string& levelname_, int mode, bool flip_level_)
+GameSession::GameSession(const std::string& levelname_, int mode, bool flip_level_, Statistics* statistics)
   : level(0), currentsector(0), st_gl_mode(mode),
-    end_sequence(NO_ENDSEQUENCE), levelname(levelname_), flip_level(flip_level_)
+    end_sequence(NO_ENDSEQUENCE), levelname(levelname_), flip_level(flip_level_),
+    best_level_statistics(statistics)
 {
   current_ = this;
   
@@ -184,7 +185,7 @@ GameSession::levelintro(void)
   if(level->get_author().size())
     context.draw_text_center(white_small_text,
       std::string(_("by ")) + level->get_author(), 
-      Vector(0, 400), LAYER_FOREGROUND1);
+      Vector(0, 360), LAYER_FOREGROUND1);
 
 
   if(flip_level)
@@ -192,6 +193,9 @@ GameSession::levelintro(void)
       _("Level Vertically Flipped!"),
       Vector(0, 310), LAYER_FOREGROUND1);
 
+  if(best_level_statistics != NULL)
+    best_level_statistics->draw_message_info(context, "Best Level Statistics");
+
   context.do_drawing();
 
   SDL_Event event;
@@ -546,7 +550,7 @@ GameSession::check_end_conditions()
       tux->invincible_timer.start(7000); //FIXME: Implement a winning timer for the end sequence (with special winning animation etc.)
 
       // add left time to stats
-      global_stats.set_points(TIME_NEEDED_STAT, time_left.get_gone());
+      global_stats.set_points(TIME_NEEDED_STAT, time_left.get_gone() / 1000);
     }
   else if (!end_sequence && tux->is_dead())
     {
index c72a3a1..1bc6b10 100644 (file)
@@ -45,6 +45,7 @@ extern int game_started;
 
 class Level;
 class Sector;
+class Statistics;
 
 namespace SuperTux {
 class DrawingContext;
@@ -95,7 +96,7 @@ public:
   DrawingContext* context;
   Timer time_left;
 
-  GameSession(const std::string& level, int mode, bool flip_level_ = false);
+  GameSession(const std::string& level, int mode, bool flip_level_ = false, Statistics* statistics = NULL);
   ~GameSession();
 
   /** Enter the busy loop */
@@ -134,6 +135,8 @@ private:
 
   void on_escape_press();
   void process_menu();
+
+  Statistics* best_level_statistics;
 };
 
 std::string slotinfo(int slot);
index 0aa3f57..79db1a1 100644 (file)
@@ -44,6 +44,16 @@ stat_name_to_string(int stat_enum)
     }
 }
 
+int
+my_min(int a, int b)
+{
+if(a == -1)
+  return b;
+if(b == -1)
+  return a;
+return std::min(a, b);
+}
+
 Statistics::Statistics()
 {
   timer.init(true);
@@ -77,6 +87,9 @@ Statistics::write(LispWriter& writer)
 void
 Statistics::draw_worldmap_info(DrawingContext& context)
 {
+  if(stats[SCORE_STAT] == -1)  // not initialized yet
+    return;
+
   if(!timer.check())
     {
     timer.start(TOTAL_DISPLAY_TIME);
@@ -113,9 +126,29 @@ Statistics::draw_worldmap_info(DrawingContext& context)
 }
 
 void
-Statistics::draw_message_info(DrawingContext& context)
+Statistics::draw_message_info(DrawingContext& context, std::string title)
 {
-  // TODO
+  if(stats[SCORE_STAT] == -1)  // not initialized yet
+    return;
+
+  context.draw_text_center(gold_text, title, Vector(0, 400), LAYER_GUI);
+
+  char str[128];
+  for(int i = 0; i < NUM_STATS; i++)
+    {
+    if(i == SCORE_STAT)
+      sprintf(str, _("Max score: %d"), stats[SCORE_STAT]);
+    else if(i == BADGUYS_SQUISHED_STAT)
+      sprintf(str, _("Max fragging: %d"), stats[BADGUYS_SQUISHED_STAT]);
+    else if(i == SHOTS_STAT)
+      sprintf(str, _("Min shots: %d"), stats[SHOTS_STAT]);
+    else if(i == TIME_NEEDED_STAT)
+      sprintf(str, _("Min time needed: %d"), stats[TIME_NEEDED_STAT]);
+    else// if(i == JUMPS_STAT)
+      sprintf(str, _("Min jumps: %d"), stats[JUMPS_STAT]);
+
+    context.draw_text_center(white_text, str, Vector(0, 430 + i*22), LAYER_GUI);
+    }
 }
 
 void
@@ -147,12 +180,13 @@ void
 Statistics::merge(Statistics& stats_)
 {
   stats[SCORE_STAT] = std::max(stats[SCORE_STAT], stats_.stats[SCORE_STAT]);
-  stats[JUMPS_STAT] = std::min(stats[JUMPS_STAT], stats_.stats[JUMPS_STAT]);
+  if(stats[JUMPS_STAT] != -1)
+    stats[JUMPS_STAT] = my_min(stats[JUMPS_STAT], stats_.stats[JUMPS_STAT]);
   stats[BADGUYS_SQUISHED_STAT] =
     std::max(stats[BADGUYS_SQUISHED_STAT], stats_.stats[BADGUYS_SQUISHED_STAT]);
-  stats[SHOTS_STAT] = std::min(stats[SHOTS_STAT], stats_.stats[SHOTS_STAT]);
+  stats[SHOTS_STAT] = my_min(stats[SHOTS_STAT], stats_.stats[SHOTS_STAT]);
   stats[TIME_NEEDED_STAT] =
-    std::min(stats[TIME_NEEDED_STAT], stats_.stats[TIME_NEEDED_STAT]);
+    my_min(stats[TIME_NEEDED_STAT], stats_.stats[TIME_NEEDED_STAT]);
 }
 
 void
index 8ffb4e4..72250c3 100644 (file)
@@ -57,7 +57,7 @@ public:
   /* Draw to the worldmap or a game message */
   // TODO: make this functions working
   void draw_worldmap_info(DrawingContext& context);
-  void draw_message_info(DrawingContext& context);
+  void draw_message_info(DrawingContext& context, std::string title);
 
   /* Add / Set / Get points to/from one of the stats this can keep track of */
   void add_points(int stat, int points);
index 65d45d4..5c09101 100644 (file)
@@ -834,7 +834,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())
                 {
@@ -908,18 +909,20 @@ WorldMap::update(float delta)
                   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);
+//                  sprintf(str, _("SCORE: %d"), total_stats.get_points(SCORE_STAT));
+//                  context.draw_text_center(gold_text, str,
+//                      Vector(0, 230), LAYER_FOREGROUND1);
 
                   sprintf(str, _("COINS: %d"), player_status.distros);
                   context.draw_text_center(gold_text, str,
                       Vector(0, screen->w - 32), 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();