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

View File

@@ -1,7 +1,8 @@
(ns frontend.il.update (ns frontend.il.update
(: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]))
(defn apply-velocity [e dt] (defn apply-velocity [e dt]
(if (contains? e :velocity) (if (contains? e :velocity)
@@ -58,16 +59,17 @@
(apply-player-position send-interval) (apply-player-position send-interval)
(apply-player-shoot mouse-pos)))) (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] (defn update-from-server [state rx-chan]
(loop [state-acc state (loop [state-acc state
{:keys [kind] :as msg} (async/poll! rx-chan)] {:keys [type] :as msg} (async/poll! rx-chan)]
(if (and msg (= kind "sync")) (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 (recur
(deep-merge state-acc msg) (update-in state-acc [:entities] merge tx-entities)
(async/poll! rx-chan)) (async/poll! rx-chan)))
state-acc))) state-acc)))

View File

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

View File

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