188 lines
6.5 KiB
Fennel
188 lines
6.5 KiB
Fennel
(local pxl8 (require :pxl8))
|
|
(local debug-ui (require :mod.debug_ui))
|
|
|
|
(var world nil)
|
|
(var cam-x 1000)
|
|
(var cam-y 64)
|
|
(var cam-z 1000)
|
|
(var cam-yaw 0)
|
|
(var cam-pitch 0)
|
|
(var bob-time 0)
|
|
(var show-debug-ui false)
|
|
(var affine false)
|
|
(var fps 0)
|
|
(var fps-accumulator 0)
|
|
(var fps-frame-count 0)
|
|
|
|
(local move-speed 200)
|
|
(local turn-speed 2.0)
|
|
(local bob-speed 8.0)
|
|
(local bob-amount 4.0)
|
|
(local max-pitch 1.5)
|
|
(local cell-size 64)
|
|
(local grid-size 32)
|
|
|
|
(fn init []
|
|
(set world (pxl8.world_new))
|
|
(let [result (pxl8.world_generate world {
|
|
:type pxl8.PROCGEN_CAVE
|
|
:width 32
|
|
:height 32
|
|
:seed 42
|
|
:density 0.45
|
|
:iterations 4})]
|
|
(if (< result 0)
|
|
(pxl8.error (.. "Failed to generate cave - result: " result))
|
|
(do
|
|
(let [floor-tex (pxl8.procgen_tex {:name "floor"
|
|
:seed 11111
|
|
:width 64
|
|
:height 64
|
|
:base_color 20})
|
|
ceiling-tex (pxl8.procgen_tex {:name "ceiling"
|
|
:seed 22222
|
|
:width 64
|
|
:height 64
|
|
:base_color 0})
|
|
wall-tex (pxl8.procgen_tex {:name "wall"
|
|
:seed 12345
|
|
:width 64
|
|
:height 64
|
|
:base_color 4})]
|
|
|
|
(pxl8.upload_atlas)
|
|
|
|
(let [result (pxl8.world_apply_textures world [
|
|
{:name "floor"
|
|
:texture_id floor-tex
|
|
:rule (fn [normal] (> normal.y 0.7))}
|
|
{:name "ceiling"
|
|
:texture_id ceiling-tex
|
|
:rule (fn [normal] (< normal.y -0.7))}
|
|
{:name "wall"
|
|
:texture_id wall-tex
|
|
:rule (fn [normal] (and (<= normal.y 0.7) (>= normal.y -0.7)))}])]
|
|
(when (< result 0)
|
|
(pxl8.error (.. "Failed to apply textures - result: " result)))))))))
|
|
|
|
(fn update [dt]
|
|
(set fps-accumulator (+ fps-accumulator dt))
|
|
(set fps-frame-count (+ fps-frame-count 1))
|
|
|
|
(when (>= fps-accumulator 0.25)
|
|
(set fps (/ fps-frame-count fps-accumulator))
|
|
(set fps-accumulator 0)
|
|
(set fps-frame-count 0))
|
|
|
|
(when (pxl8.key_pressed "F8")
|
|
(set show-debug-ui (not show-debug-ui))
|
|
(pxl8.ui_window_set_open "Debug Menu (F8)" show-debug-ui))
|
|
|
|
(when (pxl8.world_is_loaded world)
|
|
(let [forward-x (- (math.sin cam-yaw))
|
|
forward-z (- (math.cos cam-yaw))
|
|
right-x (math.cos cam-yaw)
|
|
right-z (- (math.sin cam-yaw))
|
|
grid-max (* grid-size cell-size)
|
|
move-delta (* move-speed dt)]
|
|
|
|
(var moving false)
|
|
(var move-forward 0)
|
|
(var move-right 0)
|
|
|
|
(when (pxl8.key_down "w")
|
|
(set move-forward (+ move-forward 1))
|
|
(set moving true))
|
|
|
|
(when (pxl8.key_down "s")
|
|
(set move-forward (- move-forward 1))
|
|
(set moving true))
|
|
|
|
(when (pxl8.key_down "q")
|
|
(set move-right (- move-right 1))
|
|
(set moving true))
|
|
|
|
(when (pxl8.key_down "e")
|
|
(set move-right (+ move-right 1))
|
|
(set moving true))
|
|
|
|
(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)
|
|
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)))))))))
|
|
|
|
(when (and (>= new-x 0) (<= new-x grid-max)
|
|
(>= new-z 0) (<= new-z grid-max))
|
|
(set cam-x new-x)
|
|
(set cam-z new-z))
|
|
|
|
(when (or (pxl8.key_down "left") (pxl8.key_down "a"))
|
|
(set cam-yaw (+ cam-yaw (* turn-speed dt))))
|
|
|
|
(when (or (pxl8.key_down "right") (pxl8.key_down "d"))
|
|
(set cam-yaw (- cam-yaw (* turn-speed dt))))
|
|
|
|
(when (pxl8.key_down "up")
|
|
(set cam-pitch (math.min max-pitch (+ cam-pitch (* turn-speed dt)))))
|
|
|
|
(when (pxl8.key_down "down")
|
|
(set cam-pitch (math.max (- max-pitch) (- cam-pitch (* turn-speed dt)))))
|
|
|
|
(if moving
|
|
(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))))))))
|
|
|
|
(fn frame []
|
|
(pxl8.clr 0)
|
|
|
|
(when (pxl8.world_is_loaded world)
|
|
(let [bob-offset (* (math.sin bob-time) bob-amount)
|
|
eye-y (+ cam-y bob-offset)
|
|
forward-x (- (math.sin cam-yaw))
|
|
forward-z (- (math.cos cam-yaw))
|
|
target-x (+ cam-x forward-x)
|
|
target-y (+ eye-y (math.sin cam-pitch))
|
|
target-z (+ cam-z forward-z)]
|
|
|
|
(pxl8.clear_zbuffer)
|
|
(pxl8.set_backface_culling true)
|
|
(pxl8.set_wireframe false)
|
|
|
|
(let [aspect (/ (pxl8.get_width) (pxl8.get_height))
|
|
fov 1.047]
|
|
(pxl8.set_projection (pxl8.mat4_perspective fov aspect 1.0 4096.0)))
|
|
|
|
(pxl8.set_view (pxl8.mat4_lookat
|
|
[cam-x eye-y cam-z]
|
|
[target-x target-y target-z]
|
|
[0 1 0]))
|
|
|
|
(pxl8.set_model (pxl8.mat4_identity))
|
|
|
|
(pxl8.set_affine_textures affine)
|
|
(pxl8.world_render world [cam-x eye-y cam-z])
|
|
|
|
(pxl8.text (.. "Pos: " (string.format "%.0f" cam-x) ","
|
|
(string.format "%.0f" cam-y) ","
|
|
(string.format "%.0f" cam-z)) 10 25 12)
|
|
|
|
(let [new-state (debug-ui.render {:show-debug-ui show-debug-ui
|
|
:fps fps
|
|
:wireframe false
|
|
:auto-rotate false
|
|
:orthographic false
|
|
:use-texture true
|
|
:affine affine})]
|
|
(when (not= new-state.show-debug-ui nil) (set show-debug-ui new-state.show-debug-ui))
|
|
(when (not= new-state.affine nil) (set affine new-state.affine))))))
|
|
|
|
{:init init
|
|
:update update
|
|
:frame frame}
|