Skip to content

In Game Time

Jay Thakkar edited this page Oct 12, 2024 · 3 revisions

Game Time Management in the Day-Night Cycle System

Overview

The game utilizes a sophisticated time management system to control various time-dependent features, such as the day-night cycle, event scheduling, and gameplay mechanics that rely on time progression. The primary classes involved in this system are:

  • GameTime
  • InGameTime
  • DayNightCycle The following descriptions provide an in-depth overview of these classes, their responsibilities, and how they interact within the game.

Classes

1. GameTime

Purpose:

GameTime manages the global game time, tracking the passage of time since the game started. It provides time-related utilities that are essential for updating game logic, animations, and physics simulations.

Responsibilities:

  • Time Scaling: Allows adjustment of the game's time scale to speed up or slow down the passage of time (e.g., for slow-motion effects).
  • Delta Time Management: Provides the time elapsed between frames, both scaled and unscaled.
  • Global Time Tracking: Keeps track of the total time since the game began.

Key Methods:

  • getDeltaTime(): Returns the scaled time passed since the last frame.
  • getRawDeltaTime(): Returns the unscaled time passed since the last frame.
  • getTime(): Returns the total time passed since the game started. Usage Example:
GameTime gameTime = new GameTime();
float deltaTime = gameTime.getDeltaTime();
// Use deltaTime for updating game logic

2. InGameTime

Purpose:

InGameTime manages the in-game clock that can be paused and resumed independently of the real-world time. It is crucial for in-game events and mechanics that need to be paused (e.g., when opening a menu).

Responsibilities:

  • Pause and Resume Functionality: Allows pausing and resuming the in-game time without affecting the global game time.
  • In-Game Time Tracking: Keeps track of the time elapsed in the game's world, excluding any paused durations.

Key Methods:

pause(): Pauses the in-game time. resume(): Resumes the in-game time. getTime(): Returns the current in-game time, adjusted for any paused periods.

Usage Example:

InGameTime inGameTime = new InGameTime();
inGameTime.pause();
// Game is paused
inGameTime.resume();
// Game resumes
long currentTime = inGameTime.getTime();

3. DayNightCycle

Purpose:

DayNightCycle controls the ambient lighting and simulates the passage of day and night within the game world. It relies on InGameTime to determine the current time of day and adjusts the lighting accordingly.

Responsibilities:

  • Ambient Light Adjustment: Changes the game's ambient light based on the time of day to create a realistic day-night cycle.
  • Time of Day Calculation: Computes the current time of day as a normalized value between 0.0 (midnight) and 1.0 (next midnight).
  • Pause and Resume Cycle: Allows pausing and resuming the day-night cycle, synchronizing with the in-game time pauses.

Key Methods:

  • update(): Updates the ambient light based on the current time of day.
  • getTimeOfDay(): Returns the normalized time of day.
  • pause(): Pauses the day-night cycle.
  • resume(): Resumes the day-night cycle.

Usage Example:

RayHandler rayHandler = new RayHandler(world);
DayNightCycle dayNightCycle = new DayNightCycle(rayHandler);
dayNightCycle.update();
// Adjusts the ambient light based on the time of day

Interactions and Workflow

Initialization:

GameTime is initialized at the start of the game to track the global time. InGameTime is initialized to manage the time within the game world, separate from real-world time. DayNightCycle is created with a reference to the RayHandler for controlling lighting effects.


Time Progression:

As the game runs, GameTime keeps track of the elapsed time, and InGameTime advances unless it is paused. DayNightCycle uses InGameTime.getTime() to determine the current time of day and updates the ambient light accordingly. Pausing and Resuming:

When the game is paused (e.g., the player opens a menu), InGameTime.pause() is called. DayNightCycle checks if the isPaused flag is set and skips updates when paused. Upon resuming, InGameTime.resume() is called, and DayNightCycle continues updating the ambient light.

Lighting Updates:

DayNightCycle.update() is called regularly (e.g., every frame or at fixed intervals). It calculates the time of day using getTimeOfDay() and interpolates between predefined colors to simulate the changing sky. The interpolated color is applied to the ambient light via RayHandler.setAmbientLight().


UML

The following UML displays the relationships between the classes regarding Time.

image

Sequence Diagram

The following sequence diagram displays the relationships between the methods called and illustrates the interactions between GameTime, InGameTime, DayNightCycle, RayHandler, and other components during the game's execution.

image

Clone this wiki locally