Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
143 additions
and
31 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
(ns caves.entities.aspects.digger) | ||
|
||
(defprotocol Digger | ||
(dig [this world dx dy] | ||
"Dig a location.") | ||
(can-dig? [this world dx dy] | ||
"Return whether the entity can dig the new location.")) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
(ns caves.entities.aspects.mobile) | ||
|
||
|
||
(defprotocol Mobile | ||
(move [this world dx dy] | ||
"Move this entity to a new location.") | ||
(can-move? [this world dx dy] | ||
"Return whether the entity can move to the new location.")) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
(ns caves.entities.core) | ||
|
||
|
||
(defprotocol Entity | ||
(tick [this world] | ||
"Update the world to handle the passing of a tick for this entity.")) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
(ns caves.entities.player | ||
(:use [caves.entities.core :only [Entity]] | ||
[caves.entities.aspects.mobile :only [Mobile move can-move?]] | ||
[caves.entities.aspects.digger :only [Digger dig can-dig?]] | ||
[caves.world :only [find-empty-tile get-tile-kind set-tile-floor]])) | ||
|
||
|
||
(defrecord Player [id loc]) | ||
|
||
(defn offset-coords [[x y] dx dy] | ||
[(+ x dx) (+ y dy)]) | ||
|
||
(defn check-tile | ||
"Take a player and an offset, and check that the tile at the destination | ||
passes the given predicate." | ||
[player world dx dy pred] | ||
(let [[x y] (offset-coords (:loc player) dx dy) | ||
dest-tile (get-tile-kind world x y)] | ||
(pred dest-tile))) | ||
|
||
(defn dir-to-offset [dir] | ||
(case dir | ||
:w [-1 0] | ||
:e [1 0] | ||
:n [0 -1] | ||
:s [0 1] | ||
:nw [-1 -1] | ||
:ne [1 -1] | ||
:sw [-1 1] | ||
:se [1 1])) | ||
|
||
|
||
(extend-type Player Entity | ||
(tick [this world] | ||
world)) | ||
|
||
(extend-type Player Mobile | ||
(move [this world dx dy] | ||
(if (can-move? this world dx dy) | ||
(update-in world [:player :loc] offset-coords dx dy) | ||
world)) | ||
(can-move? [this world dx dy] | ||
(check-tile this world dx dy #{:floor}))) | ||
|
||
(extend-type Player Digger | ||
(dig [this world dx dy] | ||
(if (can-dig? this world dx dy) | ||
(let [[tx ty] (offset-coords (:loc this) dx dy)] | ||
(set-tile-floor world tx ty)) | ||
world)) | ||
(can-dig? [this world dx dy] | ||
(check-tile this world dx dy #{:wall}))) | ||
|
||
|
||
(defn make-player [world] | ||
(->Player :player (find-empty-tile world))) | ||
|
||
(defn move-player [world direction] | ||
(let [player (:player world) | ||
[dx dy] (dir-to-offset direction)] | ||
(cond | ||
(can-move? player world dx dy) (move player world dx dy) | ||
(can-dig? player world dx dy) (dig player world dx dy) | ||
:else world))) |
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
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
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