Clojure script game engine overview

15
A Functional Game Engine in ClojureScript Alex Kehayias @alexkehayias http://github.com/alexkehayias/chocolatier

Transcript of Clojure script game engine overview

Page 1: Clojure script game engine overview

A Functional Game Engine in

ClojureScript

Alex Kehayias@alexkehayias

http://github.com/alexkehayias/chocolatier

Page 2: Clojure script game engine overview
Page 3: Clojure script game engine overview
Page 4: Clojure script game engine overview

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

Page 5: Clojure script game engine overview

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

Page 6: Clojure script game engine overview

Example

Page 7: Clojure script game engine overview

Essentials

Page 8: Clojure script game engine overview

System

• A function that returns updated state

• A function that works on a collection of entities

that have the given component ID(s)

Page 9: Clojure script game engine overview

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

Page 10: Clojure script game engine overview

Entity

• A unique ID

• A collection of component IDs

Page 11: Clojure script game engine overview

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)

Page 12: Clojure script game engine overview

State

{…}

Page 13: Clojure script game engine overview

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

Page 14: Clojure script game engine overview

Demo

Page 15: Clojure script game engine overview

TODO

• Improve performance of core functions

• WebGL with Canvas fallback

• Clean up some boilerplate with macros

• Sound

• Animation

• Build a real game!