Global constants and persistent state for the Transposer game. Defines direction enum, draw depth constants, debug flag, score fields (including HighScore property that saves on set), and simple JSON persistence to a local save file.
using System.Text.Json;
namespace Sandbox.Transposer;
/// <summary>
/// Direction constants used for collision resolution and movement.
/// </summary>
public enum Direction { Left, Right, Up, Down }
/// <summary>
/// Global constants and state shared across the Transposer game.
/// Mirrors the original Unity Globals class — depth layers for draw
/// ordering and persistent score tracking.
/// </summary>
public static class Globals
{
// ── Debug ────────────────────────────────────────────────────────────
public static bool DebugHitboxes { get; set; }
// ── Depth layers (lower number = drawn on top) ──────────────────────
public const int DEPTH_TEXT = -8;
public const int DEPTH_PARTICLE = -7;
public const int DEPTH_BLOOD_PARTICLE = -6;
public const int DEPTH_PLAYER = -5;
public const int DEPTH_ENEMY = -4;
public const int DEPTH_SHINE = -3;
public const int DEPTH_COIN = -2;
public const int DEPTH_TRAIL = -1;
public const int DEPTH_TILE = 0;
public const int GRID_SQUARE_STARTING_SIZE = 64;
public const string LEADERBOARD_STAT = "transposer_score_v1";
// ── Score tracking (persisted across scene switches) ────────────────
public static bool HasPlayedOnce { get; set; }
public static int LastNumSwaps { get; set; }
public static int LastScore { get; set; }
private static int _highScore;
public static int HighScore
{
get => _highScore;
set
{
_highScore = value;
SaveToDisk();
}
}
// ── Persistence ─────────────────────────────────────────────────────
private const string SAVE_FILE = "transposer_save.json";
private class SaveData
{
public int HighScore { get; set; }
}
/// <summary>
/// Load high score from disk. Call once at game startup.
/// </summary>
public static void LoadFromDisk()
{
try
{
if ( FileSystem.Data.FileExists( SAVE_FILE ) )
{
var json = FileSystem.Data.ReadAllText( SAVE_FILE );
var data = JsonSerializer.Deserialize<SaveData>( json );
if ( data is not null )
_highScore = data.HighScore;
}
}
catch ( System.Exception e )
{
Log.Warning( $"Failed to load Transposer save: {e.Message}" );
}
}
private static void SaveToDisk()
{
try
{
var data = new SaveData { HighScore = _highScore };
var json = JsonSerializer.Serialize( data );
FileSystem.Data.WriteAllText( SAVE_FILE, json );
}
catch ( System.Exception e )
{
Log.Warning( $"Failed to save Transposer data: {e.Message}" );
}
}
}