1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- (ns bombnet.web.game
- (:require [cljs.core.async :refer [<! >! chan]]
- [bombnet.web.display :refer [render-game]])
- (:require-macros [cljs.core.async.macros :refer [go]]))
- (defrecord Game [conn state local])
- (defn ^:private send-msg [game & params]
- (let [{conn :conn} @game]
- (bombnet.web.ws/send! conn (apply hash-map (conj params :type)))))
- (defn ^:private send-action [game & params]
- (let [action (apply hash-map (conj params :type))]
- (send-msg game "action"
- :action action)
- (swap! game update [:local :action] action)))
- (defn action-move [game dir]
- (send-action game "move" :dir dir))
- (defn action-bomb [game timer]
- (let [diagonal? (get-in @game [:local :diagonal?])]
- (send-action game "bomb" :timer timer :diagonal? diagonal?)))
- (defn action-nop [game]
- (send-action game "nop"))
- (defn toggle-diagonal [game]
- (swap! game update-in [:local :diagonal?] not))
- (defn msg-handler [game msg]
- (cond
- (:game msg) (do
- (swap! game assoc :state (:game msg))
- (render-game game))
- (:message msg) (.log js/console (:message msg))))
- (defn key-handler [game code]
- (let [{conn :conn} game
- k (fn [v] (aget js/ROT (str "VK_" v)))
- move (partial action-move game)
- bomb (partial action-bomb game)]
- (condp #(= (k %) %2) code
- "R" (send-msg game "start")
- "X" (toggle-diagonal game)
- "PERIOD" (action-nop game)
- "UP" (move "up")
- "DOWN" (move "down")
- "LEFT" (move "left")
- "RIGHT" (move "right")
- "1" (bomb 1)
- "2" (bomb 2)
- "3" (bomb 3)
- "4" (bomb 4)
- "5" (bomb 5)
- nil)))
- (defn init-game [id conn]
- (atom (->Game conn nil {:id id})))
|