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

;; DECOMP BEGINS

(deftype jungle-elevator (plat-button)
  ((bottom-height       float)
   (teleport-if-below-y float)
   (teleport-if-above-y float)))


(defmethod can-activate? ((this jungle-elevator))
  (and ((method-of-type plat-button can-activate?) this) (task-complete? *game-info* (game-task jungle-tower))))

(defstate plat-button-move-downward (jungle-elevator)
  :virtual #t
  :enter
    (behavior ()
      (let ((t9-0 (-> (method-of-type plat-button plat-button-move-downward) enter))) (if t9-0 (t9-0)))
      (set! (-> self draw light-index) (the-as uint 255))
      (set! (-> self bottom-height) (-> jungle bottom-height))
      (set! (-> jungle bottom-height) (-> jungleb bottom-height))
      (send-event *target* 'reset-pickup 'eco))
  :exit
    (behavior ()
      (set! (-> jungle bottom-height) (-> self bottom-height))
      (let ((t9-0 (-> (method-of-type plat-button plat-button-move-downward) exit))) (if t9-0 (t9-0))))
  :trans
    (behavior ()
      (let ((s5-0 (new 'stack-no-clear 'vector))
            (gp-0 (new 'stack-no-clear 'vector)))
        (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)))
      (if (and (>= (-> self path-pos) 0.2) (not (time-elapsed? (-> self state-time) (seconds 0.2))))
        (load-commands-set! *level* (load-command-get-index *level* 'jungle 0)))))

(defstate plat-button-move-upward (jungle-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 draw light-index) (the-as uint 255))
      (set! (-> self bottom-height) (-> jungle bottom-height))
      (set! (-> jungle bottom-height) (-> jungleb bottom-height))
      (set! (-> self grab-player?) (process-grab? *target*)))
  :exit
    (behavior ()
      (set! (-> jungle bottom-height) (-> self bottom-height))
      (let ((t9-0 (-> (method-of-type plat-button plat-button-move-upward) exit))) (if t9-0 (t9-0))))
  :trans
    (behavior ()
      (let ((t9-0 (-> (method-of-type plat-button plat-button-move-upward) trans))) (if t9-0 (t9-0)))
      (if (and (< (-> self path-pos) 0.8) (not (time-elapsed? (-> self state-time) (seconds 0.2))))
        (load-commands-set! *level* (load-command-get-index *level* 'jungle 1)))))

(defstate plat-button-at-end (jungle-elevator)
  :virtual #t
  :code
    (behavior ()
      (cond
        ((!= (-> self path-pos) 0.0)
         (set! (-> self draw light-index) (the-as uint 1))
         (let ((a1-1 (res-lump-struct (-> self entity) 'continue-name structure)))
           (when a1-1
             (let ((v1-4 (set-continue! *game-info* (the-as basic a1-1)))) (load-commands-set! *level* (-> v1-4 load-commands))))))
        (else (set! (-> self draw light-index) (the-as uint 255)) (load-commands-set! *level* '())))
      (let ((t9-4 (-> (method-of-type plat-button plat-button-at-end) code)))
        (if t9-4 ((the-as (function none :behavior plat-button) t9-4))))))

(defmethod should-teleport? ((this jungle-elevator))
  (let ((f0-0 (-> (camera-pos) y)))
    (case (-> this path-pos)
      ((0.0) (when (< f0-0 (-> this teleport-if-below-y)) (set! (-> this draw light-index) (the-as uint 1)) (return #t)))
      ((1.0) (when (< (-> this teleport-if-above-y) f0-0) (set! (-> this draw light-index) (the-as uint 255)) (return #t)))))
  #f)

(defmethod can-target-move? ((this jungle-elevator))
  (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)))
  (set! (-> this draw light-index) (the-as uint 255))
  (set! (-> this bidirectional?) #f)
  (set! (-> this should-grab-player?) #t)
  (none))
