]> git.jsancho.org Git - gacela.git/commitdiff
Trash
authorJavier Sancho <jsf@jsancho.org>
Thu, 23 Jun 2016 00:03:28 +0000 (02:03 +0200)
committerJavier Sancho <jsf@jsancho.org>
Thu, 23 Jun 2016 00:03:28 +0000 (02:03 +0200)
src/entity.scm [new file with mode: 0644]
src/gacela.scm
src/system.scm
src/utils.scm [new file with mode: 0644]
tests/entities.scm

diff --git a/src/entity.scm b/src/entity.scm
new file mode 100644 (file)
index 0000000..9f435dd
--- /dev/null
@@ -0,0 +1,36 @@
+;;; Gacela, a GNU Guile extension for fast games development
+;;; Copyright (C) 2013 by Javier Sancho Fernandez <jsf at jsancho dot org>
+;;;
+;;; This program is free software: you can redistribute it and/or modify
+;;; it under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation, either version 3 of the License, or
+;;; (at your option) any later version.
+;;;
+;;; This program is distributed in the hope that it will be useful,
+;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (gacela entity)
+  #:use-module (bongodb)
+  #:export (make-entity-set
+           add-entities
+           entities-count))
+
+(define (make-entity-set . entities)
+  (add-entities (make-collection) entities))
+
+(define (add-entities entity-set . entities)
+  (let ((entities (filter
+                  (lambda (e) (not (null? e)))
+                  entities)))
+    (cond (entities
+          (apply insert (cons entity-set entities)))
+         (else
+          entity-set))))
+
+(define (entities-count entity-set)
+  (count entity-set))
index 7fb242fdb94561afce498a328e8629105bed7e03..3b65aff208d01470b6954ef2166c9096e8b11649 100644 (file)
 (define-module (gacela gacela)
   #:use-module (gacela system)
   #:use-module (ice-9 threads)
 (define-module (gacela gacela)
   #:use-module (gacela system)
   #:use-module (ice-9 threads)
-  #:use-module (srfi srfi-1))
+  #:use-module (srfi srfi-1)
+  #:export (make-world))
 
 
 ;;; Entities and components
 
 
 
 ;;; Entities and components
 
+(define (make-world . entities)
+  (apply make-entity-set entities))
+
 (define entities-mutex (make-mutex))
 (define game-entities '())
 (define game-components '())
 (define entities-mutex (make-mutex))
 (define game-entities '())
 (define game-components '())
index 1df24f9ba03d711b4ba6f7097e83aa273aded760..50e9534b451f34c961150745e30bf49f433133ac 100644 (file)
 
 
 (define-module (gacela system)
 
 
 (define-module (gacela system)
+  #:use-module ((bongodb) #:renamer (symbol-prefix-proc 'bongodb:))
   #:use-module (ice-9 receive)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-9)
   #:use-module (ice-9 receive)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-9)
-  #:use-module (srfi srfi-9 gnu))
+  #:use-module (srfi srfi-9 gnu)
+  #:export (define-component
+           export-component
+           get-component-type
+           make-entity-set
+           entity-list
+           entity-count
+           new-entity
+           get-entity
+           remove-entity
+           set-entity
+           set-entity-components
+           remove-entity-components
+           modify-entities
+           entities-changes
+           entities-changes?
+           get-entities-changes
+           find-entities-by-components
+           define-system
+           make-system
+           join-systems
+           thread-systems
+           get-key
+           get-component))
 
 
 ;;; Component definitions
 
 
 ;;; Component definitions
 (define (get-component-type component)
   (record-type-name (record-type-descriptor 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 (make-entity-set . changes)
 
 ;;; Entities and components
 
 (define (make-entity-set . changes)
-  (modify-entities
-   (cons (make-hash-table) (make-hash-table))
-   changes))
+  (modify-entities (bongodb:make-collection) changes))
 
 (define (entity-list entity-set)
 
 (define (entity-list entity-set)
-  (hash-map->list (lambda (k v) (cons k v)) (car entity-set)))
+  (bongodb:find entity-set))
 
 (define (entity-count entity-set)
 
 (define (entity-count entity-set)
-  (hash-count (const #t) (car entity-set)))
+  (bongodb:count entity-set))
 
 (define (normalize-components components)
   (map
 
 (define (normalize-components components)
   (map
         c))
    components))
 
         c))
    components))
 
-(define (register-components entity components clist)
-  (cond ((null? components) clist)
-       (else
-        (let* ((type (car components))
-               (elist (hash-ref clist type)))
-          (hash-set! clist type
-            (cond (elist
-                   (lset-adjoin eq? elist entity))
-                  (else
-                   (list entity))))
-          (register-components entity (cdr components) clist)))))
-
-(define (unregister-components entity components clist)
-  (cond ((null? components) clist)
-       (else
-        (let* ((type (car components))
-               (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 (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 (new-entity entity-set . new-components)
+  (receive (new-set new-keys) (bongodb:insert entity-set (normalize-components new-components))
+    (values new-set (car new-keys))))
+
+(define (get-entity entity-set key)
+  (let ((entity (bongodb:find entity-set (bongodb:$eq '_id key))))
+    (and entity (car entity))))
 
 (define (remove-entity key)
   (lambda (entity-set)
 
 (define (remove-entity key)
   (lambda (entity-set)
        (else
         (modify-entities ((car changes) entity-set) (cdr changes)))))
 
        (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
 
 
 ;;; Making systems
 
            (else
             (run-wait (cdr thd) (cons (join-thread (car thd)) 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
 
 
 ;;; Entities and components access inside systems
 
 
 (define (get-component component-name entity)
   (assoc-ref (cdr entity) component-name))
 
 (define (get-component component-name entity)
   (assoc-ref (cdr entity) component-name))
-
-(export get-key
-       get-component)
diff --git a/src/utils.scm b/src/utils.scm
new file mode 100644 (file)
index 0000000..5f56f05
--- /dev/null
@@ -0,0 +1,33 @@
+;;; Gacela, a GNU Guile extension for fast games development
+;;; Copyright (C) 2016 by Javier Sancho Fernandez <jsf at jsancho dot org>
+;;;
+;;; This program is free software: you can redistribute it and/or modify
+;;; it under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation, either version 3 of the License, or
+;;; (at your option) any later version.
+;;;
+;;; This program is distributed in the hope that it will be useful,
+;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+(define-module (gacela utils)
+  #:export (make-producer))
+
+(define (make-producer body)
+  (define resume #f)
+  (lambda (real-send)
+    (define send-to real-send)
+    (define (send value-to-send)
+      (set! send-to
+       (call/cc
+        (lambda (k)
+          (set! resume k)
+          (send-to value-to-send)))))
+    (if resume
+       (resume real-send)
+       (body send))))
index c0a9a51d289562154b09eb0eea57dadd378e569f..2fd532aaef7a4dd851fd00b935ca75c6f29d1971 100644 (file)
@@ -1,5 +1,5 @@
 ;;; Gacela, a GNU Guile extension for fast games development
 ;;; Gacela, a GNU Guile extension for fast games development
-;;; Copyright (C) 2013 by Javier Sancho Fernandez <jsf at jsancho dot org>
+;;; Copyright (C) 2016 by Javier Sancho Fernandez <jsf at jsancho dot org>
 ;;;
 ;;; This program is free software: you can redistribute it and/or modify
 ;;; it under the terms of the GNU General Public License as published by
 ;;;
 ;;; This program is free software: you can redistribute it and/or modify
 ;;; it under the terms of the GNU General Public License as published by
 ;;; You should have received a copy of the GNU General Public License
 ;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; You should have received a copy of the GNU General Public License
 ;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-(use-modules (gacela system)
+(use-modules (gacela entity)
+            (ice-9 receive)
             (srfi srfi-64))
 
 (test-begin "entities")
 
             (srfi srfi-64))
 
 (test-begin "entities")
 
-(define-component a x y)
-(define-component b)
-
-(define entities (make-entity-set))
+(define entity-set (make-entity-set))
+(define key #f)
 
 ; Creating entities
 
 ; Creating entities
-(set! entities ((new-entity (make-a 1 2) (make-b)) entities))
-(set! entities ((new-entity (make-a 10 20)) entities))
-((new-entity (make-a 10 20)) entities)
-(test-eqv 2 (length (entity-list entities)))
+
+(receive (e k) (add-entities entity-set '((a . (1 2)) (b . #f)))
+  (set! entity-set e)
+  (set! key (car k)))
+(set! entity-set (add-entities entity-set '((a . (10 20)))))
+(add-entities entity-set '((a . (10 20))))
+(test-eqv 2 (entities-count entity-set))
+
+;; (define-component a x y)
+;; (define-component b)
+
+;; (define entities (make-entity-set))
+;; (define key #f)
+
+;; ; Modifying entities
+;; (define component (assoc-ref (get-entity entities key) 'a))
+;; (test-eqv 1 (a-x component))
+;; (test-eqv 2 (a-y component))
 
 (test-end "entities")
 
 (test-end "entities")