(define-module (game render food)
  #:use-module (game model food)
  #:use-module (game model level)
  #:use-module (game util assets)
  #:use-module (chickadee math vector)
  #:use-module (chickadee graphics sprite)
  #:use-module (chickadee graphics texture)
  #:use-module (chickadee graphics text)
  #:export (render-foods)
  )


(define healthy-food-offset 3226)
(define healthy-food-cycle 15)
(define bad-food-offset 3026)
(define bad-food-cycle 8)

(define (food-index food cycle)
  (let ((x (coord->cell (vec2-x (food-position food))))
        (y (coord->cell (vec2-y (food-position food)))))
    (remainder (+ (* x 37)  (* 73 y)) cycle)))

(define (render-food food)
  (let ((index (food-index food (if (food-healthy? food) healthy-food-cycle bad-food-cycle)))
        (food-offset (if (food-healthy? food) healthy-food-offset bad-food-offset)))
    (when (not (food-eaten? food))
      (draw-sprite 
       (texture-atlas-ref tile-atlas (+ food-offset index))
       (food-position food)))))


(define (render-foods foods)
  (for-each render-food foods)
  (draw-text 
   (format #f "Calories Eaten: ~a" (food-total-calories-eaten foods)) 
   (vec2 16 4)))

