(define-module (gacela system)
+ #:use-module (ice-9 receive)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9))
;;; Component definitions
-(define (make-symbol . args)
+(define (symbol-concatenate . args)
(string->symbol
(string-concatenate
(map (lambda (a) (if (symbol? a) (symbol->string a) a)) args))))
`(begin
(use-modules (srfi srfi-9) (srfi srfi-9 gnu))
(define-record-type ,name
- (,(make-symbol "make-" name) ,@args)
- ,(make-symbol name "?")
- ,@(map (lambda (a) (list a (make-symbol name "-" a) (make-symbol "set-" name "-" a "!"))) args))
+ (,(symbol-concatenate "make-" name) ,@args)
+ ,(symbol-concatenate name "?")
+ ,@(map (lambda (a) (list a (symbol-concatenate name "-" a) (symbol-concatenate "set-" name "-" a "!"))) args))
(set-record-type-printer! ,name
(lambda (record port)
(format port "#<[~a]" ',name)
- ,@(map (lambda (a) `(format port " ~a: ~a" ',a (,(make-symbol name "-" a) record))) args)
+ ,@(map (lambda (a) `(format port " ~a: ~a" ',a (,(symbol-concatenate name "-" a) record))) args)
(format port ">")))
',name))
+(define (export-component component)
+ (let ((name (record-type-name component))
+ (m (current-module)))
+ (module-export! m (list
+ (symbol-concatenate "make-" name)
+ (symbol-concatenate name "?")))
+ (for-each
+ (lambda (a)
+ (module-export! (current-module)
+ (list
+ (symbol-concatenate name "-" a)
+ (symbol-concatenate "set-" name "-" a "!"))))
+ (record-type-fields component))))
+
(define (get-component-type component)
(record-type-name (record-type-descriptor component)))
(export define-component
+ export-component
get-component-type)
+;;; Entities and components
+
+(define (register-components entity components clist)
+ (cond ((null? components) clist)
+ (else
+ (let* ((type (car components))
+ (elist (assoc-ref clist type)))
+ (register-components entity (cdr components)
+ (assoc-set! clist type
+ (cond (elist
+ (lset-adjoin eq? elist entity))
+ (else
+ (list entity)))))))))
+
+(define (unregister-components entity components clist)
+ (cond ((null? components) clist)
+ (else
+ (let* ((type (car components))
+ (elist (lset-difference eq? (assoc-ref clist type) (list entity))))
+ (unregister-components entity (cdr components)
+ (cond ((null? elist)
+ (assoc-remove! clist type))
+ (else
+ (assoc-set! clist type elist))))))))
+
+(define (new-entity new-components entities components)
+ (let ((key (gensym)))
+ (values
+ (acons key new-components entities)
+ (register-components key
+ (map (lambda (c) (car c)) new-components)
+ components)
+ key)))
+
+(define (remove-entity key entities components)
+ (let ((clist (map (lambda (c) (car c)) (assoc-ref entities key))))
+ (values
+ (assoc-remove! entities key)
+ (unregister-components key clist components))))
+
+(define (set-entity key new-components entities components)
+ (let ((clist (map (lambda (c) (car c)) (assoc-ref entities key)))
+ (nclist (map (lambda (c) (car c)) new-components)))
+ (values
+ (assoc-set! entities key new-components)
+ (register-components key (lset-difference eq? nclist clist)
+ (unregister-components key (lset-difference eq? clist nclist) components)))))
+
+(define (set-entity-components key new-components entities components)
+ (define (set-components clist new-components)
+ (cond ((null? new-components)
+ clist)
+ (else
+ (set-components
+ (if (cdar new-components)
+ (assoc-set! clist (caar new-components) (cdar new-components))
+ (assoc-remove! clist (caar new-components)))
+ (cdr new-components)))))
+ (set-entity key (set-components (alist-copy (assoc-ref entities key)) new-components) entities components))
+
+(define (set-entities new-entities entities components)
+ (cond ((null? new-entities)
+ (values entities components))
+ (else
+ (cond ((not (caar new-entities))
+ (receive (e c k) (new-entity (cdar new-entities) entities components)
+ (set-entities (cdr new-entities) e c)))
+ ((not (cdar new-entities))
+ (receive (e c) (remove-entity (caar new-entities) entities components)
+ (set-entities (cdr new-entities) e c)))
+ (else
+ (receive (e c) (set-entity-components (caar new-entities) (cdar new-entities) entities components)
+ (set-entities (cdr new-entities) e c)))))))
+
+
+(export new-entity
+ remove-entity
+ set-entity
+ set-entity-components
+ set-entities)
+
+
;;; Making systems
(define* (find-entities-by-components c t)
(lambda (entities components)
(let* ((e (find-entities-by-components components component-types))
(e* (map (lambda (x) (assoc x entities)) e))
- (e** (map (lambda (x) (cons (car x) (filter (lambda (x) (memq (get-component-type x) component-types)) (cdr x)))) e*)))
- e**)))
+ (e** (map (lambda (x) (cons (car x) (filter (lambda (x) (memq (car x) component-types)) (cdr x)))) e*))
+ (res (system-fun e**)))
+ (lambda* (#:optional (entities2 #f) (components2 #f))
+ (let ((e (if (and entities2 components2) entities2 entities))
+ (c (if (and entities2 components2) components2 components)))
+ (set-entities res e c))))))
(export find-entities-by-components