Add basic game manager

This commit is contained in:
nephryte 2024-05-25 02:53:46 -04:00
parent 942570a6bb
commit 61769a8165
4 changed files with 76 additions and 4 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 B

View file

@ -0,0 +1,22 @@
(define-module (game game-manager)
#:pure
#:use-module (scheme base)
#:use-module (ces system html-canvas-renderer)
#:use-module (game level-manager)
#:export (init-game-entities
init-game-systems))
(define canvas (get-element-by-id "canvas"))
(define context (get-context canvas "2d"))
(define room-background (make-image "assets/backgrounds/background.png"))
(define (init-game-entities)
"Returns a list of initial game entities. "
(list (create-player!)
(create-room! room-background 0 0 128 64)))
(define (init-game-systems)
"Returns a list of initial game systems.
Note: Systems are executed sequentially. "
(list (create-html-canvas-renderer context)))

View file

@ -8,6 +8,7 @@
#:use-module (ces entity) #:use-module (ces entity)
#:use-module (ces entity-manager) #:use-module (ces entity-manager)
#:use-module (game-core) #:use-module (game-core)
#:use-module (dom image)
#:export (add-game-object-components #:export (add-game-object-components
game-object? game-object?
create-game-object!)) create-game-object!))
@ -23,10 +24,10 @@
(define (add-game-object-components entity x y width height sprite) (define (add-game-object-components entity x y width height sprite)
(let* ((entity-id (game-entity-id entity)) (let* ((entity-id (game-entity-id entity))
(e-pos (add-position entity entity-id x y)) (entity (add-position entity entity-id x y))
(e-hit (add-hitbox entity entity-id x y width height)) (entity (add-hitbox entity entity-id x y width height))
(e-sprite (add-sprite entity entity-id sprite width height))) (entity (add-sprite entity entity-id sprite width height)))
e-sprite)) entity))
(define (game-object? entity) (define (game-object? entity)
(has-components? entity 'position 'hitbox 'sprite)) (has-components? entity 'position 'hitbox 'sprite))

View file

@ -0,0 +1,49 @@
(define-module (game level-manager)
#: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)
#:use-module (game game-objects)
#:export ())
;; Player
(define (player? entity)
(and (game-object? entity)
(has-components? entity 'keyboard-input)))
(define player-sprite (make-image "assets/sprites/player.png"))
(define (create-player!)
(let ((initial-entity (create-game-object! 0 0 16 16 player-sprite)))
(add-component initial-entity 'keyboard-input
(make-inputs '() '() '()))))
;; Room
(define (room? entity)
(has-components? entity 'hitbox 'sprite)) ;; + 'room-objects ?
;; As it stands a game object is technically also a room.
;; This will not be the case in the future -- especially once tilemaps work.
(define (add-background entity entity-id image width height)
(set-component entity 'sprite (make-sprite entity-id image width height)))
(define (add-bounding-box entity entity-id x y width height)
(set-component entity 'hitbox (make-hitbox entity-id x y width height)))
(define (add-room-components entity background x y width height)
(let* ((room-entity-id (game-entity-id entity))
(entity (add-background entity room-entity-id sprite width height))
(entity (add-bounding-box entity room-entity-id x y width height)))))
(define (create-room! background x y width height)
(let* ((e-manager (get-entity-manager))
(entity (create-entity! e-manager)))
(reset-entity! e-manager
(add-room-components entity background x y width height))))
;; Floor
;; A floor is a list of rooms