restructuring, keeping objects inside bounding box

master
Josha von Gizycki 7 years ago
parent 91b35cba12
commit fe586f841a

@ -4,7 +4,8 @@
</head>
<body>
<canvas id="gamecanvas" height="400" width="600"></canvas>
<canvas id="gamecanvas" height="400" width="600"
style="border: 1px solid black"></canvas>
<script src="js/cljsbuild-main.js"></script>
</body>
</html>

@ -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]))

@ -23,6 +23,9 @@
:h 10
:v 5
:d :?
:keep-in (assoc (:dimensions gamestate)
:x 0
:y 0)
}
}))

@ -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)))

Loading…
Cancel
Save