--- /dev/null
+;;; 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))
(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
+(define (make-world . entities)
+ (apply make-entity-set entities))
+
(define entities-mutex (make-mutex))
(define game-entities '())
(define game-components '())
(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 (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
(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)
- (modify-entities
- (cons (make-hash-table) (make-hash-table))
- changes))
+ (modify-entities (bongodb:make-collection) changes))
(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)
- (hash-count (const #t) (car entity-set)))
+ (bongodb:count entity-set))
(define (normalize-components components)
(map
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 (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)
(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
(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
(define (get-component component-name entity)
(assoc-ref (cdr entity) component-name))
-
-(export get-key
- get-component)
--- /dev/null
+;;; 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))))
;;; 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
;;; 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")
-(define-component a x y)
-(define-component b)
-
-(define entities (make-entity-set))
+(define entity-set (make-entity-set))
+(define key #f)
; 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")