Browse Source

recalcWakeTime()

David Rose 17 years ago
parent
commit
02d94cc8d2

+ 23 - 0
direct/src/task/TaskOrig.py

@@ -187,6 +187,29 @@ class Task:
     def setDelay(self, delay):
         self.delayTime = delay
 
+    def recalcWakeTime(self):
+        """If the task is currently sleeping, this resets its wake
+        time to the current time + self.delayTime.  It is as if the
+        task had suddenly returned task.again.  The task will sleep
+        for its current delay seconds before running again.  This
+        method may therefore be used to make the task wake up sooner
+        or later than it would have otherwise.
+
+        If the task is not already sleeping, this method has no
+        effect."""
+
+        # This is a little bit hacky--we're poking around in global
+        # space more than we should--but this code will be going away
+        # soon in favor of the new task manager anyway.
+        
+        now = globalClock.getFrameTime()
+        self.wakeTime = now + self.delayTime
+
+        # Very important that we re-sort the priority queue after
+        # monkeying with the wake times.
+        heapify(taskMgr._TaskManager__doLaterList)
+        
+
     def setStartTimeFrame(self, startTime, startFrame):
         self.starttime = startTime
         self.startframe = startFrame

+ 2 - 1
panda/src/event/asyncTask.I

@@ -79,7 +79,8 @@ get_manager() const {
 //               added will not affect the task's wake time; it will
 //               only affect the task if it is re-added to the queue
 //               in the future, for instance if the task returns
-//               DS_again.
+//               DS_again.  Howver, see recalc_wake_time() if you wish
+//               to apply the delay effect immediately.
 ////////////////////////////////////////////////////////////////////
 INLINE void AsyncTask::
 set_delay(double delay) {

+ 29 - 0
panda/src/event/asyncTask.cxx

@@ -114,6 +114,35 @@ get_wake_time() const {
   return 0.0;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: AsyncTask::recalc_wake_time
+//       Access: Published
+//  Description: If the task is currently sleeping on a task
+//               chain, this resets its wake time to the current time
+//               + get_delay().  It is as if the task had suddenly
+//               returned DS_again.  The task will sleep for its
+//               current delay seconds before running again.  This
+//               method may therefore be used to make the task wake up
+//               sooner or later than it would have otherwise.
+//
+//               If the task is not already sleeping, this method has
+//               no effect.
+////////////////////////////////////////////////////////////////////
+void AsyncTask::
+recalc_wake_time() {
+  if (_manager != (AsyncTaskManager *)NULL) {
+    MutexHolder holder(_manager->_lock);
+    if (_state == S_sleeping) {
+      double now = _manager->_clock->get_frame_time();
+      _wake_time = now + _delay;
+      _start_time = _wake_time;
+
+      make_heap(_chain->_sleeping.begin(), _chain->_sleeping.end(), 
+                AsyncTaskChain::AsyncTaskSortWakeTime());
+    }
+  }
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: AsyncTask::get_elapsed_time
 //       Access: Published

+ 1 - 0
panda/src/event/asyncTask.h

@@ -79,6 +79,7 @@ PUBLISHED:
   INLINE bool has_delay() const;
   INLINE double get_delay() const;
   double get_wake_time() const;
+  void recalc_wake_time();
   
   INLINE double get_start_time() const;
   double get_elapsed_time() const;