summary history branches tags files
commit:d69d412afb2153aa5dc33951672dd4823ab9b306
author:mrmekon
committer:mrmekon
date:Tue Apr 3 01:58:50 2012 -0400
parents:c258614ba550b3c278f134c364af4f62003519af
Player is captured and pulled down the level when a flipper lands on him.
diff --git a/tempest/tempest.cljs b/tempest/tempest.cljs
line changes: +0/-2
index a3e8d8a..8a6cf99
--- a/tempest/tempest.cljs
+++ b/tempest/tempest.cljs
@@ -85,8 +85,6 @@ Publicly exported functions to embed Tempest game in HTML.
     (draw/draw-board bgcontext dims level)
     (events/listen handler "key" (fn [e] (c/queue-keypress e)))
 
-    (.log js/console (pr-str (enemy-on-each-segment level)))
-    
     (let [empty-game-state (c/build-game-state)
           game-state (assoc empty-game-state
                        :player (c/build-player level 7)

diff --git a/tempest/tempest/core.cljs b/tempest/tempest/core.cljs
line changes: +79/-10
index 96fc7ab..938793e
--- a/tempest/tempest/core.cljs
+++ b/tempest/tempest/core.cljs
@@ -46,8 +46,10 @@ after passing through all the other functions.  This implements the game loop.
        remove-collided-entities
        update-projectile-locations
        update-enemy-locations
+       check-if-player-captured
        update-entity-is-flipping
        update-entity-flippyness
+       animate-player-capture
        update-frame-count
        maybe-render-fps-display
        schedule-next-frame
@@ -93,6 +95,7 @@ after passing through all the other functions.  This implements the game loop.
    :flip-stride 1
    :flip-max-angle 0
    :flip-cur-angle 0
+   :can-flip false
    })
 
 (defn build-flipper
@@ -108,6 +111,7 @@ after passing through all the other functions.  This implements the game loop.
     :flip-max-angle 0
     :flip-cur-angle 0
     :flip-permanent-dir nil
+    :can-flip true
     ))
 
 (defn flip-angle-stride
@@ -171,7 +175,7 @@ flipper appears to flip 'inside' the level:
 
 (defn update-entity-stop-flipping
   "Updates an entity and marks it as not currently flipping."
-  [flipper]
+  [flipper]  
   (assoc flipper
     :stride (:old-stride flipper)
     :flip-dir (DirectionEnum "NONE")
@@ -212,13 +216,15 @@ flipper appears to flip 'inside' the level:
    is on the outermost edge of the level, and will randomly be true if it
    has not reached the edge."
   [flipper]
-  (let [should-flip (and (or
-                          (= (:step flipper) 50)
-                          (= (:step flipper) 100)
-                          (= (:step flipper) 150)
-                          (= (:step flipper) 200)
-                          )
-                         (= (:flip-dir flipper) (DirectionEnum "NONE")))
+  (let [should-flip (and
+                     (true? (:can-flip flipper))
+                     (or
+                      (= (:step flipper) 50)
+                      (= (:step flipper) 100)
+                      (= (:step flipper) 150)
+                      (= (:step flipper) 200)
+                      )
+                     (= (:flip-dir flipper) (DirectionEnum "NONE")))
         permanent-dir (:flip-permanent-dir flipper)
         flip-dir (or permanent-dir (random-direction))
         flip-seg-idx (segment-for-flip-direction flipper flip-dir)
@@ -231,6 +237,52 @@ flipper appears to flip 'inside' the level:
      (not (nil? permanent-dir)) (swap-flipper-permanent-dir flipper)
      :else flipper)))
 
+(defn mark-player-captured
+  [player]
+  (assoc player
+    :captured true
+    :stride -4))
+
+(defn mark-enemy-capturing
+  [enemy]
+  (assoc enemy
+    :capturing true
+    :can-flip false
+    :stride -4))
+
+(defn enemy-is-on-player?
+  "Returns true if given enemy and player are on top of each other."
+  [player enemy]
+  (and (= (:segment player) (:segment enemy))
+       (= (:step player) (:step enemy))
+       (= (DirectionEnum "NONE") (:flip-dir enemy))))
+
+(defn player-and-enemies-if-captured
+  "Given player and current list of enemies, returns an updated player
+   and updated enemy list if an enemy is capturing the player in vector
+   [player enemy-list].  Returns nil if no capture occurred."
+  [player enemy-list]
+  (let [{colliders true missers false}
+        (group-by (partial enemy-is-on-player? player) enemy-list)]
+    (when-let [[enemy & rest] colliders]
+      [(mark-player-captured player)
+       (cons (mark-enemy-capturing enemy) (concat missers rest))])))
+
+(defn check-if-player-captured
+  "If player is not already captured, checks all enemies to see if they
+   are now capturing the player.  See player-and-enemies-if-captured.
+   If capture is in progress, returns game-state with player and enemy-list
+   updated."
+  [game-state]
+  (if (:captured (:player game-state))
+    game-state
+    (if-let [[player enemy-list] (player-and-enemies-if-captured
+                                  (:player game-state)
+                                  (:enemy-list game-state))]
+      (assoc game-state :enemy-list enemy-list :player player)
+      game-state)))
+
+
 (defn update-entity-is-flipping
   "Decide if an enemy should start flipping for every enemy on the level."
   [game-state]
@@ -270,8 +322,11 @@ flipper appears to flip 'inside' the level:
   [level seg-idx]
   {:segment seg-idx
    :level level
+   :captured false
    :step (:steps level)
-   :bullet-stride -5})
+   :bullet-stride -5
+   :stride 0
+   :path path/*player-path*})
 
 (defn entity-next-step
   "Returns the next step position of given entity, taking into account
@@ -432,6 +487,14 @@ flipper appears to flip 'inside' the level:
     ))
 
 
+(defn animate-player-capture
+  [global-state]
+  (if (:captured (:player global-state))
+    (assoc global-state
+      :player (update-entity-position! (:player global-state)))
+    global-state))
+
+
 
 (defn collisions-with-projectile
   "Returns map with keys true and false.  Values under true key have or
@@ -466,7 +529,7 @@ flipper appears to flip 'inside' the level:
   (let [level (:level player)
         seg-idx (:segment player)
         stride (:bullet-stride player)
-        step (:steps level)]
+        step (:step player)]
     (conj projectile-list
           (build-projectile level seg-idx stride :step step))))
 
@@ -529,6 +592,12 @@ flipper appears to flip 'inside' the level:
       key-codes/LEFT  (assoc game-state
                         :player
                         (assoc player :segment (segment-entity-cw player)))
+      key-codes/UP (assoc game-state
+                        :player
+                        (assoc player :step (- (:step player) 10)))
+      key-codes/DOWN (assoc game-state
+                        :player
+                        (assoc player :step (+ (:step player) 10)))
       key-codes/SPACE (assoc game-state
                         :projectile-list
                         (add-player-projectile projectile-list player))

diff --git a/tempest/tempest/draw.cljs b/tempest/tempest/draw.cljs
line changes: +3/-4
index b80e8c6..b0a3ac2
--- a/tempest/tempest/draw.cljs
+++ b/tempest/tempest/draw.cljs
@@ -100,10 +100,9 @@ level functions to draw complete game entities using the primitives.
   (doseq []
     (.beginPath context)
     (draw-path context
-               (vec (map js/Math.round
-                         (path/polar-to-cartesian-centered
-                          (path/polar-entity-coord player)
-                          dims)))
+               (path/polar-to-cartesian-centered
+                (path/polar-entity-coord player)
+                dims)
                (path/round-path (path/player-path-on-level player))
                true)
     (.closePath context)))

diff --git a/tempest/tempest/path.cljs b/tempest/tempest/path.cljs
line changes: +30/-13
index 9d01e96..6787ff0
--- a/tempest/tempest/path.cljs
+++ b/tempest/tempest/path.cljs
@@ -310,15 +310,15 @@ given level."
 ;; Path that defines player.
 (def ^{:doc "Path, in polar coordinates, describing the player's ship."}
   *player-path*
-  [[40 90]
-   [44 196]
-   [27 333]
-   [17 135]
-   [30 11]
-   [30 349]
-   [17 225]
-   [27 27]
-   [44 164]])
+  [[24 90]
+   [26 196]
+   [16 333]
+   [10 135]
+   [18 11]
+   [18 349]
+   [10 225]
+   [16 27]
+   [26 164]])
 
 (defn bounding-box-from-radius
   [origin radius]
@@ -332,10 +332,12 @@ given level."
   "Returns the path of polar coordinates to draw the player correctly at its
    current location.  It corrects for size and angle."
   [player]
-  (let [coord (polar-entity-coord player)]
-    (scale-path 0.6 (rotate-path
-     (enemy-angle player)
-     *player-path*))))
+  ;;(rotate-path (enemy-angle player) (:path player)))
+  (rotate-path (enemy-angle player)
+               (player-path-with-width
+                 (* 0.75 (entity-desired-width player))
+                 (= (:step player) (:steps (:level player))))))
+
 
 (defn flipper-path-bounding-circle-radius
   "Returns the radius of the bounding circle around the given flipper's path."
@@ -374,6 +376,21 @@ given level."
      [(/ r 2) 16]]))
 
 
+(defn player-path-with-width
+  "Returns a path to draw a projectile with the given width."
+  [width offset?]
+  (let [r (/ (/ width 2) (js/Math.cos (util/deg-to-rad 16)))
+        offset (if offset? r 0)]
+  [[offset 90]
+   [r 196]
+   [(* 0.62 r) 333]
+   [(* 0.38 r) 135]
+   [(* 0.69 r) 11]
+   [(* 0.69 r) 349]
+   [(* 0.38 r) 225]
+   [(* 0.62 r) 27]
+   [r 164]]))
+
 (defn projectile-path-with-width
   "Returns a path to draw a projectile with the given width."
   [width]