summary history branches tags files
commit:096fccc06f8ca5c1c6dbd5a2c63d4fb5b36712c6
author:mrmekon
committer:mrmekon
date:Sat Apr 7 00:03:37 2012 -0400
parents:fdac1f7e6641af02c34baf6ba14948b8e8d639a2
Enemies shoot, but bullets don't do anything yet.  FPS monitor moved down.
diff --git a/src/tempest_cljs/views/welcome.clj b/src/tempest_cljs/views/welcome.clj
line changes: +1/-1
index 7c2d0e5..8f7fae3
--- a/src/tempest_cljs/views/welcome.clj
+++ b/src/tempest_cljs/views/welcome.clj
@@ -16,7 +16,7 @@
                                  "background-color: #000000;")}]
    [:canvas#canv-fg {:width "1000" :height "900"
                      :style (str "position: absolute; z-index: 1;")}]
-   [:p#fps "FPS 0.0"]
+   [:p#fps {:style "color: #FFFFFF; position: absolute; top: 910px"} "FPS 0.0"]
    (javascript-tag (str "tempest.canvasDraw(" (pr-str level) ");"))
    ))
 

diff --git a/tempest/tempest/core.cljs b/tempest/tempest/core.cljs
line changes: +56/-5
index 7aa9403..b0ea0df
--- a/tempest/tempest/core.cljs
+++ b/tempest/tempest/core.cljs
@@ -53,8 +53,10 @@ after passing through all the other functions.  This implements the game loop.
        draw-board
        render-frame
        remove-collided-entities
+       remove-collided-bullets
        update-projectile-locations
        update-enemy-locations
+       maybe-enemies-shoot
        maybe-make-enemy
        check-if-player-captured
        check-if-enemies-remain
@@ -65,7 +67,8 @@ after passing through all the other functions.  This implements the game loop.
        maybe-render-fps-display
        schedule-next-frame
        ))
-  
+
+
 (defn game-logic-non-playable
   "Called by next-game-state for non-playable animations."
   [game-state]
@@ -142,12 +145,14 @@ after passing through all the other functions.  This implements the game loop.
   "Returns a dictionary describing a projectile (bullet) on the given level,
    in the given segment, with a given stride (steps per update to move, with
    negative meaning in and positive meaning out), and given step to start on."
-  [level seg-idx stride & {:keys [step] :or {step 0}}]
+  [level seg-idx stride & {:keys [step from-enemy?]
+                           :or {step 0 from-enemy? false}}]
   {:step step
    :stride stride
    :segment seg-idx
    :level level
    :path-fn path/projectile-path-on-level
+   :from-enemy? from-enemy?
    })
 
 (def ^{:doc "Enumeration of directions a flipper can be flipping."}
@@ -177,6 +182,7 @@ after passing through all the other functions.  This implements the game loop.
    :flip-max-angle 0
    :flip-cur-angle 0
    :can-flip false
+   :shoot-probability 0
    })
 
 (defn build-flipper
@@ -193,8 +199,26 @@ after passing through all the other functions.  This implements the game loop.
     :flip-cur-angle 0
     :flip-permanent-dir nil
     :can-flip true
+    :shoot-probability 0.005
     ))
 
+(defn projectiles-after-shooting
+  [enemy-list projectile-list]
+  (loop [[enemy & enemies] enemy-list
+         projectiles projectile-list]
+    (if (nil? enemy) projectiles
+        (if (and (<= (rand) (:shoot-probability enemy))
+                 (not= (:step enemy) (:steps (:level enemy))))
+          (recur enemies (add-enemy-projectile projectiles enemy))
+          (recur enemies projectiles)))))
+
+(defn maybe-enemies-shoot
+  [game-state]
+  (let [enemies (:enemy-list game-state)
+        projectiles (:projectile-list game-state)]
+  (assoc game-state
+    :projectile-list (projectiles-after-shooting enemies projectiles))))
+
 (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
@@ -518,7 +542,7 @@ flipper appears to flip 'inside' the level:
                          (:step bullet)
                          (entity-next-step bullet)
                          entity)]
-         (if collision?
+         (if (and (not (:from-enemy? bullet)) collision?)
            (recur (decrement-enemy-hits entity)
                   nil
                   (concat projectiles-out (rest projectiles-in))
@@ -678,6 +702,17 @@ flipper appears to flip 'inside' the level:
    (>= (:step projectile) (:steps (:level projectile))) true
    :else false))
 
+(defn add-enemy-projectile
+  "Add a new projectile to the global list of live projectiles, originating
+   from the given enemy, on the segment he is currently on."
+  [projectile-list enemy]
+  (let [level (:level enemy)
+        seg-idx (:segment enemy)
+        stride (+ (:stride enemy) 2)
+        step (:step enemy)]
+    (conj projectile-list
+          (build-projectile level seg-idx stride :step step :from-enemy? true))))
+
 (defn add-player-projectile
   "Add a new projectile to the global list of live projectiles, originating
    from the given player, on the segment he is currently on."
@@ -833,11 +868,18 @@ The setTimeout fail-over is hard-coded to attempt 30fps.
          enemy-list :enemy-list
          projectile-list :projectile-list
          player :player}
-        game-state]
+        game-state
+        {enemy-shots true player-shots false}
+        (group-by :from-enemy? projectile-list)]
     (if (not (:is-dead? player))
       (draw/draw-player context dims level player))
     (draw/draw-entities context dims level enemy-list {:r 150 :g 10 :b 10})
-    (draw/draw-entities context dims level projectile-list {:r 255 :g 255 :b 255})
+    (draw/draw-entities context dims level
+                        player-shots
+                        {:r 255 :g 255 :b 255})
+    (draw/draw-entities context dims level
+                        enemy-shots
+                        {:r 150 :g 15 :b 150})
     game-state))
 
 (defn remove-collided-entities
@@ -853,6 +895,15 @@ The setTimeout fail-over is hard-coded to attempt 30fps.
         :projectile-list plist
         :enemy-list elist))))
 
+(defn remove-collided-bullets
+  "TODO: remove bullets that hit each other"
+  [game-state]
+  (let [projectile-list (:projectile-list game-state)]
+    ;; Gets a group of lists of projectils, where each group contains
+    ;; projectiles in the same spot.
+    (filter #(> (count %) 1) (vals (group-by #(select-keys % [:segment :step]) projectile-list)))
+    game-state))
+
 (defn update-projectile-locations
   "Returns game-state with all projectiles updated to have new positions
    based on their speeds and current position."