+ (hash-set! clist type elist)))
+ (unregister-components entity (cdr components) clist)))))
+
+(define (component-names components)
+ (map (lambda (c) (car c)) components))
+
+(define (entity-component-names key entity-set)
+ (component-names
+ (hash-ref (car entity-set) key)))
+
+(define (entity-ref key entity-set)
+ (hash-get-handle (car entity-set) key))
+
+(define (new-entity . new-components)
+ (lambda (entity-set)
+ (let ((key (gensym))
+ (nc (normalize-components new-components)))
+ (hash-set! (car entity-set) key nc)
+ (register-components key (component-names nc) (cdr entity-set))
+ (values
+ entity-set
+ (cons key nc)))))
+
+(define (remove-entity key)
+ (lambda (entity-set)
+ (let ((clist (entity-component-names key entity-set))
+ (entity (entity-ref key entity-set)))
+ (hash-remove! (car entity-set) key)
+ (unregister-components key clist (cdr entity-set))
+ (values
+ entity-set
+ entity))))
+
+(define (set-entity key . new-components)
+ (lambda (entity-set)
+ (let* ((nc (normalize-components new-components))
+ (clist (entity-component-names key entity-set))
+ (nclist (component-names nc)))
+ (hash-set! (car entity-set) key nc)
+ (register-components key
+ (lset-difference eq? nclist clist)
+ (unregister-components key (lset-difference eq? clist nclist) (cdr entity-set)))
+ (values
+ entity-set
+ (cons key nc)))))
+
+(define (set-entity-components key . new-components)
+ (lambda (entity-set)
+ (let ((nc (normalize-components new-components))
+ (clist (alist-copy (hash-ref (car entity-set) key))))
+ (for-each
+ (lambda (c)
+ (set! clist (assoc-set! clist (car c) (cdr c))))
+ nc)
+ (hash-set! (car entity-set) key clist)
+ (register-components key (component-names nc) (cdr entity-set))
+ (values
+ entity-set
+ (cons key clist)))))
+
+(define (remove-entity-components key . old-components)
+ (lambda (entity-set)
+ (let ((clist (alist-copy (hash-ref (car entity-set) key))))
+ (for-each
+ (lambda (c)
+ (set! clist (assoc-remove! clist c)))
+ old-components)
+ (hash-set! (car entity-set) key clist)
+ (unregister-components key old-components (cdr entity-set))
+ (values
+ entity-set
+ (cons key clist)))))
+
+(define (modify-entities entity-set changes)
+ (cond ((null? changes)
+ entity-set)
+ (else
+ (modify-entities ((car changes) entity-set) (cdr changes)))))
+
+(export make-entity-set
+ entity-list
+ entity-count
+ new-entity
+ remove-entity
+ set-entity
+ set-entity-components
+ remove-entity-components
+ modify-entities)
+
+
+;;; Making systems
+
+(define-record-type entities-changes-type
+ (entities-changes changes)
+ entities-changes?
+ (changes get-entities-changes))
+
+(define (append-changes changes)
+ (entities-changes
+ (apply append
+ (map get-entities-changes changes))))
+
+(define (find-entities-by-components entity-set clist)
+ (cond ((null? clist) '())
+ (else
+ (let ((e (hash-ref (cdr entity-set) (car clist) '()))
+ (e* (find-entities-by-components entity-set (cdr clist))))
+ (if (null? e*)
+ e
+ (lset-intersection eq? e e*))))))
+
+(define-syntax make-system
+ (syntax-rules ()
+ ((_ ((name (component-type ...)) ...) form ...)
+ (lambda (entity-set)
+ (let ((name (map (lambda (x)
+ (cons (car x)
+ (filter (lambda (x)
+ (memq (car x) '(component-type ...)))
+ (cdr x))))
+ (map (lambda (x)
+ (entity-ref x entity-set))
+ (find-entities-by-components entity-set '(component-type ...)))))
+ ...)
+ form
+ ...)))))
+
+(define-syntax define-system
+ (syntax-rules ()
+ ((_ system-name ((name (component-type ...)) ...) form ...)
+ (define system-name
+ (make-system ((name (component-type ...)) ...)
+ form
+ ...)))))
+
+(define (composed-systems-result results)
+ (let ((changes (filter (lambda (r) (entities-changes? r)) results)))
+ (cond ((null? changes)
+ (car results))
+ (else
+ (append-changes changes)))))
+
+(define (join-systems . systems)
+ (lambda (entity-set)
+ (let run ((s systems) (res '()))
+ (cond ((null? s)
+ (composed-systems-result res))
+ (else
+ (run (cdr s) (cons ((car s) entity-set) res)))))))
+
+(define (thread-systems . systems)
+ (lambda (entity-set)
+ (let run-wait ((thd
+ (map (lambda (s)
+ (call-with-new-thread
+ (lambda () (s entity-set))))
+ systems))
+ (res '()))
+ (cond ((null? thd)
+ (composed-systems-result res))
+ (else
+ (run-wait (cdr thd) (cons (join-thread (car thd)) res)))))))
+
+(export entities-changes
+ entities-changes?
+ get-entities-changes
+ find-entities-by-components
+ define-system
+ make-system
+ join-systems
+ thread-systems)
+
+
+;;; Entities and components access inside systems