core/state.ts

/**
 * Enum-like state constants used by terrain that has an on/off condition:
 * Door (open/closed), Gate (open/closed), Switch (on/off), etc.
 *
 * Mirrors State.java exactly. Serialized to lowercase strings "on"/"off"/"unknown".
 */

export class State {
  name: string;
  constructor(name: string) {
    this.name = name;
  }

  isOn(): boolean {
    return this === ON;
  }

  isOff(): boolean {
    return this === OFF;
  }

  /** Return the opposite of this state (ON↔OFF). */
  opposite(): State {
    return (this === ON) ? OFF : ON;
  }

  toString(): string {
    return this.name;
  }
}

export const ON = new State("on");
export const OFF = new State("off");

/** Parse a state string (case-insensitive) to a State constant. */
export function stateFromString(str: string): State {
  return str.toLowerCase() == "on" ? ON : OFF;
}