This commit is contained in:
2026-07-20 23:17:41 -06:00
parent 96ed142f14
commit 0e24e171da
7 changed files with 48 additions and 21 deletions

View File

@@ -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 []))

View File

@@ -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]

View File

@@ -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)))

View 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})))

View File

@@ -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)

View File

@@ -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)))

View File

@@ -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)))