Add window->screen and screen->window

This commit is contained in:
oakes
2014-04-19 11:50:11 -04:00
parent 47cf7a9111
commit dde4541314
2 changed files with 75 additions and 26 deletions

View File

@@ -1,6 +1,7 @@
(ns play-clj.core (ns play-clj.core
(:require [clojure.set] (:require [clojure.set]
[play-clj.entities :as e] [play-clj.entities :as e]
[play-clj.math :as m]
[play-clj.utils :as u]) [play-clj.utils :as u])
(:import [com.badlogic.gdx Application Audio Files Game Gdx Graphics Input (:import [com.badlogic.gdx Application Audio Files Game Gdx Graphics Input
InputMultiplexer InputProcessor Net Screen] InputMultiplexer InputProcessor Net Screen]

View File

@@ -239,6 +239,48 @@ in the `layer`.
[object k & options] [object k & options]
`(u/call! ^MapObject ~object ~k ~@options)) `(u/call! ^MapObject ~object ~k ~@options))
; coordinates
(defn screen->window
"Returns a map with the provided x,y,z values converted from screen to window
coordinates.
(screen->window 10 10)
(screen->window 10 10 0)
(screen->window {:x 10 :y 10 :z 0})"
([screen {:keys [x y z] :or {x 0 y 0 z 0} :as entity}]
(let [^Camera camera (u/get-obj screen :camera)
coords (m/vector-3 x y z)]
(.project camera coords)
(assoc entity
:x (. coords x)
:y (. coords y)
:z (. coords z))))
([screen x y]
(screen->window screen {:x x :y y}))
([screen x y z]
(screen->window screen {:x x :y y :z z})))
(defn window->screen
"Returns a map with the provided x,y,z values converted from window to screen
coordinates.
(window->screen 10 10)
(window->screen 10 10 0)
(window->screen {:x 10 :y 10 :z 0})"
([screen {:keys [x y z] :or {x 0 y 0 z 0} :as entity}]
(let [^Camera camera (u/get-obj screen :camera)
coords (m/vector-3 x y z)]
(.unproject camera coords)
(assoc entity
:x (. coords x)
:y (. coords y)
:z (. coords z))))
([screen x y]
(window->screen screen {:x x :y y}))
([screen x y z]
(window->screen screen {:x x :y y :z z})))
(defn ^:private tiled-map-prop (defn ^:private tiled-map-prop
[screen] [screen]
(let [^BatchTiledMapRenderer renderer (u/get-obj screen :renderer) (let [^BatchTiledMapRenderer renderer (u/get-obj screen :renderer)
@@ -250,36 +292,42 @@ in the `layer`.
:height (.get prop "height")})) :height (.get prop "height")}))
(defn screen->isometric (defn screen->isometric
"Returns a copy of the provided map with x/y values converted from screen "Returns a map with the provided x,y values converted from screen to isometric
to isometric map coordinates. map coordinates.
(screen->isometric screen {:x 64 :y 32})" (screen->isometric screen {:x 64 :y 32})
[screen {:keys [x y] :as entity}] (screen->isometric screen 64 32)"
(let [{:keys [unit-scale tile-width tile-height]} (tiled-map-prop screen) ([screen {:keys [x y] :or {x 0 y 0} :as entity}]
half-tile-width (/ (* tile-width unit-scale) 2) (let [{:keys [unit-scale tile-width tile-height]} (tiled-map-prop screen)
half-tile-height (/ (* tile-height unit-scale) 2)] half-tile-width (/ (* tile-width unit-scale) 2)
(assoc entity half-tile-height (/ (* tile-height unit-scale) 2)]
:x (/ (- (/ x half-tile-width) (assoc entity
(/ y half-tile-height)) :x (/ (- (/ x half-tile-width)
2) (/ y half-tile-height))
:y (/ (+ (/ y half-tile-height) 2)
(/ x half-tile-width)) :y (/ (+ (/ y half-tile-height)
2)))) (/ x half-tile-width))
2))))
([screen x y]
(screen->isometric screen {:x x :y y})))
(defn isometric->screen (defn isometric->screen
"Returns a copy of the provided map with x/y values converted from isometric "Returns a map with the provided x,y values converted from isometric map to
map to screen coordinates. screen coordinates.
(isometric->screen screen {:x 2 :y 1})" (isometric->screen screen {:x 2 :y 1})
[screen {:keys [x y] :as entity}] (isometric->screen screen 2 1)"
(let [{:keys [unit-scale tile-width tile-height]} (tiled-map-prop screen) ([screen {:keys [x y] :as entity}]
half-tile-width (/ (* tile-width unit-scale) 2) (let [{:keys [unit-scale tile-width tile-height]} (tiled-map-prop screen)
half-tile-height (/ (* tile-height unit-scale) 2)] half-tile-width (/ (* tile-width unit-scale) 2)
(assoc entity half-tile-height (/ (* tile-height unit-scale) 2)]
:x (+ (* x half-tile-width) (assoc entity
(* y half-tile-width)) :x (+ (* x half-tile-width)
:y (+ (* -1 x half-tile-height) (* y half-tile-width))
(* y half-tile-height))))) :y (+ (* -1 x half-tile-height)
(* y half-tile-height)))))
([screen x y]
(isometric->screen screen {:x x :y y})))
; renderers ; renderers