diff --git a/assets/backgrounds/background.png b/assets/backgrounds/background.png new file mode 100644 index 0000000..dbb9cd1 Binary files /dev/null and b/assets/backgrounds/background.png differ 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