←back to thread

333 points freetonik | 1 comments | | HN request time: 0.233s | source
1. billnreed ◴[] No.42471409[source]
I've seen this engine before and thought it was really cool!

I've been playing around with writing my own turn-based game engine with TypeScript: https://github.com/snowbillr/turn-based-game-engine

It's a WIP still, but what's really been the struggle for me is coming up with a data structure and traversal algorithm for the turn order. I wanted that to be extremely configurable by whoever uses the library.

```

engine.defineFlow((f) => {

  f.node({
    actions: f.actions(WelcomeMessage),
  })

  f.node(
    {
      actions: f.actions(RoundStart),
      cleanups: f.cleanups(RoundEnd, TestLog),
    },
    players.map(player => f.node({
      playerId: player.id,
      actions: f.actions(TurnStart),
      cleanups: f.cleanups(TurnEnd),
    })),
  )
});

```

This example shows a TicTacToe game defined like this:

```

- welcome

- round

  - player 1 turn

  - player 2 turn
```

What I ended up with was essentially a tree that's traversed depth-first to enter each node, and then goes back up the same traversal path until a sibling node is found to dive into.

That lets the user define rounds/turns/phases as children of each other in whatever shape they want.

It's been a real fun project!