Created
May 4, 2012 13:46
-
-
Save tinyrobot/2594883 to your computer and use it in GitHub Desktop.
Playing with game related functions in clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Testing some potential basic game functions | |
;; moved-by : list list -> list | |
;; computes a new position by adding an x,y pair to an x,y pair | |
;; example: (moved-by '(1,1) '(0,1)) -> '(1,2) | |
(defn moved-by [position amount] | |
(map + position amount)) | |
;; test moved-by: (moved-by '(1,1) '(0,1)) -> '(1,2) | |
(println (= '(1 2) (moved-by '(1,1) '(0,1)))) | |
;; all-moved-by : list list -> list | |
;; computes a list of positions by adding a list of x,y pairs to a list of x,y | |
;; pairs | |
;; example: (all-moved-by '((1,1) (1,1)) '((1,2) (2,3))) -> ((2,3) (3,4)) | |
(defn all-moved-by [positions amounts] | |
(map moved-by positions amounts)) | |
;; test all-moved-by: (all-moved-by '((1,1) (1,1)) '((2,3) (3,4))) | |
(println (= (all-moved-by '((1,1) (1,1)) '((1,2) (2,3))) '((2,3) (3,4)))) | |
;; make-spatial : -> map | |
;; initializes and returns a map | |
;; example: (make-spatial) -> {} | |
(defn make-spatial [] {:position '(0 0) :velocity '(0 0)}) | |
;;test make-spatial: (make-spatial) -> {} | |
(println (= (make-spatial) {:position '(0 0) :velocity '(0 0)})) | |
;; apply-gravity : list -> list | |
;; increments y component of list by gravity const | |
;; example: (apply-gravity '(0 0)) -> '(0 1) | |
(defn apply-gravity [position] | |
(list (first position) (+ 1 (last position)))) | |
;;test apply-gravity: (apply-gravity '(0 0)) -> '(0 1) | |
(println (= (apply-gravity '(0 0)) '(0 1))) | |
;; apply-forces : spatial -> spatial | |
;; applies world forces to a spatial, returning a new spatial state | |
;; example: (apply-forces (make-spatial)) -> {:position '(0 0) :velocity '(0 1)} | |
(defn apply-forces [spatial] | |
(-> spatial | |
(assoc :velocity (apply-gravity (:velocity spatial))))) | |
;; test apply-forces: | |
;; (apply-forces (make-spatial)) -> {:position '(0 0) :velocity '(0 1)} | |
(println (= (apply-forces (make-spatial)) {:position '(0 0) :velocity '(0 1)})) | |
;; move-spatial : map -> map | |
;; moves the spatial's position by the player's velocity returning new position | |
;; example: (update-spatial {:position '(0 0) :velocity '(1 1)}) -> '(1 1) | |
(defn move-spatial [spatial] | |
(-> spatial | |
(assoc :position (moved-by (:position spatial) (:velocity spatial))))) | |
(println (= | |
(move-spatial {:position '(0 0) :velocity '(1 1)}) | |
{:position '(1 1) :velocity '(1 1)})) | |
;; update-spatial : spatial -> spatial | |
;; makes basic physics updates to a spatial | |
;; example: (update-spatial (make-spatial)) -> {:velocity '(0 1) :position '(0 1)} | |
(defn update-spatial [spatial] | |
(move-spatial (apply-forces spatial))) | |
;; test update-spatial: | |
;; (update-spatial (make-spatial)) -> {:velocity '(0 1) :position '(0 0)} | |
(println (= (update-spatial (make-spatial)) {:position '(0 1) :velocity '(0 1)})) | |
;; step : -> map | |
;; creates and steps the player one game iteration. eventually should be all entities | |
;; example: (step) -> {} | |
(defn step [] | |
(let [player (make-spatial)] | |
(-> player | |
update-spatial))) | |
;; test step | |
(println (= (step) (update-spatial (make-spatial)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment