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

View File

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