// Actions. public override void SetTimer(int nTimerId, bool bAbsolute, int nMilliSecs, MHEngine engine) { // First find any existing timer with the same Id. for (int i = 0; i < m_Timers.Count; i++) { MHTimer timer = m_Timers[i]; if (timer.Identifier == nTimerId) { // If the Position wasn't given then we must remove the timer // if it exists, otherwise update the Position // if (nMilliSecs == -1) // { m_Timers.Remove(timer); // } // else // { // timer.Position = nMilliSecs; // } break; } } // If the time has passed we don't set up a timer. TimeSpan currentTime = MHTimer.getCurrentTimeSpan(); if (nMilliSecs < 0 || (bAbsolute && m_StartTime.Add(MHTimer.getMillisecondsTimeSpan(nMilliSecs)) < currentTime)) { return; } long nActualMillis; // Adjust the millisec accordingly if (bAbsolute) { TimeSpan tsMillis = MHTimer.getMillisecondsTimeSpan(nMilliSecs); TimeSpan tsResult = m_StartTime.Add(tsMillis); nActualMillis = tsResult.Ticks; } else { TimeSpan tsMillis = MHTimer.getMillisecondsTimeSpan(nMilliSecs); TimeSpan tsResult = currentTime.Add(tsMillis); nActualMillis = (long)tsResult.TotalMilliseconds; } // Create and add the timer MHTimer pTimer = new MHTimer(nTimerId, nActualMillis, bAbsolute); m_Timers.Add(pTimer); }
// Checks the timers and fires any relevant events. Returns the millisecs to the // next event or zero if there aren't any. public int CheckTimers(MHEngine engine) { TimeSpan currentTime = MHTimer.getCurrentTimeSpan(); // Get current time int nMSecs = 0; List <MHTimer> executedTimers = new List <MHTimer>(); for (int i = 0; i < m_Timers.Count; i++) { MHTimer timer = m_Timers[i]; // Use <= rather than < here so we fire timers with zero time immediately. if (timer.PositionTimeSpan <= currentTime) { // If the time has passed trigger the event and remove the timer from the queue. engine.EventTriggered(this, EventTimerFired, new MHUnion(timer.Identifier)); executedTimers.Add(timer); // Add to list of executed timers which are removed later } else { // This has not yet expired. Set "nMSecs" to the earliest time we have. int nMSecsToGo = timer.PositionTimeSpan.Subtract(currentTime).Milliseconds; if (nMSecs == 0 || nMSecsToGo < nMSecs) { nMSecs = nMSecsToGo; } } } // Remove all executed timers for (int i = 0; i < executedTimers.Count; i++) { MHTimer timer = executedTimers[i]; m_Timers.Remove(timer); } return(nMSecs); }
public override void Activation(MHEngine engine) { if (RunningStatus) { return; } base.Activation(engine); // Run any start-up actions. engine.AddActions(m_StartUp); engine.RunActions(); // Activate the ingredients in order. for (int i = 0; i < m_Items.Size; i++) { MHIngredient pIngredient = m_Items.GetAt(i); if (pIngredient.InitiallyActive()) { pIngredient.Activation(engine); } } m_fRunning = true; // Record the time here. This is the basis for absolute times. m_StartTime = MHTimer.getCurrentTimeSpan(); // Don't generate IsRunning here - that's done by the sub-classes. }