(include "sci.sh")
(use "Main")
(use "Class_255_0")
(use "Cycle")
(use "TheMenuBar")
(use "Obj")
(script 996)


(local
    local0[23]

)
(class User of Obj
    (properties
        alterEgo 0
        canInput 0
        controls 0
        echo 32
        prevDir 0
        prompt "Enter input"
        inputLineAddr ""
    )

    (method (doit)
        (var newEvent, newEventType, newEventMessage)
        (if ((send (= newEvent (Event:new())):type()))
            = gNewEvent newEvent
            = newEventType (send newEvent:type())
            MapKeyToDir(newEvent)
            (if (TheMenuBar)
                (TheMenuBar:handleEvent(newEvent))
            )
            GlobalToLocal(newEvent)
            (send gGame:handleEvent(newEvent))
            (if (not (send newEvent:claimed()))
                (switch ((send newEvent:type()))
                    (case 1
                        (if (controls and IsObject(alterEgo))
                            (send alterEgo:setMotion(MoveTo (send newEvent:x()) (send newEvent:y())))
                            = prevDir 0
                            (send newEvent:claimed(1))
                        )
                    )
                    (case 64
                        (if (controls and IsObject(alterEgo))
                            = newEventMessage (send newEvent:message())
                            (if (((== newEventType 4) and (== newEventMessage prevDir)) and IsObject((send alterEgo:mover())))
                                = newEventMessage 0
                            )
                            = prevDir 
                                (if (== newEventType 4)
                                    newEventMessage
                                )(else
                                    0
                                )
                            (send alterEgo:setDirection(newEventMessage))
                            (send newEvent:claimed(1))
                        )
                    )
                    (default 
                        (send gCast:eachElementDo(232 newEvent))
                    )
                )
            )
            (if (((== (send newEvent:type()) 4) and not (send newEvent:claimed())) and ((== (send newEvent:message()) echo) or ((<= 32 (send newEvent:message())) and (<= (send newEvent:message()) 127))))
                (self:getInput(newEvent))
            )
        )
        (send newEvent:dispose())
        = gNewEvent 0
    )


    (method (canControl theControls)
        (if (paramTotal)
            = controls theControls
            = prevDir 0
        )
        return controls
    )


    (method (getInput param1)
        (if (not canInput)
            return 
        )
        (if (<> (send param1:message()) echo)
            Format(@local0 996 0 (send param1:message()))
        )
        (if (proc255_2(@local0 45 prompt) and Parse(@local0 param1))
            (send param1:type(128))
            = inputLineAddr @local0
            (self:said(param1))
        )
    )


    (method (said param1)
        (if (TheMenuBar)
            (TheMenuBar:handleEvent(param1))
        )
        (send gCast:eachElementDo(232 param1))
        (send gGame:handleEvent(param1))
        (if (not (send param1:claimed()))
            (send gGame:pragmaFail(@local0))
        )
    )

)
