+(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 (map (lambda (c) `(,(get-component-type c) . ,c)) new-components) entities)
+ (register-components
+ key
+ (map (lambda (c) (get-component-type 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))))
+
+(export new-entity
+ remove-entity)