From 6b92812ab2d9a8b5ed11e1a5ce390c6f41a7b10d Mon Sep 17 00:00:00 2001 From: oakes Date: Sun, 29 Dec 2013 20:22:14 -0500 Subject: [PATCH] Add 2d and global functions --- common/src/play_clj/2d.clj | 45 +++++++++++++++++++++++++++ common/src/play_clj/core.clj | 37 +++++++++++++--------- common/src/play_clj/global.clj | 57 ++++++++++++++++++++++++++++++++++ common/src/play_clj/render.clj | 24 -------------- 4 files changed, 124 insertions(+), 39 deletions(-) create mode 100644 common/src/play_clj/2d.clj create mode 100644 common/src/play_clj/global.clj diff --git a/common/src/play_clj/2d.clj b/common/src/play_clj/2d.clj new file mode 100644 index 0000000..5ced4f9 --- /dev/null +++ b/common/src/play_clj/2d.clj @@ -0,0 +1,45 @@ +(in-ns 'play-clj.core) + +; drawing + +(defmulti sprite-batch #(-> % :renderer class) :default nil) + +(defmethod sprite-batch nil [screen] + (SpriteBatch.)) + +(defmethod sprite-batch BatchTiledMapRenderer [screen] + (.getSpriteBatch (:renderer screen))) + +(defn draw! + ([screen] + (draw! screen (:entities screen))) + ([screen entities] + (let [batch (sprite-batch screen)] + (.begin batch) + (doseq [{:keys [image x y width height]} entities] + (when (and image x y width height) + (.draw batch image (float x) (float y) (float width) (float height)))) + (.end batch) + batch))) + +; textures + +(defn image + [^String internal-path] + (-> internal-path Texture. TextureRegion.)) + +(defn split-image + ([^String internal-path size] + (split-image internal-path size size)) + ([^String internal-path width height] + (-> internal-path image (.split width height)))) + +(defmacro animation + [& args] + `(Animation. ~@args)) + +(defn get-animation-frame + ([screen ^Animation animation] + (get-animation-frame screen animation true)) + ([screen ^Animation animation is-looping?] + (.getKeyFrame animation (:total-time screen) is-looping?))) diff --git a/common/src/play_clj/core.clj b/common/src/play_clj/core.clj index 9eb01db..f765743 100644 --- a/common/src/play_clj/core.clj +++ b/common/src/play_clj/core.clj @@ -1,8 +1,8 @@ (ns play-clj.core - (:import [com.badlogic.gdx Game Gdx Screen] - [com.badlogic.gdx.graphics.g2d SpriteBatch] - [com.badlogic.gdx.graphics - Camera Color GL20 OrthographicCamera PerspectiveCamera] + (:import [com.badlogic.gdx Game Gdx Input$Keys Screen] + [com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera + PerspectiveCamera Texture] + [com.badlogic.gdx.graphics.g2d Animation SpriteBatch TextureRegion] [com.badlogic.gdx.maps.tiled TmxMapLoader] [com.badlogic.gdx.maps.tiled.renderers BatchTiledMapRenderer @@ -11,14 +11,18 @@ IsometricTiledMapRenderer OrthogonalTiledMapRenderer])) +(load "2d") +(load "global") (load "render") -(defn clear! - ([] (clear! 0 0 0 0)) - ([r g b a] - (doto (Gdx/gl) - (.glClearColor r g b a) - (.glClear GL20/GL_COLOR_BUFFER_BIT)))) +(defn find-pos + [val coll] + (let [pos (if (number? val) + val + (.indexOf coll val))] + (if (and (>= pos 0) (< pos (count coll))) + pos + nil))) (defn defscreen* [{:keys [on-show on-render on-dispose on-hide on-pause on-resize on-resume @@ -37,12 +41,15 @@ (conj (:entities @screen)) (swap! screen assoc :entities))) rem-entity (fn [entity] - (->> (:entities @screen) - (remove #(= % entity)) - (swap! screen assoc :entities))) + (when-let [pos (find-pos entity (:entities @screen))] + (->> (subvec (:entities @screen) (inc pos)) + (concat (subvec (:entities @screen) 0 pos)) + vec + (swap! screen assoc :entities)))) upd-entity (fn [entity args] - (rem-entity entity) - (add-entity (apply assoc entity args)))] + (when-let [pos (find-pos entity (:entities @screen))] + (swap! screen assoc-in + [:entities pos] (apply assoc entity args))))] (proxy [Screen] [] (show [] (swap! screen assoc diff --git a/common/src/play_clj/global.clj b/common/src/play_clj/global.clj new file mode 100644 index 0000000..723c932 --- /dev/null +++ b/common/src/play_clj/global.clj @@ -0,0 +1,57 @@ +(in-ns 'play-clj.core) + +; graphics + +(defn clear! + ([] (clear! 0 0 0 0)) + ([r g b a] + (doto (Gdx/gl) + (.glClearColor r g b a) + (.glClear GL20/GL_COLOR_BUFFER_BIT)))) + +(defn game* + [key] + (case key + :width `(.getWidth (Gdx/graphics)) + :height `(.getHeight (Gdx/graphics)) + :is-fullscreen? `(.isFullscreen (Gdx/graphics)) + :is-touched? `(.isTouched (Gdx/input)) + :x `(.getX (Gdx/input)) + :y `(.getY (Gdx/input)) + nil)) + +(defmacro game + [key] + `~(game* key)) + +; input + +(defn resolve-key + [key] + (if (keyword? key) + (case key + :up Input$Keys/DPAD_UP + :down Input$Keys/DPAD_DOWN + :left Input$Keys/DPAD_LEFT + :right Input$Keys/DPAD_RIGHT + nil) + key)) + +(defn resolve-touch + [key] + (case key + :down `(> (game :y) (* (game :height) (/ 2 3))) + :up `(< (game :y) (/ (game :height) 3)) + :left `(< (game :x) (/ (game :width) 3)) + :right `(> (game :x) (* (game :width) (/ 2 3))) + false)) + +(defmacro is-pressed? + [key] + `(.isKeyPressed (Gdx/input) ~(resolve-key key))) + +(defmacro is-touched? + ([] + `(game :is-touched?)) + ([key] + `(and (game :is-touched?) ~(resolve-touch key)))) diff --git a/common/src/play_clj/render.clj b/common/src/play_clj/render.clj index f1aa318..ee0c725 100644 --- a/common/src/play_clj/render.clj +++ b/common/src/play_clj/render.clj @@ -1,5 +1,3 @@ -(ns play-clj.render) - (in-ns 'play-clj.core) ; renderers @@ -46,25 +44,3 @@ (defn resize-camera! [{:keys [^Camera camera]} width height] (.setToOrtho camera false width height)) - -; draw entities - -(defmulti sprite-batch #(-> % :renderer class) :default nil) - -(defmethod sprite-batch nil [screen] - (SpriteBatch.)) - -(defmethod sprite-batch BatchTiledMapRenderer [screen] - (.getSpriteBatch (:renderer screen))) - -(defn draw! - ([screen] - (draw! screen (:entities screen))) - ([screen entities] - (let [batch (sprite-batch screen)] - (.begin batch) - (doseq [{:keys [image x y width height]} entities] - (when (and image x y width height) - (.draw batch image (float x) (float y) (float width) (float height)))) - (.end batch) - batch)))