Skip to content
DuncanSzabaga edited this page Nov 23, 2025 · 1 revision

This document explains the current timer implementation and provides a guide for implementing a full turn-based chess system in the future.

1. Current Timer Implementation

Image

The timer is a simple countdown mechanism managed by the Timer object.

How it Works

  1. Initialization (Create Event):

    • Reads global.turnTime (e.g., "5 mins").
    • Parses the string to calculate total seconds.
    • Converts seconds to game frames: timer_total_frames = total_seconds * room_speed.
    • Sets timer_frames_left to this total.
  2. Countdown (Step Event):

    • Checks if global.timer_enabled is true.
    • Decrements timer_frames_left by 1 every frame.
    • Stops at 0.
  3. Display (Draw Event):

    • Calculates minutes (div 60) and seconds (mod 60) from the remaining frames.
    • Formats them as MM:SS string.
    • Draws the text on screen.

Global Variables

  • global.turnTime: String setting the duration (e.g., "5 mins").
  • global.timer_enabled: Boolean to pause/resume the timer.
  • global.turnOrder: String tracking whose turn it is (e.g., "player", "cpu").

2. Future Implementation: Turn-Based Chess

To implement a proper turn-based system where the timer resets each turn, follow these steps:

Step 1: Centralize Turn Management

Create a TurnManager script or object to handle state changes. Do not scatter this logic across buttons or pieces.

// Script: TurnManager.gml

function end_turn() {
    // 1. Switch Sides
    if (global.turnOrder == "player") {
        global.turnOrder = "cpu";
    } else {
        global.turnOrder = "player";
    }

    // 2. Reset Timer
    with (Timer) {
        timer_frames_left = timer_total_frames;
    }

    // 3. Trigger AI (if CPU turn)
    if (global.turnOrder == "cpu") {
        // alarm[0] = 60; // Wait 1 second then move
    }
}

Step 2: Connect to Move Logic

Call end_turn() only after a valid move is completed.

  • In BoardPiece (Mouse Left Pressed):
    • Currently, the code moves the piece and updates global.boardLineUp.
    • Add: Check if the move is valid, execute it, and then call end_turn().

Step 3: Handle Time-Outs

Update the Timer object to trigger a game-over or forced turn switch when time runs out.

In Timer (Step Event):

if (timer_frames_left <= 0) {
    timer_frames_left = 0;
    
    // Option A: Lose the game
    // show_message(global.turnOrder + " ran out of time!");
    
    // Option B: Force end turn
    // end_turn(); 
}

Step 4: Visual Feedback

  • Use TurnIndicator to show not just text, but maybe a color change (Green for Player, Red for CPU).
  • Flash the timer red when it drops below 10 seconds.

Clone this wiki locally