summary history branches tags files
commit:eeccfc86f88149d15d5a39096190939d679087b9
author:mrmekon
committer:mrmekon
date:Fri Apr 6 19:45:20 2012 -0400
parents:e55c3a85da6d7533751c497390b482a1969ad843
Flippers added randomly to the level while playing.
diff --git a/tempest/tempest.cljs b/tempest/tempest.cljs
line changes: +13/-10
index 5e66c1b..943d3ee
--- a/tempest/tempest.cljs
+++ b/tempest/tempest.cljs
@@ -72,9 +72,9 @@ Publicly exported functions to embed Tempest game in HTML.
 (defn ^:export canvasDraw
   "Begins a game of tempest.  'level' specified as a string representation
    of an integer."
-  [level]
+  [level-str]
   (let [document (dom/getDocument)
-        level (get levels/*levels* (- (js/parseInt level) 1))
+        level-idx (- (js/parseInt level-str) 1)
         canvas (dom/getElement "canv-fg")
         context (.getContext canvas "2d")
         bgcanvas (dom/getElement "canv-bg")
@@ -85,14 +85,17 @@ Publicly exported functions to embed Tempest game in HTML.
     (events/listen handler "key" (fn [e] (c/queue-keypress e)))
 
     (let [empty-game-state (c/build-game-state)
-          game-state (assoc empty-game-state
-                       :player (c/build-player level 7)
-                       :level level
-                       :context context
-                       :bgcontext bgcontext
-                       :dims dims
-                       :anim-fn (c/animationFrameMethod)
-                       :enemy-list (enemy-on-each-segment level))]
+          game-state (assoc
+                         (c/change-level
+                          (assoc empty-game-state
+                            :context context
+                            :bgcontext bgcontext
+                            :dims dims
+                            :anim-fn (c/animationFrameMethod)
+                            :enemy-list )
+                          level-idx)
+                       :enemy-list (enemy-on-each-segment
+                                    (get levels/*levels* level-idx)))]
       (c/next-game-state game-state))))
 
 

diff --git a/tempest/tempest/core.cljs b/tempest/tempest/core.cljs
line changes: +37/-0
index 83a7232..d4e004d
--- a/tempest/tempest/core.cljs
+++ b/tempest/tempest/core.cljs
@@ -54,6 +54,7 @@ after passing through all the other functions.  This implements the game loop.
        remove-collided-entities
        update-projectile-locations
        update-enemy-locations
+       maybe-make-enemy
        check-if-player-captured
        update-entity-is-flipping
        update-entity-flippyness
@@ -86,6 +87,7 @@ after passing through all the other functions.  This implements the game loop.
    :bgcontext nil
    :anim-fn identity
    :dims {:width 0 :height 0}
+   :level-idx 0
    :level nil
    :frame-count 0
    :frame-time 0
@@ -95,6 +97,14 @@ after passing through all the other functions.  This implements the game loop.
    :zoom 0.0
    })
 
+(defn change-level
+  "Changes current level of game."
+  [game-state level-idx]
+  (let [level (get levels/*levels* level-idx)]
+    (assoc game-state
+      :level-idx level-idx
+      :level level
+      :player (build-player level 0))))
 
 (defn build-projectile
   "Returns a dictionary describing a projectile (bullet) on the given level,
@@ -153,6 +163,30 @@ after passing through all the other functions.  This implements the game loop.
     :can-flip true
     ))
 
+(defn maybe-make-enemy
+  "Randomly create new enemies if the level needs more.  Each level has a total
+   count and probability of arrival for each type of enemy.  When a new enemy
+   is added by this function, the total count for that type is decremented.
+   If zero enemies are on the board, probability of placing one is increased
+   two-fold to avoid long gaps with nothing to do."
+  [game-state]
+  (let [level (:level game-state)
+        segments (count (:segments level))
+        remaining (:remaining level)
+        enemy-list (:enemy-list game-state)
+        r (if (pos? (count enemy-list)) (rand) (/ (rand) 2))]
+    (cond
+     (and (<= r (:flipper (:probability level))) (pos? (:flipper remaining)))
+     (do
+       (assoc game-state
+         :enemy-list (cons (build-flipper level (rand-int segments)) enemy-list)
+         :level (assoc level
+                  :remaining (assoc remaining
+                               :flipper (dec (:flipper remaining))))))
+     :else game-state)))
+
+;;(count (:segments level))
+
 (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
@@ -277,16 +311,19 @@ flipper appears to flip 'inside' the level:
      :else flipper)))
 
 (defn mark-player-captured
+  "Marks player as being captured."
   [player]
   (assoc player
     :captured? true
     :stride -4))
 
 (defn mark-enemy-capturing
+  "Marks enemy as having captured the player."
   [enemy]
   (assoc enemy
     :capturing true
     :can-flip false
+    :step (- (:step enemy) 10) ;; looks better if enemy leads player
     :stride -4))
 
 (defn enemy-is-on-player?

diff --git a/tempest/tempest/levels.cljs b/tempest/tempest/levels.cljs
line changes: +25/-9
index 492e20c..d949b25
--- a/tempest/tempest/levels.cljs
+++ b/tempest/tempest/levels.cljs
@@ -252,20 +252,36 @@ Functions related to generating paths representing levels.
    vector of segments constructed of pairs of lines, and a length function.
    This function takes a vector of lines, and a boolean specifying whether
    the level is a closed loop, or open."
-  [lines loops?]
+  [lines loops? enemy-count enemy-probability]
   {:lines lines
    :loops? loops?
    :segments (build-segment-list (- (count lines) 1) loops?)
    :length-fn *default-length-fn*
-   :steps *default-steps-per-segment*})
+   :steps *default-steps-per-segment*
+   :remaining enemy-count
+   :probability enemy-probability})
 
 (def *levels*
-  [ (make-level-entry *level1_lines* false)
-    (make-level-entry *level2_lines* true)
-    (make-level-entry *level3_lines* false)
-    (make-level-entry *level4_lines* false)
-    (make-level-entry *level5_lines* false)
-    (make-level-entry *level6_lines* true)
-    (make-level-entry *level7_lines* false)])
+  [ (make-level-entry *level1_lines* false
+                      {:flipper 20}
+                      {:flipper 0.01})
+    (make-level-entry *level2_lines* true
+                      {:flipper 20}
+                      {:flipper 0.01})
+    (make-level-entry *level3_lines* false
+                      {:flipper 20}
+                      {:flipper 0.01})
+    (make-level-entry *level4_lines* false
+                      {:flipper 20}
+                      {:flipper 0.01})
+    (make-level-entry *level5_lines* false
+                      {:flipper 20}
+                      {:flipper 0.01})
+    (make-level-entry *level6_lines* true
+                      {:flipper 20}
+                      {:flipper 0.01})
+    (make-level-entry *level7_lines* false
+                      {:flipper 20}
+                      {:flipper 0.01})])