add jump to demo #9
This commit is contained in:
parent
55b7a44af3
commit
53ea4dd883
3 changed files with 92 additions and 70 deletions
|
|
@ -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)
|
||||
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)))))))))
|
||||
(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)))))))
|
||||
|
||||
(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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue