;;-*-Lisp-*-
(in-package goal)

;; definition of type ticky
(deftype ticky (structure)
  ((delay-til-ramp    time-frame  :offset-assert   0)
   (delay-til-timeout time-frame  :offset-assert   8)
   (starting-time     time-frame  :offset-assert  16)
   (last-tick-time    time-frame  :offset-assert  24)
   )
  :method-count-assert 12
  :size-assert         #x20
  :flag-assert         #xc00000020
  (:methods
    (sleep (_type_ time-frame) none 9)
    (reached-delay? (_type_ time-frame) symbol 10)
    (completed? (_type_) symbol 11)
    )
  )

;; definition for method 3 of type ticky
(defmethod inspect ticky ((obj ticky))
  (format #t "[~8x] ~A~%" obj 'ticky)
  (format #t "~Tdelay-til-ramp: ~D~%" (-> obj delay-til-ramp))
  (format #t "~Tdelay-til-timeout: ~D~%" (-> obj delay-til-timeout))
  (format #t "~Tstarting-time: ~D~%" (-> obj starting-time))
  (format #t "~Tlast-tick-time: ~D~%" (-> obj last-tick-time))
  obj
  )

;; definition for method 9 of type ticky
;; INFO: Return type mismatch int vs none.
(defmethod sleep ticky ((obj ticky) (arg0 time-frame))
  (set! (-> obj starting-time) (-> *display* base-frame-counter))
  (set! (-> obj delay-til-timeout) arg0)
  (set! (-> obj delay-til-ramp) (max 0 (+ arg0 (seconds -4))))
  (set! (-> obj last-tick-time) 0)
  0
  (none)
  )

;; definition for method 11 of type ticky
(defmethod completed? ticky ((obj ticky))
  (let ((gp-0 #f))
    (let ((v1-2 (- (-> *display* base-frame-counter) (-> obj starting-time))))
      (cond
        ((>= v1-2 (-> obj delay-til-timeout))
         (set! gp-0 #t)
         )
        (else
          (let* ((f0-1 (fmin 1.0 (/ (the float (max 0 (- v1-2 (-> obj delay-til-ramp))))
                                    (the float (- (-> obj delay-til-timeout) (-> obj delay-til-ramp)))
                                    )
                             )
                       )
                 (v1-7 (the int (lerp 105.0 41.0 f0-1)))
                 )
            (when (>= (- (-> *display* base-frame-counter) (-> obj last-tick-time)) v1-7)
              (set! (-> obj last-tick-time) (-> *display* base-frame-counter))
              (sound-play "stopwatch")
              )
            )
          )
        )
      )
    gp-0
    )
  )

;; definition for method 10 of type ticky
(defmethod reached-delay? ticky ((obj ticky) (arg0 time-frame))
  (>= (- (-> *display* base-frame-counter) (-> obj starting-time)) arg0)
  )
