Flipper flipping more complete, working on level 6. Flippers flip 'inside' the level in both directions.
handler (goog.events.KeyHandler. document)
dims {:width (.-width canvas) :height (.-height canvas)}]
- (.log js/console (str "Animation function: " (pr-str c/*animMethod*)))
-
(draw/draw-board bgcontext dims level)
(events/listen handler "key" (fn [e] (c/queue-keypress e)))
:anim-fn (c/animationFrameMethod)
:enemy-list (list
(c/build-flipper level 0 :step 0)
- (c/build-flipper level 8 :step 60)
(c/build-flipper level 1 :step 0)
- (c/build-flipper level 1 :step 10)
- (c/build-flipper level 1 :step 20)
- (c/build-flipper level 1 :step 30)
- (c/build-flipper level 1 :step 40)
- (c/build-flipper level 1 :step 50)
- (c/build-flipper level 1 :step 60)
- (c/build-flipper level 1 :step 70)
+ (c/build-flipper level 2 :step 0)
(c/build-flipper level 3 :step 0)
- (c/build-flipper level 3 :step 10)
- (c/build-flipper level 3 :step 20)
- (c/build-flipper level 3 :step 30)
- (c/build-flipper level 3 :step 40)
- (c/build-flipper level 3 :step 50)
- (c/build-flipper level 3 :step 60)
- (c/build-flipper level 3 :step 70)
+ (c/build-flipper level 4 :step 0)
+ (c/build-flipper level 5 :step 0)
+ (c/build-flipper level 6 :step 0)
(c/build-flipper level 7 :step 0)
- (c/build-flipper level 7 :step 10)
- (c/build-flipper level 7 :step 20)
- (c/build-flipper level 7 :step 30)
- (c/build-flipper level 7 :step 40)
- (c/build-flipper level 7 :step 50)
(c/build-flipper level 8 :step 0)
- (c/build-flipper level 8 :step 10)
- (c/build-flipper level 8 :step 20)
- (c/build-flipper level 8 :step 30)
- (c/build-flipper level 8 :step 40)
- (c/build-flipper level 8 :step 50)
- (c/build-flipper level 11 :step 10)))]
+ (c/build-flipper level 9 :step 0)
+ (c/build-flipper level 10 :step 0)
+ (c/build-flipper level 11 :step 0)
+ (c/build-flipper level 12 :step 0)
+ (c/build-flipper level 13 :step 0)
+ (c/build-flipper level 14 :step 0)
+ (c/build-flipper level 15 :step 0)
+ (c/build-flipper level 16 :step 0)
+ (c/build-flipper level 17 :step 0)
+ (c/build-flipper level 18 :step 0)
+ (c/build-flipper level 19 :step 0)
+ (c/build-flipper level 20 :step 0)
+ (c/build-flipper level 21 :step 0)
+ (c/build-flipper level 22 :step 0)
+ (c/build-flipper level 23 :step 0)
+ (c/build-flipper level 24 :step 0)
+ (c/build-flipper level 25 :step 0)
+ (c/build-flipper level 26 :step 0)
+ (c/build-flipper level 27 :step 0)
+ (c/build-flipper level 28 :step 0)
+ (c/build-flipper level 29 :step 0)
+ (c/build-flipper level 30 :step 0)
+ (c/build-flipper level 31 :step 0)
+ (c/build-flipper level 32 :step 0)
+ (c/build-flipper level 33 :step 0)
+ (c/build-flipper level 34 :step 0)
+ (c/build-flipper level 35 :step 0)
+ (c/build-flipper level 36 :step 0)
+ (c/build-flipper level 37 :step 0)
+ (c/build-flipper level 38 :step 0)
+ (c/build-flipper level 39 :step 0)
+ (c/build-flipper level 40 :step 0)
+ (c/build-flipper level 41 :step 0)
+ (c/build-flipper level 42 :step 0)
+ (c/build-flipper level 43 :step 0)
+ (c/build-flipper level 44 :step 0)
+ (c/build-flipper level 45 :step 0)
+ (c/build-flipper level 46 :step 0)
+ ))]
(c/next-game-state game-state))))
-
+
+(comment
+)
remove-collided-entities
update-projectile-locations
update-enemy-locations
- update-enemy-flippyness
+ update-entity-is-flipping
+ update-entity-flippyness
update-frame-count
maybe-render-fps-display
schedule-next-frame
:flip-stride 1
:flip-max-angle 0
:flip-cur-angle 0
+ :flip-permanent-dir nil
))
+;; TODO: CW usually right, not always
+;; CCW rotation goes through level
+;; adding 360 deg to angle fixes it... but when??
+
+(defn flip-angle-stride
+ "Returns the angle stride of a flipper, which is how many radians to
+increment his current flip angle by to be completely flipped onto his
+destination segment (angle of max-angle) on 'steps' number of increments,
+going clockwise if cw? is true, or counter-clockwise otherwise.
+
+### Implementation details:
+
+There are three known possibilities for determining the stride such that the
+flipper appears to flip 'inside' the level:
+
+ * max-angle/steps -- If flipper is going clockwise and max-angle is less
+ than zero, or if flipper is going counter-clockwise and max-angle is
+ greater than zero.
+ * (max-angle - 2PI)/steps -- If flipper is going clockwise and max-angle
+ is greater than zero.
+ * (max-angle + 2PI)/steps -- If flipper is going counter-clockwise and
+ max-angle is less than zero.
+"
+ [max-angle steps cw?]
+ (let [dir0 (/ max-angle steps)
+ dir1 (/ (- max-angle 6.2831853) steps)
+ dir2 (/ (+ max-angle 6.2831853) steps)]
+ (cond
+ (<= max-angle 0) (if cw? dir0 dir2)
+ :else (if cw? dir1 dir0))))
+
(defn mark-flipper-for-flipping
- [flipper direction stride seg-idx cw?]
+ [flipper direction seg-idx cw?]
+ (let [point (path/flip-point-between-segments
+ (:level flipper)
+ (:segment flipper)
+ seg-idx
+ (:step flipper)
+ cw?)
+ max-angle (path/flip-angle-between-segments
+ (:level flipper)
+ (:segment flipper)
+ seg-idx
+ cw?)
+ step-count 10
+ stride (flip-angle-stride max-angle step-count cw?)
+ permanent (if (= (:steps (:level flipper))
+ (:step flipper)) direction nil)]
+ (assoc flipper
+ :stride 0
+ :old-stride (:stride flipper)
+ :flip-dir (DirectionEnum direction)
+ :flip-cur-angle 0
+ :flip-to-segment seg-idx
+ :flip-point point
+ :flip-max-angle max-angle
+ :flip-stride stride
+ :flip-steps-remaining step-count
+ :flip-permanent-dir permanent)))
+
+(defn update-entity-stop-flipping
+ [flipper]
(assoc flipper
- :stride 0
- :flip-dir (DirectionEnum direction)
- :flip-stride stride
+ :stride (:old-stride flipper)
+ :flip-dir (DirectionEnum "NONE")
:flip-cur-angle 0
- :flip-point (path/flip-point-between-segments
- (:level flipper)
- (:segment flipper)
- seg-idx
- (:step flipper)
- cw?)
- :flip-max-angle (path/flip-angle-between-segments
- (:level flipper)
- (:segment flipper)
- seg-idx
- cw?)))
+ :segment (:flip-to-segment flipper)))
(defn random-direction-string
[]
(defn maybe-engage-flipping
[flipper]
- (let [should-flip (and (= (:step flipper) 100) (= (:flip-dir flipper) (DirectionEnum "NONE")))
- flip-dir (random-direction-string)
+ (let [should-flip (and (or
+ (= (:step flipper) 50)
+ (= (:step flipper) 100)
+ (= (:step flipper) 150)
+ (= (:step flipper) 200)
+ )
+ (= (:flip-dir flipper) (DirectionEnum "NONE")))
+ flip-dir (or (:flip-permanent-dir flipper) (random-direction-string))
flip-seg-idx (segment-for-flip-direction flipper flip-dir)
cw? (= flip-dir "CW")]
(if (and should-flip
(not= flip-seg-idx (:segment flipper)))
- (mark-flipper-for-flipping flipper flip-dir 1 flip-seg-idx cw?)
+ (mark-flipper-for-flipping flipper flip-dir flip-seg-idx cw?)
flipper)))
(defn consider-flipping
(cons (maybe-engage-flipping entity) newlist))))
) entity-list []))
-(defn update-enemy-flippyness
+(defn update-entity-is-flipping
[game-state]
(let [{enemy-list :enemy-list} game-state]
(assoc game-state :enemy-list (consider-flipping enemy-list))))
+(defn update-entity-flippyness
+ [game-state]
+ (let [{enemy-list :enemy-list} game-state]
+ (assoc game-state :enemy-list (map update-flip-angle enemy-list))))
+
+(defn update-flip-angle
+ [flipper]
+ (let [new-angle (+ (:flip-stride flipper) (:flip-cur-angle flipper))
+ remaining (dec (:flip-steps-remaining flipper))]
+ (if (not= (:flip-dir flipper) (DirectionEnum "NONE"))
+ (if (< remaining 0)
+ (update-entity-stop-flipping flipper)
+ (assoc flipper
+ :flip-cur-angle new-angle
+ :flip-steps-remaining remaining))
+ flipper)))
+
(defn build-player
"Returns a dictionary describing a player on the given level and segment."
true
(:flip-point entity)
;;[20 -20]
- (:flip-max-angle entity)
+ (:flip-cur-angle entity)
;;0
)
(.closePath context)))
[level seg-idx-cur seg-idx-new cw?]
(let [angle-cur (segment-angle level seg-idx-cur)
angle-new (segment-angle level seg-idx-new)]
- (- 0 (- (+ angle-new 3.14159265) angle-cur))
+ (mod (- 0 (- (+ angle-new 3.14159265) angle-cur)) 6.2831853)
))
(defn flip-point-between-segments
[x1 y1] (polar-to-cartesian-coords
(polar-segment-midpoint level seg-idx-cur step))
edge-points (cartesian-edge-coordinates level seg-idx-new step)]
- (.log js/console (pr-str "Edge points: " edge-points
- "\nPivot point: " [x0 y0]))
- (.log js/console (pr-str "Result: " [(- x0 x1) (- y0 y1)]))
[(- x1 x0) (- y0 y1)]))
(comment