fix jitter kind of?

This commit is contained in:
2026-07-31 22:17:41 -06:00
parent 507faf8875
commit 2bd7ad1a28
2 changed files with 14 additions and 12 deletions

View File

@@ -23,7 +23,7 @@
right :a
up :w
down :s} (get-in @state [:player :keymap])
speed (get-in @state [:player :speed] 5)]
speed (get-in @state [:player :speed] 2.33)]
(swap! state update-in [:player :velocity] assoc
:x (* speed (+ left right))
:y (* speed (+ up down))))))
@@ -49,7 +49,6 @@
entity-relative-mouse-pos (.toLocal ^js entity-layer global-mouse-pos)
send-interval 25 ; Send every 25ms (10 tps)
elapsed (.-elapsedMS t)
{player :player} @state
{:keys [player entities players]} (-> (update @state :elapsed + elapsed)
(update/update-state :entities dt layers)
(update/update-state :players dt layers)
@@ -77,6 +76,7 @@
x :x
y :y} thing]
(when renderable
(js/console.log "Rendering thing: " x y)
(set! (.. renderable -x) x)
(set! (.. renderable -y) y))))))

View File

@@ -20,20 +20,23 @@
(assoc e :renderable renderable))
e))
(def min-distance-for-update 1)
(defn apply-server-position [e]
(let [{:keys [x y tx ty]} e
speed (get e :speed 15)
min-distance-for-update 1
has-server-pos (not (and (nil? tx) (nil? ty)))
has-actual-pos (not (and (nil? x) (nil? y)))]
has-actual-pos (not (and (nil? x) (nil? y)))
velocityless-entity (dissoc e :velocity)]
(cond
(and (= x tx) (= y ty)) e
(and has-server-pos (not has-actual-pos)) (assoc e :x tx :y ty)
; If the position given by the server is the same as the current position, do nothing
(and has-server-pos (= x tx) (= y ty)) velocityless-entity
; When the entity has a rendered position, and a new server position
(and has-server-pos has-actual-pos) (let [delta (ilmath/euclidean-distance x y tx ty)]
(cond
(> delta min-distance-for-update) (assoc e :velocity (ilmath/velocity-for x y tx ty speed)
:else e)))
(if (> delta min-distance-for-update)
(assoc velocityless-entity :velocity (ilmath/velocity-for x y tx ty speed))
velocityless-entity))
; When the entity has no rendered position, and a new server position
has-server-pos (assoc velocityless-entity :x tx :y ty)
:else e)))
(defn apply-player-shoot [state mouse-pos]
@@ -71,13 +74,12 @@
(assoc state key new-values)))
(defn update-player [state dt send-interval layers mouse-pos sb]
(println state)
(let [{player :player} state
updated-player (-> player
(apply-renderable layers)
(assoc :shootbox sb)
(apply-velocity dt))]
(if-not (nil? player)
(if-not (and (nil? player) (nil? (player :renderable)))
(-> (assoc state :player updated-player)
(apply-player-position send-interval)
(apply-player-shoot mouse-pos))