News for hackers. Stuff that matters.
Re:More State Machine Love: From Reflection to Statecharts by codetrotter on Saturday March 17, 2018 @07:40AM (#16606542)
I recently built a state machine for a game I am working on. My state machine is inspired by reading https://statecharts.github.io/ and also by some of the dispatch stuff from SICP though it had been a while since I read SICP.

The way I wrote my code is I model the state machine and the states as separate objects and I model the transitions between states as events with messages that the states inform the state machine of and then the state machine looks at what is the current state and what is the current event and it looks at what state if any it should transition to as a result of this.

Here is the class for my state machine object: https://github.com/ctsrc/ITR-LotPT/blob/master/src/statechar...

And here is where I create states and register said states and their transitions with an instance of my state machine: https://github.com/ctsrc/ITR-LotPT/blob/master/src/main.js

Prior to reading about statecharts my state machines used to be messy and hard to reason about. The one I mentioned above feels very clean and I am very happy with it.

As you can see this state machine I use for transitioning from resource loading to main menu and then to the main game, as well as going between the in-game and paused states. For the main game itself I do not use a separate state machine because currently I feel that if I added that stuff there I would be adding overhead without much benefit. The game is still in an early stage and far from finished. The game is in fact not even playable yet. But like I said I feel good about that state machine of mine.

There are no comments on this comment yet.