Add timer
This commit is contained in:
65
src/play_clj/core_utils.clj
Normal file
65
src/play_clj/core_utils.clj
Normal 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))
|
||||
Reference in New Issue
Block a user