shooty
This commit is contained in:
@@ -38,6 +38,7 @@
|
|||||||
(when (count outbox)
|
(when (count outbox)
|
||||||
(go
|
(go
|
||||||
(doseq [msg outbox]
|
(doseq [msg outbox]
|
||||||
|
(println state)
|
||||||
(println "Writing event: " msg)
|
(println "Writing event: " msg)
|
||||||
(>! tx-chan (js/JSON.stringify (clj->js msg))))))
|
(>! tx-chan (js/JSON.stringify (clj->js msg))))))
|
||||||
(assoc state :outbox []))
|
(assoc state :outbox []))
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
y :y
|
y :y
|
||||||
sprite :sprite} entity
|
sprite :sprite} entity
|
||||||
entity-sprite (pixi/Sprite.)]
|
entity-sprite (pixi/Sprite.)]
|
||||||
(print "Creating sprite: " sprite " for entity " entity)
|
|
||||||
(.. pixi -Assets
|
(.. pixi -Assets
|
||||||
(load sprite)
|
(load sprite)
|
||||||
(then (fn [loaded-texture]
|
(then (fn [loaded-texture]
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
(:require
|
(:require
|
||||||
[frontend.il.entity :as entity]
|
[frontend.il.entity :as entity]
|
||||||
[cljs.core.async :as async]
|
[cljs.core.async :as async]
|
||||||
[goog.dom :as dom]))
|
[il.common.velocity :as v]))
|
||||||
|
|
||||||
(defn apply-velocity [e dt]
|
(defn apply-velocity [e dt]
|
||||||
(if (contains? e :velocity)
|
(if (contains? e :velocity)
|
||||||
@@ -20,6 +20,17 @@
|
|||||||
(assoc e :renderable renderable))
|
(assoc e :renderable renderable))
|
||||||
e))
|
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]
|
(defn apply-player-shoot [state mouse-pos]
|
||||||
(if (and (get-in state [:player :keymap :lmb] false)
|
(if (and (get-in state [:player :keymap :lmb] false)
|
||||||
(< (+ (get-in state [:player :last-attack] -1)
|
(< (+ (get-in state [:player :last-attack] -1)
|
||||||
@@ -43,9 +54,11 @@
|
|||||||
(update :outbox conj {:type "position" :x x :y y}))
|
(update :outbox conj {:type "position" :x x :y y}))
|
||||||
state)))
|
state)))
|
||||||
|
|
||||||
|
;; TODO: Cleanup entities somehow
|
||||||
(defn update-entities [state dt layers]
|
(defn update-entities [state dt layers]
|
||||||
(let [{entities :entities} state
|
(let [{entities :entities} state
|
||||||
new-entities (update-vals entities #(-> %
|
new-entities (update-vals entities #(-> %
|
||||||
|
(apply-server-position)
|
||||||
(apply-renderable layers)
|
(apply-renderable layers)
|
||||||
(apply-velocity dt)))]
|
(apply-velocity dt)))]
|
||||||
(assoc state :entities new-entities)))
|
(assoc state :entities new-entities)))
|
||||||
@@ -59,17 +72,25 @@
|
|||||||
(apply-player-position send-interval)
|
(apply-player-position send-interval)
|
||||||
(apply-player-shoot mouse-pos))))
|
(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]
|
(defn update-from-server [state rx-chan]
|
||||||
(loop [state-acc state
|
(loop [state-acc state
|
||||||
{:keys [type] :as msg} (async/poll! rx-chan)]
|
{:keys [type] :as msg} (async/poll! rx-chan)]
|
||||||
(if (and (not (nil? msg)) (= type "sync"))
|
(if (and (not (nil? msg)) (= type "sync"))
|
||||||
(let [entities (msg :entities)
|
(let [entities (msg :entities)
|
||||||
|
state-entities (state-acc :entities)
|
||||||
tx-entities (update-vals entities (fn [e] {:sprite (:sprite e)
|
tx-entities (update-vals entities (fn [e] {:sprite (:sprite e)
|
||||||
:x (:x e)
|
|
||||||
:y (:y e)
|
|
||||||
:tx (:x e)
|
:tx (:x e)
|
||||||
:ty (:y e)}))]
|
:ty (:y e)}))
|
||||||
|
merged-entities (deep-merge state-entities tx-entities)]
|
||||||
(recur
|
(recur
|
||||||
(update-in state-acc [:entities] merge tx-entities)
|
(assoc state-acc :entities merged-entities)
|
||||||
(async/poll! rx-chan)))
|
(async/poll! rx-chan)))
|
||||||
state-acc)))
|
state-acc)))
|
||||||
|
|||||||
11
src/il/common/velocity.cljc
Normal file
11
src/il/common/velocity.cljc
Normal file
@@ -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})))
|
||||||
@@ -26,7 +26,8 @@
|
|||||||
(= type "position") (handler/handle-position @state state-chan player-id msg)
|
(= type "position") (handler/handle-position @state state-chan player-id msg)
|
||||||
(= type "shoot") (handler/handle-shoot @state state-chan player-id msg)
|
(= type "shoot") (handler/handle-shoot @state state-chan player-id msg)
|
||||||
(= type "invalid") (>! out "Hello World!"))
|
(= type "invalid") (>! out "Hello World!"))
|
||||||
(println msg))
|
(println msg)
|
||||||
|
(println @state))
|
||||||
(recur))
|
(recur))
|
||||||
(do
|
(do
|
||||||
(s/change state-chan update :players dissoc player-id)
|
(s/change state-chan update :players dissoc player-id)
|
||||||
@@ -61,7 +62,6 @@
|
|||||||
(recur (conj inputs args) (inc count))
|
(recur (conj inputs args) (inc count))
|
||||||
inputs)))]
|
inputs)))]
|
||||||
(let [event-state (reduce (fn [state event-args]
|
(let [event-state (reduce (fn [state event-args]
|
||||||
(println event-args)
|
|
||||||
(apply (first event-args) state (vec (rest event-args))))
|
(apply (first event-args) state (vec (rest event-args))))
|
||||||
@state
|
@state
|
||||||
events-to-apply)
|
events-to-apply)
|
||||||
|
|||||||
@@ -1,24 +1,20 @@
|
|||||||
(ns il.handler
|
(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}]
|
(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))
|
(s/change state-chan update-in [:players player-id] assoc :x x :y y))
|
||||||
|
|
||||||
(defn handle-shoot [state state-chan player-id msg]
|
(defn handle-shoot [state state-chan player-id msg]
|
||||||
(let [bullet-id (random-uuid)
|
(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
|
{tx :x ty :y} msg
|
||||||
_ (println state)
|
speed 25 ; TODO: Get projectile speed from player weapon
|
||||||
_ (println msg)
|
velocity (v/velocity-for x y tx ty speed)
|
||||||
_ (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)
|
|
||||||
bullet {:sprite "/sprite/projectile.png" ; TODO: Render sprite based on player weapon
|
bullet {:sprite "/sprite/projectile.png" ; TODO: Render sprite based on player weapon
|
||||||
:x x
|
:x x
|
||||||
:y y
|
:y y
|
||||||
:velocity {:x vx :y vy}}]
|
:speed speed
|
||||||
|
:velocity velocity}]
|
||||||
(s/change state-chan update :entities assoc bullet-id bullet)))
|
(s/change state-chan update :entities assoc bullet-id bullet)))
|
||||||
|
|||||||
@@ -13,5 +13,4 @@
|
|||||||
(defn system [state]
|
(defn system [state]
|
||||||
(let [entities (:entities state)
|
(let [entities (:entities state)
|
||||||
new-entities (update-vals entities velocity-move)]
|
new-entities (update-vals entities velocity-move)]
|
||||||
(println "Applying movement system: " state)
|
|
||||||
(assoc state :entities new-entities)))
|
(assoc state :entities new-entities)))
|
||||||
|
|||||||
Reference in New Issue
Block a user