refactor: decouple sim from framework, remove voxel geometry
This commit is contained in:
parent
c538641ec8
commit
5a565844dd
41 changed files with 477 additions and 2407 deletions
|
|
@ -1,4 +1,5 @@
|
|||
(local pxl8 (require :pxl8))
|
||||
(local bit (require :bit))
|
||||
(local effects (require :pxl8.effects))
|
||||
(local net (require :pxl8.net))
|
||||
|
||||
|
|
@ -10,36 +11,37 @@
|
|||
(local bob-amount 4.0)
|
||||
(local bob-speed 8.0)
|
||||
(local cam-smoothing 0.25)
|
||||
(local ceiling-height 120)
|
||||
(local chunk-size 64)
|
||||
(local cursor-sensitivity 0.010)
|
||||
(local gravity 600)
|
||||
(local grid-size 64)
|
||||
(local jump-velocity 180)
|
||||
(local land-recovery-speed 20)
|
||||
(local land-squash-amount -4)
|
||||
(local max-pitch 1.5)
|
||||
(local move-speed 200)
|
||||
(local player-eye-height 64)
|
||||
(local player-height 72)
|
||||
(local player-radius 12)
|
||||
(local step-height 24)
|
||||
(local day-length 1800)
|
||||
(local turn-speed 4.0)
|
||||
|
||||
(local SIM_FLAG_GROUNDED 4)
|
||||
|
||||
(local sim-cfg (pxl8.sim_config {:move_speed 150
|
||||
:gravity 600
|
||||
:jump_velocity 180
|
||||
:player_radius 12
|
||||
:player_height 72
|
||||
:max_pitch 1.5
|
||||
:friction 6.0
|
||||
:ground_accel 10.0
|
||||
:air_accel 1.0
|
||||
:stop_speed 100.0}))
|
||||
|
||||
(var auto-run-cancel-key nil)
|
||||
(var auto-run? false)
|
||||
(var was-auto-running false)
|
||||
(var bob-time 0)
|
||||
(var cam-pitch 0)
|
||||
(var day-time 0)
|
||||
(var cam-x 416)
|
||||
(var cam-y 600)
|
||||
(var cam-y 0)
|
||||
(var cam-yaw 0)
|
||||
(var cam-z 416)
|
||||
(var camera nil)
|
||||
(var ceiling-tex nil)
|
||||
(var floor-tex nil)
|
||||
(var grounded true)
|
||||
(var land-squash 0)
|
||||
(var last-dt 0.016)
|
||||
(var light-time 0)
|
||||
|
|
@ -51,37 +53,16 @@
|
|||
(var real-time 0)
|
||||
(var smooth-cam-x 416)
|
||||
(var smooth-cam-z 416)
|
||||
(var vel-y 0)
|
||||
(var was-grounded true)
|
||||
(var trim-tex nil)
|
||||
(var wall-tex nil)
|
||||
(var world nil)
|
||||
|
||||
(local cursor-look? true)
|
||||
(local MOSS_COLOR 200)
|
||||
(local PLASTER_COLOR 16)
|
||||
(local STONE_FLOOR_START 37)
|
||||
(local STONE_WALL_START 2)
|
||||
(local WOOD_COLOR 88)
|
||||
|
||||
(fn find-floor [x y z in-bsp]
|
||||
(if (not world)
|
||||
0
|
||||
(if in-bsp
|
||||
0
|
||||
(let [start-y (math.max (+ y 64) 256)
|
||||
ray (world:ray x start-y z x (- y 1000) z)]
|
||||
(if ray.hit
|
||||
(+ ray.point.y 1)
|
||||
128)))))
|
||||
|
||||
(fn find-ceiling [x y z max-height]
|
||||
(if (not world)
|
||||
nil
|
||||
(let [ray (world:ray x (+ y 1) z x (+ y max-height) z)]
|
||||
(if ray.hit
|
||||
(- ray.point.y 1)
|
||||
nil))))
|
||||
|
||||
(fn preload []
|
||||
(when (not network)
|
||||
(set network (net.get))
|
||||
|
|
@ -90,6 +71,7 @@
|
|||
(when (not world)
|
||||
(set world (pxl8.get_world))
|
||||
(when world
|
||||
(world:set_sim_config sim-cfg)
|
||||
(world:init_local_player cam-x cam-y cam-z))))
|
||||
|
||||
(fn is-connected []
|
||||
|
|
@ -126,6 +108,11 @@
|
|||
(sky.generate-stars)
|
||||
(preload)
|
||||
|
||||
(when world
|
||||
(world:init_local_player cam-x cam-y cam-z)
|
||||
(set smooth-cam-x cam-x)
|
||||
(set smooth-cam-z cam-z))
|
||||
|
||||
(when (not ceiling-tex)
|
||||
(set ceiling-tex (textures.plaster-wall 66666 PLASTER_COLOR)))
|
||||
(when (not floor-tex)
|
||||
|
|
@ -149,9 +136,6 @@
|
|||
(set bsp-materials-setup true))))))
|
||||
|
||||
(fn sample-input []
|
||||
(var move-forward 0)
|
||||
(var move-right 0)
|
||||
|
||||
(when (pxl8.key_pressed "`")
|
||||
(set auto-run? (not auto-run?))
|
||||
(when (and auto-run? (pxl8.key_down "w"))
|
||||
|
|
@ -163,19 +147,15 @@
|
|||
(when (and auto-run-cancel-key (not (pxl8.key_down auto-run-cancel-key)))
|
||||
(set auto-run-cancel-key nil))
|
||||
|
||||
(when (or (pxl8.key_down "w") auto-run?)
|
||||
(set move-forward (+ move-forward 1)))
|
||||
(when (and (pxl8.key_down "s") (not= auto-run-cancel-key "s"))
|
||||
(set move-forward (- move-forward 1)))
|
||||
(when (pxl8.key_down "a")
|
||||
(set move-right (- move-right 1)))
|
||||
(when (pxl8.key_down "d")
|
||||
(set move-right (+ move-right 1)))
|
||||
(set was-auto-running auto-run?)
|
||||
|
||||
{:move_x move-right
|
||||
:move_y move-forward
|
||||
:look_dx (pxl8.mouse_dx)
|
||||
:look_dy (pxl8.mouse_dy)})
|
||||
(pxl8.make_input_msg
|
||||
{:move_x (+ (if (pxl8.key_down "d") 1 0) (if (pxl8.key_down "a") -1 0))
|
||||
:move_y (+ (if (or (pxl8.key_down "w") auto-run?) 1 0)
|
||||
(if (and (pxl8.key_down "s") (not= auto-run-cancel-key "s")) -1 0))
|
||||
:look_dx (pxl8.mouse_dx)
|
||||
:look_dy (pxl8.mouse_dy)
|
||||
:buttons (if (pxl8.key_down "space") 1 0)}))
|
||||
|
||||
(fn update [dt]
|
||||
(set last-dt dt)
|
||||
|
|
@ -185,137 +165,81 @@
|
|||
(set portal-cooldown (- portal-cooldown dt)))
|
||||
|
||||
(when (and world network (<= portal-cooldown 0))
|
||||
(let [chunk (world:active_chunk)
|
||||
in-bsp (not= chunk nil)
|
||||
(door-x door-z) (entities.get-door-position)
|
||||
(let [(door-x door-z) (entities.get-door-position)
|
||||
door-radius (entities.get-door-radius)
|
||||
dist-to-door (math.sqrt (+ (* (- cam-x door-x) (- cam-x door-x))
|
||||
(* (- cam-z door-z) (- cam-z door-z))))]
|
||||
(when (< dist-to-door door-radius)
|
||||
(if in-bsp
|
||||
(do
|
||||
(pxl8.info "Exiting through door...")
|
||||
(let [exit-x (+ door-x 50)
|
||||
exit-z door-z
|
||||
exit-y 600]
|
||||
(network:exit_chunk exit-x exit-y exit-z)
|
||||
(set cam-x exit-x)
|
||||
(set cam-z exit-z)
|
||||
(set cam-y exit-y))
|
||||
(set vel-y 0)
|
||||
(set grounded false)
|
||||
(set smooth-cam-x cam-x)
|
||||
(set smooth-cam-z cam-z)
|
||||
(set bsp-materials-setup false)
|
||||
(set portal-cooldown 2.0))
|
||||
(do
|
||||
(pxl8.info "Entering through door...")
|
||||
(network:enter_chunk 1)
|
||||
(set cam-x 416)
|
||||
(set cam-z 416)
|
||||
(set cam-y 0)
|
||||
(set vel-y 0)
|
||||
(set grounded true)
|
||||
(set smooth-cam-x 416)
|
||||
(set smooth-cam-z 416)
|
||||
(set portal-cooldown 2.0))))))
|
||||
(let [current-id (network:chunk_id)]
|
||||
(if (= current-id 1)
|
||||
(do
|
||||
(pxl8.info "Door: BSP 1 -> BSP 2")
|
||||
(network:enter_scene 1 2 416 0 416)
|
||||
(world:init_local_player 416 0 416)
|
||||
(set cam-x 416)
|
||||
(set cam-y 0)
|
||||
(set cam-z 416)
|
||||
(set smooth-cam-x 416)
|
||||
(set smooth-cam-z 416)
|
||||
(set bsp-materials-setup false)
|
||||
(set portal-cooldown 2.0))
|
||||
(= current-id 2)
|
||||
(do
|
||||
(pxl8.info "Door: BSP 2 -> BSP 1")
|
||||
(network:enter_scene 1 1 416 0 416)
|
||||
(world:init_local_player 416 0 416)
|
||||
(set cam-x 416)
|
||||
(set cam-y 0)
|
||||
(set cam-z 416)
|
||||
(set smooth-cam-x 416)
|
||||
(set smooth-cam-z 416)
|
||||
(set bsp-materials-setup false)
|
||||
(set portal-cooldown 2.0)))))))
|
||||
|
||||
(let [chunk (world:active_chunk)
|
||||
chunk-id (if network (network:chunk_id) -1)
|
||||
expecting-bsp (> chunk-id 0)
|
||||
voxel-space (and (not chunk) (= chunk-id 0))
|
||||
ready (or voxel-space (and chunk (chunk:ready)))]
|
||||
(when world
|
||||
(let [input (sample-input)
|
||||
grid-max (if voxel-space 100000 (* grid-size chunk-size))
|
||||
movement-yaw cam-yaw]
|
||||
(let [input-msg (sample-input)
|
||||
player (world:local_player)]
|
||||
|
||||
(let [player (world:local_player)]
|
||||
(when player
|
||||
(set cam-x player.pos.x)
|
||||
(set cam-y player.pos.y)
|
||||
(set cam-z player.pos.z)
|
||||
(set cam-yaw player.yaw)
|
||||
(set cam-pitch player.pitch)))
|
||||
(world:push_input input-msg)
|
||||
|
||||
(when (and voxel-space grounded)
|
||||
(let [floor-y (find-floor cam-x cam-y cam-z false)
|
||||
height-diff (- floor-y cam-y)]
|
||||
(if (and (>= height-diff (- step-height)) (<= height-diff step-height))
|
||||
(let [lerp-speed 0.3
|
||||
smooth-y (+ (* cam-y (- 1 lerp-speed)) (* floor-y lerp-speed))]
|
||||
(set cam-y smooth-y))
|
||||
(when (< height-diff (- step-height))
|
||||
(set grounded false)))))
|
||||
(when player
|
||||
(set cam-x player.pos.x)
|
||||
(set cam-y player.pos.y)
|
||||
(set cam-z player.pos.z)
|
||||
(set cam-yaw player.yaw)
|
||||
(set cam-pitch player.pitch))
|
||||
|
||||
(let [now-grounded (if player (not= (bit.band player.flags SIM_FLAG_GROUNDED) 0) true)]
|
||||
(when (and (not was-grounded) now-grounded)
|
||||
(set land-squash land-squash-amount))
|
||||
(set was-grounded now-grounded))
|
||||
|
||||
(set smooth-cam-x (+ (* smooth-cam-x (- 1 cam-smoothing)) (* cam-x cam-smoothing)))
|
||||
(set smooth-cam-z (+ (* smooth-cam-z (- 1 cam-smoothing)) (* cam-z cam-smoothing)))
|
||||
|
||||
(when (and (pxl8.key_pressed "space") grounded)
|
||||
(set vel-y jump-velocity)
|
||||
(set grounded false))
|
||||
|
||||
(when (or (not grounded) (not= vel-y 0))
|
||||
(set vel-y (- vel-y (* gravity dt)))
|
||||
|
||||
(if (> vel-y 0)
|
||||
(let [new-y (+ cam-y (* vel-y dt))
|
||||
head-y (+ new-y player-height)
|
||||
ceiling-y (if voxel-space
|
||||
(find-ceiling cam-x cam-y cam-z 200)
|
||||
ceiling-height)]
|
||||
(if (and ceiling-y (>= head-y ceiling-y))
|
||||
(do
|
||||
(set cam-y (- ceiling-y player-height))
|
||||
(set vel-y 0))
|
||||
(set cam-y new-y)))
|
||||
(let [new-y (+ cam-y (* vel-y dt))
|
||||
floor-y (find-floor cam-x cam-y cam-z (not voxel-space))]
|
||||
(if (<= new-y floor-y)
|
||||
(do
|
||||
(set cam-y floor-y)
|
||||
(set land-squash land-squash-amount)
|
||||
(set vel-y 0)
|
||||
(set grounded true))
|
||||
(do
|
||||
(set cam-y new-y)
|
||||
(set grounded false))))))
|
||||
|
||||
(when (< land-squash 0)
|
||||
(set land-squash (math.min 0 (+ land-squash (* land-recovery-speed dt)))))
|
||||
|
||||
(let [moving (or (not= input.move_x 0) (not= input.move_y 0))]
|
||||
(if (and moving grounded)
|
||||
(let [moving (or (not= input-msg.move_x 0) (not= input-msg.move_y 0))]
|
||||
(if (and moving was-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))))))
|
||||
|
||||
(set day-time (% (+ day-time (/ dt day-length)) 1))
|
||||
(set light-time (+ light-time (* dt 0.5)))
|
||||
(set real-time (+ real-time dt))))))
|
||||
(set real-time (+ real-time dt)))))
|
||||
|
||||
(fn frame []
|
||||
(pxl8.clear 1)
|
||||
|
||||
(when (not camera)
|
||||
(pxl8.error "camera is nil!"))
|
||||
|
||||
(when (not world)
|
||||
(pxl8.error "world is nil!"))
|
||||
(when (or (not camera) (not world))
|
||||
(lua "return"))
|
||||
|
||||
(let [chunk (when world (world:active_chunk))
|
||||
expecting-bsp (and network (> (network:chunk_id) 0))
|
||||
voxel-space (and world (not chunk) (not expecting-bsp))
|
||||
ready (or voxel-space (and chunk (chunk:ready)))]
|
||||
ready (and chunk (chunk:ready))]
|
||||
|
||||
(when (not camera)
|
||||
(pxl8.text "camera nil" 5 30 12))
|
||||
(when (not world)
|
||||
(pxl8.text "world nil" 5 40 12))
|
||||
(when (and world (not ready))
|
||||
(pxl8.text (.. "Waiting... chunk=" (tostring chunk) " voxel=" (tostring voxel-space)) 5 50 12))
|
||||
|
||||
(when (and camera world)
|
||||
(when ready
|
||||
(let [bob-offset (* (math.sin bob-time) bob-amount)
|
||||
eye-y (+ cam-y player-eye-height bob-offset land-squash)
|
||||
forward-x (- (math.sin cam-yaw))
|
||||
|
|
@ -364,7 +288,7 @@
|
|||
(when chunk
|
||||
(entities.render-fireball light-x light-y light-z (menu.is-wireframe)))
|
||||
|
||||
(entities.render-door (menu.is-wireframe) (if voxel-space 128 0))
|
||||
(entities.render-door (menu.is-wireframe) 0)
|
||||
|
||||
(pxl8.end_frame_3d)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,8 @@
|
|||
(local pxl8 (require :pxl8))
|
||||
(local music (require :mod.music))
|
||||
(local world-mod (require :pxl8.world))
|
||||
(local net-mod (require :pxl8.net))
|
||||
|
||||
(var paused false)
|
||||
(var gui nil)
|
||||
(var render-distance 3)
|
||||
(var sim-distance 4)
|
||||
(var current-panel :main)
|
||||
(var selected-item nil)
|
||||
(var current-items [])
|
||||
|
|
@ -18,11 +14,7 @@
|
|||
(var wireframe false)
|
||||
|
||||
(fn init []
|
||||
(set gui (pxl8.create_gui))
|
||||
(let [w (world-mod.World.get)]
|
||||
(when w
|
||||
(set render-distance (w:get_render_distance))
|
||||
(set sim-distance (w:get_sim_distance)))))
|
||||
(set gui (pxl8.create_gui)))
|
||||
|
||||
(fn show []
|
||||
(set paused true)
|
||||
|
|
@ -97,7 +89,7 @@
|
|||
(set selected-item label))
|
||||
(let [is-selected (= selected-item label)]
|
||||
(when is-selected
|
||||
(pxl8.rect (- x 3) (- y 3) (+ w 6) (+ h 6) 15))
|
||||
(pxl8.rect (- x 3) (- y 3) (+ w 6) (+ h 6) (pxl8.gui_color 4)))
|
||||
(let [clicked (gui:button id x y w h label)]
|
||||
(when clicked
|
||||
(set selected-item label))
|
||||
|
|
@ -129,14 +121,14 @@
|
|||
(music.stop)
|
||||
(music.start))))
|
||||
|
||||
(pxl8.gui_label 215 185 "Volume/Devices: TODO" 15)
|
||||
(pxl8.gui_label 215 185 "Volume/Devices: TODO" (pxl8.gui_color 4))
|
||||
|
||||
(when (menu-button 20 215 210 210 30 "Back")
|
||||
(set current-panel :main)
|
||||
(set selected-item nil)))
|
||||
|
||||
(fn draw-gfx-panel []
|
||||
(pxl8.gui_window 200 60 240 220 "GFX")
|
||||
(pxl8.gui_window 200 60 240 195 "GFX")
|
||||
|
||||
(let [baked-label (if baked-lighting "Baked Lighting: On" "Baked Lighting: Off")]
|
||||
(when (menu-button 40 215 107 210 24 baked-label)
|
||||
|
|
@ -146,24 +138,15 @@
|
|||
(when (menu-button 41 215 134 210 24 dynamic-label)
|
||||
(set dynamic-lighting (not dynamic-lighting))))
|
||||
|
||||
(pxl8.gui_label 215 162 (.. "Render: " render-distance) 15)
|
||||
(let [(changed new-val) (gui:slider_int 30 215 175 210 14 render-distance 1 8)]
|
||||
(when changed
|
||||
(set render-distance new-val)
|
||||
(let [w (world-mod.World.get)
|
||||
n (net-mod.get)]
|
||||
(when w (w:set_render_distance new-val))
|
||||
(when n (n:set_chunk_settings new-val sim-distance)))))
|
||||
|
||||
(let [tex-label (if textures "Textures: On" "Textures: Off")]
|
||||
(when (menu-button 42 215 194 210 24 tex-label)
|
||||
(when (menu-button 42 215 161 210 24 tex-label)
|
||||
(set textures (not textures))))
|
||||
|
||||
(let [wire-label (if wireframe "Wireframe: On" "Wireframe: Off")]
|
||||
(when (menu-button 43 215 221 210 24 wire-label)
|
||||
(when (menu-button 43 215 188 210 24 wire-label)
|
||||
(set wireframe (not wireframe))))
|
||||
|
||||
(when (menu-button 32 215 248 210 24 "Back")
|
||||
(when (menu-button 32 215 218 210 24 "Back")
|
||||
(set current-panel :main)
|
||||
(set selected-item nil)))
|
||||
|
||||
|
|
|
|||
|
|
@ -327,4 +327,58 @@
|
|||
(g:destroy)
|
||||
tex-id)))
|
||||
|
||||
(fn textures.rough-stone [seed base-color]
|
||||
(let [g (build-graph seed
|
||||
(fn [ctx]
|
||||
(let [cell (voronoi-cell ctx 8)
|
||||
edge (voronoi-edge ctx 8)
|
||||
crack-threshold (const ctx 0.03)
|
||||
is-crack (sub ctx crack-threshold edge)
|
||||
crack-color (const ctx (- base-color 3))
|
||||
roughness (noise-turbulence ctx 3 24 0.6)
|
||||
detail (noise-value ctx 48)
|
||||
combined (add ctx (mul ctx roughness (const ctx 0.5))
|
||||
(add ctx (mul ctx cell (const ctx 0.3))
|
||||
(mul ctx detail (const ctx 0.2))))
|
||||
stone-quant (quantize ctx combined base-color 6)]
|
||||
(select ctx is-crack crack-color stone-quant))))]
|
||||
(let [tex-id (g:eval_texture 64 64)]
|
||||
(g:destroy)
|
||||
tex-id)))
|
||||
|
||||
(fn textures.packed-dirt [seed base-color]
|
||||
(let [g (build-graph seed
|
||||
(fn [ctx]
|
||||
(let [base-noise (noise-fbm ctx 3 16 0.5)
|
||||
detail (noise-value ctx 32)
|
||||
combined (add ctx (mul ctx base-noise (const ctx 0.6))
|
||||
(mul ctx detail (const ctx 0.4)))
|
||||
dirt-quant (quantize ctx combined base-color 5)
|
||||
pebble (voronoi-cell ctx 12)
|
||||
pebble-edge (voronoi-edge ctx 12)
|
||||
pebble-threshold (const ctx 0.08)
|
||||
is-pebble (sub ctx pebble-threshold pebble-edge)
|
||||
pebble-quant (quantize ctx pebble (- base-color 2) 4)]
|
||||
(select ctx is-pebble pebble-quant dirt-quant))))]
|
||||
(let [tex-id (g:eval_texture 64 64)]
|
||||
(g:destroy)
|
||||
tex-id)))
|
||||
|
||||
(fn textures.grass-top [seed base-color]
|
||||
(let [g (build-graph seed
|
||||
(fn [ctx]
|
||||
(let [blade-x (mul ctx ctx.x (const ctx 32))
|
||||
blade-y (mul ctx ctx.y (const ctx 4))
|
||||
blades (noise-fbm-at ctx blade-x blade-y 3 6 0.5)
|
||||
fine (noise-value ctx 64)
|
||||
patch (noise-fbm ctx 2 6 0.4)
|
||||
combined (add ctx (mul ctx blades (const ctx 0.4))
|
||||
(add ctx (mul ctx fine (const ctx 0.3))
|
||||
(mul ctx patch (const ctx 0.3))))
|
||||
grass-quant (quantize ctx combined base-color 6)]
|
||||
grass-quant)))]
|
||||
(let [tex-id (g:eval_texture 64 64)]
|
||||
(g:destroy)
|
||||
tex-id)))
|
||||
|
||||
textures
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue