Event buses (as hash tables for speed)
This commit is contained in:
parent
04a6555cbd
commit
f1d6342089
2 changed files with 71 additions and 2 deletions
|
|
@ -35,13 +35,13 @@ Like entities, the addition of systems is queued for the beginning of each frame
|
|||
Systems can access resources via free variables or by being formed as closures. Systems only ever take an entity (a component list) as input.
|
||||
|
||||
** Event Buses
|
||||
An event bus is an association list of symbols and records. They allow for delegating changes sent from other objects.
|
||||
An event bus is a hash table of symbols and records. They allow for delegating changes sent from other objects.
|
||||
The event record can contain any arbitrary data. The symbol the record is associated by is unique within the association list.
|
||||
Event records stay in the bus until removed by another system.
|
||||
Systems can push and pull events to and from the bus.
|
||||
Systems can choose to either pop the event off the bus (removing it from further processing) or just peek at the event.
|
||||
The symbol an event is associated by is essentially an "action". The event bus can have multiple events of the same type (for example, multiple different key presses can occur in a frame). Only one event of each name (for example, only 1 event can be called "jump", which might correspond to a space key press) is allowed in the bus at any one time.
|
||||
Custom event buses can be created and stored in the ~event-buses~ association list. System can query a particular event bus within that list for a particular event.
|
||||
Custom event buses can be created and stored in the ~event-buses~ hash table. Systems can query a particular event bus within that list for a particular event.
|
||||
|
||||
** Resources
|
||||
When a resource, such as a font or texture, is loaded from the filesystem, the pointer is stored in an SRFI-69 hash table ~resources~ keyed by the path.
|
||||
|
|
|
|||
|
|
@ -220,4 +220,73 @@
|
|||
(assert (symbol? name))
|
||||
(queue-del-system name)
|
||||
name)
|
||||
|
||||
;; System execution
|
||||
;; TODO: Implement
|
||||
|
||||
;; Event buses hash table
|
||||
(define event-buses (make-hash-table))
|
||||
|
||||
;; Event bus interface
|
||||
(export register-event-bus remove-event-bus fetch-event-bus
|
||||
push-event peek-event pop-event)
|
||||
|
||||
;; Register a new event bus
|
||||
(define (register-event-bus name)
|
||||
(assert (symbol? name))
|
||||
(if (hash-table-exists? event-buses name)
|
||||
#f
|
||||
(begin
|
||||
(hash-table-set! event-buses name (make-hash-table))
|
||||
name)))
|
||||
|
||||
;; Remove an event bus
|
||||
(define (remove-event-bus name)
|
||||
(assert (symbol? name))
|
||||
(if (hash-table-exists? event-buses name)
|
||||
(begin
|
||||
(hash-table-delete! event-buses name)
|
||||
name)
|
||||
#f))
|
||||
|
||||
;; Fetch an event bus by name, or #f if it doesn't exist
|
||||
(define (fetch-event-bus name)
|
||||
(assert (symbol? name))
|
||||
(if (hash-table-exists? event-buses name)
|
||||
(hash-table-ref event-buses name)
|
||||
#f))
|
||||
|
||||
;; Push an event to the specified bus, return #f on failure (if the bus doesn't exist
|
||||
(define (push-event bus action event)
|
||||
(assert (symbol? bus))
|
||||
(assert (symbol? action))
|
||||
(assert (record? event))
|
||||
(let ((event-bus (fetch-event-bus bus)))
|
||||
(if event-bus
|
||||
(begin
|
||||
(hash-table-set! event-bus action event)
|
||||
#t)
|
||||
#f)))
|
||||
|
||||
;; Retrieve an event from the event bus, if it exists. Return false if it doesn't
|
||||
(define (peek-event bus action)
|
||||
(assert (symbol? bus))
|
||||
(assert (symbol? action))
|
||||
(let ((event-bus (fetch-event-bus bus)))
|
||||
(if (and event-bus
|
||||
(hash-table-exists? event-bus action))
|
||||
(hash-table-ref event-bus action)
|
||||
#f)))
|
||||
|
||||
;; Retrieve an event from the event bus, if it exists, then delete it. Return false if it doesn't exist.
|
||||
(define (pop-event bus action)
|
||||
(assert (symbol? bus))
|
||||
(assert (symbol? action))
|
||||
(let ((event-bus (fetch-event-bus bus)))
|
||||
(if (and event-bus
|
||||
(hash-table-exists? event-bus action))
|
||||
(let ((event (hash-table-ref event-bus action)))
|
||||
(hash-table-delete! event-bus action)
|
||||
event)
|
||||
#f)))
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue