make game_session do main_loop->set_speed(0) on pause, reverted TimeScheduler changes
authorMatthias Braun <matze@braunis.de>
Mon, 16 Apr 2007 18:39:00 +0000 (18:39 +0000)
committerMatthias Braun <matze@braunis.de>
Mon, 16 Apr 2007 18:39:00 +0000 (18:39 +0000)
SVN-Revision: 4989

src/game_session.cpp
src/game_session.hpp
src/mainloop.cpp
src/mainloop.hpp
src/scripting/time_scheduler.cpp
src/scripting/time_scheduler.hpp

index b9b7510..e966fbc 100644 (file)
@@ -98,7 +98,7 @@ GameSession::GameSession(const std::string& levelfile_, Statistics* statistics)
   currentsector = NULL;
 
   game_pause = false;
-  Scripting::TimeScheduler::instance->set_pause(game_pause);
+  speed_before_pause = main_loop->get_speed();
 
   statistics_backdrop.reset(new Surface("images/engine/menu/score-backdrop.png"));
 
@@ -117,7 +117,6 @@ void
 GameSession::restart_level()
 {
   game_pause   = false;
-  Scripting::TimeScheduler::instance->set_pause(game_pause);
   end_sequence = 0;
 
   main_controller->reset();
@@ -168,11 +167,6 @@ GameSession::~GameSession()
   delete playback_demo_stream;
   delete demo_controller;
 
-  if (game_pause) {
-    game_pause = false;
-    Scripting::TimeScheduler::instance->set_pause(game_pause);
-  }
-
   current_ = NULL;
 }
 
@@ -285,7 +279,9 @@ GameSession::on_escape_press()
   if(currentsector->player->is_dying() || end_sequence)
   {
     // Let the timers run out, we fast-forward them to force past a sequence
-    if (end_sequence) end_sequence->stop();
+    if (end_sequence)
+      end_sequence->stop();
+
     currentsector->player->dying_timer.start(FLT_EPSILON);
     return;   // don't let the player open the menu, when he is dying
   }
@@ -301,15 +297,16 @@ GameSession::on_escape_press()
 void
 GameSession::toggle_pause()
 {
-  if(Menu::current() == NULL) {
+  if(!game_pause) {
+    speed_before_pause = main_loop->get_speed();
+    main_loop->set_speed(0);
     Menu::set_current(game_menu.get());
     game_menu->set_active_item(MNID_CONTINUE);
     game_pause = true;
-    Scripting::TimeScheduler::instance->set_pause(game_pause);
   } else {
+    main_loop->set_speed(speed_before_pause);
     Menu::set_current(NULL);
     game_pause = false;
-    Scripting::TimeScheduler::instance->set_pause(game_pause);
   }
 }
 
@@ -349,7 +346,6 @@ GameSession::process_events()
   // end of pause mode?
   if(!Menu::current() && game_pause) {
     game_pause = false;
-    Scripting::TimeScheduler::instance->set_pause(game_pause);
   }
 
   // playback a demo?
@@ -489,9 +485,9 @@ GameSession::update(float elapsed_time)
       currentsector->update(elapsed_time);
     } else {
       if (!end_sequence->is_tux_stopped()) {
-        currentsector->update(elapsed_time/2);
+        currentsector->update(elapsed_time);
       } else {
-        end_sequence->update(elapsed_time/2);
+        end_sequence->update(elapsed_time);
       }
     }
   }
@@ -599,39 +595,25 @@ GameSession::start_sequence(const std::string& sequencename)
   }
 
   // abort if a sequence is already playing
