- (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)
+ (elist (lset-difference eq? (hash-ref clist type) (list entity))))
+ (cond ((null? elist)
+ (hash-remove! clist type))
+ (else
+ (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)