add jump to demo 9

This commit is contained in:
asrael 2025-11-15 08:30:51 -06:00
parent 55b7a44af3
commit b27abeffb5
3 changed files with 92 additions and 70 deletions

View file

@ -5,10 +5,10 @@
(var time 0)
(var active-demo :logo)
(var particles nil)
(var fire-init false)
(var rain-init false)
(var snow-init false)
(var use-famicube-palette false)
(var fire-init? false)
(var rain-init? false)
(var snow-init? false)
(var use-famicube-palette? false)
(var logo-x 256)
(var logo-y 148)
@ -32,18 +32,18 @@
(when (pxl8.key_pressed "4") (set active-demo :raster))
(when (pxl8.key_pressed "5")
(set active-demo :fire)
(set fire-init false))
(set fire-init? false))
(when (pxl8.key_pressed "6")
(set active-demo :rain)
(set rain-init false))
(set rain-init? false))
(when (pxl8.key_pressed "7")
(set active-demo :snow)
(set snow-init false))
(set snow-init? false))
(when (pxl8.key_pressed "8") (set active-demo :cube3d))
(when (pxl8.key_pressed "9") (set active-demo :worldgen))
(when (pxl8.key_pressed "=")
(set use-famicube-palette (not use-famicube-palette))
(local palette-path (if use-famicube-palette "res/palettes/famicube.ase" "res/sprites/pxl8_logo.ase"))
(set use-famicube-palette? (not use-famicube-palette?))
(local palette-path (if use-famicube-palette? "res/palettes/famicube.ase" "res/sprites/pxl8_logo.ase"))
(pxl8.load_palette palette-path))
(case active-demo
@ -81,28 +81,28 @@
:fire (do
(pxl8.clr 0)
(when particles
(when (not fire-init)
(when (not fire-init?)
(pxl8.particles_clear particles)
(pxl8.vfx_fire particles 160 140 100 12)
(set fire-init true))
(set fire-init? true))
(pxl8.particles_render particles)))
:rain (do
(pxl8.clr 0)
(when particles
(when (not rain-init)
(when (not rain-init?)
(pxl8.particles_clear particles)
(pxl8.vfx_rain particles 320 10.0)
(set rain-init true))
(set rain-init? true))
(pxl8.particles_render particles)))
:snow (do
(pxl8.clr 0)
(when particles
(when (not snow-init)
(when (not snow-init?)
(pxl8.particles_clear particles)
(pxl8.vfx_snow particles 320 5.0)
(set snow-init true))
(set snow-init? true))
(pxl8.particles_render particles)))
:cube3d (cube3d.frame)

View file

@ -4,20 +4,20 @@
(var angle-x 0)
(var angle-y 0)
(var angle-z 0)
(var auto-rotate true)
(var orthographic true)
(var wireframe true)
(var auto-rotate? true)
(var orthographic? true)
(var wireframe? true)
(var time 0)
(var zoom 5.0)
(var texture-id nil)
(var use-texture false)
(var affine false)
(var use-texture? false)
(var affine? false)
(var cam-x 0)
(var cam-y 2)
(var cam-z 12)
(var cam-yaw 0)
(var cam-pitch -0.2)
(var show-debug-ui false)
(var show-debug-ui? false)
(var fps 0)
(var fps-accumulator 0)
(var fps-frame-count 0)
@ -26,19 +26,19 @@
(set angle-x 0)
(set angle-y 0)
(set angle-z 0)
(set auto-rotate true)
(set orthographic true)
(set wireframe true)
(set auto-rotate? true)
(set orthographic? true)
(set wireframe? true)
(set time 0)
(set zoom 5.0)
(set use-texture false)
(set affine false)
(set use-texture? false)
(set affine? false)
(set cam-x 0)
(set cam-y 2)
(set cam-z 12)
(set cam-yaw 0)
(set cam-pitch -0.2)
(set show-debug-ui false)
(set show-debug-ui? false)
(set fps 0)
(set fps-accumulator 0)
(set fps-frame-count 0)
@ -87,7 +87,7 @@
(let [wheel-y (pxl8.mouse_wheel_y)]
(when (and (not= wheel-y 0) (not (pxl8.ui_has_mouse_focus)))
(if orthographic
(if orthographic?
(set zoom (math.max 0.5 (math.min (- zoom (* wheel-y 0.2)) 10.0)))
(let [zoom-speed 0.5
forward-x (* (math.sin cam-yaw) wheel-y zoom-speed)
@ -103,7 +103,7 @@
right-x (* (math.cos cam-yaw) move-speed dt)
right-z (* (- (math.sin cam-yaw)) move-speed dt)]
(if orthographic
(if orthographic?
(do
(when (pxl8.key_down "w")
(set zoom (math.max 0.5 (- zoom (* zoom-speed dt)))))
@ -149,20 +149,20 @@
(set cam-pitch (math.max -1.5 (math.min cam-pitch 1.5))))
(when (pxl8.key_pressed " ")
(set wireframe (not wireframe)))
(set wireframe? (not wireframe?)))
(when (pxl8.key_pressed "f")
(set affine (not affine)))
(set affine? (not affine?)))
(when (pxl8.key_pressed "p")
(set orthographic (not orthographic)))
(set orthographic? (not orthographic?)))
(when (pxl8.key_pressed "r")
(set auto-rotate (not auto-rotate)))
(set auto-rotate? (not auto-rotate?)))
(when (pxl8.key_pressed "t")
(set use-texture (not use-texture)))
(set use-texture? (not use-texture?)))
(when (pxl8.key_pressed "F8")
(set show-debug-ui (not show-debug-ui))
(pxl8.ui_window_set_open "Debug Menu (F8)" show-debug-ui))
(set show-debug-ui? (not show-debug-ui?))
(pxl8.ui_window_set_open "Debug Menu (F8)" show-debug-ui?))
(when auto-rotate
(when auto-rotate?
(set angle-x (+ angle-x (* dt 0.7)))
(set angle-y (+ angle-y (* dt 0.5)))
(set angle-z (+ angle-z (* dt 0.3)))))
@ -178,7 +178,7 @@
(pxl8.set_model model))
(let [vertices (make-cube-vertices)]
(if (and use-texture texture-id)
(if (and use-texture? texture-id)
(let [faces (make-cube-faces-with-uvs)]
(each [_i face-data (ipairs faces)]
(let [tri-indices face-data.tri
@ -206,11 +206,11 @@
(pxl8.clr 0)
(pxl8.clear_zbuffer)
(pxl8.set_affine_textures affine)
(pxl8.set_affine_textures affine?)
(pxl8.set_backface_culling true)
(pxl8.set_wireframe wireframe)
(pxl8.set_wireframe wireframe?)
(if orthographic
(if orthographic?
(let [size zoom
aspect (/ (pxl8.get_width) (pxl8.get_height))
w (* size aspect)
@ -233,19 +233,19 @@
(draw-cube [3 1 -7] 0.9 1.2)
(draw-cube [0 -2 -10] 1.1 -0.7)
(let [new-state (debug-ui.render {:show-debug-ui show-debug-ui
(let [new-state (debug-ui.render {:show-debug-ui show-debug-ui?
:fps fps
:wireframe wireframe
:auto-rotate auto-rotate
:orthographic orthographic
:use-texture use-texture
:affine affine})]
(when (not= new-state.show-debug-ui nil) (set show-debug-ui new-state.show-debug-ui))
(when (not= new-state.wireframe nil) (set wireframe new-state.wireframe))
(when (not= new-state.auto-rotate nil) (set auto-rotate new-state.auto-rotate))
(when (not= new-state.orthographic nil) (set orthographic new-state.orthographic))
(when (not= new-state.use-texture nil) (set use-texture new-state.use-texture))
(when (not= new-state.affine nil) (set affine new-state.affine))))
:wireframe wireframe?
:auto-rotate auto-rotate?
:orthographic orthographic?
:use-texture use-texture?
:affine affine?})]
(when (not= new-state.show-debug-ui nil) (set show-debug-ui? new-state.show-debug-ui))
(when (not= new-state.wireframe nil) (set wireframe? new-state.wireframe))
(when (not= new-state.auto-rotate nil) (set auto-rotate? new-state.auto-rotate))
(when (not= new-state.orthographic nil) (set orthographic? new-state.orthographic))
(when (not= new-state.use-texture nil) (set use-texture? new-state.use-texture))
(when (not= new-state.affine nil) (set affine? new-state.affine))))
{:init init
:update update

View file

@ -2,11 +2,14 @@
(var world nil)
(var cam-x 1000)
(local cam-y 64)
(var cam-y 64)
(var cam-z 1000)
(var cam-yaw 0)
(var cam-pitch 0)
(var bob-time 0)
(var velocity-y 0)
(var grounded? true)
(var land-squash 0)
(local move-speed 200)
(local turn-speed 2.0)
@ -15,6 +18,11 @@
(local max-pitch 1.5)
(local cell-size 64)
(local grid-size 32)
(local gravity -800)
(local jump-force 175)
(local ground-y 64)
(local land-squash-amount -4)
(local land-recovery-speed 20)
(fn init []
(set world (pxl8.world_new))
@ -72,31 +80,28 @@
(var move-right 0)
(when (pxl8.key_down "w")
(set move-forward (+ move-forward 1))
(set moving true))
(set move-forward (+ move-forward 1)))
(when (pxl8.key_down "s")
(set move-forward (- move-forward 1))
(set moving true))
(set move-forward (- move-forward 1)))
(when (pxl8.key_down "q")
(set move-right (- move-right 1))
(set moving true))
(set move-right (- move-right 1)))
(when (pxl8.key_down "e")
(set move-right (+ move-right 1))
(set moving true))
(set move-right (+ move-right 1)))
(set moving (or (not= move-forward 0) (not= move-right 0)))
(var new-x cam-x)
(var new-z cam-z)
(when moving
(let [len (math.sqrt (+ (* move-forward move-forward) (* move-right move-right)))]
(when (> len 0)
(let [norm-forward (/ move-forward len)
(let [len (math.sqrt (+ (* move-forward move-forward) (* move-right move-right)))
norm-forward (/ move-forward len)
norm-right (/ move-right len)]
(set new-x (+ new-x (* move-delta (+ (* forward-x norm-forward) (* right-x norm-right)))))
(set new-z (+ new-z (* move-delta (+ (* forward-z norm-forward) (* right-z norm-right)))))))))
(set new-z (+ new-z (* move-delta (+ (* forward-z norm-forward) (* right-z norm-right)))))))
(when (and (>= new-x 0) (<= new-x grid-max)
(>= new-z 0) (<= new-z grid-max))
@ -115,7 +120,24 @@
(when (pxl8.key_down "down")
(set cam-pitch (math.max (- max-pitch) (- cam-pitch (* turn-speed dt)))))
(if moving
(when (and (pxl8.key_pressed "space") grounded?)
(set velocity-y jump-force)
(set grounded? false))
(set velocity-y (+ velocity-y (* gravity dt)))
(set cam-y (+ cam-y (* velocity-y dt)))
(when (<= cam-y ground-y)
(when (not grounded?)
(set land-squash land-squash-amount))
(set cam-y ground-y)
(set velocity-y 0)
(set grounded? true))
(when (< land-squash 0)
(set land-squash (math.min 0 (+ land-squash (* land-recovery-speed dt)))))
(if (and moving grounded?)
(set bob-time (+ bob-time (* dt bob-speed)))
(let [target-phase (* (math.floor (/ bob-time math.pi)) math.pi)]
(set bob-time (+ (* bob-time 0.8) (* target-phase 0.2))))))))
@ -125,7 +147,7 @@
(when (pxl8.world_is_loaded world)
(let [bob-offset (* (math.sin bob-time) bob-amount)
eye-y (+ cam-y bob-offset)
eye-y (+ cam-y bob-offset land-squash)
forward-x (- (math.sin cam-yaw))
forward-z (- (math.cos cam-yaw))
target-x (+ cam-x forward-x)