; CommandBehaviour Example
; ------------------------

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

; The Switchyard: a gate that a plate opens, and a lamp a timer shows
world=LoadWorldAsync("../../../samples/behaviours/switchyard.world")
While Not WorldReady(world)
    If WorldLoadState(world)=WORLD_LOAD_FAILED Then RuntimeError WorldLoadError(world)
    Cls
    Text 0,0,"Loading the switchyard..."
    Flip
Wend

SetWorld world
camera=FindWorldEntityByName(world,"Camera")
PositionEntity camera,0,3,-4
PointEntity camera,FindWorldEntity(world,"gate")
gate=FindWorldEntity(world,"gate")
lamp=FindWorldEntity(world,"lamp")

While Not KeyDown(1)

    ; O opens the gate, C closes it, T toggles it; R resets the lamp's timer
    If KeyHit(24) Then CommandBehaviour gate,"door","open"
    If KeyHit(46) Then CommandBehaviour gate,"door","close"
    If KeyHit(20) Then CommandBehaviour gate,"door","toggle"
    If KeyHit(19) Then CommandBehaviour lamp,"timer","reset"
    If KeyHit(57) Then CommandBehaviour lamp,"timer","start"

    ; The commands take effect at the next UpdateWorld, which also steps
    ; the world's physics: a world with doors steps its own
    UpdateWorld
    RenderWorld

    Text 0,0,"O: open  C: close  T: toggle the gate   Space: start  R: reset the lamp's timer   Esc: exit"
    Text 0,20,"Gate: "+BehaviourState(gate,"door")+"   Lamp's timer: "+BehaviourState(lamp,"timer")

    Flip

Wend

FreeWorld world
End
