;;-*-Lisp-*-
(in-package goal)
(bundles "VI2.DGO")
(require "levels/village2/village2-obs.gc")
(require "engine/common-obs/plat-button.gc")

;; DECOMP BEGINS

(deftype sunken-elevator (plat-button)
  ((play-at-top-going-up-camera? symbol)
   (teleport-if-below-y          float)
   (teleport-if-above-y          float)))


(defskelgroup *sunken-elevator-sg*
  sunken-elevator
  sunken-elevator-lod0-jg
  sunken-elevator-pressed-ja
  ((sunken-elevator-lod0-mg (meters 999999)))
  :bounds (static-spherem 0 -1 0 6.6))

(defmethod should-teleport? ((this sunken-elevator))
  (let ((f0-0 (-> (camera-pos) y)))
    (case (-> this path-pos)
      ((0.0) (if (< f0-0 (-> this teleport-if-below-y)) (return #t)))
      ((1.0) (if (< (-> this teleport-if-above-y) f0-0) (return #t)))))
  #f)

(defstate plat-button-pressed (sunken-elevator)
  :virtual #t
  :enter
    (behavior ()
      (let ((t9-0 (-> (method-of-type plat-button plat-button-pressed) enter))) (if t9-0 (t9-0)))
      (case (-> self path-pos)
        ((0.0)
         (load-state-want-display-level 'sunken 'display)
         (let ((gp-0 (get-process *default-dead-pool* village2cam #x4000)))
           (set! (-> (the-as village2cam
                             (-> (when gp-0
                                   (let ((t9-3 (method-of-type village2cam activate)))
                                     (t9-3 (the-as village2cam gp-0) self 'village2cam (the-as pointer #x70004000)))
                                   (run-now-in-process gp-0
                                                       pov-camera-init-by-other
                                                       (-> self spawn-pos)
                                                       *village2cam-sg*
                                                       "elevator-at-top-going-down"
                                                       0
                                                       #f
                                                       '())
                                   (-> gp-0 ppointer))
                                 0))
                     seq)
                 (the-as uint 0)))
         0)
        ((1.0)
         (let ((v1-10 (process-spawn village2cam
                                     :init
                                     pov-camera-init-by-other
                                     (-> self spawn-pos)
                                     *village2cam-sg*
                                     "elevator-at-top-going-down"
                                     0
                                     #f
                                     '()
                                     :to
                                     self)))
           (set! (-> (the-as village2cam (-> v1-10 0)) seq) (the-as uint 1)))))))

(defstate plat-button-move-upward (sunken-elevator)
  :virtual #t
  :enter
    (behavior ()
      (let ((t9-0 (-> (method-of-type plat-button plat-button-move-upward) enter))) (if t9-0 (t9-0)))
      (set! (-> self play-at-top-going-up-camera?) #t))
  :trans
    (behavior ()
      (let ((t9-0 (-> (method-of-type plat-button plat-button-move-upward) trans))) (if t9-0 (t9-0)))
      (when (and (-> self play-at-top-going-up-camera?) (>= 0.14 (-> self path-pos)))
        (set! *teleport* #t)
        (set! (-> self play-at-top-going-up-camera?) #f)
        (load-state-want-display-level 'sunken 'special)
        (let ((v1-8 (process-spawn village2cam
                                   :init
                                   pov-camera-init-by-other
                                   (-> self spawn-pos)
                                   *village2cam-sg*
                                   "elevator-at-top-going-down"
                                   0
                                   #f
                                   '()
                                   :to
                                   self)))
          (set! (-> (the-as village2cam (-> v1-8 0)) seq) (the-as uint 2))))))

(defstate plat-button-move-downward (sunken-elevator)
  :virtual #t
  :trans
    (behavior ()
      (let ((s5-0 (new 'stack-no-clear 'vector))
            (gp-0 (new 'stack-no-clear 'vector)))
        (set! *teleport* #t)
        (set! (-> s5-0 quad) (-> self root trans quad))
        (call-parent-state-handler trans)
        (vector-! gp-0 (-> self root trans) s5-0)
        (when (< (-> self path-pos) 0.9)
          (move-by-vector! (-> *target* control) gp-0)
          (send-event *target* 'reset-height)))))

(defmethod can-target-move? ((this sunken-elevator))
  (set! (-> this play-at-top-going-up-camera?) #f)
  (let ((s5-0 (new 'stack-no-clear 'vector)))
    (eval-path-curve! (-> this path) s5-0 0.4 'interp)
    (set! (-> this teleport-if-above-y) (-> s5-0 y))
    (eval-path-curve! (-> this path) s5-0 0.6 'interp)
    (set! (-> this teleport-if-below-y) (-> s5-0 y)))
  (none))

(defmethod plat-button-method-27 ((this sunken-elevator))
  (ja-channel-set! 1)
  (cond
    ((can-activate? this)
     (let ((s5-0 (-> this skel root-channel 0)))
       (joint-control-channel-group-eval! s5-0 (the-as art-joint-anim (-> this draw art-group data 2)) num-func-identity)
       (set! (-> s5-0 frame-num) 0.0)))
    (else
     (let ((s5-1 (-> this skel root-channel 0)))
       (joint-control-channel-group-eval! s5-1 (the-as art-joint-anim (-> this draw art-group data 2)) num-func-identity)
       (set! (-> s5-1 frame-num) (the float (+ (-> (the-as art-joint-anim (-> this draw art-group data 2)) data 0 length) -1))))))
  (ja-post)
  (update-transforms! (-> this root))
  (none))

(defmethod plat-button-method-31 ((this sunken-elevator))
  (initialize-skeleton this *sunken-elevator-sg* '())
  0
  (none))
