From fe586f841aacdcf4a9bcd6d31cf86993f9cfb3b0 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Tue, 17 Oct 2017 17:22:23 +0200 Subject: [PATCH] restructuring, keeping objects inside bounding box --- resources/public/index.html | 3 ++- src/cljs/topdown2d/core.cljs | 10 +++++-- src/cljs/topdown2d/demoscene.cljs | 3 +++ src/cljs/topdown2d/objects.cljs | 44 ++++++++++++++++++++----------- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/resources/public/index.html b/resources/public/index.html index 0a1baf6..f568c1c 100644 --- a/resources/public/index.html +++ b/resources/public/index.html @@ -4,7 +4,8 @@ - + diff --git a/src/cljs/topdown2d/core.cljs b/src/cljs/topdown2d/core.cljs index 5f11ec1..1fb108f 100644 --- a/src/cljs/topdown2d/core.cljs +++ b/src/cljs/topdown2d/core.cljs @@ -13,7 +13,10 @@ :now 0 :fps 0 } - :keys [] + :dimensions { + :w 600 + :h 400 + } :scene :demo :scenes { :demo { @@ -54,7 +57,10 @@ (update-scene))) (defn draw-step [gamestate] - (.clearRect (:2d gamestate) 0 0 400 600) + (.clearRect (:2d gamestate) + 0 0 + (get-in gamestate [:dimensions :w]) + (get-in gamestate [:dimensions :h])) (.fillText (:2d gamestate) (int (get-in gamestate [:timing :fps])) diff --git a/src/cljs/topdown2d/demoscene.cljs b/src/cljs/topdown2d/demoscene.cljs index 7a0de49..d70e175 100644 --- a/src/cljs/topdown2d/demoscene.cljs +++ b/src/cljs/topdown2d/demoscene.cljs @@ -23,6 +23,9 @@ :h 10 :v 5 :d :? + :keep-in (assoc (:dimensions gamestate) + :x 0 + :y 0) } })) diff --git a/src/cljs/topdown2d/objects.cljs b/src/cljs/topdown2d/objects.cljs index f44ec42..5b19ab9 100644 --- a/src/cljs/topdown2d/objects.cljs +++ b/src/cljs/topdown2d/objects.cljs @@ -1,18 +1,32 @@ (ns topdown2d.objects) +(defn in? [obj box] + (let [{:keys [x y w h]} obj + {bx :x by :y bw :w bh :h} box] + (println obj x) + (and + (> x bx) + (> y by) + (< (+ x w) (+ bx bw)) + (< (+ y h) (+ by bh))))) + (defn move [obj] - (let [{:keys [x y v d]} obj] - (cond - (= d :w) - (assoc obj - :x (- x v)) - (= d :e) - (assoc obj - :x (+ x v)) - (= d :n) - (assoc obj - :y (- y v)) - (= d :s) - (assoc obj - :y (+ y v)) - :else obj))) + (let [{:keys [x y v d]} obj + moved (cond + (= d :w) + (assoc obj + :x (- x v)) + (= d :e) + (assoc obj + :x (+ x v)) + (= d :n) + (assoc obj + :y (- y v)) + (= d :s) + (assoc obj + :y (+ y v)) + :else obj) + keep-in (:keep-in obj)] + (if (or (nil? keep-in) (and keep-in (in? moved keep-in))) + moved + obj)))