Options
All
  • Public
  • Public/Protected
  • All
Menu

Tracks state as provided, and provides information on status of the current state. Also allows transitioning to a prior state from a temporary state (e.g. a pause dialog back to game play).

Example

enum GameState {
Ready,
Playing,
Paused,
GameOver
}

// In the constructor of whatever needs state.
this.state = new State<GameState>(GameState.Ready)

// In the update of the scene/object/whatever.
this.state.update(dt)

switch (this.state) {
case GameState.Ready:
if (this.state.first) {
console.log('First frame of this state')
}

// Go to Playing state after 1.5 seconds
if (this.state.time > 1.5) {
this.state.set(GameState.Playing)
}
break

default:
console.log(this.state.get())
}

Type parameters

  • EnumType

Hierarchy

  • State

Index

Constructors

Properties

Methods

Constructors

constructor

  • new State(state: EnumType): State
  • Initialize State object.

    Example

    const state = new State<MyEnum>(MyEnum.StateOne)

    Parameters

    • state: EnumType

      The initial state to set.

    Returns State

Properties

first

first: boolean

Whether this is the first frame of the current state.

time

time: number

Number of seconds in the current state.

Methods

back

  • back(): void
  • Return to the previous state (if there is one).

    Returns void

get

  • get(): EnumType
  • Get the current state.

    Returns EnumType

is

  • is(state: EnumType): boolean
  • Whether the current state is the one provided.

    Parameters

    • state: EnumType

      State to check for.

    Returns boolean

isIn

  • isIn(...states: EnumType[]): boolean
  • Whether the current state is one of the provided states.

    Parameters

    • Rest ...states: EnumType[]

      Array of possible states.

    Returns boolean

set

  • set(state: EnumType): void
  • Set the current state and store the previous state to allow going back.

    Parameters

    • state: EnumType

      The new state to change to.

    Returns void

update

  • update(dt: number): void
  • Update the state.

    Should be called in parent object's update function.

    Parameters

    • dt: number

      Delta time since last update.

    Returns void