Add dynamic method generation and change the API
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
(ns play-clj.core
|
||||
(:require [play-clj.utils :as utils])
|
||||
(:require [clojure.set :as set]
|
||||
[play-clj.utils :as utils])
|
||||
(:import [com.badlogic.gdx Game Gdx Input$Keys Screen]
|
||||
[com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera
|
||||
PerspectiveCamera Texture]
|
||||
@@ -19,7 +20,6 @@
|
||||
(load "core_2d")
|
||||
(load "core_global")
|
||||
(load "core_render")
|
||||
(load "core_ui")
|
||||
|
||||
(defn- dummy [& args])
|
||||
|
||||
|
||||
@@ -16,14 +16,6 @@
|
||||
|
||||
(defn draw-actor!
|
||||
[^SpriteBatch batch {:keys [^Actor actor] :as entity}]
|
||||
(doseq [[k v] entity]
|
||||
(case k
|
||||
:x (.setX actor (float v))
|
||||
:y (.setY actor (float v))
|
||||
:width (.setWidth actor (float v))
|
||||
:height (.setHeight actor (float v))
|
||||
:text (.setText actor (str v))
|
||||
nil))
|
||||
(.draw ^Actor actor batch 1))
|
||||
|
||||
(defn draw-image!
|
||||
@@ -33,12 +25,12 @@
|
||||
(defn draw-entity!
|
||||
[^SpriteBatch batch entity]
|
||||
(cond
|
||||
(isa? (type entity) Actor)
|
||||
(draw-actor! batch {:actor entity})
|
||||
(:actor entity)
|
||||
(draw-actor! batch entity)
|
||||
(:image entity)
|
||||
(draw-image! batch entity)
|
||||
(isa? (type entity) Actor)
|
||||
(draw-actor! batch {:actor entity})))
|
||||
(draw-image! batch entity)))
|
||||
|
||||
(defn draw! [{:keys [renderer] :as screen} entities]
|
||||
(assert renderer)
|
||||
@@ -51,38 +43,23 @@
|
||||
|
||||
; textures
|
||||
|
||||
(defn image
|
||||
[val & {:keys [] :as options}]
|
||||
(let [^TextureRegion
|
||||
img (if (string? val)
|
||||
(-> ^String val Texture. TextureRegion.)
|
||||
(TextureRegion. ^TextureRegion val))]
|
||||
(doseq [[k v] options]
|
||||
(case k
|
||||
:x (.setRegionX img v)
|
||||
:y (.setRegionY img v)
|
||||
:width (.setRegionWidth img v)
|
||||
:height (.setRegionHeight img v)
|
||||
:region (.setRegion img
|
||||
^long (nth v 0) ^long (nth v 1)
|
||||
^long (nth v 2) ^long (nth v 3))
|
||||
:flip (.flip img (nth v 0) (nth v 1))
|
||||
nil))
|
||||
(defn create-image*
|
||||
[img]
|
||||
(cond
|
||||
(string? img)
|
||||
(-> ^String img Texture. TextureRegion.)
|
||||
(isa? img TextureRegion)
|
||||
(TextureRegion. ^TextureRegion img)
|
||||
:else
|
||||
img))
|
||||
|
||||
(defn image-width
|
||||
[^TextureRegion img]
|
||||
(.getRegionWidth img))
|
||||
(defmacro create-image
|
||||
[img & options]
|
||||
`(utils/calls! ^TextureRegion (create-image* ~img) ~@options))
|
||||
|
||||
(defn image-height
|
||||
[^TextureRegion img]
|
||||
(.getRegionHeight img))
|
||||
|
||||
(defn split-image
|
||||
([val size]
|
||||
(split-image val size size))
|
||||
([val width height]
|
||||
(-> val ^TextureRegion image (.split width height))))
|
||||
(defmacro image
|
||||
[img k & options]
|
||||
`(utils/call! ^TextureRegion ~img ~k ~@options))
|
||||
|
||||
(defmacro animation
|
||||
[duration images & args]
|
||||
|
||||
@@ -10,6 +10,12 @@
|
||||
(.glClearColor (float r) (float g) (float b) (float a))
|
||||
(.glClear GL20/GL_COLOR_BUFFER_BIT))))
|
||||
|
||||
(defmacro color
|
||||
[& args]
|
||||
`~(if (keyword? (first args))
|
||||
`(Color. ^Color (utils/gdx-static-field :graphics :Color ~(first args)))
|
||||
`(Color. ~@args)))
|
||||
|
||||
(defn game*
|
||||
[key]
|
||||
(case key
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
(in-ns 'play-clj.core)
|
||||
|
||||
(defmacro color
|
||||
[& args]
|
||||
`~(if (keyword? (first args))
|
||||
`(Color. ^Color (utils/gdx-static-field :graphics :Color ~(first args)))
|
||||
`(Color. ~@args)))
|
||||
|
||||
(defn create-actor
|
||||
[actor]
|
||||
{:actor actor :x 0 :y 0})
|
||||
|
||||
(defn label
|
||||
[text color]
|
||||
(->> (Label$LabelStyle. (BitmapFont.) color)
|
||||
(Label. text)
|
||||
create-actor))
|
||||
@@ -26,3 +26,25 @@
|
||||
(defn gdx-into-array
|
||||
[a]
|
||||
(Array. true (into-array a) 1 (count a)))
|
||||
|
||||
(defn key->method
|
||||
[k]
|
||||
(let [parts (split-key k)]
|
||||
(->> (rest parts)
|
||||
(map s/capitalize)
|
||||
(cons (first parts))
|
||||
(s/join "")
|
||||
(str ".")
|
||||
symbol)))
|
||||
|
||||
(defmacro call!
|
||||
[obj k & args]
|
||||
`(~(key->method k) ~obj ~@(flatten args)))
|
||||
|
||||
(defn calls!*
|
||||
[[k v]]
|
||||
(flatten (list (key->method k) v)))
|
||||
|
||||
(defmacro calls!
|
||||
[obj & {:keys [] :as args}]
|
||||
`(doto ~obj ~@(map calls!* args)))
|
||||
|
||||
Reference in New Issue
Block a user