split hero entity in model and render module

This commit is contained in:
Peter Tillemans 2024-05-20 19:49:32 +02:00
parent 229939ba81
commit 98b8d18e1c
3 changed files with 57 additions and 28 deletions

View file

@ -1,6 +1,8 @@
(define-module (game main)
#:use-module (game model level)
#:use-module (game model hero)
#:use-module (game render level)
#:use-module (game render hero)
#:use-module (chickadee)
#:use-module (chickadee math rect)
#:use-module (chickadee math vector)
@ -12,44 +14,27 @@
#:use-module (system repl coop-server)
#:export (launch-game))
(define hero-texture #f)
(define hero-atlas #f)
(define hero-position (vec2 32.0 32.0))
(define level #f)
(define repl #f)
(define hero #f)
(define (load)
(render-level-load)
(set! hero-texture (load-image "assets/images/lr_penguin2.png"))
(set! hero-atlas (split-texture hero-texture 32 32))
(render-hero-load)
(set! level (level-parse-file "assets/levels/level-1.map"))
(render-level-set! level)
(set! hero (hero-load))
(set! repl (spawn-coop-repl-server)))
(define (update dt)
(poll-coop-repl-server repl)
(set-vec2-x! hero-position
(floor-remainder (+ (vec2-x hero-position) (* 50.0 dt)) 608.0)))
(define (draw-hero i)
(let ((x (* 64 (remainder i 8)))
(y (* 32 (quotient i 8)))
)
(draw-sprite
(texture-atlas-ref hero-atlas i)
(vec2 (+ x 16) y)
#:scale (vec2 1.0 1.0))
(draw-text (format #f "~A" i) (vec2 x y) #:color white))
)
(set! hero
(hero-with-x hero
(floor-remainder (+ (hero-x hero) (* 50.0 dt)) 608.0))))
(define (draw _alpha)
(render-level-draw level)
(let ((hero-index (+ 56 (remainder (inexact->exact (floor (vec2-x hero-position))) 8))))
(draw-sprite
(texture-atlas-ref hero-atlas hero-index)
(vec2+ hero-position (vec2 16.0 0.0))))
(render-hero hero)
)
(define (launch-game args)

View file

@ -3,10 +3,16 @@
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-9 gnu)
#:use-module (srfi srfi-64)
#:use-module (chickadee math vector)
#:export (hero-load
hero-x hero-with-x
hero-y hero-with-y
hero-state hero-with-state
hero-bloat hero-with-bloat
hero-position
))
)
)
(define-immutable-record-type <hero>
(%make-hero x y state bloat)
hero?
@ -17,13 +23,15 @@
)
(define default-x 320)
(define default-y 16)
(define default-y 32)
(define default-state 'stationary)
(define default-bloat 1.0)
(define (hero-load)
(%make-hero default-x default-y default-state default-bloat))
(define (hero-position hero)
(vec2 (hero-x hero) (hero-y hero)))
;; Tests
[test-begin "hero model"]
@ -43,4 +51,6 @@
(test-equal 1.5 (hero-bloat (hero-with-bloat hero 1.5)))
(test-equal default-bloat (hero-bloat hero)))
(test-equal (vec2 default-x default-y) (hero-position (hero-load)))
(test-end "hero model")

34
game/render/hero.scm Normal file
View file

@ -0,0 +1,34 @@
(define-module (game render hero)
#:use-module (game model hero)
#:use-module (chickadee)
#:use-module (chickadee math vector)
#:use-module (chickadee graphics sprite)
#:use-module (chickadee graphics texture)
#:export (render-hero
render-hero-load)
)
(define hero-texture #f)
(define hero-atlas #f)
(define (render-hero-load)
(set! hero-texture (load-image "assets/images/lr_penguin2.png"))
(set! hero-atlas (split-texture hero-texture 32 32)))
;; start index of the walk animation
(define hero-walk-offset 56)
(define (hero-sprite hero)
(let* ((x (inexact->exact (floor (vec2-x (hero-position hero)))))
(x-offset (remainder x 8))
(hero-index (+ hero-walk-offset x-offset)))
(texture-atlas-ref hero-atlas hero-index)))
(define (render-hero hero)
(draw-sprite
(hero-sprite hero)
(vec2+
(hero-position hero)
(vec2 16.0 0.0))))