(include "sci.sh")
(use "Main")
(use "Obj")
(script 992)



(class Cycle of Obj
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
    )

    (method (init theClient)
        (if (paramTotal)
            = client theClient
        )
        = completed (= cycleCnt 0)
    )


    (method (nextCel)
        ++cycleCnt
        return 
            (if (<= cycleCnt (send client:cycleSpeed))
                (send client:cel)
            )(else
                = cycleCnt 0
                (if (& (send client:signal) $1000)
                    (send client:cel)
                )(else
                    + (send client:cel) cycleDir
                )
            )
    )


    (method (cycleDone)
    )


    (method (motionCue)
        (send client:cycler(0))
        (if (completed and IsObject(caller))
            (send caller:cue())
        )
        (self:dispose())
    )

)
(class Fwd of Cycle
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
    )

    (method (doit)
        (var FwdNextCel)
        (if (> (= FwdNextCel (self:nextCel())) (send client:lastCel()))
            (self:cycleDone())
        )(else
            (send client:cel(FwdNextCel))
        )
    )


    (method (cycleDone)
        (send client:cel(0))
    )

)
(class Walk of Fwd
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
    )

    (method (doit)
        (var temp0)
        (if (not (send client:isStopped()))
            (super:doit())
        )
    )

)
(class CT of Cycle
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
        endCel 0
    )

    (method (init param1 param2 theCycleDir theCaller)
        (var clientLastCel)
        (super:init(param1))
        = cycleDir theCycleDir
        (if (== paramTotal 4)
            = caller theCaller
        )
        = endCel 
            (if (> param2 (= clientLastCel (send client:lastCel())))
                clientLastCel
            )(else
                param2
            )
    )


    (method (doit)
        (var temp0[2])
        (send client:)
        (if ((== cycleCnt 0) and (== endCel (send client:cel)))
            (self:cycleDone())
        )
    )


    (method (cycleDone)
        = completed 1
        (if (caller)
            = global37 1
        )(else
            (self:motionCue())
        )
    )

)
(class End of CT
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
        endCel 0
    )

    (method (init param1 param2)
        (super:init(param1 (send param1:lastCel()) 1 
            (if (== paramTotal 2)
                param2
            )(else
                0
            )
))
    )

)
(class Beg of CT
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
        endCel 0
    )

    (method (init param1 param2)
        (super:init(param1 0 -1 
            (if (== paramTotal 2)
                param2
            )(else
                0
            )
))
    )

)
(class SyncWalk of Fwd
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
        xLast 0
        yLast 0
    )

    (method (doit)
        (var clientMover)
        (if (IsObject((= clientMover (send client:mover))) and ((<> (send client:x) xLast) or (<> (send client:y) yLast)))
            = xLast (send client:x)
            = yLast (send client:y)
            (super:doit())
        )
    )


    (method (nextCel)
        = cycleCnt (send client:cycleSpeed)
        (super:nextCel())
    )

)
(class Motion of Obj
    (properties
        client 0
        caller 0
        x 0
        y 0
        dx 0
        dy 0
        {b-moveCnt} 0
        {b-i1} 0
        {b-i2} 0
        {b-di} 0
        {b-xAxis} 0
        {b-incr} 0
        completed 0
        xLast 0
        yLast 0
    )

    (method (init theClient theX theY theCaller)
        (var temp0[2], clientX, clientCycler)
        (if (>= paramTotal 1)
            = client theClient
            (if (>= paramTotal 2)
                = x theX
                (if (>= paramTotal 3)
                    = y theY
                    (if (>= paramTotal 4)
                        = caller theCaller
                    )
                )
            )
        )
        = yLast (= xLast (= {b-moveCnt} (= completed 0)))
        (if (= clientCycler (send client:cycler))
            (send (= clientCycler (send client:cycler)):cycleCnt(0))
        )
        (if (GetDistance(= clientX (send client:x) = clientCycler (send client:y) x y))
            (send client:setHeading(GetAngle(clientX clientCycler x y)))
        )
        InitBresen(self)
    )


    (method (doit)
        (var temp0[6])
        DoBresen(self)
    )


    (method (moveDone)
        = completed 1
        (if (caller)
            = global37 1
        )(else
            (self:motionCue())
        )
    )


    (method (setTarget theX theY)
        (if (paramTotal)
            = x theX
            = y theY
        )
    )


    (method (onTarget)
        return 
            (if (== (send client:x) x)
                == (send client:y) y
            )(else
                0
            )
    )


    (method (motionCue)
        (send client:mover(0))
        (if (completed and IsObject(caller))
            (send caller:cue())
        )
        (self:dispose())
    )

)
(class MoveTo of Motion
    (properties
        client 0
        caller 0
        x 0
        y 0
        dx 0
        dy 0
        {b-moveCnt} 0
        {b-i1} 0
        {b-i2} 0
        {b-di} 0
        {b-xAxis} 0
        {b-incr} 0
        completed 0
        xLast 0
        yLast 0
    )

    (method (init param1)
        (super:init(rest param1))
    )


    (method (onTarget)
        return 
            (if (<= Abs((- (send client:x) x)) (send client:xStep))
                <= Abs((- (send client:y) y)) (send client:yStep)
            )(else
                0
            )
    )

)
