fix growup, lightmap experiments with willowisp
[supertux.git] / src / title.cpp
index 66ffb3c..f4d6ea0 100644 (file)
 #include "control/codecontroller.hpp"
 #include "main.hpp"
 #include "exceptions.hpp"
+#include "msg.hpp"
 
 static Surface* bkg_title;
 static Surface* logo;
 //static Surface* img_choose_subset;
 
-static bool walking;
-static Timer random_timer;
-
 static int frame;
 
 static GameSession* titlesession;
@@ -92,7 +90,7 @@ void resume_demo()
 
 void update_load_save_game_menu(Menu* menu)
 {
-  printf("update loadsavemenu.\n");
+  msg_debug("update loadsavemenu");
   for(int i = 1; i < 6; ++i) {
     MenuItem& item = menu->get_item_by_id(i);
     item.kind = MN_ACTION;
@@ -142,8 +140,8 @@ void generate_contrib_menu()
       contrib_subsets.push_back(subset.release());
     } catch(std::exception& e) {
 #ifdef DEBUG
-      std::cerr << "Couldn't parse levelset info for '"
-        << *it << "': " << e.what() << "\n";
+      msg_warning("Couldn't parse levelset info for '"
+        << *it << "': " << e.what() << "");
 #endif
     }
   }
@@ -166,7 +164,7 @@ std::string get_level_name(const std::string& filename)
     level->get("name", name);
     return name;
   } catch(std::exception& e) {
-    std::cerr << "Problem getting name of '" << filename << "'.\n";
+    msg_warning("Problem getting name of '" << filename << "'.");
     return "";
   }
 }
@@ -242,8 +240,12 @@ void check_contrib_subset_menu()
 
 void draw_demo(float elapsed_time)
 {
+  static Timer randomWaitTimer;
+  static Timer jumpPushTimer;
+  static Timer jumpRecoverTimer;
   static float last_tux_x_pos = -1;
   static float last_tux_y_pos = -1;
+
   Sector* sector  = titlesession->get_current_sector();
   Player* tux = sector->player;
 
@@ -251,18 +253,31 @@ void draw_demo(float elapsed_time)
 
   controller->update();
   controller->press(Controller::RIGHT);
+
+  // Determine how far we moved since last frame
+  float dx = fabsf(last_tux_x_pos - tux->get_pos().x); 
+  float dy = fabsf(last_tux_y_pos - tux->get_pos().y); 
+  // Calculate space to check for obstacles 
+  Rect lookahead = Rect(tux->get_bbox());
+  lookahead.move(Vector(lookahead.get_width()*2,0));
   
-  if(random_timer.check() || 
-      (walking && fabsf(last_tux_x_pos - tux->get_pos().x)) < .1) {
-    walking = false;
-  } else {
-      if(!walking && fabsf(tux->get_pos().y - last_tux_y_pos) < .1) {
-        random_timer.start(float(rand() % 3000 + 3000) / 1000.);
-        walking = true;
-      }
+  // Check if we should press the jump button
+  bool randomJump = !randomWaitTimer.started();
+  bool mayJump = !jumpRecoverTimer.started();
+  bool notMoving = (dx+dy < 0.1);
+  bool pathBlocked = !sector->is_free_space(lookahead); 
+  if ((notMoving || pathBlocked || randomJump) && mayJump) {
+    float jumpDuration = float(rand() % 200 + 500) / 1000.0;
+    jumpPushTimer.start(jumpDuration);
+    jumpRecoverTimer.start(jumpDuration+0.1);
+    randomWaitTimer.start(float(rand() % 3000 + 3000) / 1000.0);
   }
-  if(!walking)
-    controller->press(Controller::JUMP);
+
+  // Keep jump button pressed
+  if (jumpPushTimer.started()) controller->press(Controller::JUMP);
+
+  // Remember last position, so we can determine if we moved
   last_tux_x_pos = tux->get_pos().x;
   last_tux_y_pos = tux->get_pos().y;
 
@@ -279,7 +294,6 @@ void draw_demo(float elapsed_time)
 /* --- TITLE SCREEN --- */
 void title()
 {
-  walking = true;
   //LevelEditor* leveleditor;
   controller = new CodeController();
 
@@ -299,8 +313,6 @@ void title()
   /* --- Main title loop: --- */
   frame = 0;
 
-  random_timer.start(float(rand() % 2000 + 2000) / 1000.0);
-
   Uint32 lastticks = SDL_GetTicks();
   
   Menu::set_current(main_menu);
@@ -343,7 +355,7 @@ void title()
               Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
       context.draw_text(white_small_text,
         _(
-"Copyright (c) 2005 SuperTux Devel Team\n"
+"Copyright (c) 2006 SuperTux Devel Team\n"
 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
 "redistribute it under certain conditions; see the file COPYING for details.\n"
         ),
@@ -369,16 +381,6 @@ void title()
                   // Contrib Menu
                   generate_contrib_menu();
                   break;
-#if 0
-                case MNID_LEVELEDITOR: {
-                  LevelEdtiro* leveleditor = new LevelEditor();
-                  leveleditor->run();
-                  delete leveleditor;
-                  Menu::set_current(main_menu);
-                  resume_demo();
-                  break;
-                }
-#endif
                 case MNID_CREDITS:
                   sound_manager->stop_music();
                   fadeout(500);
@@ -408,7 +410,7 @@ void title()
                 
                 if(confirm_dialog(bkg_title, str.c_str())) {
                   str = "save/slot" + stream.str() + ".stsg";
-                  printf("Removing: %s\n",str.c_str());
+                  msg_debug("Removing: " << str);
                   PHYSFS_delete(str.c_str());
                 }