wip procgen
This commit is contained in:
parent
a653eae745
commit
79a678f162
18 changed files with 1317 additions and 127 deletions
131
demo/main.fnl
131
demo/main.fnl
|
|
@ -1,9 +1,9 @@
|
|||
(local pxl8 (require :pxl8))
|
||||
(local bsp_world (require :mod.bsp_world))
|
||||
(local cube3d (require :mod.cube3d))
|
||||
(local worldgen (require :mod.worldgen))
|
||||
|
||||
(var time 0)
|
||||
(var current-effect 1)
|
||||
(var active-demo :logo)
|
||||
(var particles nil)
|
||||
(var fire-init false)
|
||||
(var rain-init false)
|
||||
|
|
@ -17,103 +17,96 @@
|
|||
(var logo-sprite nil)
|
||||
|
||||
(global init (fn []
|
||||
(bsp_world.init)
|
||||
(cube3d.init)
|
||||
(worldgen.init)
|
||||
(pxl8.load_palette "res/sprites/pxl8_logo.ase")
|
||||
(set logo-sprite (pxl8.load_sprite "res/sprites/pxl8_logo.ase"))
|
||||
(set particles (pxl8.particles_new 1000))))
|
||||
|
||||
(global update (fn [dt]
|
||||
(bsp_world.update dt)
|
||||
(set time (+ time dt))
|
||||
|
||||
(when (pxl8.key_pressed "1")
|
||||
(set current-effect 1))
|
||||
(when (pxl8.key_pressed "2")
|
||||
(set current-effect 2))
|
||||
(when (pxl8.key_pressed "3")
|
||||
(set current-effect 3))
|
||||
(when (pxl8.key_pressed "4")
|
||||
(set current-effect 4))
|
||||
(when (pxl8.key_pressed "1") (set active-demo :logo))
|
||||
(when (pxl8.key_pressed "2") (set active-demo :plasma))
|
||||
(when (pxl8.key_pressed "3") (set active-demo :tunnel))
|
||||
(when (pxl8.key_pressed "4") (set active-demo :raster))
|
||||
(when (pxl8.key_pressed "5")
|
||||
(set current-effect 5)
|
||||
(set active-demo :fire)
|
||||
(set fire-init false))
|
||||
(when (pxl8.key_pressed "6")
|
||||
(set current-effect 6)
|
||||
(set active-demo :rain)
|
||||
(set rain-init false))
|
||||
(when (pxl8.key_pressed "7")
|
||||
(set current-effect 7)
|
||||
(set active-demo :snow)
|
||||
(set snow-init false))
|
||||
(when (pxl8.key_pressed "8")
|
||||
(set current-effect 8))
|
||||
(when (pxl8.key_pressed "9")
|
||||
(set current-effect 0))
|
||||
(when (pxl8.key_pressed "0")
|
||||
(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"))
|
||||
(pxl8.load_palette palette-path))
|
||||
|
||||
(case current-effect
|
||||
1 (do
|
||||
(set logo-x (+ logo-x (* logo-dx dt)))
|
||||
(set logo-y (+ logo-y (* logo-dy dt)))
|
||||
(when (or (< logo-x 0) (> logo-x 512))
|
||||
(set logo-dx (- logo-dx)))
|
||||
(when (or (< logo-y 0) (> logo-y 296))
|
||||
(set logo-dy (- logo-dy))))
|
||||
8 (cube3d.update dt)
|
||||
_ nil)
|
||||
(case active-demo
|
||||
:logo (do
|
||||
(set logo-x (+ logo-x (* logo-dx dt)))
|
||||
(set logo-y (+ logo-y (* logo-dy dt)))
|
||||
(when (or (< logo-x 0) (> logo-x 512))
|
||||
(set logo-dx (- logo-dx)))
|
||||
(when (or (< logo-y 0) (> logo-y 296))
|
||||
(set logo-dy (- logo-dy))))
|
||||
:cube3d (cube3d.update dt)
|
||||
:worldgen (worldgen.update dt))
|
||||
|
||||
(when particles
|
||||
(pxl8.particles_update particles dt))))
|
||||
|
||||
(global frame (fn []
|
||||
(case current-effect
|
||||
0 (bsp_world.frame)
|
||||
(case active-demo
|
||||
:logo (do
|
||||
(pxl8.clr 0)
|
||||
(when logo-sprite
|
||||
(pxl8.sprite logo-sprite logo-x logo-y 128 64)))
|
||||
|
||||
1 (do
|
||||
(pxl8.clr 0)
|
||||
(when logo-sprite
|
||||
(pxl8.sprite logo-sprite logo-x logo-y 128 64)))
|
||||
:plasma (pxl8.vfx_plasma time 0.10 0.04 1)
|
||||
|
||||
2 (pxl8.vfx_plasma time 0.10 0.04 1)
|
||||
:tunnel (pxl8.vfx_tunnel time 2.0 0.25)
|
||||
|
||||
3 (pxl8.vfx_tunnel time 2.0 0.25)
|
||||
:raster (do
|
||||
(pxl8.clr 0)
|
||||
(local bars [{:base_y 60 :amplitude 30 :height 16 :speed 2.0 :phase 0 :color 1 :fade_color 18}
|
||||
{:base_y 180 :amplitude 35 :height 16 :speed 1.8 :phase 2.0 :color 1 :fade_color 27}
|
||||
{:base_y 300 :amplitude 25 :height 16 :speed 2.2 :phase 4.0 :color 1 :fade_color 24}])
|
||||
(pxl8.vfx_raster_bars bars time))
|
||||
|
||||
4 (do
|
||||
(pxl8.clr 0)
|
||||
(local bars [{:base_y 60 :amplitude 30 :height 16 :speed 2.0 :phase 0 :color 1 :fade_color 18}
|
||||
{:base_y 180 :amplitude 35 :height 16 :speed 1.8 :phase 2.0 :color 1 :fade_color 27}
|
||||
{:base_y 300 :amplitude 25 :height 16 :speed 2.2 :phase 4.0 :color 1 :fade_color 24}])
|
||||
(pxl8.vfx_raster_bars bars time))
|
||||
:fire (do
|
||||
(pxl8.clr 0)
|
||||
(when particles
|
||||
(when (not fire-init)
|
||||
(pxl8.particles_clear particles)
|
||||
(pxl8.vfx_fire particles 160 140 100 12)
|
||||
(set fire-init true))
|
||||
(pxl8.particles_render particles)))
|
||||
|
||||
5 (do
|
||||
(pxl8.clr 0)
|
||||
(when particles
|
||||
(when (not fire-init)
|
||||
(pxl8.particles_clear particles)
|
||||
(pxl8.vfx_fire particles 160 140 100 12)
|
||||
(set fire-init true))
|
||||
(pxl8.particles_render particles)))
|
||||
:rain (do
|
||||
(pxl8.clr 0)
|
||||
(when particles
|
||||
(when (not rain-init)
|
||||
(pxl8.particles_clear particles)
|
||||
(pxl8.vfx_rain particles 320 10.0)
|
||||
(set rain-init true))
|
||||
(pxl8.particles_render particles)))
|
||||
|
||||
6 (do
|
||||
(pxl8.clr 0)
|
||||
(when particles
|
||||
(when (not rain-init)
|
||||
(pxl8.particles_clear particles)
|
||||
(pxl8.vfx_rain particles 320 10.0)
|
||||
(set rain-init true))
|
||||
(pxl8.particles_render particles)))
|
||||
:snow (do
|
||||
(pxl8.clr 0)
|
||||
(when particles
|
||||
(when (not snow-init)
|
||||
(pxl8.particles_clear particles)
|
||||
(pxl8.vfx_snow particles 320 5.0)
|
||||
(set snow-init true))
|
||||
(pxl8.particles_render particles)))
|
||||
|
||||
7 (do
|
||||
(pxl8.clr 0)
|
||||
(when particles
|
||||
(when (not snow-init)
|
||||
(pxl8.particles_clear particles)
|
||||
(pxl8.vfx_snow particles 320 5.0)
|
||||
(set snow-init true))
|
||||
(pxl8.particles_render particles)))
|
||||
:cube3d (cube3d.frame)
|
||||
|
||||
8 (cube3d.frame)
|
||||
:worldgen (worldgen.frame)
|
||||
|
||||
_ (pxl8.clr 0))))
|
||||
|
|
|
|||
151
demo/mod/worldgen.fnl
Normal file
151
demo/mod/worldgen.fnl
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
(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)
|
||||
|
||||
(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))
|
||||
(pxl8.info "Generated procedural cave"))))
|
||||
|
||||
(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))
|
||||
cell-size 64
|
||||
grid-min 0
|
||||
grid-max (* 32 cell-size)]
|
||||
|
||||
(var moving false)
|
||||
(var new-x cam-x)
|
||||
(var new-z cam-z)
|
||||
|
||||
(when (pxl8.key_down "w")
|
||||
(set new-x (+ new-x (* forward-x move-speed dt)))
|
||||
(set new-z (+ new-z (* forward-z move-speed dt)))
|
||||
(set moving true))
|
||||
|
||||
(when (pxl8.key_down "s")
|
||||
(set new-x (- new-x (* forward-x move-speed dt)))
|
||||
(set new-z (- new-z (* forward-z move-speed dt)))
|
||||
(set moving true))
|
||||
|
||||
(when (pxl8.key_down "q")
|
||||
(set new-x (- new-x (* right-x move-speed dt)))
|
||||
(set new-z (- new-z (* right-z move-speed dt)))
|
||||
(set moving true))
|
||||
|
||||
(when (pxl8.key_down "e")
|
||||
(set new-x (+ new-x (* right-x move-speed dt)))
|
||||
(set new-z (+ new-z (* right-z move-speed dt)))
|
||||
(set moving true))
|
||||
|
||||
(when (and (>= new-x grid-min) (<= new-x grid-max)
|
||||
(>= new-z grid-min) (<= 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 (+ cam-pitch (* turn-speed dt)))
|
||||
(when (> cam-pitch 1.5) (set cam-pitch 1.5)))
|
||||
|
||||
(when (pxl8.key_down "down")
|
||||
(set cam-pitch (- cam-pitch (* turn-speed dt)))
|
||||
(when (< cam-pitch -1.5) (set cam-pitch -1.5)))
|
||||
|
||||
(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.text (.. "Pos: " (string.format "%.0f" cam-x) ","
|
||||
(string.format "%.0f" cam-y) ","
|
||||
(string.format "%.0f" cam-z)) 10 25 12)
|
||||
|
||||
(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]))
|
||||
|
||||
(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}
|
||||
Loading…
Add table
Add a link
Reference in a new issue