From 234f1343adb9f9f98f9f8e9a3cda04b5790834eb Mon Sep 17 00:00:00 2001 From: TakeV Date: Fri, 24 May 2024 17:37:55 -0400 Subject: [PATCH] Add game-objects.scm --- Makefile | 3 ++- game.scm | 3 ++- modules/ces/component/game-object.scm | 17 ------------ modules/game/game-objects.scm | 38 +++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 19 deletions(-) delete mode 100644 modules/ces/component/game-object.scm create mode 100644 modules/game/game-objects.scm diff --git a/Makefile b/Makefile index 8631994..6b010c8 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ modules := \ $(wildcard modules/ces/system/*.scm) \ $(wildcard modules/dom/*.scm) \ $(wildcard modules/math/*.scm) \ - $(wildcard modules/lib/*.scm) + $(wildcard modules/lib/*.scm) \ + $(wildcard modules/game/*.scm) game.wasm: game.scm $(modules) guild compile-wasm -L modules -o $@ $< diff --git a/game.scm b/game.scm index b8eea8c..0862ebd 100644 --- a/game.scm +++ b/game.scm @@ -42,7 +42,8 @@ (ces system tilemap-renderer) (ces system-manager) (ces entity-manager) - (game-core)) + (game-core) + (game game-objects)) (log-to-console! "Getting canvas") (define canvas (get-element-by-id "canvas")) diff --git a/modules/ces/component/game-object.scm b/modules/ces/component/game-object.scm deleted file mode 100644 index 6fae178..0000000 --- a/modules/ces/component/game-object.scm +++ /dev/null @@ -1,17 +0,0 @@ -(define-module (ces component game-object) - #:pure - #:use-module (scheme base) - #:export (game-object? - make-game-object - game-object-entity-id - game-object-sprite - game-object-pos-x - game-object-pos-y)) - -(define-record-type - (make-game-object entity-id sprite pos-x pos-y) - game-object? - (entity-id game-object-entity-id) - (sprite game-object-sprite) - (pos-x game-object-pos-x) - (pos-y game-object-pos-y)) diff --git a/modules/game/game-objects.scm b/modules/game/game-objects.scm new file mode 100644 index 0000000..e6cb0c9 --- /dev/null +++ b/modules/game/game-objects.scm @@ -0,0 +1,38 @@ +(define-module (game game-objects) + #:pure + #:use-module (scheme base) + #:use-module (ces component) + #:use-module (ces component hitbox) + #:use-module (ces component position) + #:use-module (ces component sprite) + #:use-module (ces entity) + #:use-module (ces entity-manager) + #:use-module (game-core) + #:export (add-game-object-components + game-object? + create-game-object!)) + +(define (add-position entity entity-id x y) + (set-component entity 'position (create-position entity-id x y))) + +(define (add-hitbox entity entity-id x y w h) + (set-component entity 'hitbox (make-hitbox entity-id x y w h))) + +(define (add-sprite entity entity-id sprite w h) + (set-component entity 'sprite (make-sprite entity-id sprite w h))) + +(define (add-game-object-components entity x y width height sprite) + (let* ((entity-id (game-entity-id entity)) + (e-pos (add-position entity entity-id x y)) + (e-hit (add-hitbox entity entity-id x y width height)) + (e-sprite (add-sprite entity entity-id sprite width height))) + e-sprite)) + +(define (game-object? entity) + (has-components? entity 'position 'hitbox 'sprite)) + +(define (create-game-object! x y width height sprite) + (let* ((e-manager (get-entity-manager)) + (entity (create-entity! e-manager))) + (reset-entity! e-manager + (add-game-object-components entity x y width height sprite))))