Add tilemap system

This commit is contained in:
nephryte 2024-05-25 21:17:11 -04:00
parent 355f41a2cc
commit d48efbdba4
4 changed files with 77 additions and 30 deletions

View file

@ -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 <tile>
(make-tile image width height)
tile?
(image tile-image)
(width tile-width)
(height tile-height))
(define-record-type <tileset>
(make-tileset tile-width tile-height tiles)
tileset?
(tile-width ts-tile-width)
(tile-height ts-tile-height)
(tiles ts-tiles))
(define-record-type <tilemap-layer>
(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 <tilemap>
(make-tilemap tiles t-map tile-size)
(make-tilemap ts layers)
tilemap?
(tiles tilemap-tiles)
(t-map tilemap-map)
(tile-size tilemap-tile-size))
(ts tm-tileset)
(tm tm-layers))

View file

@ -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?

View file

@ -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.

View file

@ -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