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

;; name: plat.gc
;; name in dgo: plat
;; dgos: GAME, COMMON

;; DECOMP BEGINS

(deftype plat (base-plat)
  ((path-pos  float)
   (sound-id  sound-id)
   (sync      sync-eased  :inline)
   )
  (:state-methods
    plat-idle
    plat-path-active
    )
  (:methods
    (plat-path-sync (_type_) object)
    )
  )


(defskelgroup skel-plat plat 0 4
              ((1 (meters 20)) (2 (meters 40)) (3 (meters 999999)))
              :bounds (static-spherem 0 -0.5 0 3)
              )

(defmethod get-art-group ((this plat))
  "@returns The associated [[art-group]]"
  (art-group-get-by-name *level* "skel-plat" (the-as (pointer uint32) #f))
  )

(defmethod init-plat-collision! ((this plat))
  "TODO - collision stuff for setting up the platform"
  (let ((collision-shape (new 'process 'collide-shape this (collide-list-enum hit-by-player))))
    (let ((collision-mesh (new 'process 'collide-shape-prim-mesh collision-shape (the-as uint 0) (the-as uint 0))))
      (set! (-> collision-mesh prim-core collide-as) (collide-spec pusher))
      (set! (-> collision-mesh prim-core collide-with) (collide-spec jak player-list))
      (set! (-> collision-mesh prim-core action) (collide-action solid rideable))
      (set! (-> collision-mesh transform-index) 0)
      (set-vector! (-> collision-mesh local-sphere) 0.0 0.0 0.0 13107.2)
      (set! (-> collision-shape total-prims) (the-as uint 1))
      (set! (-> collision-shape root-prim) collision-mesh)
      )
    (pusher-init collision-shape)
    (set! (-> collision-shape nav-radius) (* 0.75 (-> collision-shape root-prim local-sphere w)))
    (let ((prim (-> collision-shape root-prim)))
      (set! (-> collision-shape backup-collide-as) (-> prim prim-core collide-as))
      (set! (-> collision-shape backup-collide-with) (-> prim prim-core collide-with))
      )
    (set! (-> this root) (the-as collide-shape-moving collision-shape))
    )
  0
  (none)
  )

(defmethod init-plat! ((this plat))
  "Does any necessary initial platform setup.
For example for an elevator pre-compute the distance between the first and last points (both ways) and clear the sound."
  0
  (none)
  )

(defmethod base-plat-method-32 ((this plat))
  0
  (none)
  )

(defmethod plat-path-sync ((this plat))
  "If the `sync` period is greater than `0` then transition the state to [[plat::35]]
otherwise, [[plat::34]]

@see [[sync-eased]]"
  (cond
    ((logtest? (-> this path flags) (path-control-flag not-found))
     (go (method-of-object this plat-idle))
     )
    ((> (-> this sync period) 0)
     (go (method-of-object this plat-path-active))
     )
    (else
      (go (method-of-object this plat-idle))
      )
    )
  )

(defstate plat-idle (plat)
  :virtual #t
  :event plat-event
  :trans (behavior ()
    (execute-effects self)
    )
  :code (behavior ()
    (plat-trans)
    (rider-post)
    (suspend)
    (until #f
      (when (not (-> self bouncing))
        (plat-trans)
        (rider-post)
        (logior! (-> self mask) (process-mask sleep-code))
        (suspend)
        0
        )
      (while (-> self bouncing)
        (plat-trans)
        (rider-post)
        (suspend)
        )
      )
    #f
    )
  )

(defstate plat-path-active (plat)
  :virtual #t
  :event plat-event
  :exit (behavior ()
    (sound-stop (-> self sound-id))
    )
  :trans (behavior ()
    (set! (-> self path-pos) (get-norm! (-> self sync) 0))
    (get-point-at-percent-along-path! (-> self path) (-> self basetrans) (-> self path-pos) 'interp)
    (if (< (vector-vector-distance (-> self root trans) (ear-trans 0)) 81920.0)
        (sound-play "eco-plat-hover" :id (-> self sound-id) :position (-> self root trans))
        )
    (plat-trans)
    )
  :code (behavior ()
    (until #f
      (ja-no-eval :group! (ja-group) :num! (seek!) :frame-num 0.0)
      (until (ja-done? 0)
        (suspend)
        (ja :num! (seek!))
        )
      )
    #f
    )
  :post plat-post
  )

;; WARN: Return type mismatch object vs none.
(defmethod init-from-entity! ((this plat) (entity entity-actor))
  "Typically the method that does the initial setup on the process, potentially using the [[entity-actor]] provided as part of that.
This commonly includes things such as:
- stack size
- collision information
- loading the skeleton group / bones
- sounds"
  (logior! (-> this mask) (process-mask platform))
  (init-plat-collision! this)
  (process-drawable-from-entity! this entity)
  (initialize-skeleton this (the-as skeleton-group (get-art-group this)) (the-as pair 0))
  (update-transforms (-> this root))
  (stop-bouncing! this)
  (base-plat-method-32 this)
  (set! (-> this fact)
        (new 'process 'fact-info this (pickup-type eco-pill-random) (-> *FACT-bank* default-eco-pill-green-inc))
        )
  (let ((params (new 'stack-no-clear 'sync-info-params)))
    (let ((v1-15 0))
      (if (not (logtest? (-> this fact options) (actor-option loop)))
          (set! v1-15 (logior v1-15 1))
          )
      (set! (-> params sync-type) 'sync-eased)
      (set! (-> params sync-flags) (the-as sync-flags v1-15))
      )
    (set! (-> params period) (the-as uint 1200))
    (set! (-> params entity) entity)
    (set! (-> params percent) 0.0)
    (set! (-> params ease-in) 0.15)
    (set! (-> params ease-out) 0.15)
    (set! (-> params pause-in) 0.0)
    (set! (-> params pause-out) 0.0)
    (initialize! (-> this sync) params)
    )
  (set! (-> this path) (new 'process 'curve-control this 'path -1000000000.0))
  (logior! (-> this path flags) (path-control-flag display draw-line draw-point draw-text))
  (set! (-> this sound-id) (new-sound-id))
  (cond
    ((logtest? (-> this path flags) (path-control-flag not-found))
     (set! (-> this path-pos) 0.0)
     (init-plat! this)
     (plat-path-sync this)
     )
    ((> (-> this sync period) 0)
     (set! (-> this path-pos) (get-norm! (-> this sync) 0))
     (get-point-at-percent-along-path! (-> this path) (-> this root trans) (-> this path-pos) 'interp)
     (init-plat! this)
     (plat-path-sync this)
     )
    (else
      (set! (-> this path-pos) 0.0)
      (get-point-at-percent-along-path! (-> this path) (-> this root trans) (-> this path-pos) 'interp)
      (init-plat! this)
      (plat-path-sync this)
      )
    )
  (none)
  )

(deftype drop-plat (base-plat)
  ((art-name         string)
   (anim             spool-anim)
   (break-anim-name  string)
   (safe-time        time-frame)
   (hit-point        vector  :inline)
   )
  (:state-methods
    idle
    (fall symbol)
    )
  )


;; WARN: Return type mismatch base-plat vs drop-plat.
(defmethod relocate ((this drop-plat) (arg0 int))
  (if (nonzero? (-> this break-anim-name))
      (&+! (-> this break-anim-name) arg0)
      )
  (let ((v1-5 (-> this anim anim-name)))
    (if (and (>= (the-as int v1-5) (-> *kernel-context* relocating-min))
             (< (the-as int v1-5) (-> *kernel-context* relocating-max))
             )
        (set! (-> this anim anim-name) (&+ (the-as external-art-buffer (-> this anim anim-name)) arg0))
        )
    )
  (the-as drop-plat ((method-of-type base-plat relocate) this arg0))
  )

(defstate idle (drop-plat)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('touch 'attack 'bonk)
       (let* ((proc-temp proc)
              (proc-focus (if (type? proc-temp process-focusable)
                              (the-as process-focusable proc-temp)
                              )
                          )
              )
         (cond
           ((and proc-focus (focus-test? proc-focus edge-grab))
            (set! (-> self safe-time) (+ (current-time) (seconds 0.2)))
            (return (the-as object #f))
            )
           ((not (time-elapsed? (-> self safe-time) (seconds 0.05)))
            (return (the-as object #f))
            )
           )
         (set! (-> self hit-point quad) (-> self root trans quad))
         (set! proc-focus (if (type? proc process-focusable)
                              (the-as process-focusable proc)
                              )
               )
         (set! (-> self hit-point quad) (-> (get-trans proc-focus 0) quad))
         )
       (if (zero? (-> self bounce-time))
           (start-bouncing! self)
           )
       #f
       )
      )
    )
  :trans plat-trans
  :code (behavior ()
    (add-process *gui-control* self (gui-channel art-load) (gui-action queue) (-> self anim name) -99.0 0)
    (until #f
      (when (not (-> self bouncing))
        (logior! (-> self mask) (process-mask sleep-code))
        (suspend)
        0
        )
      (while (-> self bouncing)
        (suspend)
        )
      (go-virtual fall #f)
      )
    #f
    )
  :post plat-post
  )

(defstate fall (drop-plat)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('edge-grabbed)
       (if (time-elapsed? (-> self state-time) (seconds 0.5))
           (send-event proc 'end-mode)
           )
       )
      (('die)
       (go-virtual fall #t)
       )
      )
    )
  :enter (behavior ((arg0 symbol))
    (set-time! (-> self state-time))
    )
  :exit (behavior ()
    (ja-abort-spooled-anim (-> self anim) (the-as art-joint-anim #f) -1)
    )
  :trans rider-trans
  :code (behavior ((arg0 symbol))
    (process-entity-status! self (entity-perm-status subtask-complete) #t)
    (let ((v1-1 (-> self root root-prim)))
      (set! (-> v1-1 prim-core collide-as) (collide-spec))
      (set! (-> v1-1 prim-core collide-with) (collide-spec))
      )
    0
    (if (not arg0)
        (ja-play-spooled-anim
          (-> self anim)
          (ja-group)
          (the-as art-joint-anim #f)
          (the-as (function process-drawable symbol) false-func)
          )
        )
    (ja-channel-set! 0)
    (suspend)
    (logior! (-> self mask) (process-mask sleep))
    (suspend)
    0
    )
  :post rider-post
  )
