Files
gitea-docker/desktop/src-common/advent/screens/rooms.clj

34 lines
1.9 KiB
Clojure

(ns advent.screens.rooms
(:require [advent.zone :as zone]))
(defn make-entity [id entity]
(merge entity
{:mouse-in? (fn [entities x y]
(let [{entity-x :x entity-y :y width :width scale-x :scale-x scale-y :scale-y height :height region :object} (get-in entities [:room :entities id])
width (or width (.getRegionWidth region))
height (or height (.getRegionHeight region))]
((zone/box entity-x entity-y (+ entity-x (* width (or 1 scale-x))) (+ entity-y (* height (or scale-y 1)))) x y)))}
(when (or (:scripts entity) (:script entity))
{:get-script (fn [cursor [x y]]
(if (= :main cursor)
(:script entity)
(when-let [scripts (:scripts entity)]
(scripts (:value cursor)))))})))
(defn make [& {:keys [collision interactions entities] :as params}]
(let [interactions-as-list (for [[id spec] interactions]
(merge spec {:mouse-in? (fn [_ x y]
((apply zone/box (:box spec)) x y))
:get-script (fn [cursor [x y]]
(if (= :main cursor)
(:script spec)
(when-let [scripts (:scripts spec)]
(scripts (:value cursor)))))
:id id}))
entities (into {} (for [[id entity] entities]
[id (make-entity id entity)]))]
(merge params {:collision (advent.pathfind/map-from-resource collision)
:interactions interactions-as-list
:entities entities})))