diff --git a/src/frontend/il/app.cljs b/src/frontend/il/app.cljs index ee92c2a..a573643 100644 --- a/src/frontend/il/app.cljs +++ b/src/frontend/il/app.cljs @@ -38,6 +38,7 @@ (when (count outbox) (go (doseq [msg outbox] + (println state) (println "Writing event: " msg) (>! tx-chan (js/JSON.stringify (clj->js msg)))))) (assoc state :outbox [])) diff --git a/src/frontend/il/entity.cljs b/src/frontend/il/entity.cljs index f8acc0c..16c7838 100644 --- a/src/frontend/il/entity.cljs +++ b/src/frontend/il/entity.cljs @@ -6,7 +6,6 @@ y :y sprite :sprite} entity entity-sprite (pixi/Sprite.)] - (print "Creating sprite: " sprite " for entity " entity) (.. pixi -Assets (load sprite) (then (fn [loaded-texture] diff --git a/src/frontend/il/update.cljs b/src/frontend/il/update.cljs index 2dd8dd5..7954602 100644 --- a/src/frontend/il/update.cljs +++ b/src/frontend/il/update.cljs @@ -2,7 +2,7 @@ (:require [frontend.il.entity :as entity] [cljs.core.async :as async] - [goog.dom :as dom])) + [il.common.velocity :as v])) (defn apply-velocity [e dt] (if (contains? e :velocity) @@ -20,6 +20,17 @@ (assoc e :renderable renderable)) e)) + +(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 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)) + :else e))) + (defn apply-player-shoot [state mouse-pos] (if (and (get-in state [:player :keymap :lmb] false) (< (+ (get-in state [:player :last-attack] -1) @@ -43,9 +54,11 @@ (update :outbox conj {:type "position" :x x :y y})) state))) +;; TODO: Cleanup entities somehow (defn update-entities [state dt layers] (let [{entities :entities} state new-entities (update-vals entities #(-> % + (apply-server-position) (apply-renderable layers) (apply-velocity dt)))] (assoc state :entities new-entities))) @@ -59,17 +72,25 @@ (apply-player-position send-interval) (apply-player-shoot mouse-pos)))) +(defn- deep-merge [& maps] + (apply merge-with + (fn [old new] + (if (and (map? old) (map? new)) + (deep-merge old new) + new)) + maps)) + (defn update-from-server [state rx-chan] (loop [state-acc state {:keys [type] :as msg} (async/poll! rx-chan)] (if (and (not (nil? msg)) (= type "sync")) (let [entities (msg :entities) + state-entities (state-acc :entities) tx-entities (update-vals entities (fn [e] {:sprite (:sprite e) - :x (:x e) - :y (:y e) :tx (:x e) - :ty (:y e)}))] + :ty (:y e)})) + merged-entities (deep-merge state-entities tx-entities)] (recur - (update-in state-acc [:entities] merge tx-entities) + (assoc state-acc :entities merged-entities) (async/poll! rx-chan))) state-acc))) diff --git a/src/il/common/velocity.cljc b/src/il/common/velocity.cljc new file mode 100644 index 0000000..32f1fc1 --- /dev/null +++ b/src/il/common/velocity.cljc @@ -0,0 +1,11 @@ +(ns il.common.velocity) + +(defn velocity-for [x y tx ty speed] + (let [dx (- tx x) + dy (- ty y) + h (Math/hypot dx dy) + vx (* (/ dx h) speed) + vy (* (/ dy h) speed)] + (if (zero? h) + {:x 0.0 :y 0.0} + {:x vx :y vy}))) diff --git a/src/il/core.clj b/src/il/core.clj index 0375525..3e2efbd 100644 --- a/src/il/core.clj +++ b/src/il/core.clj @@ -26,7 +26,8 @@ (= type "position") (handler/handle-position @state state-chan player-id msg) (= type "shoot") (handler/handle-shoot @state state-chan player-id msg) (= type "invalid") (>! out "Hello World!")) - (println msg)) + (println msg) + (println @state)) (recur)) (do (s/change state-chan update :players dissoc player-id) @@ -61,7 +62,6 @@ (recur (conj inputs args) (inc count)) inputs)))] (let [event-state (reduce (fn [state event-args] - (println event-args) (apply (first event-args) state (vec (rest event-args)))) @state events-to-apply) diff --git a/src/il/handler.clj b/src/il/handler.clj index b1d27d0..facdc3c 100644 --- a/src/il/handler.clj +++ b/src/il/handler.clj @@ -1,24 +1,20 @@ (ns il.handler - (:require [il.state :as s])) + (:require [il.state :as s] + [il.common.velocity :as v])) (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)) (defn handle-shoot [state state-chan player-id msg] (let [bullet-id (random-uuid) - {x :x y :y} (get-in state [:players player-id]) + player (get-in state [:players player-id]) + {x :x y :y} player {tx :x ty :y} msg - _ (println state) - _ (println msg) - _ (println (get-in state [:players player-id])) - dx (- tx x) - dy (- ty y) - h (Math/sqrt (+ (Math/pow dx 2) (Math/pow dy 2))) - speed 15 ; TODO: Calculate projectile speed based on player stats - vx (* (/ dx h) speed) - vy (* (/ dy h) speed) + speed 25 ; TODO: Get projectile speed from player weapon + velocity (v/velocity-for x y tx ty speed) bullet {:sprite "/sprite/projectile.png" ; TODO: Render sprite based on player weapon :x x :y y - :velocity {:x vx :y vy}}] + :speed speed + :velocity velocity}] (s/change state-chan update :entities assoc bullet-id bullet))) diff --git a/src/il/system/movement.clj b/src/il/system/movement.clj index dc7bdf8..7d55b2e 100644 --- a/src/il/system/movement.clj +++ b/src/il/system/movement.clj @@ -13,5 +13,4 @@ (defn system [state] (let [entities (:entities state) new-entities (update-vals entities velocity-move)] - (println "Applying movement system: " state) (assoc state :entities new-entities)))