This commit is contained in:
nephryte 2024-05-26 14:44:25 -04:00
parent d36ceb989d
commit f178cd558a
10 changed files with 127 additions and 61 deletions

View file

@ -44,7 +44,6 @@
(ces system-manager)
(ces entity-manager)
(game-core)
(game game-objects)
(game game-manager)
(logging)
(media))

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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 <level-manager>
(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

10
modules/game/level.scm Normal file
View file

@ -0,0 +1,10 @@
(define-module (game level)
#:pure
#:use-module (logging)
#:use-module (scheme base)
#:export ())
(define-record-type <level>
(make-level)
level?)