add collision
This commit is contained in:
parent
8baf5f06ea
commit
2427e3a504
7 changed files with 302 additions and 52 deletions
|
|
@ -46,6 +46,8 @@
|
|||
(when transition-pending
|
||||
(when (and (= active-demo :worldgen) (not= transition-pending :worldgen))
|
||||
(pxl8.set_relative_mouse_mode false))
|
||||
(when (and (not= active-demo :worldgen) (= transition-pending :worldgen))
|
||||
(pxl8.set_relative_mouse_mode true))
|
||||
(set active-demo transition-pending)
|
||||
(set transition-pending nil)
|
||||
(when (= active-demo :fire) (set fire-init? false))
|
||||
|
|
|
|||
|
|
@ -1,14 +1,24 @@
|
|||
(local pxl8 (require :pxl8))
|
||||
|
||||
(local bob-amount 4.0)
|
||||
(local bob-speed 8.0)
|
||||
(var bob-time 0)
|
||||
(var world nil)
|
||||
|
||||
(var auto-run? false)
|
||||
(var cam-pitch 0)
|
||||
(var cam-x 1000)
|
||||
(var cam-y 64)
|
||||
(var cam-yaw 0)
|
||||
(var cam-z 1000)
|
||||
(var cursor-look? true)
|
||||
|
||||
(var smooth-cam-x 1000)
|
||||
(var smooth-cam-z 1000)
|
||||
(local cam-smoothing 0.25)
|
||||
|
||||
(local bob-amount 4.0)
|
||||
(local bob-speed 8.0)
|
||||
(var bob-time 0)
|
||||
(local cell-size 64)
|
||||
(local cursor-sensitivity 0.008)
|
||||
(local gravity -800)
|
||||
(local grid-size 64)
|
||||
(var grounded? true)
|
||||
|
|
@ -18,13 +28,9 @@
|
|||
(local land-squash-amount -4)
|
||||
(var land-squash 0)
|
||||
(local max-pitch 1.5)
|
||||
(var mouse-look? true)
|
||||
(local mouse-sensitivity 0.008)
|
||||
(local move-speed 200)
|
||||
(local turn-speed 2.0)
|
||||
(var velocity-y 0)
|
||||
(var world nil)
|
||||
(var auto-run? false)
|
||||
|
||||
(fn init []
|
||||
(set world (pxl8.world_new))
|
||||
|
|
@ -111,27 +117,31 @@
|
|||
|
||||
(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))
|
||||
(let [(resolved-x resolved-y resolved-z) (pxl8.world_resolve_collision world cam-x cam-y cam-z new-x cam-y new-z 5)]
|
||||
(set cam-x resolved-x)
|
||||
(set cam-z resolved-z)))
|
||||
|
||||
(when mouse-look?
|
||||
(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 cursor-look?
|
||||
(let [dx (pxl8.mouse_dx)
|
||||
dy (pxl8.mouse_dy)]
|
||||
(set cam-yaw (- cam-yaw (* dx mouse-sensitivity)))
|
||||
(set cam-yaw (- cam-yaw (* dx cursor-sensitivity)))
|
||||
(set cam-pitch (math.max (- max-pitch)
|
||||
(math.min max-pitch
|
||||
(- cam-pitch (* dy mouse-sensitivity)))))))
|
||||
(- cam-pitch (* dy cursor-sensitivity)))))))
|
||||
|
||||
(when (and (not mouse-look?) (pxl8.key_down "left"))
|
||||
(when (and (not cursor-look?) (pxl8.key_down "left"))
|
||||
(set cam-yaw (+ cam-yaw (* turn-speed dt))))
|
||||
|
||||
(when (and (not mouse-look?) (pxl8.key_down "right"))
|
||||
(when (and (not cursor-look?) (pxl8.key_down "right"))
|
||||
(set cam-yaw (- cam-yaw (* turn-speed dt))))
|
||||
|
||||
(when (and (not mouse-look?) (pxl8.key_down "up"))
|
||||
(when (and (not cursor-look?) (pxl8.key_down "up"))
|
||||
(set cam-pitch (math.min max-pitch (+ cam-pitch (* turn-speed dt)))))
|
||||
|
||||
(when (and (not mouse-look?) (pxl8.key_down "down"))
|
||||
(when (and (not cursor-look?) (pxl8.key_down "down"))
|
||||
(set cam-pitch (math.max (- max-pitch) (- cam-pitch (* turn-speed dt)))))
|
||||
|
||||
(when (and (pxl8.key_pressed "space") grounded?)
|
||||
|
|
@ -164,9 +174,9 @@
|
|||
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)
|
||||
target-x (+ smooth-cam-x forward-x)
|
||||
target-y (+ eye-y (math.sin cam-pitch))
|
||||
target-z (+ cam-z forward-z)]
|
||||
target-z (+ smooth-cam-z forward-z)]
|
||||
|
||||
(pxl8.clear_zbuffer)
|
||||
(pxl8.set_backface_culling true)
|
||||
|
|
@ -177,13 +187,13 @@
|
|||
(pxl8.set_projection (pxl8.mat4_perspective fov aspect 1.0 4096.0)))
|
||||
|
||||
(pxl8.set_view (pxl8.mat4_lookat
|
||||
[cam-x eye-y cam-z]
|
||||
[smooth-cam-x eye-y smooth-cam-z]
|
||||
[target-x target-y target-z]
|
||||
[0 1 0]))
|
||||
|
||||
(pxl8.set_model (pxl8.mat4_identity))
|
||||
|
||||
(pxl8.world_render world [cam-x eye-y cam-z])
|
||||
(pxl8.world_render world [smooth-cam-x eye-y smooth-cam-z])
|
||||
|
||||
(let [cx (/ (pxl8.get_width) 2)
|
||||
cy (/ (pxl8.get_height) 2)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue