ClojureScript Game Engine Development with Construct 2

Clojure game programming benefits most from use of a game engine. While game engines are not specifically written for Clojure, Clojure is written to run on many VMs, and can take advantage of many game engines.

Construct 2 happens to be a great game engine for use with Clojure in the form of ClojureScript. ClojureScript integrates very nicely with Construct 2’s plugins and behaviors. Construct 2 allows for Clojure games (in the form of ClojureScript) to reach practically every platform out there for 2D game publishing.

Construct 2 / ClojureScript games can reach any platform HTML 5 games can reach, including:

  • All modern web browsers
  • Behave like native PC, Mac, and Linux apps using the Node-Webkit wrapper
  • Be published by Chrome Web Store, Facebook, Kongregate, NewGrounds, Firefox Marketplace, iOS and Android app stores, the Windows 8 Store, and Amazon
  • And can easily integrate advertising into games published to your own web site

For those who don’t know, … ClojureScript is a modern Functional Lisp that compiles down to JavaScript using Google’s Closure compiler. I like using it (and it’s parent, Clojure) for AI development and controller logic in games.

Construct 2 is a wonderful RAD tool for 2D game creation, so handles much of the harder tasks in game development, such as collision detection, sprite sheet animation, events, and anything else you might need for standard 2D game creation. Once you hit Construct 2’s limits, you can start writing ClojureScript logic to fill in the game logic that is missing.

I wasn’t sure at first that ClojureScript and the Construct 2 game engine could play nicely together. I figured in theory, I could use ClojureScript in plug-ins for Construct 2, but as far as I know it hasn’t been done before.

It took some doing, but I integrated some compiled ClojureScript with my first custom Construct 2 plugin and they work perfectly together.

Benefits of ClojureScript and the Construct 2 Game Engine

As mentioned above, Construct 2 allows the quick and easy creation of any type 2D games. There are plenty of OpenSource sounds, music, and graphics on the web, if you don’t have the resources or skill to create your own.

Construct 2 opens up potential for game sales in most online software market places, and creation of 2D games for OS X, Linux, and Windows platforms. In addition, Construct 2 based games work very nicely in all HTML 5 compliant browsers.

In short, whether you want to create native feeling games that behave like native PC games, mobile games for phones and tablets, or web based games, Construct 2 takes care of compilation for you.

Clojure (specifically ClojureScript) brings a whole host of extras for savvy developers to take advantage of. ClojureScript is a Lisp, as already mentioned. Lisps have long been recognized for their power in developing AIs, and powerful domain specific solutions.

Clojure, and by extension ClojureScript, is a functional language. This allows for cleaner code that is easier to debug and maintain. ClojureScript encourages immutable data and use of first-class functions. This allows for very powerful constructs to make programming your game logic easier.

ClojureScript allows access to JavaScript’s APIs and other libraries, while taking advantage of Google’s Closure compiler. What that means is, you can write standard ClojureScript, and then take advantage of advanced optimizations that Goolge’s Closure compiler provides. Construct 2 integrates Google’s Closure compiler’s advance mode, so you get your cake and can eat it too.

ClojureScript Game Engine

Prerequisites: For the rest of this article, you will need to familiarize yourself with Clojure or ClojureScript, know at least a little JavaScript, and spend a few days learning the ins and outs of the Construct 2 game engine. Sorry. There’s no shortcuts to greatness.

My goal here is to give the steps that are not documented elsewhere. I’ll just give links to documentation that already exists. For example, Construct 2 Plugin and Behavior creation is already documented very nicely on their website, so I’ll gloss over that. However, I don’t know of any tutorials explaining how to integrate ClojureScript with Construct 2 or other JavaScript compatible HTML 5 game engines, so I’ll spend more time on the details for that.

Steps for creating a ClojureScript / Construct 2 game:

  1. Create the sprites, Layouts, and Event Sheets for Construct 2If you are new to Construct 2, you should look at the top-down shooter tutorial and the platform game tutorials. It will amaze you that you can make a full game that’s fun to play without ever writing a line of code. Doing even one of these tutorials will give you a basic knowledge of sprites, layouts and event sheets in Construct 2.
  2. Stub out your Construct 2 Plugin or Behavior using the JavaScript SDKScirra (the maker of Construct 2) has an excellent manual focusing on the the Construct 2 JavaScript SDK. There is even a set of templates that you can download and get started.
  3. Create your Clojure code and get it working (or do this directly in ClojureScript)I tend to do my pure Clojure coding work in Eclipse, and then migrate it over to ClojureScript before compiling for integration into Construct 2.
  4. Create a ClojureScript project’lein’ is a great tool for setting up Clojure and ClojureScript projects. I used the following template for creation of a project for compiling my ClojureScript for use in Construct 2. If there is enough interest, creation of a Construct 2 specific template is possible, just not done yet.lein new cljs-kickoff <project name>
  5. Update the dependencies and plugins in the project.clj file, as follows::dependencies [[org.clojure/clojure “1.7.0”]
    [org.clojure/clojurescript “0.0-3308”]
    [javax.servlet/servlet-api “2.5”]
    [ring “1.4.0”]]
    :plugins [[lein-cljsbuild “1.0.6”]
    [lein-ring “0.9.6”]]
  6. Set the Closure compiler optimizations to read as follows:optimizations :simpleThe simple compiler optimization allows for all your ClojureScript code and related libraries to be compiled into one file, but still allow for further advanced optimization by Construct 2 when exporting to the final platform game’s distribution platform.
  7. Copy your code over to the following directory and integrate it with the client.cljs file<project-name>/src/cljs/<project-name>/client.cljsI hooked the ^:export’ed function to the js/alert in the handle-click function that already existed in the initial project. By doing that, I can test my code on my favorite web browser before integrating with Construct 2.
  8. Change your ClojureScript’s namespace to match your plugin.This isn’t absolutely necessary, but “hello-world” name spaces feel kind of cheesy.
  9. Compile and create common.js for Construct 2’s use.Change the cljs.js file found in the resources directory to common.js<project-name>/resources/public/js/cljs.jsThe common.js file is used by Construct 2’s plugins and behaviors. Don’t worry about dead code or optimizing as Construct 2 uses the advanced Closure compiler mode to do that for you.
  10. Hook up your stubbed out JavaScript code to the new common.js exported functionsThis is the code that you created when setting up your custom Construct 2 plugin or behavior.
  11. Comment out any non-applicable JavaScript code.At this point your ClojureScript has been compiled into a minimally optimized Common.js file. There is some leftover code from the ClojureScript kickoff project. Construct 2 will complain about it when you try to load your plugin. Go ahead and delete the left over code that prevents Construct 2 from compiling and loading your plugin or behavior. The leftover bits will be at the bottom of the common.js file.
  12. Don’t forget that dashes (‘-‘) become underscores (‘_’) in function names when moving from ClojureScript to Javascript.

It should just work now. If you have any questions, feel free to contact me on Twitter @TGeneDavis, and I’ll see what I can do to help out.