Skip to content

Instantly share code, notes, and snippets.

@oakes
Last active July 15, 2017 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oakes/20e11ede32df6168e6ed to your computer and use it in GitHub Desktop.
Save oakes/20e11ede32df6168e6ed to your computer and use it in GitHub Desktop.
Standalone Nightmod Template
(ns my-game.core
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[play-clj.core :refer :all]
[play-clj.g2d :refer :all]
[play-clj.g3d :refer :all]
[play-clj.math :refer :all]
[play-clj.physics :refer :all]
[play-clj.ui :refer :all])
(:import [java.io StringWriter]))
(def out (atom nil))
(def ^:const pref-name "my-game")
(declare my-game-game overlay-screen)
(defn set-game-screen!
[& game-screens]
(->> (conj (vec game-screens) overlay-screen)
(apply set-screen! my-game-game)
on-gl))
(defmacro load-game-file
[n]
(some->> (io/resource n)
slurp
(format "(do %s\n)")
read-string))
(defn game-pref
[]
(try (edn/read-string (pref! "nightmod.settings" :get-string pref-name))
(catch Exception _ {})))
(defn game-pref!
[& keyvals]
(->> (if (map? (first keyvals))
(first keyvals)
(apply assoc (game-pref) keyvals))
pr-str
(pref! "nightmod.settings" :put-string pref-name)))
(defn restart-game!
[]
(on-gl (binding [*out* (StringWriter.)]
(try (eval (load-game-file "core.clj"))
(finally (reset! out (str *out*)))))))
(defscreen overlay-screen
:on-show
(fn [screen entities]
(update! screen :camera (orthographic) :renderer (stage))
(-> (label "" (color :white)
:set-wrap true
:set-alignment (bit-or (align :left) (align :bottom)))
(scroll-pane (style :scroll-pane nil nil nil nil nil))
(assoc :id :text :x 5 :y 5)))
:on-render
(fn [screen entities]
(->> (for [e entities]
(case (:id e)
:text (do (label! (scroll-pane! e :get-widget)
:set-text (or @out ""))
e)
e))
(render! screen)))
:on-resize
(fn [{:keys [width height] :as screen} entities]
(height! screen height)
(for [e entities]
(case (:id e)
:text (assoc e :width width :height (- height (* 2 (:y e))))
e))))
(set-screen-wrapper!
(fn [screen screen-fn]
(binding [*out* (StringWriter.)]
(try (screen-fn)
(finally (when (seq (str *out*))
(reset! out (str *out*))))))))
(set-asset-manager! (asset-manager))
(defgame my-game-game
:on-create
(fn [this]
(restart-game!)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment