restructuring, keeping objects inside bounding box

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

@ -4,7 +4,8 @@
</head> </head>
<body> <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> <script src="js/cljsbuild-main.js"></script>
</body> </body>
</html> </html>

@ -13,7 +13,10 @@
:now 0 :now 0
:fps 0 :fps 0
} }
:keys [] :dimensions {
:w 600
:h 400
}
:scene :demo :scene :demo
:scenes { :scenes {
:demo { :demo {
@ -54,7 +57,10 @@
(update-scene))) (update-scene)))
(defn draw-step [gamestate] (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 (.fillText
(:2d gamestate) (:2d gamestate)
(int (get-in gamestate [:timing :fps])) (int (get-in gamestate [:timing :fps]))

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

@ -1,18 +1,32 @@
(ns topdown2d.objects) (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] (defn move [obj]
(let [{:keys [x y v d]} obj] (let [{:keys [x y v d]} obj
(cond moved (cond
(= d :w) (= d :w)
(assoc obj (assoc obj
:x (- x v)) :x (- x v))
(= d :e) (= d :e)
(assoc obj (assoc obj
:x (+ x v)) :x (+ x v))
(= d :n) (= d :n)
(assoc obj (assoc obj
:y (- y v)) :y (- y v))
(= 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)))
moved
obj)))

Loading…
Cancel
Save