Skip to content

Instantly share code, notes, and snippets.

@hmans hmans/about.md
Created Apr 19, 2017

Embed
What would you like to do?
Elm-flavoured Incremental Game Framework?

Here's a tiny, but fully functional (ha) incremental game framework. Some notes:

  • Built with CoffeeScript. I like CoffeeScript very much.
  • Inspired by Elm, minus the types, obviously.
  • Uses Inferno.js as its rendering layer. It's extremely fast.
  • main.coffee dispatches to a state object, because games typically employ finite state machines. With a single state, this is unneccessary, of course.

-- Hendrik Mans, hendrik@mans.de

{ h, run, merge } = require './mental'
model = ->
state: "idle"
credits: 0
states =
idle:
view: (model, dispatch) ->
h '#game', [
h 'p', "Credits: $#{model.credits}"
h 'button', onClick: dispatch("increase"), "Increase!"
h 'button', onClick: dispatch("decrease"), "Decrease!"
]
update: (model, action, payload...) ->
switch action
when "increase"
merge model, credits: model.credits + 1
when "decrease"
merge model, credits: model.credits - 1
getState = (name) ->
states[name] or throw "State #{name} not implemented"
view = (model, dispatch) ->
getState model.state
.view model, dispatch
update = (model, action, payload...) ->
getState model.state
.update model, action, payload
document.addEventListener 'DOMContentLoaded', ->
run { model, view, update }
ticker = require './mental/ticker'
{ render, h } = require './mental/renderer'
merge = (original, update) ->
Object.assign {}, original, update
run = ({model, view, update}) ->
model = model()
el = document.getElementById 'root'
dispatch = (action, payload...) -> () ->
model = update model, action, payload...
shouldRender = ->
true
tick = ->
if shouldRender()
tree = view model, dispatch
render tree, root
ticker.start 30, tick
module.exports = { h, run, merge }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.