Add timer

This commit is contained in:
oakes
2014-02-22 18:59:10 -05:00
parent 030c823699
commit a806010b17
3 changed files with 71 additions and 16 deletions

View File

@@ -24,11 +24,13 @@
[com.badlogic.gdx.physics.box2d ContactListener Joint World]
[com.badlogic.gdx.scenes.scene2d Actor Stage]
[com.badlogic.gdx.scenes.scene2d.utils ActorGestureListener Align
ChangeListener ClickListener DragListener FocusListener]))
ChangeListener ClickListener DragListener FocusListener]
[com.badlogic.gdx.utils Timer$Task]))
(load "core_global")
(load "core_graphics")
(load "core_listeners")
(load "core_utils")
(defn ^:private reset-changed!
"Internal use only"
@@ -38,7 +40,7 @@
(defn defscreen*
"Internal use only"
[{:keys [on-show on-render on-hide on-pause on-resize on-resume]
[{:keys [on-show on-render on-hide on-pause on-resize on-resume on-timer]
:as options}]
(let [screen (atom {})
entities (atom [])
@@ -63,6 +65,8 @@
(swap! screen assoc
:total-time 0
:update-fn! #(apply swap! screen %1 %2)
:execute-fn! execute-fn!
:on-timer on-timer
:ui-listeners (ui-listeners options execute-fn!)
:g2dp-listener (contact-listener options execute-fn!))
(execute-fn! on-show))

View File

@@ -0,0 +1,65 @@
(in-ns 'play-clj.core)
(defmacro scaling
"Returns a static field from [Scaling](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Scaling.html)
(scaling :fill)
(scaling :fill-x)
(scaling :fill-y)
(scaling :fit)
(scaling :none)
(scaling :stretch)
(scaling :stretch-x)
(scaling :stretch-y)"
[k]
`~(u/gdx-field :utils :Scaling k))
(defn ^:private task*
"Internal use only."
[{:keys [execute-fn! on-timer]} id]
(proxy [Timer$Task] []
(run []
(execute-fn! on-timer :id id))))
(defn ^:private timer*
"Internal use only."
[]
(some-> (Class/forName "com.badlogic.gdx.utils.Timer")
(try (catch Exception _))
.newInstance))
(defn ^:private create-and-add-timer!
"Internal use only."
[{:keys [update-fn!] :as screen} id]
(when-let [timer (timer*)]
(update-fn! assoc-in [[:timers id] timer])
timer))
(defn add-timer!
"Returns a [Timer](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Timer.html)
that runs the :on-timer function according to the given arguments
; wait 2 seconds and run once
(add-timer! screen :spawn-enemy 2)
; wait 2 seconds and run forever at 10 second intervals
(add-timer! screen :spawn-enemy 2 10)
; wait 2 seconds, run once, and then run 3 more times at 10 second intervals
(add-timer! screen :spawn-enemy 2 10 3)"
([screen id delay]
(doto (create-and-add-timer! screen id)
(.scheduleTask (task* screen id) delay)))
([screen id delay interval]
(doto (create-and-add-timer! screen id)
(.scheduleTask (task* screen id) delay interval)))
([screen id delay interval repeat]
(doto (create-and-add-timer! screen id)
(.scheduleTask (task* screen id) delay interval repeat))))
(defn remove-timer!
"Stops and removes the timer associated with `id`, returning it or nil if not
found"
[{:keys [update-fn!] :as screen} id]
(when-let [timer (get-in screen [:timers id])]
(.stop timer)
(update-fn! update-in [[:timers] dissoc id])
timer))

View File

@@ -86,20 +86,6 @@
[& args]
(apply gdx (add-divider args "$")))
(defmacro scaling
"Returns a static field from [Scaling](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Scaling.html)
(scaling :fill)
(scaling :fill-x)
(scaling :fill-y)
(scaling :fit)
(scaling :none)
(scaling :stretch)
(scaling :stretch-x)
(scaling :stretch-y)"
[k]
`~(gdx-field :utils :Scaling k))
; java interop
(defmacro call!