diff --git a/modules/ces/component/tilemap.scm b/modules/ces/component/tilemap.scm index 295d013..d51af8b 100644 --- a/modules/ces/component/tilemap.scm +++ b/modules/ces/component/tilemap.scm @@ -1,15 +1,54 @@ (define-module (ces component tilemap) #:pure #:use-module (scheme base) - #:export (tilemap? + #:export (make-tile + tile? + tile-image + tile-width + tile-height + make-tileset + tileset? + ts-tile-width + ts-tile-height + ts-tiles + make-tilemap-layer + tilemap-layer? + tl-data + tl-x + tl-y + tl-width + tl-height make-tilemap - tilemap-tiles - tilemap-map - tilemap-tile-size)) + tilemap? + tm-tileset + tm-layers + )) + +(define-record-type + (make-tile image width height) + tile? + (image tile-image) + (width tile-width) + (height tile-height)) + +(define-record-type + (make-tileset tile-width tile-height tiles) + tileset? + (tile-width ts-tile-width) + (tile-height ts-tile-height) + (tiles ts-tiles)) + +(define-record-type + (make-tile-layer data x y width height) + tilemap-layer? + (data tl-data) + (x tl-x) + (y tl-y) + (width tl-width) + (height tl-height)) (define-record-type - (make-tilemap tiles t-map tile-size) + (make-tilemap ts layers) tilemap? - (tiles tilemap-tiles) - (t-map tilemap-map) - (tile-size tilemap-tile-size)) \ No newline at end of file + (ts tm-tileset) + (tm tm-layers)) \ No newline at end of file diff --git a/modules/ces/system/tilemap-renderer.scm b/modules/ces/system/tilemap-renderer.scm index 3b38e8e..d27c284 100644 --- a/modules/ces/system/tilemap-renderer.scm +++ b/modules/ces/system/tilemap-renderer.scm @@ -15,16 +15,25 @@ (define (render-tilemap renderer entity dt) (lambda (dt) - (let* ((tilemap (get-component entity 'tilemap)) - (tilemap-tiles (tilemap-tiles tilemap)) - (tilemap-map (tilemap-map tilemap)) - (tile-size (tilemap-tile-size tilemap))) - (for-each (lambda (tile-data) - (let* ((index (car tile-data)) - (x (cadr tile-data)) - (y (cddr tile-data)) - (img (vector-ref tilemap-tiles index))) - (renderer img x y tile-size tile-size))) tilemap-map)))) + (let* ((tm (get-component entity 'tilemap)) + (tm-tileset (tm-tileset tm)) + (tm-layers (tm-layers tm)) + (ts-tiles (ts-tiles tm-tileset)) + (ts-tile-width (ts-tile-width tm-tileset)) + (ts-tile-height (ts-tile-height tm-tileset))) + (for-each (lambda (layer) + (let ((data (tl-data layer)) + (x-offset (tl-x layer)) + (y-offset (tl-y layer))) + (for-each (lambda (index) + (let* ((tile (vector-ref ts-tiles index)) + (img (tile-image tile)) + (tile-width (tile-width tile)) + (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)))) + (define (create-tilemap-renderer renderer) (create-system wants-entity? diff --git a/modules/game/game-manager.scm b/modules/game/game-manager.scm index 910df5e..d2a9235 100644 --- a/modules/game/game-manager.scm +++ b/modules/game/game-manager.scm @@ -10,9 +10,10 @@ (define (init-game-entities) "Returns a list of initial game entities. " - (let ((room-background (get-image (get-media-library) 'room-background))) + (let ((room-background (get-image (get-media-library) 'room-background)) + (room-tilemap '())) (list (create-player!) - (create-room! room-background 0 0 128 64)))) + (create-room! room-tilemap)))) (define (init-game-systems draw-proc context) "Returns a list of initial game systems. diff --git a/modules/game/level-manager.scm b/modules/game/level-manager.scm index 327baca..235cd2c 100644 --- a/modules/game/level-manager.scm +++ b/modules/game/level-manager.scm @@ -27,9 +27,7 @@ ;; 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. + (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))) @@ -37,17 +35,17 @@ (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 background width height)) - (entity (add-bounding-box entity room-entity-id x y width height))) - entity)) +(define (add-tilemap entity tm) + (set-component entity 'tilemap tm)) -(define (create-room! background x y width height) +(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))) (reset-entity! e-manager - (add-room-components entity background x y width height)))) + (add-room-components entity tm)))) ;; Floor ;; A floor is a list of rooms