From a365afd7e2a002c533b0463f0778f9c4918a1eb4 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Thu, 27 Jun 2013 11:28:13 +0200 Subject: [PATCH] Making systems; preprocessing entities and components data for game systems * src/system.scm: find-entities-by-components make-system --- src/system.scm | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/system.scm b/src/system.scm index b4f4870..4da0b1c 100644 --- a/src/system.scm +++ b/src/system.scm @@ -16,6 +16,7 @@ (define-module (gacela system) + #:use-module (srfi srfi-1) #:use-module (srfi srfi-9)) @@ -45,3 +46,27 @@ (export define-component get-component-type) + + +;;; Making systems + +(define* (find-entities-by-components c t) + (cond ((null? t) '()) + (else + (let* ((e (assoc-ref c (car t))) + (e* (if e e '()))) + (cond ((null? (cdr t)) e*) + (else + (lset-intersection eq? e* (find-entities-by-components c (cdr t))))))))) + + +(define (make-system component-types system-fun) + (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**))) + + +(export find-entities-by-components + make-system) -- 2.39.5