Add game-objects.scm

This commit is contained in:
TakeV 2024-05-24 17:37:55 -04:00
parent dd5f0ef6f3
commit 234f1343ad
Signed by: TakeV
GPG key ID: A64F41345C7400AF
4 changed files with 42 additions and 19 deletions

View file

@ -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 $@ $<

View file

@ -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"))

View file

@ -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 <game-object>
(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))

View file

@ -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))))