Make the 'get-components' proceedure a little more intuitive

This commit is contained in:
TakeV 2024-05-22 01:36:57 -04:00
parent 45c4dcecd5
commit 12205afa02
Signed by: TakeV
GPG key ID: A64F41345C7400AF

View file

@ -38,24 +38,17 @@
false otherwise."
(hashtable-ref (game-entity-components entity) component-name #f))
;; Returns a vector of components matching the provided critera
;; If only the entity is provided, return all the entities.
;; If a list or vector is provided, returns a vector with
;; the component which matches that component-name
;; (get-components renderable-entity ['position 'sprite])
;; would return [POSITION-COMPONENT SPRITE-COMPONENT]
(define get-components
(case-lambda
((entity) (hashtable-entries entity))
((entity names)
(let ((component-search
(lambda (component-name)
(get-component entity component-name))))
(match names
((? list?)
(list->vector (map component-search names)))
((? vector?)
(list->vector (vector-map component-search names))))))))
(define (get-components entity name . names)
"Returns a list of components matching the provided critera
(get-components renderable-entity 'position 'sprite)
would return '(POSITION-COMPONENT SPRITE-COMPONENT)
The order is preserved. If a component with that name
does not exist, false is returned in place of the component. "
(append
(map (lambda (n)
(get-component entity n))
names)
`(,name)))
(define (set-component! entity component-name component)
"Replaces the component in entity with component-name.