Add 2d and global functions
This commit is contained in:
45
common/src/play_clj/2d.clj
Normal file
45
common/src/play_clj/2d.clj
Normal file
@@ -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?)))
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
(ns play-clj.core
|
(ns play-clj.core
|
||||||
(:import [com.badlogic.gdx Game Gdx Screen]
|
(:import [com.badlogic.gdx Game Gdx Input$Keys Screen]
|
||||||
[com.badlogic.gdx.graphics.g2d SpriteBatch]
|
[com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera
|
||||||
[com.badlogic.gdx.graphics
|
PerspectiveCamera Texture]
|
||||||
Camera Color GL20 OrthographicCamera PerspectiveCamera]
|
[com.badlogic.gdx.graphics.g2d Animation SpriteBatch TextureRegion]
|
||||||
[com.badlogic.gdx.maps.tiled TmxMapLoader]
|
[com.badlogic.gdx.maps.tiled TmxMapLoader]
|
||||||
[com.badlogic.gdx.maps.tiled.renderers
|
[com.badlogic.gdx.maps.tiled.renderers
|
||||||
BatchTiledMapRenderer
|
BatchTiledMapRenderer
|
||||||
@@ -11,14 +11,18 @@
|
|||||||
IsometricTiledMapRenderer
|
IsometricTiledMapRenderer
|
||||||
OrthogonalTiledMapRenderer]))
|
OrthogonalTiledMapRenderer]))
|
||||||
|
|
||||||
|
(load "2d")
|
||||||
|
(load "global")
|
||||||
(load "render")
|
(load "render")
|
||||||
|
|
||||||
(defn clear!
|
(defn find-pos
|
||||||
([] (clear! 0 0 0 0))
|
[val coll]
|
||||||
([r g b a]
|
(let [pos (if (number? val)
|
||||||
(doto (Gdx/gl)
|
val
|
||||||
(.glClearColor r g b a)
|
(.indexOf coll val))]
|
||||||
(.glClear GL20/GL_COLOR_BUFFER_BIT))))
|
(if (and (>= pos 0) (< pos (count coll)))
|
||||||
|
pos
|
||||||
|
nil)))
|
||||||
|
|
||||||
(defn defscreen*
|
(defn defscreen*
|
||||||
[{:keys [on-show on-render on-dispose on-hide on-pause on-resize on-resume
|
[{:keys [on-show on-render on-dispose on-hide on-pause on-resize on-resume
|
||||||
@@ -37,12 +41,15 @@
|
|||||||
(conj (:entities @screen))
|
(conj (:entities @screen))
|
||||||
(swap! screen assoc :entities)))
|
(swap! screen assoc :entities)))
|
||||||
rem-entity (fn [entity]
|
rem-entity (fn [entity]
|
||||||
(->> (:entities @screen)
|
(when-let [pos (find-pos entity (:entities @screen))]
|
||||||
(remove #(= % entity))
|
(->> (subvec (:entities @screen) (inc pos))
|
||||||
(swap! screen assoc :entities)))
|
(concat (subvec (:entities @screen) 0 pos))
|
||||||
|
vec
|
||||||
|
(swap! screen assoc :entities))))
|
||||||
upd-entity (fn [entity args]
|
upd-entity (fn [entity args]
|
||||||
(rem-entity entity)
|
(when-let [pos (find-pos entity (:entities @screen))]
|
||||||
(add-entity (apply assoc entity args)))]
|
(swap! screen assoc-in
|
||||||
|
[:entities pos] (apply assoc entity args))))]
|
||||||
(proxy [Screen] []
|
(proxy [Screen] []
|
||||||
(show []
|
(show []
|
||||||
(swap! screen assoc
|
(swap! screen assoc
|
||||||
|
|||||||
57
common/src/play_clj/global.clj
Normal file
57
common/src/play_clj/global.clj
Normal file
@@ -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))))
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
(ns play-clj.render)
|
|
||||||
|
|
||||||
(in-ns 'play-clj.core)
|
(in-ns 'play-clj.core)
|
||||||
|
|
||||||
; renderers
|
; renderers
|
||||||
@@ -46,25 +44,3 @@
|
|||||||
(defn resize-camera!
|
(defn resize-camera!
|
||||||
[{:keys [^Camera camera]} width height]
|
[{:keys [^Camera camera]} width height]
|
||||||
(.setToOrtho camera false 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)))
|
|
||||||
|
|||||||
Reference in New Issue
Block a user