Clojure script game engine overview

Preview:

Citation preview

A Functional Game Engine in

ClojureScript

Alex Kehayias@alexkehayias

http://github.com/alexkehayias/chocolatier

Design Principles

• Browser repl driven

• Favor pure functions

• Testable with data

• Global state in one place

• Minimize access to state

• Use mutability only when performance can be greatly improved

• Favor messaging over direct state access

• Extensible

• Inspectable

What is it?

A way to describe the transitions of game state as

a sequence of (mostly) pure functions.

• Functional entity component system

• Wrapper for Pixi.js rendering library

• A collection of reusable components such as

tiling, collision detection, keyboard input, etc

Example

Essentials

System

• A function that returns updated state

• A function that works on a collection of entities

that have the given component ID(s)

Component

• A function that returns updated component state and, optionally, a collection of events for one entity

• By default, component state is specific to the component/entity being worked on

• Polymorphism achieved via dispatching on entity ID using multimethods

Entity

• A unique ID

• A collection of component IDs

Events

• Global, opt-in

• Anything can emit events at any point in the game loop and are available immediately

• By default, component functions can return a collection of events to be emitted

• Events can be subscribed to using selectors

• Event queue should be cleared at the beginning or end of the game loop (built in event-system does this already)

State

{…}

Declaring the game

• It’s a hashmap

• Ordering doesn’t matter

• System call order is determined by the scene

• Modify the game construct while running

• Hot reloadable

Demo

TODO

• Improve performance of core functions

• WebGL with Canvas fallback

• Clean up some boilerplate with macros

• Sound

• Animation

• Build a real game!