summary history branches tags files
commit:52c4098461dc61c56c01287c85221b82e6737005
author:mrmekon
committer:mrmekon
date:Mon Apr 2 15:26:35 2012 -0400
parents:3deca636149160c403314ac7233ae60461e87208
Flipper flipping more complete, working on level 6.  Flippers flip 'inside' the level in both directions.
diff --git a/tempest/tempest.cljs b/tempest/tempest.cljs
line changes: +46/-29
index 1853b0e..8721b73
--- a/tempest/tempest.cljs
+++ b/tempest/tempest.cljs
@@ -76,8 +76,6 @@ Publicly exported functions to embed Tempest game in HTML.
         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)))
     
@@ -90,36 +88,55 @@ Publicly exported functions to embed Tempest game in HTML.
                        :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
+)

diff --git a/tempest/tempest/core.cljs b/tempest/tempest/core.cljs
line changes: +94/-20
index efe7331..2e4d96c
--- a/tempest/tempest/core.cljs
+++ b/tempest/tempest/core.cljs
@@ -46,7 +46,8 @@ after passing through all the other functions.  This implements the game loop.
        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
@@ -102,26 +103,76 @@ after passing through all the other functions.  This implements the game loop.
     :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
   []
@@ -137,13 +188,19 @@ after passing through all the other functions.  This implements the game loop.
 
 (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
@@ -156,11 +213,28 @@ after passing through all the other functions.  This implements the game loop.
                 (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."

diff --git a/tempest/tempest/draw.cljs b/tempest/tempest/draw.cljs
line changes: +1/-1
index 912a1c6..e643dfa
--- a/tempest/tempest/draw.cljs
+++ b/tempest/tempest/draw.cljs
@@ -123,7 +123,7 @@ level functions to draw complete game entities using the primitives.
                true
                (:flip-point entity)
                ;;[20 -20]
-               (:flip-max-angle entity)
+               (:flip-cur-angle entity)
                ;;0
                )
     (.closePath context)))

diff --git a/tempest/tempest/path.cljs b/tempest/tempest/path.cljs
line changes: +1/-4
index 228428e..049754e
--- a/tempest/tempest/path.cljs
+++ b/tempest/tempest/path.cljs
@@ -59,7 +59,7 @@ given level."
   [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
@@ -71,9 +71,6 @@ given level."
         [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