dev mode, more tuning

This commit is contained in:
Jeremy Penner 2023-10-29 22:17:14 -04:00
parent 367eebfd68
commit 729fb32d29

View file

@ -1,3 +1,5 @@
(import :CoreLibs/crank)
(local reload (require :meta.reload)) (local reload (require :meta.reload))
(local {: defmulti} (require :meta.multimethod)) (local {: defmulti} (require :meta.multimethod))
(local {: inc : dec &as tbl} (require :meta.table)) (local {: inc : dec &as tbl} (require :meta.table))
@ -8,20 +10,15 @@
(local gfx playdate.graphics) (local gfx playdate.graphics)
(local devmode true)
(local update (defmulti #$1)) (local update (defmulti #$1))
(local children (defmulti #$2)) (local children (defmulti #$2))
(local scene (defmulti #$1)) (local scene (defmulti #$1))
(fn destroy-all [type ctx] (cache.destroy type) (tset ctx type nil)) (fn destroy-all [type ctx] (cache.destroy type) (tset ctx type nil))
(fn destroy-key [type id ctx ?key] (cache.destroy type id) (tset (. ctx type) (or ?key id) nil)) (fn destroy-key [type id ctx ?key] (cache.destroy type id) (tset (. ctx type) (or ?key id) nil))
(fn get-id [?f-id key ctx type] (if ?f-id (?f-id key (. ctx type)) key))
(fn destroy-keys [type ctx keys ?f-id]
(each [_ key (ipairs keys)]
(destroy-key type (get-id ?f-id key ctx type) ctx key)))
(fn destroy-item [type id ctx ?index] (cache.destroy type id) (table.remove (. ctx type) (or ?index id))) (fn destroy-item [type id ctx ?index] (cache.destroy type id) (table.remove (. ctx type) (or ?index id)))
(fn destroy-items [type ctx indexes ?f-id]
(each [_ index (ipairs (tbl.sorted indexes #(> $1 $2)))]
(destroy-item type (get-id ?f-id index ctx type) ctx index)))
(update:defmethod :default #nil) (update:defmethod :default #nil)
(children:defmethod :default (fn [val] (iter.ivalues val))) (children:defmethod :default (fn [val] (iter.ivalues val)))
@ -71,22 +68,29 @@
ratio (if (> crank 180) (/ (- 360 crank) -130) (/ crank 130)) ratio (if (> crank 180) (/ (- 360 crank) -130) (/ crank 130))
x (* (/ (+ ratio 1) 2) screenw)] x (* (/ (+ ratio 1) 2) screenw)]
(for [ibucket 1 bucket.count] (for [ibucket 1 bucket.count]
(let [buckety (- 230 (* (- ibucket 1) 20))] (let [buckety (+ 190 (* (- ibucket 1) 20))]
(doto (sprite :bucket ibucket) (: :moveTo x buckety))))))) (doto (sprite :bucket ibucket) (: :moveTo x buckety)))))))
(let [image (make-image 20 30 #(gfx.fillRoundRect 0 0 20 30 6))] (let [image (make-image 20 30 #(gfx.fillRoundRect 0 0 20 30 6))]
(defsprite :bomber #(with-image $1 image 0.5 0))) (defsprite :bomber #(with-image $1 image 0.5 0)))
(fn bomberspeed [ctx] (+ ctx.level 5)) (fn bomberspeed [ctx] (or ctx.bomberspeed (+ ctx.level 5)))
(fn bombervelocity [ctx dx] (fn bombervelocity [ctx dx]
(let [sign (if (< dx 0) -1 1) (let [sign (if (< dx 0) -1 1)
speed (math.min (bomberspeed ctx) (math.abs dx))] speed (math.min (bomberspeed ctx) (math.abs dx))]
(* speed sign))) (* speed sign)))
(fn bombspeed [ctx] (+ ctx.level 3)) (fn bombspeed [ctx] (or ctx.bombspeed (if (< ctx.level 7) (+ ctx.level 3) (+ 9 (/ ctx.level 10)))))
(fn bombcount [ctx] 50) (fn bombcount [ctx] (or ctx.bombcount 40))
(fn bombgap [ctx] (or ctx.bombgap 30))
(fn bombdropframecount [ctx] (fn bombdropframecount [ctx]
(math.ceil (/ 25 (bombspeed ctx)))) (math.ceil (/ (bombgap ctx) (bombspeed ctx))))
(when devmode
(tbl.extend state {:dev
{:overrides [:bomberspeed :bombspeed :bombgap :bombcount :level]
:funcs [ bomberspeed bombspeed bombgap bombcount #$1.level]
:i 1}}))
(fn drop-bomb [ctx] (fn drop-bomb [ctx]
(when (> ctx.bomber.bombs-left 0) (when (> ctx.bomber.bombs-left 0)
@ -121,12 +125,22 @@
(set ctx.level 1) (set ctx.level 1)
(set ctx.score 0) (set ctx.score 0)
(dirty :background) (dirty :background)
(set ctx.bucket.count 3)))) (set ctx.bucket.count 3))
(when (and devmode (playdate.buttonJustPressed :up))
(playdate.getCrankTicks 6)
(set ctx.scene :dev))))
(defsprite :background #(with-bg-draw $1 (fn [] (defsprite :background #(with-bg-draw $1 (fn []
(gfx.drawLine 0 33 screenw 33) (gfx.drawLine 0 33 screenw 33)
(gfx.drawText (tostring state.level) 75 8) (gfx.drawText (tostring state.level) 75 8)
(gfx.drawText (tostring state.score) 320 8)))) (gfx.drawText (tostring state.score) 320 8)
(when (= state.scene :dev)
(var y 40)
(each [i override (ipairs state.dev.overrides)]
(gfx.drawText (.. (if (= i state.dev.i) "** " " ")
override ": " ((. state.dev.funcs i) state)) 5 y)
(set y (+ y 25)))))))
(dirty :background) (dirty :background)
(let [image (make-image 14 14 #(gfx.fillCircleAtPoint 7 7 7))] (let [image (make-image 14 14 #(gfx.fillCircleAtPoint 7 7 7))]
@ -143,6 +157,27 @@
(do-to-children keys update ctx)) (do-to-children keys update ctx))
(scene:defmethod :ingame #(scene-update [:bomber :bombs :bucket] $2)) (scene:defmethod :ingame #(scene-update [:bomber :bombs :bucket] $2))
(scene:defmethod :dev (fn [_ ctx]
(when (and (playdate.buttonJustPressed :up) (> ctx.dev.i 1))
(dec ctx.dev :i))
(when (and (playdate.buttonJustPressed :down) (< ctx.dev.i (length ctx.dev.overrides)))
(inc ctx.dev :i))
(let [key (. ctx.dev.overrides ctx.dev.i)
func (. ctx.dev.funcs ctx.dev.i)
ticks (playdate.getCrankTicks 6)]
(when (playdate.buttonJustPressed :b)
(tset ctx key nil))
(when (not= ticks 0)
(tset ctx key (+ ticks (func ctx)))))
(when (and (playdate.buttonJustPressed :left) (> ctx.bucket.count 1))
(cache.destroy :bucket ctx.bucket.count)
(dec ctx.bucket :count))
(when (playdate.buttonJustPressed :right)
(inc ctx.bucket :count))
(when (playdate.buttonJustPressed :a)
(set ctx.scene :ingame))
(dirty :background)
(scene-update [:bucket] ctx)))
(fn playdate.update [] (fn playdate.update []
(scene state.scene state) (scene state.scene state)