From f178cd558a03f145798533470736dfdfd104e678 Mon Sep 17 00:00:00 2001 From: nephryte Date: Sun, 26 May 2024 14:44:25 -0400 Subject: [PATCH] Update --- game.scm | 1 - ...cs-engine-2d.scm => physics-2d-engine.scm} | 7 ++- modules/ces/system/tilemap-renderer.scm | 10 +++- .../{game-objects.scm => entity/object.scm} | 6 +-- modules/game/entity/physics-2d.scm | 19 +++++++ modules/game/entity/player.scm | 28 ++++++++++ modules/game/entity/room.scm | 35 ++++++++++++ modules/game/game-manager.scm | 18 ++++--- modules/game/level-manager.scm | 54 +++---------------- modules/game/level.scm | 10 ++++ 10 files changed, 127 insertions(+), 61 deletions(-) rename modules/ces/system/{physics-engine-2d.scm => physics-2d-engine.scm} (90%) rename modules/game/{game-objects.scm => entity/object.scm} (89%) create mode 100644 modules/game/entity/physics-2d.scm create mode 100644 modules/game/entity/player.scm create mode 100644 modules/game/entity/room.scm create mode 100644 modules/game/level.scm diff --git a/game.scm b/game.scm index 24572b7..dd48b21 100644 --- a/game.scm +++ b/game.scm @@ -44,7 +44,6 @@ (ces system-manager) (ces entity-manager) (game-core) - (game game-objects) (game game-manager) (logging) (media)) diff --git a/modules/ces/system/physics-engine-2d.scm b/modules/ces/system/physics-2d-engine.scm similarity index 90% rename from modules/ces/system/physics-engine-2d.scm rename to modules/ces/system/physics-2d-engine.scm index e4b392b..921e0f9 100644 --- a/modules/ces/system/physics-engine-2d.scm +++ b/modules/ces/system/physics-2d-engine.scm @@ -14,6 +14,10 @@ (and (game-entity? entity) (apply has-components? desired-components))) +(define (resolve-collision rock hard-place dt) + +) + (define (update-physics-entity entity dt) ;; do math (let (;(imass (get-component entity 'imass)) @@ -29,7 +33,8 @@ (v (velocity-vector vel)) (a (velocity-vector acc)) (p* (vec2-add! p (vec2-mul-scalar! v dt))) - (v* (vec2-mul-scalar! (vec2-add! v (vec2-mul-scalar! a dt)) (* friction dt)))) + (v* (vec2-add! v (vec2-mul-scalar! a dt))) + (v* (vec2-mul-scalar! (* friction dt)))) ;; update component(s) (let* ((updated-entity (set-component entity 'position (make-position id p*))) (updated-entity (set-component entity 'velocity (make-velocity id v*))))) diff --git a/modules/ces/system/tilemap-renderer.scm b/modules/ces/system/tilemap-renderer.scm index f332d30..ba3d948 100644 --- a/modules/ces/system/tilemap-renderer.scm +++ b/modules/ces/system/tilemap-renderer.scm @@ -5,6 +5,8 @@ #:use-module (ces component) #:use-module (ces component tilemap) #:use-module (ces entity) + #:use-module (hoot debug) + #:use-module (logging) #:export (create-tilemap-renderer)) (define desired-components '('tilemap)) @@ -14,6 +16,8 @@ (has-components? entity 'tilemap))) (define (render-tilemap renderer entity dt) + (parameterize ((*logger* pk)) + (write-log! "render-tilemap") (lambda (dt) (let* ((tm (get-component entity 'tilemap)) (tm-tileset (tm-tileset tm)) @@ -34,12 +38,14 @@ (tile-height (tile-height tile)) (x (+ x-offset (* (modulo index tl-width) ts-tile-width))) (y (+ y-offset (* (modulo index tl-height) ts-tile-height)))) - (renderer img x y tile-width tile-height))) data))) tm-layers)))) + (renderer img x y tile-width tile-height))) data))) tm-layers))))) (define (create-tilemap-renderer renderer) + (parameterize ((*logger* pk)) + (write-log! "create-tilemap-renderer") (create-system wants-entity? (lambda (entity dt) (render-tilemap renderer entity dt)) - 'tilemap-renderer)) + 'tilemap-renderer))) diff --git a/modules/game/game-objects.scm b/modules/game/entity/object.scm similarity index 89% rename from modules/game/game-objects.scm rename to modules/game/entity/object.scm index cb1a5d8..55c6025 100644 --- a/modules/game/game-objects.scm +++ b/modules/game/entity/object.scm @@ -1,4 +1,4 @@ -(define-module (game game-objects) +(define-module (game entity object) #:pure #:use-module (scheme base) #:use-module (hoot debug) @@ -16,7 +16,7 @@ create-game-object!)) (define (add-position entity entity-id x y) - (write-log! "Running game-objects:add-position") + (write-log! "Running object:add-position") (set-component entity 'position (create-position entity-id x y))) (define (add-hitbox entity entity-id x y w h) @@ -27,7 +27,7 @@ (define (add-game-object-components entity x y width height sprite) (unless (game-entity? entity) - (error "Invalid entity passed to game-objects:add-game-object-components")) + (error "Invalid entity passed to object:add-game-object-components")) (let* ((entity-id (game-entity-id entity)) (p-entity (add-position entity entity-id x y)) (h-entity (add-hitbox p-entity entity-id x y width height)) diff --git a/modules/game/entity/physics-2d.scm b/modules/game/entity/physics-2d.scm new file mode 100644 index 0000000..7133796 --- /dev/null +++ b/modules/game/entity/physics-2d.scm @@ -0,0 +1,19 @@ +(define-module (game entity physics-2d) + #:pure + #:use-module (scheme base) + #:use-module (hoot debug) + #:use-module (ces component) + #:use-module (ces component hitbox) + #:use-module (ces component position) + #:use-module (ces component velocity) + #:use-module (ces component acceleration) + #:use-module (ces entity) + #:use-module (ces entity-manager) + #:use-module (game-core) + #:use-module (logging) + #:export ()) + +;; Physics 2D +(define (physics-2d? entity) + (has-components? entity 'hitbox 'position 'velocity 'acceleration)) + diff --git a/modules/game/entity/player.scm b/modules/game/entity/player.scm new file mode 100644 index 0000000..7fdfd0e --- /dev/null +++ b/modules/game/entity/player.scm @@ -0,0 +1,28 @@ +(define-module (game entity player) + #:pure + #:use-module (scheme base) + #:use-module (hoot debug) + #:use-module (ces component) + #:use-module (ces component inputs) + #:use-module (ces component hitbox) + #:use-module (ces component position) + #:use-module (ces component velocity) + #:use-module (ces component acceleration) + #:use-module (ces entity) + #:use-module (ces entity-manager) + #:use-module (game-core) + #:use-module (logging) + #:use-module (media) + #:use-module (game entity object) + #:export (player? + create-player!)) + +;; Player +(define (player? entity) + (and (game-object? entity) + (has-components? entity 'keyboard-input))) + +(define (create-player!) + (let* ((player-sprite (get-image (get-media-library) 'player)) + (initial-entity (create-game-object! 0 0 16 16 'player-sprite))) + (set-component initial-entity 'keyboard-input (create-inputs)))) diff --git a/modules/game/entity/room.scm b/modules/game/entity/room.scm new file mode 100644 index 0000000..6013f3a --- /dev/null +++ b/modules/game/entity/room.scm @@ -0,0 +1,35 @@ +(define-module (game entity room) + #:pure + #:use-module (scheme base) + #:use-module (hoot debug) + #:use-module (ces component) + #:use-module (ces component tilemap) + #:use-module (ces component sprite) + #:use-module (ces component hitbox) + #:use-module (ces entity) + #:use-module (ces entity-manager) + #:use-module (game-core) + #:use-module (logging) + #:export (room? + create-room!)) + +;; Room +(define (room? entity) + (has-components? entity 'tilemap)) ;; + 'room-objects ? + +(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-tilemap entity tm) + (set-component entity 'tilemap tm)) + +(define (add-room-components entity tm) + (add-tilemap entity tm)) + +(define (create-room! tm) + (let* ((e-manager (get-entity-manager)) + (entity (create-entity! e-manager))) + (add-room-components entity tm))) \ No newline at end of file diff --git a/modules/game/game-manager.scm b/modules/game/game-manager.scm index 9585f38..29d97fc 100644 --- a/modules/game/game-manager.scm +++ b/modules/game/game-manager.scm @@ -2,17 +2,20 @@ #:pure #:use-module (scheme base) #:use-module (ces system html-canvas-renderer) - #:use-module (ces system sprite-renderer) + #:use-module (ces system tilemap-renderer) #:use-module (dom image) #:use-module (media) #:use-module (ces component tilemap) - #:use-module (game level-manager) + #:use-module (game entity player) + #:use-module (game entity room) + #:use-module (hoot debug) #:use-module (logging) #:export (init-game-entities init-game-systems)) (define (init-game-entities) "Returns a list of initial game entities. " + (parameterize ((*logger* pk)) (write-log! "Init game entities") (let* ((room-background (get-image (get-media-library) 'room-background)) (ts-vec @@ -25,18 +28,19 @@ (tl-vec (write-log! #(2 2 2 2 3 2 2 2 - 1 1 1 1 1 1 1 1 - 1 1 1 1 1 1 1 1 - 1 1 1 1 1 1 1 1))) + 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1))) (room-tilemap (make-tilemap (make-tileset ts-vec 16 16) (list (make-tilemap-layer tl-vec 0 0 8 4))))) (list (create-player!) - (create-room! room-tilemap)))) + (create-room! room-tilemap))))) (define (init-game-systems draw-proc context) "Returns a list of initial game systems. Note: Systems are executed sequentially. " - (list (create-sprite-rendering-system draw-proc))) + (parameterize ((*logger* pk)) + (list (create-html-canvas-renderer draw-proc context)))) diff --git a/modules/game/level-manager.scm b/modules/game/level-manager.scm index 15d314b..858f15b 100644 --- a/modules/game/level-manager.scm +++ b/modules/game/level-manager.scm @@ -1,50 +1,10 @@ (define-module (game level-manager) - #:pure - #:use-module (scheme base) - #:use-module (ces component) - #:use-module (ces component inputs) - #: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 game-objects) - #:use-module (game-core) - #:use-module (media) - #:use-module (logging) - #:export (create-player! - create-room!)) + #:pure + #:use-module (logging) + #:use-module (scheme base) + #:export ()) -;; Player -(define (player? entity) - (and (game-object? entity) - (has-components? entity 'keyboard-input))) +(define-record-type + (make-level-manager) + level-manager?) -(define (create-player!) - (let* ((player-sprite (get-image (get-media-library) 'player)) - (initial-entity (create-game-object! 0 0 16 16 'player-sprite))) - (set-component initial-entity 'keyboard-input (create-inputs)))) - -;; Room -(define (room? entity) - (has-components? entity 'tilemap)) ;; + 'room-objects ? - -(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-tilemap entity tm) - (set-component entity 'tilemap tm)) - -(define (add-room-components entity tm) - (add-tilemap entity tm)) - -(define (create-room! tm) - (let* ((e-manager (get-entity-manager)) - (entity (create-entity! e-manager))) - (add-room-components entity tm))) - -;; Floor -;; A floor is a list of rooms diff --git a/modules/game/level.scm b/modules/game/level.scm new file mode 100644 index 0000000..e13991f --- /dev/null +++ b/modules/game/level.scm @@ -0,0 +1,10 @@ +(define-module (game level) + #:pure + #:use-module (logging) + #:use-module (scheme base) + #:export ()) + +(define-record-type + (make-level) + level?) +