try to stop jitter
This commit is contained in:
@@ -2,14 +2,14 @@
|
||||
(:require
|
||||
[frontend.il.entity :as entity]
|
||||
[cljs.core.async :as async]
|
||||
[il.common.velocity :as v]))
|
||||
[il.common.math :as ilmath]))
|
||||
|
||||
(defn apply-velocity [e dt]
|
||||
(if (contains? e :velocity)
|
||||
(let [{x :x y :y vel :velocity} e
|
||||
{vx :x vy :y} vel
|
||||
dtvx (* dt vx)
|
||||
dtvy (* dt vy)]
|
||||
dtvx (Math/floor (* dt vx))
|
||||
dtvy (Math/floor (* dt vy))]
|
||||
(assoc e :x (+ x dtvx) :y (+ y dtvy)))
|
||||
e))
|
||||
|
||||
@@ -20,14 +20,20 @@
|
||||
(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)
|
||||
has-server-pos (not (and (nil? tx) (nil? ty)))
|
||||
has-actual-pos (not (and (nil? x) (nil? y)))]
|
||||
(cond
|
||||
(and (= x tx) (= y ty)) e
|
||||
(and has-server-pos (not has-actual-pos)) (assoc e :x tx :y ty)
|
||||
(and has-server-pos has-actual-pos) (assoc e :velocity (v/velocity-for x y tx ty speed))
|
||||
(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)))
|
||||
:else e)))
|
||||
|
||||
(defn apply-player-shoot [state mouse-pos]
|
||||
|
||||
1
src/il/common/constant.cljc
Normal file
1
src/il/common/constant.cljc
Normal file
@@ -0,0 +1 @@
|
||||
(ns il.common.constant)
|
||||
@@ -1,4 +1,4 @@
|
||||
(ns il.common.velocity)
|
||||
(ns il.common.math)
|
||||
|
||||
(defn velocity-for [x y tx ty speed]
|
||||
(let [dx (- tx x)
|
||||
@@ -9,3 +9,6 @@
|
||||
(if (zero? h)
|
||||
{:x 0.0 :y 0.0}
|
||||
{:x vx :y vy})))
|
||||
|
||||
(defn euclidean-distance [x1 y1 x2 y2]
|
||||
(Math/sqrt (+ (Math/pow (- x2 x1) 2) (Math/pow (- y2 y1) 2))))
|
||||
@@ -1 +0,0 @@
|
||||
(ns il.constant)
|
||||
@@ -1,6 +1,6 @@
|
||||
(ns il.handler
|
||||
(:require [il.state :as s]
|
||||
[il.common.velocity :as v]))
|
||||
[il.common.math :as ilmath]))
|
||||
|
||||
(defn handle-position [_ state-chan player-id {x :x y :y}]
|
||||
(s/change state-chan update-in [:players player-id] assoc :x x :y y))
|
||||
@@ -14,7 +14,7 @@
|
||||
{tx :x ty :y player :player} msg
|
||||
{x :x y :y} player
|
||||
speed 50 ; TODO: Get projectile speed from player weapon
|
||||
velocity (v/velocity-for x y tx ty speed)
|
||||
velocity (ilmath/velocity-for x y tx ty speed)
|
||||
bullet {:sprite "/sprite/projectile.png" ; TODO: Render sprite based on player weapon
|
||||
:x x
|
||||
:y y
|
||||
|
||||
Reference in New Issue
Block a user