display.cljs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. (ns bombnet.web.display)
  2. (defn init-display []
  3. (let [display (new js/ROT.Display (js-obj "height" 17 "width" 36))]
  4. (.appendChild (.. js/document -body) (.getContainer display))
  5. display))
  6. (defonce display (init-display))
  7. (defn ^:private %c [color & strings]
  8. (str "%c{" color "}" (apply str strings) "%c{}"))
  9. (defn render-game [game]
  10. (let [{state :state {local-id :id} :local} @game
  11. {:keys [board players bombs explosion]} state
  12. board-list (apply concat (map-indexed (fn [y row]
  13. (map-indexed (fn [x c]
  14. [x y c])
  15. row)) board))]
  16. (.clear display)
  17. (doseq [[x y c] board-list]
  18. (.draw display x y c))
  19. (doseq [[x y] explosion]
  20. (.draw display x y "X" "orange"))
  21. (doseq [{[x y] :pos counter :counter :or {counter -1}} bombs]
  22. (if (< counter 0)
  23. (.draw display x y "Q" "orange")
  24. (.draw display x y counter "orange")))
  25. (doseq [{[x y] :pos color :color} players]
  26. (.draw display x y "@" color))
  27. (.drawText display 23 0 "Players")
  28. (doseq [[index {id :id color :color}] (map-indexed #(vector % %2) (sort-by :color players))]
  29. (if (= local-id id)
  30. (.drawText display 22 (+ 2 index) (str "*" (%c color id)))
  31. (.drawText display 23 (+ 2 index) (%c color id))))))