Flippers added randomly to the level while playing.
(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")
(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))))
remove-collided-entities
update-projectile-locations
update-enemy-locations
+ maybe-make-enemy
check-if-player-captured
update-entity-is-flipping
update-entity-flippyness
:bgcontext nil
:anim-fn identity
:dims {:width 0 :height 0}
+ :level-idx 0
:level nil
:frame-count 0
:frame-time 0
: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,
: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
: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?
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})])