(define-module (game render hero)
  #:use-module (game model hero)
  #:use-module (game util assets)
  #:use-module (chickadee)
  #:use-module (chickadee math vector)
  #:use-module (chickadee graphics sprite)
  #:use-module (chickadee graphics texture)
  #:export (render-hero
            render-hero-load))

;; start index of the walk animation
(define hero-walking-offset 56)
(define hero-falling-offset 32)
(define hero-climbing-offset 48)

(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 -16.0 1.0))))
    ((fall) (draw-sprite
             (hero-sprite-falling hero)
             (vec2+ (hero-position hero) (vec2 -16.0 1.0))))
    ((climb) (draw-sprite
              (hero-sprite-climbing hero)
              (vec2+ (hero-position hero) (vec2 -16.0 1.0))))
    ((go-left) (draw-sprite
                (hero-sprite-walking hero)
                (vec2+ (hero-position hero) (vec2 16.0 1.0))
                #:scale (vec2 -1.0 1.0)
                ))
    ((go-right) (draw-sprite
                 (hero-sprite-walking hero)
                 (vec2+ (hero-position hero) (vec2 -16.0 1.0))))
    )
  )
