# HG changeset patch # User Steve Losh # Date 1314396565 14400 # Node ID 7aa6da5b1d8bc4f9b6e88bfc02f36b69af03747e # Parent 1e03d5cc372b00aedc77caf90141c2209bdfc7e2 Factor out the loop stuff into its own namespace/file. diff -r 1e03d5cc372b -r 7aa6da5b1d8b src/clojurecraft/actions.clj --- 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)] diff -r 1e03d5cc372b -r 7aa6da5b1d8b src/clojurecraft/events.clj --- 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] diff -r 1e03d5cc372b -r 7aa6da5b1d8b src/clojurecraft/loops.clj --- /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)))) + diff -r 1e03d5cc372b -r 7aa6da5b1d8b src/examples/jumpbot.clj --- 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))