Class ContextTime

java.lang.Object
com.xebisco.yield.ContextTime
All Implemented Interfaces:
Serializable

public class ContextTime extends Object implements Serializable
Represents the timing context for an application, encapsulating time-related properties such as delta time, timescale, and target sleep time. This class allows for the adjustment of the perceived speed of time (via timescale) and facilitates frame rate control by setting a target frames per second (FPS). It also tracks the total time since the start of the context, enabling various time-based operations within the application.
See Also:
  • Constructor Details

    • ContextTime

      public ContextTime(double targetFPS)
      Constructs a ContextTime object with a specified target frames per second (FPS). This constructor calculates the target sleep time based on the provided FPS.
      Parameters:
      targetFPS - The desired target FPS for the context time.
    • ContextTime

      public ContextTime()
      Default constructor for ContextTime. Initializes the object with default values.
  • Method Details

    • setTargetFPS

      public ContextTime setTargetFPS(double targetFPS)
      Sets the target frames per second (FPS) for this context. This method calculates and sets the target sleep time based on the provided FPS.
      Parameters:
      targetFPS - The desired target FPS.
      Returns:
      The current ContextTime instance for chaining method calls.
      Throws:
      IllegalArgumentException - If the targetFPS is less than or equal to zero.
    • timeScale

      public double timeScale()
      Gets the current timescale.
      Returns:
      The current timescale.
    • timeScale

      public void timeScale(double timeScale)
      Sets the timescale. This scale is applied to deltaTime to simulate faster or slower motion.
      Parameters:
      timeScale - The new timescale to set.
    • deltaTime

      public double deltaTime()
      Gets the delta time adjusted by the current timescale. This value represents the scaled time elapsed between the current and the last update.
      Returns:
      The scaled delta time.
    • setDeltaTime

      public ContextTime setDeltaTime(double deltaTime)
      Sets the delta time. This is the raw time elapsed between the current and the last update.
      Parameters:
      deltaTime - The delta time to set.
      Returns:
      The current ContextTime instance for chaining method calls.
    • targetSleepTime

      public long targetSleepTime()
      Gets the target sleep time. This is the intended duration to sleep between updates to achieve the target FPS.
      Returns:
      The target sleep time in nanoseconds.
    • setTargetSleepTime

      public ContextTime setTargetSleepTime(long targetSleepTime)
      Sets the target sleep time. This is used to adjust the update frequency to achieve the target FPS.
      Parameters:
      targetSleepTime - The target sleep time in nanoseconds.
      Returns:
      The current ContextTime instance for chaining method calls.
    • setTimeScale

      public ContextTime setTimeScale(double timeScale)
      Sets the timescale. This scale is applied to deltaTime to simulate faster or slower motion. This is an alternative method to the void timeScale(double timeScale) method, allowing method chaining.
      Parameters:
      timeScale - The new timescale to set.
      Returns:
      The current ContextTime instance for chaining method calls.
    • timeSinceStart

      public long timeSinceStart()
      Gets the time since the start of the context in nanoseconds.
      Returns:
      The time since start in nanoseconds.
    • setTimeSinceStart

      public ContextTime setTimeSinceStart(long timeSinceStart)
      Sets the time since the start of the context. This is typically updated to track the total elapsed time.
      Parameters:
      timeSinceStart - The time since start in nanoseconds.
      Returns:
      The current ContextTime instance for chaining method calls.