From: Javier Sancho Date: Tue, 16 Sep 2014 13:37:49 +0000 (+0200) Subject: More declarative way of making games X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;h=4ccfda6a7ebdf665edbf32452c8f4cbc3d010dfd;p=gacela.git More declarative way of making games * src/examples/asteroids.scm: Declarative asteroids game * src/game.scm: Functions renamed to a more declarative way, erasing 'make-' --- diff --git a/src/examples/asteroids.scm b/src/examples/asteroids.scm index ff607f4..1915e45 100644 --- a/src/examples/asteroids.scm +++ b/src/examples/asteroids.scm @@ -15,18 +15,18 @@ ;;; along with this program. If not, see . -(define-module (gacela examples asteroids) - #:use-module (gacela game)) +(define-module (gacela examples asteroids)) +; #:use-module (gacela game)) -(define asteroids (make-game "Asteroids")) -(define player (make-entity)) -(add-component player (make-transform)) -(add-component player (make-mesh)) -(add-entity asteroids player) +(define asteroids (game "Asteroids")) +(define player (entity)) +(set! player (add-component player (transform))) +(set! player (add-component player (mesh))) +(set! asteroids (add-entity asteroids player)) (define asteroids - (make-game "Asteroids" - (make-entity - (make-transform) - (make-mesh)))) + (game "Asteroids" + (entity + (transform) + (mesh)))) diff --git a/src/game.scm b/src/game.scm index 5a8b7d3..c3aab9f 100644 --- a/src/game.scm +++ b/src/game.scm @@ -25,10 +25,11 @@ ;;; Working with entities -(define-record-type entity +(define-record-type entity-type (make-entity-record id components) entity? (id entity-id) + (game entity-game set-entity-game!) (components entity-components set-entity-components!)) (set-record-type-printer! entity @@ -37,19 +38,20 @@ (entity-id record) (entity-components record)))) -(define (make-entity . components) +(define (entity . components) (make-entity-record (gensym) + #f components)) -(export make-entity +(export entity entity? entity-id) ;;; Game Definition -(define-record-type game +(define-record-type game-type (make-game-record name entities) game? (name game-name set-game-name!) @@ -68,14 +70,14 @@ '() (game-entities record)))))) -(define (make-game name . entities) +(define (game name . entities) (make-game-record name (alist->vhash (map (lambda (e) (cons (entity-id e) e)) entities)))) -(export make-game +(export game game?)