(version 2)
(include "sci.sh")
(use "Main")
(use "Osc")
(use "Obj")
(script 12)



(class Swing of Osc
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
        howManyCycles -1
        swingSpeed 1
        countSwings 0
        startFull 0
        startSwingCel 0
        stopSwingCel 0
        cycleSound 0
    )

    (method (init param1 theSwingSpeed theStartFull theCycleSound param5)
        (var theStopSwingCel)
        = cycleSound theCycleSound
        = theStopSwingCel (send param1:lastCel())
        (if (>= paramTotal 4)
            = swingSpeed theSwingSpeed
            (if (>= paramTotal 5)
                = startFull theStartFull
            )
        )
        (if (startFull)
            = startSwingCel 0
            = stopSwingCel theStopSwingCel
        )(else
            = startSwingCel (- (/ theStopSwingCel 2) 1)
            = stopSwingCel (/ theStopSwingCel 2)
        )
        (send param1:cel(startSwingCel))
        = countSwings swingSpeed
        (super:init(param1 rest param5))
    )


    (method (doit)
        (var swingNextCel)
        = swingNextCel (self:nextCel())
        (if (<> (send client:cel) swingNextCel)
            (if (startFull and ((== swingNextCel stopSwingCel) or (== swingNextCel startSwingCel)))
                (if (== countSwings 0)
                    = countSwings swingSpeed
                    (if (<> startSwingCel (/ (send client:lastCel()) 2))
                        ++startSwingCel
                    )
                    (if (<> stopSwingCel (/ (send client:lastCel()) 2))
                        --stopSwingCel
                    )
                )(else
                    --countSwings
                )
                = cycleDir neg cycleDir
                (if (<> cycleSound 0)
                    (swingSound:
                        number(cycleSound)
                        play()
                    )
                )
                (send client:cel(
                    (if ((== swingNextCel (- startSwingCel 1)) or (== swingNextCel startSwingCel))
                        startSwingCel
                    )(else
                        stopSwingCel
                    )
))
            )(else
                (if ((> swingNextCel stopSwingCel) or (< swingNextCel startSwingCel))
                    (if (proc999_5(startSwingCel stopSwingCel (- stopSwingCel 1)) and (> howManyCycles 0))
                        --howManyCycles
                    )
                    (if (== countSwings 0)
                        = countSwings swingSpeed
                        (if (> startSwingCel 0)
                            --startSwingCel
                        )
                        (if (< stopSwingCel (send client:lastCel()))
                            ++stopSwingCel
                        )
                    )(else
                        --countSwings
                    )
                    = cycleDir neg cycleDir
                    (self:cycleDone())
                )(else
                    (send client:cel(swingNextCel))
                )
            )
        )
    )


    (method (dispose)
        (swingSound:dispose())
        (super:dispose())
    )

)
(instance swingSound of FPSound
    (properties
        flags $0001
    )
)
