From 61769a8165c528ef248aabf50e00ba811638b095 Mon Sep 17 00:00:00 2001 From: nephryte Date: Sat, 25 May 2024 02:53:46 -0400 Subject: [PATCH] Add basic game manager --- assets/backgrounds/background.png | Bin 0 -> 454 bytes modules/game/game-manager.scm | 22 ++++++++++++++ modules/game/game-objects.scm | 9 +++--- modules/game/level-manager.scm | 49 ++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 assets/backgrounds/background.png create mode 100644 modules/game/game-manager.scm create mode 100644 modules/game/level-manager.scm diff --git a/assets/backgrounds/background.png b/assets/backgrounds/background.png new file mode 100644 index 0000000000000000000000000000000000000000..dbb9cd171fb72223120b8a736080649e6149223a GIT binary patch literal 454 zcmeAS@N?(olHy`uVBq!ia0vp^4M6O`!3-pya`gKGDaPU;cPEB*=VV?2IYj|JA+A6g z2>!24ZZ>52|NsB@BTK9Q8!$98q%N7w!0_*XYs(ZXt7kw3&ySv))7p})8EqK7G}|y` z>HqGYd%;0HOP4MU3JRJsWy-%PQzlRO_y7OW|3Fm8A8w zea7!*r1Z=PJNNs^U4F%KX6vcuvQEd$f`n(j6)-mA-OAs-+gQ_|XZN#o^U|8+6Zf0C zcJ@D-b9tqF*0Hpr^Bp(&RGT-}xBm1EJo_TyT+uV$RTC2BZ>0Wta>On{egl8TO)zzX o{SEUs#@`LK2e5Jf+-Cp8xOD2pt{X+HOrRL>boFyt=akR{00Ml~1poj5 literal 0 HcmV?d00001 diff --git a/modules/game/game-manager.scm b/modules/game/game-manager.scm new file mode 100644 index 0000000..53253e0 --- /dev/null +++ b/modules/game/game-manager.scm @@ -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))) \ No newline at end of file diff --git a/modules/game/game-objects.scm b/modules/game/game-objects.scm index e6cb0c9..c372f98 100644 --- a/modules/game/game-objects.scm +++ b/modules/game/game-objects.scm @@ -8,6 +8,7 @@ #:use-module (ces entity) #:use-module (ces entity-manager) #:use-module (game-core) + #:use-module (dom image) #:export (add-game-object-components game-object? create-game-object!)) @@ -23,10 +24,10 @@ (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)) + (entity (add-position entity entity-id x y)) + (entity (add-hitbox entity entity-id x y width height)) + (entity (add-sprite entity entity-id sprite width height))) + entity)) (define (game-object? entity) (has-components? entity 'position 'hitbox 'sprite)) diff --git a/modules/game/level-manager.scm b/modules/game/level-manager.scm new file mode 100644 index 0000000..88f3202 --- /dev/null +++ b/modules/game/level-manager.scm @@ -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