Refactored drawing code, and started filling out code to draw and move player. Left and right keys move player around the board, but it's unfinished.
(ns tempest.levels
- (:require [tempest.util :as util]
- [goog.dom :as dom]
- [goog.Timer :as timer]
- [goog.events :as events]
- [goog.events.EventType :as event-type]
- [goog.math :as math]))
+ (:require [tempest.util :as util]))
;;;;
;;;; Levels are defined by a vector of polar coordinates [r theta],
[goog.dom :as dom]
[goog.Timer :as timer]
[goog.events :as events]
- [goog.events.EventType :as event-type]
- [goog.math :as math]))
+ [goog.math :as math]
+ [goog.events.KeyHandler :as key-handler]
+ [goog.events.KeyCodes :as key-codes]
+ [clojure.browser.repl :as repl]))
+
+;;(repl/connect "http://localhost:9000/repl")
;;
;; TODO:
[10 85]
[20 135]])
+(def player-path
+ [[50 90]
+ [44 196]
+ [27 333]
+ [17 135]
+ [30 11]
+ [30 349]
+ [17 225]
+ [27 27]
+ [44 164]])
+
(def flipper-path
[[32 16]
:draw-fn flipper-path-with-width
:level level})
+(defn build-player [level seg-idx]
+ {:segment seg-idx
+ :level level
+ :step (+ 10 (:steps level))})
+
(defn update-enemy-position [enemy]
(if (< (:step enemy) (:steps (:level enemy)))
(assoc enemy :step (+ (:stride enemy) (:step enemy)))
point0 (polar-extend offset0 (first edges))
point1 (polar-extend offset1 (peek edges))]
(polar-distance point0 point1)))
-
+
+(defn player-path-on-level [player]
+ (let [coord (polar-player-coord player)]
+ (rotate-path
+ (enemy-angle player)
+ player-path)))
+
(defn flipper-path-on-level [flipper]
(let [coord (polar-enemy-coord flipper)]
(rotate-path
[(apply #(step-length-line level %1 %2) line0)
(apply #(step-length-line level %1 %2) line1)]))
-(defn draw-board [level]
- (let [canvas (dom/getElement "canv1")
- context (.getContext canvas "2d")
- timer (goog.Timer. 500)
- dims {:width (.-width canvas) :height (.-height canvas)}
- enemy1 (build-enemy level 0 :step 0)
- enemy2 (build-enemy level 3 :step 20)
- enemy3 (build-enemy level 8 :step 80)
- enemy4 (build-enemy level 11 :step 100)]
-
- ;;(def enemy-list (vec (map update-enemy-position enemy-list)))
- ;;(def enemy-list (map update-enemy-position enemy-list))
- ;;(dorun (map update-enemy-position enemy-list))
- (def enemy-list (update-enemy-list enemy-list))
-
- (.clearRect context 0 0 (:width dims) (:height dims))
+(defn polar-player-coord [player]
+ (let [steplen (step-length-segment-midpoint (:level player)
+ (:segment player))
+ offset (* steplen (:steps (:level player)))
+ midpoint (segment-midpoint (:level player) (:segment player))]
+ (polar-extend offset midpoint)))
+
+(defn draw-line [context point0 point1]
+ (.moveTo context (first point0) (peek point0))
+ (.lineTo context (first point1) (peek point1))
+ (.stroke context))
+
+(defn draw-player [context dims level player]
+ (doseq []
(.beginPath context)
-
- (comment (draw-path context
- (polar-to-cartesian-centered (polar-enemy-coord enemy1) dims)
- (flipper-path-on-level enemy1))
- (draw-path context
- (polar-to-cartesian-centered (polar-enemy-coord enemy2) dims)
- (flipper-path-on-level enemy2))
(draw-path context
- (polar-to-cartesian-centered (polar-enemy-coord enemy3) dims)
- (flipper-path-on-level enemy3))
+ (vec (map js/Math.round
+ (polar-to-cartesian-centered
+ (polar-player-coord player)
+ dims)))
+ (player-path-on-level player))
+ (.closePath context)
+ (.beginPath context)
+ (draw-line context
+ [(/ (:width dims) 2) (/ (:height dims) 2)]
+ (vec (map js/Math.round
+ (polar-to-cartesian-centered
+ (polar-player-coord player)
+ dims))))
+ (.closePath context)))
+
+(defn draw-enemies [context dims level]
+ (doseq [enemy *enemy-list*]
+ (.beginPath context)
(draw-path context
- (polar-to-cartesian-centered (polar-enemy-coord enemy4) dims)
- (flipper-path-on-level enemy4)))
-
- (draw-path context [200 200] player-path)
-
- (doseq [enemy enemy-list]
- ;;(.log js/console (str "Enemy: " (pr-str (polar-enemy-coord enemy))))
- ;;(.log js/console (str "Enemy: " (pr-str (:segment enemy)))))
- (draw-path context
- (polar-to-cartesian-centered (polar-enemy-coord enemy) dims)
- (flipper-path-on-level enemy)))
-
-
+ (polar-to-cartesian-centered (polar-enemy-coord enemy) dims)
+ (flipper-path-on-level enemy))
+ (.closePath context)))
+
+(defn draw-board [context dims level]
+ (doseq []
+ (.beginPath context)
(doseq [idx (range (count (:segments level)))]
(draw-rectangle
context
(rectangle-to-canvas-coords
dims (rectangle-for-segment level idx))))
- (.closePath context)
- ))
-
-
+ (.closePath context)))
+
+(defn draw-world [context dims level]
+ (doseq []
+ (.clearRect context 0 0 (:width dims) (:height dims))
+ (draw-board context dims level)
+ (draw-player context dims level (deref *player*))
+ (draw-enemies context dims level)
+ (def *enemy-list* (update-enemy-list *enemy-list*))))
+
+(defn keypress [event]
+ (let [player @*player*
+ level (:level player)
+ seg-count (count (:segments level))
+ segment (:segment player)
+ key (.-keyCode event)]
+ (condp = key
+ key-codes/LEFT (def *player*
+ (atom
+ (assoc @*player* :segment
+ (mod (+ segment 1) seg-count))))
+ key-codes/RIGHT (def *player*
+ (atom
+ (assoc @*player* :segment
+ (mod (+ (- segment 1) seg-count)
+ seg-count))))
+ nil
+ )))
(defn ^:export canvasDraw [level]
- (let [timer (goog.Timer. 50)
- level (get levels/*levels* (- (js/parseInt level) 1))]
- (def enemy-list
+ (let [document (dom/getDocument)
+ timer (goog.Timer. 50)
+ level (get levels/*levels* (- (js/parseInt level) 1))
+ canvas (dom/getElement "canv1")
+ context (.getContext canvas "2d")
+ handler (goog.events.KeyHandler. document)
+ dims {:width (.-width canvas) :height (.-height canvas)}]
+
+ (def *enemy-list*
[(build-enemy level 0 :step 0)
(build-enemy level 3 :step 20)
- (build-enemy level 8 :step 80)
+ ;;(build-enemy level 7 :step 80)
+ ;;(build-enemy level 8 :step 80)
(build-enemy level 11 :step 100)])
- (draw-board level)
- (events/listen timer goog.Timer/TICK #(draw-board level))
+ (def *player*
+ (atom (doall (build-player level 7))))
+
+ (events/listen timer goog.Timer/TICK #(draw-world context dims level))
+ (events/listen handler "key" (fn [e] (keypress e)))
(. timer (start))))
(comment (defn ^:export canvasDraw []
context (.getContext canvas "2d")
timer (goog.Timer. 500)
dims {:width 500 :height 400}]
+ ;;(.log js/console (str "Enemy: " (pr-str (:segment enemy)))))
(events/listen timer goog.Timer/TICK #(draw-random-line context dims))
(. timer (start))
)))
-(ns tempest.util
- (:require [goog.dom :as dom]
- [goog.Timer :as timer]
- [goog.events :as events]
- [goog.events.EventType :as event-type]
- [goog.math :as math]))
+(ns tempest.util)
(defn rad-to-deg [rad]
(/ (* rad 180) 3.14159265358979))