This commit is contained in:
2026-07-17 22:40:15 -06:00
parent d75a2fe251
commit 96ed142f14
5 changed files with 37 additions and 18 deletions

View File

@@ -35,7 +35,6 @@
(js/window.addEventListener "keyup" (partial handle-key state (fn [_] 0))))
(defn- drain-outbox! [{:keys [outbox] :as state} tx-chan]
(println "Draining outbox: " outbox)
(when (count outbox)
(go
(doseq [msg outbox]
@@ -104,7 +103,6 @@
:ui-layer ui-layer}
websocket (doto (js/WebSocket. (str "ws://" js/location.host "/ws"))
(.addEventListener "open" #(go-loop []
(js/console.log %)
(when-let [msg (<! tx-chan)]
(.send (.-target %) msg)
(recur))))

View File

@@ -1,7 +1,8 @@
(ns frontend.il.update
(:require
[frontend.il.entity :as entity]
[cljs.core.async :as async]))
[cljs.core.async :as async]
[goog.dom :as dom]))
(defn apply-velocity [e dt]
(if (contains? e :velocity)
@@ -58,16 +59,17 @@
(apply-player-position send-interval)
(apply-player-shoot mouse-pos))))
(defn- deep-merge [a b]
(if (and (map? a) (map? b))
(merge-with deep-merge a b)
b))
(defn update-from-server [state rx-chan]
(loop [state-acc state
{:keys [kind] :as msg} (async/poll! rx-chan)]
(if (and msg (= kind "sync"))
{:keys [type] :as msg} (async/poll! rx-chan)]
(if (and (not (nil? msg)) (= type "sync"))
(let [entities (msg :entities)
tx-entities (update-vals entities (fn [e] {:sprite (:sprite e)
:x (:x e)
:y (:y e)
:tx (:x e)
:ty (:y e)}))]
(recur
(deep-merge state-acc msg)
(async/poll! rx-chan))
(update-in state-acc [:entities] merge tx-entities)
(async/poll! rx-chan)))
state-acc)))

View File

@@ -7,7 +7,8 @@
[hiccup.core :as h]
[il.state :as s]
[il.json :as j]
[il.handler :as handler]))
[il.handler :as handler]
[il.system.movement :as movement]))
(defn ws-handler [state state-chan player-id]
(go-websocket [in out err]
@@ -59,7 +60,9 @@
(if-let [args (async/poll! state-chan)]
(recur (conj inputs args) (inc count))
inputs)))]
(let [event-state (reduce (fn [state event-args] ((event-args 0) state (rest event-args)))
(let [event-state (reduce (fn [state event-args]
(println event-args)
(apply (first event-args) state (vec (rest event-args))))
@state
events-to-apply)
system-state (reduce (fn [state system] (system state))
@@ -67,7 +70,7 @@
(event-state :systems))
entities (:entities @state)
players (:players @state)]
(swap! state merge system-state)
(swap! state conj system-state)
(doseq [{out :out} (vals players)]
(>! out (j/to-json {:type "sync"
:entities entities}))))
@@ -77,7 +80,7 @@
(recur))))
(defn -main []
(let [state (atom {:systems []
(let [state (atom {:systems [movement/system]
:entities {}
:players {}})
state-chan (async/chan)

View File

@@ -2,5 +2,4 @@
(:require [clojure.core.async :refer [>! go]]))
(defn change [state-chan & args]
(println "Appending to state-chan: " args)
(go (>! state-chan args)))

View File

@@ -0,0 +1,17 @@
(ns il.system.movement)
(defn- velocity-move [e]
(let [{x :x
y :y
vel :velocity} e]
(if-not (nil? vel)
(let [vx (get vel :x 0)
vy (get vel :y 0)]
(assoc e :x (+ vx x) :y (+ vy y)))
e)))
(defn system [state]
(let [entities (:entities state)
new-entities (update-vals entities velocity-move)]
(println "Applying movement system: " state)
(assoc state :entities new-entities)))