7aa6da5b1d8b

Factor out the loop stuff into its own namespace/file.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 26 Aug 2011 18:09:25 -0400
parents 1e03d5cc372b
children a587910b3c7d
branches/tags (none)
files src/clojurecraft/actions.clj src/clojurecraft/events.clj src/clojurecraft/loops.clj src/examples/jumpbot.clj

Changes

--- a/src/clojurecraft/actions.clj	Fri Aug 26 18:02:11 2011 -0400
+++ b/src/clojurecraft/actions.clj	Fri Aug 26 18:09:25 2011 -0400
@@ -3,6 +3,12 @@
   (:use [clojurecraft.out])
   (:require [clojurecraft.physics :as physics]))
 
+(defn handle-action-group [bot action-group]
+  (let [queue-action #(.put (:actionqueue bot) %)
+        queue-action-group #(dorun (map queue-action %))]
+    (queue-action-group action-group)))
+
+
 (defn move [bot x-change y-change z-change]
   (delay
     (let [player (:player bot)]
--- a/src/clojurecraft/events.clj	Fri Aug 26 18:02:11 2011 -0400
+++ b/src/clojurecraft/events.clj	Fri Aug 26 18:09:25 2011 -0400
@@ -1,12 +1,5 @@
-(ns clojurecraft.events)
-
-; Action Performing ----------------------------------------------------------------
-(defn- handle-action-group [bot action-group]
-  (println action-group)
-  (let [queue-action #(.put (:actionqueue bot) %)
-        queue-action-group #(dorun (map queue-action %))]
-    (queue-action-group action-group)))
-
+(ns clojurecraft.events
+  (:use [clojurecraft.actions :only [handle-action-group]]))
 
 ; Event Handlers -------------------------------------------------------------------
 (defn- fire-handler [bot event-type & args]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/clojurecraft/loops.clj	Fri Aug 26 18:09:25 2011 -0400
@@ -0,0 +1,19 @@
+(ns clojurecraft.loops
+  (:use [clojurecraft.actions :only [handle-action-group]]))
+
+; Loops ----------------------------------------------------------------------------
+(defn- run-loop [bot function sleep-ms loop-id]
+  (while (and (nil? (:exit @(:connection bot)))
+              (@(:loops bot) loop-id))
+    (handle-action-group bot ((deref function) bot))
+    (Thread/sleep sleep-ms))
+  (dosync (alter (:loops bot) dissoc loop-id)))
+
+
+(defn remove-loop [bot loop-id]
+  (dosync (alter (:loops bot) assoc loop-id nil)))
+
+(defn add-loop [bot function sleep-ms loop-id]
+  (dosync (alter (:loops bot) assoc loop-id :running))
+  (.start (Thread. #(run-loop bot function sleep-ms loop-id))))
+
--- a/src/examples/jumpbot.clj	Fri Aug 26 18:02:11 2011 -0400
+++ b/src/examples/jumpbot.clj	Fri Aug 26 18:09:25 2011 -0400
@@ -1,6 +1,7 @@
 (ns examples.jumpbot
   (:require [clojurecraft.core :as core])
   (:require [clojurecraft.events :as events])
+  (:require [clojurecraft.loops :as loops])
   (:require [clojurecraft.actions :as actions]))
 
 (defn jump [bot]
@@ -14,6 +15,6 @@
 (defn make-jumpbot [server username]
   (let [bot (core/connect server username)]
     (events/add-handler bot :dead #'handle-dead)
-    (events/add-loop bot #'jump 3000 :jump-loop)
+    (loops/add-loop bot #'jump 3000 :jump-loop)
     bot))