diff --git a/src/cljs/topdown2d/demoscene.cljs b/src/cljs/topdown2d/demoscene.cljs index d70e175..4c50d2d 100644 --- a/src/cljs/topdown2d/demoscene.cljs +++ b/src/cljs/topdown2d/demoscene.cljs @@ -23,9 +23,6 @@ :h 10 :v 5 :d :? - :keep-in (assoc (:dimensions gamestate) - :x 0 - :y 0) } })) @@ -35,7 +32,7 @@ box (assoc box :d dir)] (update-in scenedata [:data :box] - #(objects/move box)))) + #(objects/move-inside-gamestate gamestate box)))) (defn draw [gamestate scenedata] (let [{{:keys [bumper box]} :data} scenedata diff --git a/src/cljs/topdown2d/objects.cljs b/src/cljs/topdown2d/objects.cljs index 5b19ab9..d28d67c 100644 --- a/src/cljs/topdown2d/objects.cljs +++ b/src/cljs/topdown2d/objects.cljs @@ -1,32 +1,49 @@ (ns topdown2d.objects) -(defn in? [obj box] +(defn in? [obj container] (let [{:keys [x y w h]} obj - {bx :x by :y bw :w bh :h} box] - (println obj x) + {cx :x cy :y cw :w ch :h} container] (and - (> x bx) - (> y by) - (< (+ x w) (+ bx bw)) - (< (+ y h) (+ by bh))))) + (> x cx) + (> y cy) + (< (+ x w) (+ cx cw)) + (< (+ y h) (+ cy ch))))) -(defn move [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))) +(defn moved-object [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))) + +(defn bump-in-wall [obj container] + (let [{:keys [x y w h d]} obj + {cx :x cy :y cw :w ch :h} container] + (case d + :w (assoc obj :x (inc cx)) + :e (update obj :x #(- (+ cx cw) w 1)) + :n (assoc obj :y (inc cy)) + :s (update obj :y #(- (+ cy ch) h 1)) + :? obj))) + +(defn move-inside [obj container] + (let [moved (moved-object obj)] + (if (in? moved container) moved - obj))) + (bump-in-wall obj container)))) + +(defn move-inside-gamestate [gamestate obj] + (let [container (assoc (:dimensions gamestate) + :x 0 + :y 0)] + (move-inside obj container)))