This repository has been archived by the owner on Jun 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial thoughts on component-entity system
- Loading branch information
Showing
4 changed files
with
82 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
(ns enoki.component-macros) | ||
|
||
(defmacro defcomponent | ||
"Define a component-creation function, named by `type`, that accepts `params` | ||
and returns a map consisting of the remaining key-value pairs. E.g. | ||
(defcomponent foo [bar baz] | ||
:bar bar | ||
:baz (inc baz)) | ||
(foo :quux 2) ; => {:enoki.component/type :foo, :bar :quux, :baz 3}" | ||
[type params & kvs] | ||
`(defn ~type ~(vec params) | ||
(into {:enoki.component/type ~(keyword type)} | ||
(hash-map ~@kvs)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
;; Functions for initialising and managing entities. | ||
;; Entities are plain mappings of component types (keywords) to components (which | ||
;; are also maps). | ||
|
||
(ns enoki.entity) | ||
|
||
(let [next-id (atom 0)] | ||
(defn uid | ||
"Reserve and return a unique numeric ID." | ||
[] | ||
(swap! next-id inc))) | ||
|
||
(defn new | ||
"Initialise an entity with some collection of components. A unique ID is | ||
assigned and accessible via `:id`. Components are accessible by their | ||
name as a keyword; i.e. use `:foo` to fetch components defined using | ||
`(defcomponent foo ...)`." | ||
[& components] | ||
(into {:id (uid)} | ||
(map (fn [comp] [(:enoki.component/type comp) comp]) components))) | ||
|
||
(defn with-component | ||
"Find all entities that include a component of a given type." | ||
[entities component-type] | ||
(filter #(contains? % component-type) entities)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
;; Game-specific components | ||
|
||
^:clj (ns game.component | ||
(:use [enoki.component-macros :only [defcomponent]])) | ||
|
||
^:cljs (ns game.component | ||
(:use-macros [enoki.component-macros :only [defcomponent]])) | ||
|
||
(defcomponent position [x y] | ||
:x x | ||
:y y) | ||
|
||
(defcomponent sprite [image-id] | ||
:image-id image-id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters