/// <summary> /// Add to the TimeWarp Buffer /// </summary> /// <param name="amount">The amount to add</param> public void Add(double amount) { double key = Planetarium.GetUniversalTime(); CacheTimeWarpEntry newEntry = new CacheTimeWarpEntry(key, amount); bufferList.Enqueue(newEntry); totalAmount += amount; }
/// <summary> /// Take from the TimeWarp Buffer. /// </summary> /// <param name="amount">The amount we want</param> /// <param name="amountTaken">out parm containing the amount taken</param> /// <returns>true if we got what we wanted, otherwise false</returns> public bool Take(double amount, out double amountTaken) { amountTaken = 0; while (amount > 0 && bufferList.Count > 0) //We still need amount and still on the Queue { CacheTimeWarpEntry entry = bufferList.Dequeue(); //Get last entry totalAmount -= entry.amount; //Decrease total amount if (entry.amount > amount) //If we got more than we need just throw away the rest { entry.amount = amount; } amountTaken += entry.amount; //add to the amount we are giving. amount -= entry.amount; //Decrease the amount we still need. } if (amount <= 0) { return(true); } return(false); }
/// <summary> /// Updates the Buffer. Call Every FixedUpdate. Checks if Timewarp rate less than 3, throw away the buffer entries. /// Will remove any buffer entries older than fixedDeltaTime * 2 (two ticks). /// </summary> public void Update() { //if (TimeWarp.CurrentRateIndex < timeWarpStep) //If timewarp rate is less than three throw away the queue and we are done. //{ // Clear(); // return; //} //Otherwise throw away anything more than 3 ticks ago from the queue. double oldTime = Planetarium.GetUniversalTime() - (TimeWarp.fixedDeltaTime * 6); int dequeueCount = 0; foreach (CacheTimeWarpEntry entry in bufferList) { if (entry.time < oldTime) { dequeueCount++; } } for (int dqI = 0; dqI < dequeueCount; dqI++) { CacheTimeWarpEntry entry = bufferList.Dequeue(); totalAmount -= entry.amount; } }