core/state.js

/**
 * 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".
 */

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

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

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

  isUnknown() {
    return this === UNKNOWN;
  }

  /** Return the opposite of this state (ON↔OFF; UNKNOWN stays UNKNOWN). */
  opposite() {
    if (this === ON) return OFF;
    if (this === OFF) return ON;
    return UNKNOWN;
  }

  toString() {
    return this.name;
  }
}

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

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