make move code more clear and differentiate keep-in logic

master
Josha von Gizycki 7 years ago
parent fe586f841a
commit 7eb713ed39

@ -23,9 +23,6 @@
:h 10 :h 10
:v 5 :v 5
:d :? :d :?
:keep-in (assoc (:dimensions gamestate)
:x 0
:y 0)
} }
})) }))
@ -35,7 +32,7 @@
box (assoc box :d dir)] box (assoc box :d dir)]
(update-in scenedata (update-in scenedata
[:data :box] [:data :box]
#(objects/move box)))) #(objects/move-inside-gamestate gamestate box))))
(defn draw [gamestate scenedata] (defn draw [gamestate scenedata]
(let [{{:keys [bumper box]} :data} scenedata (let [{{:keys [bumper box]} :data} scenedata

@ -1,18 +1,17 @@
(ns topdown2d.objects) (ns topdown2d.objects)
(defn in? [obj box] (defn in? [obj container]
(let [{:keys [x y w h]} obj (let [{:keys [x y w h]} obj
{bx :x by :y bw :w bh :h} box] {cx :x cy :y cw :w ch :h} container]
(println obj x)
(and (and
(> x bx) (> x cx)
(> y by) (> y cy)
(< (+ x w) (+ bx bw)) (< (+ x w) (+ cx cw))
(< (+ y h) (+ by bh))))) (< (+ y h) (+ cy ch)))))
(defn move [obj] (defn moved-object [obj]
(let [{:keys [x y v d]} obj (let [{:keys [x y v d]} obj]
moved (cond (cond
(= d :w) (= d :w)
(assoc obj (assoc obj
:x (- x v)) :x (- x v))
@ -25,8 +24,26 @@
(= d :s) (= d :s)
(assoc obj (assoc obj
:y (+ y v)) :y (+ y v))
:else obj) :else obj)))
keep-in (:keep-in obj)]
(if (or (nil? keep-in) (and keep-in (in? moved keep-in))) (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 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)))

Loading…
Cancel
Save