Barn Door Script

Written by Kitsune
Barn Door Script
===========
Follow these steps to install...

1. Rez the "Door Axis" prim, size, and position along the "hinge-side" of the door.
2. Link the Door to the "Door Axis" by holding down the SHIFT key, select the door, and then finally select the "Door Axis" (Order of selection is important, the axis must be selected last).  
3. Select LINK from the Tools menu to link the two prims together.
4. Drag the two sounds and the "Door Script" onto the door.
5. Touch the door to have it open and touch again to have it close.  If you need to reverse the direction, move the door back to it's original position (by editing or by touching), open the "Door Script", and modify the "reversed" variable.

 

integer doorSteps = 4;
integer reversed  = FALSE;

rotateDoor(integer Open)
{
    rotation rot = llGetRot();
    rotation delta;
    integer  x;
    
    if (reversed) {Open = !Open;};

    if (Open)
    {
        delta = llEuler2Rot(<0, 0, PI/(doorSteps * 2)>);
    }
    else
    {
        delta = llEuler2Rot(<0, 0, -PI/(doorSteps * 2)>);
    }
    
    for (x = 0; x < doorSteps; x++)
    {
        rot = delta * rot;
        llSetRot(rot);
        llSleep(0.03125/doorSteps);
    }
}

default
{
    state_entry()
    {
        state closed;
    }
}

state closed
{
    
    touch_start(integer total_number)
    {
        llTriggerSound("dooropen", 0.5);
        rotateDoor(TRUE);
        state open;
    }
}

state open
{
    touch_start(integer num)
    {
        rotateDoor(FALSE);
        llTriggerSound("doorslam", 0.5);
        state closed;
    }
}