JSPM

@aceworks-studio/state-machine

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q72294F
  • License MIT

A Luau utility to easily create simple state machines

Package Exports

  • @aceworks-studio/state-machine
  • @aceworks-studio/state-machine/src/init.lua

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@aceworks-studio/state-machine) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

checks version GitHub top language license npm

@aceworks-studio/state-machine

A Luau utility to easily create simple state machines.

Installation

Add @aceworks-studio/state-machine in your dependencies:

yarn add @aceworks-studio/state-machine

Or if you are using npm:

npm install @aceworks-studio/state-machine

Content

create

function StateMachine.create(config: StateMachineConfiguration): () -> ()

type StateController = {
    addCleanup: (...Teardown) -> () -> (),
    next: (nextState: string, nextConfig: any) -> never,
}

type StateEvent = { state: string, config: any, time: number }
type StateHistory = { StateEvent }

type StateMachineConfiguration = {
    defaultState: string,
    defaultStateConfig: any,
    states: {
        [string]: (StateController, config: any) -> (),
    },
    onStateChange: (state: string, config: any) -> ()?,
    onStateHistoryChange: (StateHistory) -> ()?,
}

Example

create({
    defaultState = "startup",
    defaultStateConfig = { mapName = "defaultMap" },
    states = {
        startup = function(controller, config)
            controller.addCleanup(task.spawn(function()
                local model = loadMap(config.mapName)
                task.wait(2)
                controller.next("match", { model = model })
            end))
        end,
        match = function(controller, config)
            teleportAllPlayersInMap(config.model)

            controller.addCleanup(
                task.delay(30, function()
                    controller.next("expired")
                end),
                task.spawn(function()
                    Time.yieldUntil(0.2, function()
                        return not hasEnoughPlayer()
                    end)

                    local remaining = getRemainingPlayers()
                    local remainingCount = #remaining

                    if remainingCount == 1 then
                        controller.next("winner", { winner = remaining[1] })
                    else
                        controller.next("startup", { mapName = pickRandomMap() })
                    end
                end)
            )
        end,
        winner = function(controller, config)
            print("winner!", config.winner)
            controller.addCleanup(task.delay(3, function()
                controller.next("startup")
            end))
        end,
        expired = function(controller)
            print("expired!")
            controller.addCleanup(task.delay(3, function()
                controller.next("startup")
            end))
        end,
    },
})

License

This project is available under the MIT license. See LICENSE.txt for details.

Other Lua Environments Support

If you would like to use this library on a Lua environment where it is currently incompatible, open an issue (or comment on an existing one) to request the appropriate modifications.

The library uses darklua to process its code.