Skip to content

Instantly share code, notes, and snippets.

@Ionshard
Created November 3, 2017 12:01
Show Gist options
  • Select an option

  • Save Ionshard/226ecf18b69f782b7f03c1723247156c to your computer and use it in GitHub Desktop.

Select an option

Save Ionshard/226ecf18b69f782b7f03c1723247156c to your computer and use it in GitHub Desktop.
Attempted Re-frame wrapper around Brute
(ns magitek-clicker.re-brute
"A Re-frame wrapper around the brute Entity Component Systems manager"
(:require [brute.entity :as e]
[re-frame.core :as rf]))
(def db-key ::brute)
;;Events
(defn create-system
[db _]
(assoc db db-key (e/create-system)))
(rf/reg-event-db ::create-system create-system)
;;Subs
(defn system
([db] (system db nil))
([db _]
(get db db-key)))
(rf/reg-sub ::system system)
(rf/reg-sub
::get-component
:<- [::system]
(fn [system [_ entity type]]
(e/get-component system entity type)))
(rf/reg-sub
::get-all-components-on-entity
:<- [::system]
(fn [system [_ entity]]
(e/get-all-components-on-entity system entity)))
(rf/reg-sub
::get-all-entities
:<- [::system]
(fn [system _]
(e/get-all-entities system)))
(rf/reg-sub
::get-all-entities-with-component
:<- [::system]
(fn [system [_ type]]
(e/get-all-entities-with-component system type)))
;; Effects
(defn add-entity
[system id]
(e/add-entity system (or id (e/create-entity))))
(defn system-apply
[system fn args]
(apply fn system args))
(defn- brute-effects
[system context]
(let [add-entity-fx (rf/get-effect context ::add-entity)
kill-entity-fx (rf/get-effect context ::kill-entity)
add-component-fx (rf/get-effect context ::add-component)
update-component-fx (rf/get-effect context ::update-component)
remove-component-fx (rf/get-effect context ::remove-component)]
(cond-> system
add-entity-fx (system-apply add-entity add-entity-fx)
kill-entity-fx (system-apply e/kill-entity kill-entity-fx)
add-component-fx (system-apply e/add-component add-component-fx)
update-component-fx (system-apply e/update-component update-component-fx)
remove-component-fx (system-apply e/remove-component remove-component-fx))))
(def brute
(rf/->interceptor
:id ::brute
:after (fn [context]
(let [db (or (rf/get-effect context :db)
(rf/get-coeffect context :db))
system (brute-effects (get db db-key) context)
new-db (assoc db db-key system)]
(-> context
(rf/assoc-effect :db new-db)
(update :effects dissoc ::add-entity ::kill-entity ::add-component ::update-component ::remove-component))))))
(rf/reg-event-fx
::add-entity
[brute]
(fn [_ [_ id]]
{::add-entity [id]}))
(rf/reg-event-fx
::kill-entity
[brute]
(fn [_ [_ id]]
{::kill-entity [id]}))
(rf/reg-event-fx
::add-component
[brute]
(fn [_ [_ entity instance]]
{::add-component [entity instance]}))
(rf/reg-event-fx
::update-component
[brute]
(fn [_ [_ entity type fn & args]]
{::update-component (into [entity type fn] args)}))
(rf/reg-event-fx
::remove-component
[brute]
(fn [_ [_ entity instance]]
{::remove-component [entity instance]}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment