Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
WIP rendering all players
Browse files Browse the repository at this point in the history
  • Loading branch information
tskardal committed Feb 10, 2015
1 parent 2e36736 commit 5924768
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 50 deletions.
4 changes: 1 addition & 3 deletions resources/public/game.html
Expand Up @@ -8,9 +8,7 @@
<![endif]-->
<link rel="stylesheet" href="/css/main.css" type="text/css" media="screen" />
</head>
<body>

<canvas id="game" width="500" height="500"></canvas>
<body>

<!-- pointing to cljsbuild generated js file -->
<script src="/js/battlesnake.js"></script>
Expand Down
1 change: 1 addition & 0 deletions resources/public/index.html
Expand Up @@ -7,6 +7,7 @@
</head>
<body>
<div id="lobby"></div>
<canvas id="game" width="500" height="500"></canvas>
<script src="js/battlesnake.js"></script>
<script type="text/javascript">
battlesnake.lobby.init(document.getElementById("lobby"));
Expand Down
2 changes: 1 addition & 1 deletion src/clj/battlesnake/game.clj
Expand Up @@ -16,7 +16,7 @@
(let [ug (s/tick @state)]
; TODO game over?
(doseq [p players]
(>! (:ws p) (str "updated game: " ug)))
(>! (:ws p) {:type :tick :game ug}))
(reset! state ug)
(<! (timeout 1000))
(recur)))))
2 changes: 1 addition & 1 deletion src/cljs/battlesnake/connect.cljs
@@ -1,4 +1,4 @@
(ns cljs.battlesnake.connect
(:require [clojure.browser.repl :as repl]))

;(repl/connect "http://localhost:9000/repl")
(repl/connect "http://localhost:9000/repl")
28 changes: 19 additions & 9 deletions src/cljs/battlesnake/lobby.cljs
@@ -1,8 +1,9 @@
(ns battlesnake.lobby
(:require [chord.client :refer [ws-ch]]
(:require [ajax.core :refer [GET POST]]
[battlesnake.snake :as snake]
[chord.client :refer [ws-ch]]
[cljs.core.async :refer [chan <! >! put! close! timeout]]
[reagent.core :as reagent :refer [atom]]
[ajax.core :refer [GET POST]])
[reagent.core :as reagent :refer [atom]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]))

(def my-id (atom ""))
Expand All @@ -17,8 +18,7 @@
(defn error-handler [{:keys [status status-text]}]
(.log js/console (str "something bad happened: " status " " status-text)))

(defn- on-create-game [name]
(js/console.log (str "name: " name))
(defn- on-create-game [name]
(reset! game-name "")
(POST "/games"
{:params {:game-name name
Expand Down Expand Up @@ -66,13 +66,26 @@
[create-game]
[game-list]])

(def game-state (atom {}))

(defmulti on-msg :type)

(defmethod on-msg :joined [{:keys [player-id game]}]
(reset! my-id player-id)
(reset! active-game game)
(js/console.log (str "active game: " @active-game)))

(defmethod on-msg :starting [_]
(js/console.log "starting")
(go
(<! (timeout 3000))
;; TODO eww. this shared atom smells
(snake/start (.querySelector js/document "canvas#game") game-state)))

(defmethod on-msg :tick [{game :game}]
(js/console.log (str "tick for game " game))
(reset! game-state game))

(defmethod on-msg :default [msg]
(js/console.log (str "Unhandled: " msg)))

Expand All @@ -90,10 +103,7 @@
(<! (timeout 5000))
(recur)))

(defn ^:export init [parent]
(js/console.log "before")
(snake/update)
(js/console.log "after")
(defn ^:export init [parent]
(go (let [{:keys [ws-channel]} (<! (ws-ch "ws://localhost:3000/ws"))]
(listen ws-channel)
(reset! ws ws-channel)))
Expand Down
66 changes: 30 additions & 36 deletions src/cljs/battlesnake/snake.cljs
@@ -1,7 +1,6 @@
(ns battlesnake.snake
(:require [reagent.core :as reagent :refer [atom]]))

(def snake (atom {}))
(def dirs {37 [-1 0] ; left
38 [0 -1] ; up
39 [1 0] ; right
Expand All @@ -19,16 +18,6 @@
{:color "rgb(0, 200, 0)"
:location [(rand-int 50) (rand-int 50)]})

(defn create-snake []
{:body (reverse (take 15 (map vector (range) (repeat 5))))
:dir [1 0]
:type :snake})

(defn sync
"Perform synchronization with the server"
[]
)

(defn render-snake [ctx snake]
(set! (.-fillStyle ctx) "rgb(200, 0, 200)")
(doseq [[x y] (:body snake)]
Expand All @@ -49,6 +38,7 @@
(defn- add-points [& points]
(vec (apply map + points)))

;; TODO move to cljx
(defn- crash? [loc {body :body}]
(or
(some #(= % loc) body)
Expand All @@ -59,40 +49,44 @@
(< y 0)
(>= y grid-height)))))

(defn- move-snake []
(let [h (first (:body @snake))
dir (:dir @snake)
nh (add-points h dir)]
(if (crash? nh @snake)
(do
(js/alert "Game over!")
(reset! snake (create-snake))
(reset! edibles []))
(if-let [e (seq (filter #(= nh (:location %)) @edibles))]
(do
(swap! edibles #(filter (fn [x] (not= nh (:location x))) @edibles))
(swap! snake assoc :body (cons nh (:body @snake))))
(swap! snake assoc :body (cons nh (butlast (:body @snake))))))))
(comment (defn- move-snake []
(let [h (first (:body @snake))
dir (:dir @snake)
nh (add-points h dir)]
(if (crash? nh @snake)
(do
(js/alert "Game over!")
(reset! snake (create-snake))
(reset! edibles []))
(if-let [e (seq (filter #(= nh (:location %)) @edibles))]
(do
(swap! edibles #(filter (fn [x] (not= nh (:location x))) @edibles))
(swap! snake assoc :body (cons nh (:body @snake))))
(swap! snake assoc :body (cons nh (butlast (:body @snake)))))))))

(defn handle-input [e]
(when-let [dir (dirs (js/parseInt (aget e "keyCode")))]
(swap! snake assoc :dir dir)))

(defn ^:export init [canvas]
(reset! snake (create-snake))
(defn render-game [ctx game]
(doseq [p (:players game)]
(js/console.log (str "Rendering player " p))
(render-snake ctx p)))

(defn start [canvas game]
(let [body (aget js/document "body")]
(.addEventListener body "keydown" handle-input))

(js/setInterval move-snake 60)
(js/setInterval (fn []
(when (< 2 (count @edibles))
(swap! edibles butlast))
(swap! edibles conj (create-edible)) ; TODO ensure no duplicates
(js/console.log (str "edibles: " @edibles))) 5000)
(comment s
(js/setInterval move-snake 60)
(js/setInterval (fn []
(when (< 2 (count @edibles))
(swap! edibles butlast))
(swap! edibles conj (create-edible)) ; TODO ensure no duplicates
(js/console.log (str "edibles: " @edibles))) 5000))
(let [ctx (.getContext canvas "2d")]
(letfn [(render []
(.clearRect ctx 0 0 500 500)
(render-snake ctx @snake)
(render-edibles ctx @edibles)
(render-game ctx @game)
; (render-edibles ctx @edibles)
(.requestAnimationFrame js/window render))]
(.requestAnimationFrame js/window render))))

0 comments on commit 5924768

Please sign in to comment.