-  if (end_sequence) return;
+  if (end_sequence)
+         return;
 
   if (sequencename == "endsequence") {
-
-    // Determine walking direction for Tux
-    /*float xst = 1.f, xend = 2.f;
-    for(std::vector<GameObject*>::iterator i = currentsector->gameobjects.begin(); i != currentsector->gameobjects.end(); i++) {
-      SequenceTrigger* st = dynamic_cast<SequenceTrigger*>(*i);
-      if(!st)
-       continue;
-      if(st->get_sequence_name() == "stoptux")
-       xend = st->get_pos().x;
-      else if(st->get_sequence_name() == "endsequence")
-       xst = st->get_pos().y;
-    }
-
-    if (xst > xend) {
-      end_sequence = new EndSequenceWalkLeft();
-    } else {
-      end_sequence = new EndSequenceWalkRight();
-    }*/
     if (currentsector->get_players()[0]->physic.get_velocity_x() < 0) {
       end_sequence = new EndSequenceWalkLeft();
     } else {
       end_sequence = new EndSequenceWalkRight();
     }
-  }
-  else if (sequencename == "fireworks") end_sequence = new EndSequenceFireworks();
-  else {
+  } else if (sequencename == "fireworks") {
+    end_sequence = new EndSequenceFireworks();
+  else {
     log_warning << "Unknown sequence '" << sequencename << "'. Ignoring." << std::endl;
     return;
   }
 
+  /* slow down the game for end-sequence */
+  main_loop->set_speed(0.5f);
+
   currentsector->add_object(end_sequence);
   end_sequence->start();
 
index ae2a80a..fdb8f4f 100644 (file)
@@ -114,7 +114,8 @@ private:
 
   EndSequence* end_sequence;
 
-  bool game_pause;
+  bool  game_pause;
+  float speed_before_pause;
 
   std::string levelfile;
 
index 0a2daeb..314de15 100644 (file)
@@ -109,6 +109,12 @@ MainLoop::set_speed(float speed)
   this->speed = speed;
 }
 
+float
+MainLoop::get_speed() const
+{
+  return speed;
+}
+
 void
 MainLoop::draw_fps(DrawingContext& context, float fps_fps)
 {
index 244c63f..e9fa1a1 100644 (file)
@@ -38,6 +38,7 @@ public:
   void exit_screen(ScreenFade* fade = NULL);
   void quit(ScreenFade* fade = NULL);
   void set_speed(float speed);
+  float get_speed() const;
 
   /**
    * requests that a screenshot be taken after the next frame has been rendered
index 41a4d6f..d28308b 100644 (file)
@@ -31,7 +31,7 @@ namespace Scripting
 
 TimeScheduler* TimeScheduler::instance = NULL;
 
-TimeScheduler::TimeScheduler() : paused(false), last_update(0), internal_time(0)
+TimeScheduler::TimeScheduler()
 {
 }
 
@@ -42,10 +42,7 @@ TimeScheduler::~TimeScheduler()
 void
 TimeScheduler::update(float time)
 {
-  if (!paused) internal_time+=(time - last_update); 
-  last_update = time;
-
-  while(!schedule.empty() && schedule.front().wakeup_time < internal_time) {
+  while(!schedule.empty() && schedule.front().wakeup_time < time) {
     HSQOBJECT thread_ref = schedule.front().thread_ref;
 
     sq_pushobject(global_vm, thread_ref);
@@ -91,7 +88,7 @@ TimeScheduler::schedule_thread(HSQUIRRELVM scheduled_vm, float time)
     sq_pop(global_vm, 2);
     throw SquirrelError(global_vm, "Couldn't get thread weakref from vm");
   }
-  entry.wakeup_time = time - (last_update - internal_time);
+  entry.wakeup_time = time;
 
   sq_addref(global_vm, & entry.thread_ref);
   sq_pop(global_vm, 2);
@@ -100,10 +97,4 @@ TimeScheduler::schedule_thread(HSQUIRRELVM scheduled_vm, float time)
   std::push_heap(schedule.begin(), schedule.end());
 }
 
-void
-TimeScheduler::set_pause(bool paused)
-{
-  this->paused = paused;
-}
-
 }
index c431f14..d26cb76 100644 (file)
@@ -41,8 +41,6 @@ public:
 
   static TimeScheduler* instance;
 
-  void set_pause(bool paused);
-
 private:
   struct ScheduleEntry {
     /// weak reference to the squirrel vm object
@@ -59,9 +57,6 @@ private:
 
   typedef std::vector<ScheduleEntry> ScheduleHeap;
   ScheduleHeap schedule;
-  bool paused;
-  float last_update; 
-  float internal_time;
 };
 
 }