Skip to main content

TurnManager

Overview

The TurnManager class is a Singleton responsible for managing the turns and game time of all Entity objects in the game. The class handles the logic for ticking through game time, registering and unregistering entities, and ordering them for turn-taking. The class is tightly coupled with the game's primary time loop and is also linked with the GameManager to handle game ending and restarting.

Fields and Properties

  • Instance: Singleton instance.
  • OnPlayerTurnTaken: Event triggered when the player takes a turn.
  • totalTickCount: Total ticks that have occurred.
  • playerTurnsTaken: Number of turns taken by the player.
  • TicksTillPause: The number of ticks before pausing the game loop.
  • speedCounters: Dictionary to keep track of how many ticks each Entity has waited.
  • entities: List of all registered entities.
  • entitiesFlaggedForRemoval: Entities flagged for removal from turn processing.

Core Methods

  • Awake() and Start(): Initializes Singleton and hooks into GameManager events.
  • RegisterEntity(Entity entity): Adds an Entity to the list of managed entities and sets up its speed counter.
  • UnregisterEntity(Entity entity): Removes an Entity from the managed list and its speed counter.
  • Tick(): Main game loop tick. Handles the processing of each entity and decrements TicksTillPause.
  • ProcessEntity(Entity entity): Processes an individual Entity's logic based on its speed and status conditions.
  • ReorderEntities(): Reorders entities based on certain attributes (SfxProbability, Speed, Health).
  • PlayerActionTaken(int newTicksTillPause): Used to manually control ticks till pause, commonly when the player performs an action.
  • HandleOnGameEnd() and HandleOnGameRestart(): Resets the state during game ending and restarts.

Utility Methods

  • ManageStatusLogic(Entity entity): Handles logic related to entity's status conditions.

Observations and Suggestions

  1. Method Decomposition: The Tick() and ProcessEntity(Entity entity) methods are a bit long and handle multiple concerns. Consider breaking them down into smaller, more focused methods.
  2. Event Unsubscription: There is no unsubscription logic for OnPlayerTurnTaken. If not managed carefully, this could lead to memory leaks or unintended behavior.
  3. Data Integrity: Ensure that entities cannot be directly modified outside of this class to maintain the integrity of the entities list and speedCounters dictionary.
  4. Efficiency: The ReorderEntities() method sorts entities every time one is registered or unregistered. If entities are frequently changing, consider using a more efficient data structure.

The class is a key part of the game's real-time turn-based system, and changes to this class could have ripple effects across the game. Therefore, changes should be carefully planned and tested.