multibucket, better collisions

This commit is contained in:
Jeremy Penner 2023-10-28 13:39:40 -04:00
parent a7bb98983c
commit 945614d3b4

View file

@ -39,7 +39,7 @@
(tbl.extend state
{:bomber {:x 200}
:bombs []
:bucket {:x 200 :count 3}
:bucket {:count 3}
:level 1
:state :waiting
:scene :ingame}))
@ -77,9 +77,15 @@
xoffset (/ bucketw 2)
buckety 200
image (make-image bucketw bucketh #(gfx.drawRoundRect 0 0 bucketw bucketh radius))]
(defsprite :bucket #(doto $1 (with-image image 0.5 0) (with-collision 1 2)))
(draw:defmethod :bucket (fn [_ {: x}]
(doto (sprite :bucket) (: :moveTo x buckety)))))
(defsprite :bucket #(doto $1 (with-image image 0.5 0) (with-collision 1 2))))
(fn lose-life [ctx]
(let [count ctx.bucket.count]
(set ctx.bucket.count (- count 1))
(cache.destroy :bucket count)
(destroy-all :bombs ctx)
(set ctx.bombs [])
(set ctx.state :waiting)))
(update:defmethod :bucket (fn [_ bucket]
(let [crank (playdate.getCrankPosition)
@ -88,11 +94,9 @@
(> ratio 1) 1
ratio)
x (* (/ (+ ratio 1) 2) screenw)]
(set bucket.x x))
(let [collisions (: (sprite :bucket) :overlappingSprites)]
(each [_ spr (ipairs collisions)]
(print "poof")
(set spr.bomb.defused true)))))
(for [ibucket 1 bucket.count]
(let [buckety (- 220 (* (- ibucket 1) 25))]
(doto (sprite :bucket ibucket) (: :moveTo x buckety)))))))
(fn drop-bomb [ctx]
(table.insert ctx.bombs {:x ctx.bomber.x :y 30}))
@ -105,22 +109,24 @@
(update:defmethod :bomber (fn [_ _ _ ctx]
(when (playdate.buttonJustPressed :a)
(set ctx.state :bombing)
(drop-bomb ctx))))
(drop-bomb ctx))
(when (playdate.buttonJustPressed :b)
(set ctx.bucket.count 3))))
(gfx.sprite.setBackgroundDrawingCallback #(gfx.drawLine 0 33 screenw 33))
(let [image (make-image 14 14 #(gfx.fillCircleAtPoint 7 7 7))]
(defsprite :bombs #(doto $1 (with-image image 0.5 0.5) (with-collision 2 1) (tset :bomb $3)))
(draw:defmethod :bombs (fn [_ {: x : y &as bomb}]
(doto (sprite :bombs bomb) (: :moveTo x y)))))
(defsprite :bombs #(doto $1 (with-image image 0.5 0.5) (with-collision 2 1) (tset :bomb $3))))
(fn bombspeed [ctx] (+ ctx.level 5))
(fn bombspeed [ctx] (+ ctx.level 3))
(update:defmethod :bombs (fn [_ bomb ibomb ctx]
(when (= ctx.state :bombing)
(set bomb.y (+ bomb.y (bombspeed ctx)))
(when (or (> bomb.y screenh) bomb.defused)
(destroy-item :bombs bomb ctx ibomb)))))
(let [spr (doto (sprite :bombs bomb) (: :moveTo bomb.x bomb.y))
(_ y _ count) (spr:moveWithCollisions bomb.x (+ bomb.y (bombspeed ctx)))]
(if (> count 0) (destroy-item :bombs bomb ctx ibomb)
(> y screenh) (lose-life ctx)
(set bomb.y y))))))
(fn scene-update [keys ctx]
(do-to-children keys update ctx)