src/clojurecraft/events.clj @ 4c1055d72377

Hurr durr.  Hurr.
author Steve Losh <steve@stevelosh.com>
date Fri, 26 Aug 2011 18:40:45 -0400
parents 7aa6da5b1d8b
children (none)
(ns clojurecraft.events
  (:use [clojurecraft.actions :only [handle-action-group]]))

; Event Handlers -------------------------------------------------------------------
(defn- fire-handler [bot event-type & args]
  (let [action-groups (map #(apply (deref %) (into [bot] args))
                           (event-type @(:event-handlers bot)))]
    (dorun (map handle-action-group (cycle [bot]) action-groups))))

(defn fire-chat [bot message]
  (fire-handler bot :chat message))

(defn fire-dead [bot]
  (fire-handler bot :dead))


(defn add-handler [bot event-type handler]
  (dosync
    (let [current-handlers (event-type @(:event-handlers bot))
          updated-handlers (conj current-handlers handler)]
      (alter (:event-handlers bot) assoc event-type updated-handlers))))

(defn clear-handlers [bot event-type]
  (dosync (alter (:event-handlers bot) dissoc event-type)))