Skip to content

Instantly share code, notes, and snippets.

Created September 10, 2012 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3693973 to your computer and use it in GitHub Desktop.
Save anonymous/3693973 to your computer and use it in GitHub Desktop.
an adventure game
(ns adventure.core)
;; what tiles can we place entities on?
(def transparent-tiles '[door floor])
;; define all the things!
(defrecord State [clock entities grid])
(defrecord Grid [tile-map map entities])
(defrecord Entity [name coords])
;; -----------------
;; utility functions
;; -----------------
(defn get-tile [grid x y]
"get the tile at a coordinate in a grid."
((keyword (nth (nth (:map grid) y) x)) (:tile-map grid)))
(defn add-entity [grid entity]
"return a new grid with entitiy added. return nil if entity can't be added."
(let [entity-x (:x (:coords entity))
entity-y (:y (:coords entity))
tile (get-tile grid entity-x entity-y)]
(if (some #(= % tile) transparent-tiles) ; if the tile is in the transparent tile list
(assoc grid :entities (conj (:entities grid) entity))))) ; return a new grid with the entity added!
;; now do stuff!
;; ---------------
;; testing objects
;; ---------------
;; test grid
;; ---------
(def dungeon
(Grid. {:. 'floor :+ 'door :w 'wall}
'[[w w w w w]
[w . . . w]
[w w + w w]
[w . . . w]
[w . . . w]
[w . . . w]
[w w w w w]]
[]))
;; test entity
;; -----------
(def goblin (Entity. "goblin-man" {:x 1 :y 1}))
(get-tile dungeon 0 0)
;; => wall
(get-tile dungeon 1 1)
;; => floor
(get-tile dungeon 2 2)
;; => door
(add-entity dungeon goblin)
;; => #adventure.core.Grid{:tile-map {:+ door, :. floor, :w wall}, :map [[w w w w w] [w . . . w] [w w + w w] [w . . . w] [w . . . w] [w . . . w] [w w w w w]], :entities [#adventure.core.Entity{:name "goblin-man", :coords {:y 1, :x 1}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment