bloatrunner/game/render/hero.scm

64 lines
2 KiB
Scheme
Raw Normal View History

(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-walking-offset 56)
(define hero-falling-offset 32)
(define hero-climbing-offset 40)
(define (hero-sprite-walking hero)
(let* ((x (hero-x hero))
(animation-frame (remainder x 8))
(hero-index (+ hero-walking-offset animation-frame)))
(texture-atlas-ref hero-atlas hero-index)))
(define (hero-sprite-stationary _hero)
(texture-atlas-ref hero-atlas 56))
(define (hero-sprite-falling hero)
(let* ((y (hero-y hero))
(animation-frame (remainder y 8))
(hero-index (+ hero-falling-offset animation-frame)))
(texture-atlas-ref hero-atlas hero-index)))
(define (hero-sprite-climbing hero)
(let* ((y (hero-y hero))
(animation-frame (remainder y 8))
(hero-index (+ hero-climbing-offset animation-frame)))
(texture-atlas-ref hero-atlas hero-index)))
(define (render-hero hero)
(case (hero-state hero)
((stationary) (draw-sprite
(hero-sprite-stationary hero)
(vec2+ (hero-position hero) (vec2 -9.0 1.0))))
((falling) (draw-sprite
(hero-sprite-falling hero)
(vec2+ (hero-position hero) (vec2 -9.0 1.0))))
((go-left) (draw-sprite
(hero-sprite-walking hero)
(vec2+ (hero-position hero) (vec2 23.0 1.0))
#:scale (vec2 -1.0 1.0)
))
((go-right) (draw-sprite
(hero-sprite-walking hero)
(vec2+ (hero-position hero) (vec2 -9.0 1.0))))
)
)