NickolasM.
12-13-2008, 05:10 AM
Theres a door in secondlife where you click a box and it makes the door move. The box and the door 'connect' the door isnt moving though.
// NOTE: these definitions should be the same as those from the lock
// object or the door will not actually work
integer gLockChannel = 9394835;
string gOpenMsg = "MSG_OPEN";
string gCloseMsg = "MSG_CLOSE";
// where we are when closed
vector gClosedPos;
rotation gClosedRot;
// internal timer for opening
float gOpenTimer;
// displacement when open
// NOTE: if you want it to open right, you can either rotate it 180 degrees
// around the Z axis or change this to <1.5, 0.0, 0.0>
vector gOpenDelta = <-1.5, 0.0, 0.0>;
// how long it takes to slide open
float gOpenTime = 1.6;
// how many steps along the way
float gOpenTimeStep = 0.2;
key gAdmin = "";
// sound we loop when opening/closing
string gSlideSound = "space_door";
float gSlideSoundVolume = 0.6;
//
// helper functions
//
func_debug(string str)
{
// llSay(0, str);
}
debug(string str)
{
// llSay(0, str);
}
say(string str)
{
llSay(0, str);
}
//
// states begin here
//
state_entry()
{
func_debug("default state_entry");
// no physics until we need to open/close
llSetStatus(STATUS_PHYSICS, FALSE);
// but set proper values here anyway
llSetBuoyancy(1.0);
// save position
gClosedPos = llGetPos();
gClosedRot = llGetRot();
gOpenTimer = 0.0;
gAdmin = llGetOwner();
// listen for open/close messages
llListen(gLockChannel, "", "", "");
// and go to closed state
state door_closed;
}
}
state door_closed
{
on_rez(integer arg) { llResetScript(); }
state_entry()
{
func_debug("door_closed state_entry");
// enforce proper location
llSetStatus(STATUS_PHYSICS, FALSE);
llSetPos(gClosedPos);
llSetRot(gClosedRot);
}
moving_end()
{
func_debug("moving_end");
// if we get moved, remember where we are
say("old pos: " + (string)gClosedPos);
gClosedPos = llGetPos();
gClosedRot = llGetRot();
say("new pos: " + (string)gClosedPos);
}
listen(integer channel, string name, key id, string msg)
{
if ( msg == gOpenMsg )
{
state door_opening;
}
}
}
state door_opening
{
on_rez(integer arg) { llResetScript(); }
state_entry()
{
func_debug("door_opening state_entry");
// start opening sound
llLoopSound(gSlideSound, gSlideSoundVolume);
// turn on physics
// doors should never rotate
llSetStatus(STATUS_ROTATE_X|STATUS_ROTATE_Y|STATUS_ROTATE_Z, FALSE);
llSetStatus(STATUS_PHYSICS, TRUE);
// start opening process
llResetTime();
gOpenTimer = 0.0;
llSetTimerEvent(gOpenTimeStep);
}
timer()
{
float time = llGetTime();
if ( time > gOpenTime + gOpenTimeStep)
{
// stop timer interrupts
llSetTimerEvent(0.0);
// door is now open
state door_open;
} else
{
// interpolate proper position
vector pos = gOpenDelta * (time / gOpenTime);
// orient properly
pos *= gClosedRot;
// move there
llMoveToTarget(gClosedPos + pos, gOpenTimeStep * 1.5);
}
}
state_exit()
{
llStopSound();
}
}
state door_open
{
on_rez(integer arg) { llResetScript(); }
state_entry()
{
func_debug("door_open state_entry");
// enforce proper location
llSetStatus(STATUS_PHYSICS, FALSE);
llSetPos(gClosedPos + (gOpenDelta * gClosedRot));
llSetRot(gClosedRot);
}
listen(integer channel, string name, key id, string msg)
{
if ( msg == gCloseMsg )
{
state door_closing;
}
}
}
state door_closing
{
on_rez(integer arg) { llResetScript(); }
state_entry()
{
func_debug("door_closing state_entry");
// audio
llLoopSound(gSlideSound, gSlideSoundVolume);
// turn on physics
// doors should never rotate
llSetStatus(STATUS_ROTATE_X|STATUS_ROTATE_Y|STATUS_ROTATE_Z, FALSE);
llSetStatus(STATUS_PHYSICS, TRUE);
// start opening process
llResetTime();
gOpenTimer = 0.0;
llSetTimerEvent(gOpenTimeStep);
}
timer()
{
float time = llGetTime();
if ( time > gOpenTime + gOpenTimeStep)
{
// stop timer interrupts
llSetTimerEvent(0.0);
// door is now open
state door_closed;
} else
{
// interpolate proper position
vector pos = gOpenDelta * (1.0 - (time / gOpenTime));
// orient properly
pos *= gClosedRot;
// move there
llMoveToTarget(gClosedPos + pos, gOpenTimeStep * 1.5);
}
}
state_exit()
{
// HACKISH: this is also done in the state_entry() of door_closed
// but the moving_end() callback gets called from normal door
// movement hanging over into the state. Evidently state_entry()
// doesn't have to finish before other callbacks happen, or else
// the llSetStatus() doesn't immediately stop physics-based motion
llSetStatus(STATUS_PHYSICS, FALSE);
llSetPos(gClosedPos);
llSetRot(gClosedRot);
// kill closing sound
llStopSound();
}
}
// NOTE: these definitions should be the same as those from the lock
// object or the door will not actually work
integer gLockChannel = 9394835;
string gOpenMsg = "MSG_OPEN";
string gCloseMsg = "MSG_CLOSE";
// where we are when closed
vector gClosedPos;
rotation gClosedRot;
// internal timer for opening
float gOpenTimer;
// displacement when open
// NOTE: if you want it to open right, you can either rotate it 180 degrees
// around the Z axis or change this to <1.5, 0.0, 0.0>
vector gOpenDelta = <-1.5, 0.0, 0.0>;
// how long it takes to slide open
float gOpenTime = 1.6;
// how many steps along the way
float gOpenTimeStep = 0.2;
key gAdmin = "";
// sound we loop when opening/closing
string gSlideSound = "space_door";
float gSlideSoundVolume = 0.6;
//
// helper functions
//
func_debug(string str)
{
// llSay(0, str);
}
debug(string str)
{
// llSay(0, str);
}
say(string str)
{
llSay(0, str);
}
//
// states begin here
//
state_entry()
{
func_debug("default state_entry");
// no physics until we need to open/close
llSetStatus(STATUS_PHYSICS, FALSE);
// but set proper values here anyway
llSetBuoyancy(1.0);
// save position
gClosedPos = llGetPos();
gClosedRot = llGetRot();
gOpenTimer = 0.0;
gAdmin = llGetOwner();
// listen for open/close messages
llListen(gLockChannel, "", "", "");
// and go to closed state
state door_closed;
}
}
state door_closed
{
on_rez(integer arg) { llResetScript(); }
state_entry()
{
func_debug("door_closed state_entry");
// enforce proper location
llSetStatus(STATUS_PHYSICS, FALSE);
llSetPos(gClosedPos);
llSetRot(gClosedRot);
}
moving_end()
{
func_debug("moving_end");
// if we get moved, remember where we are
say("old pos: " + (string)gClosedPos);
gClosedPos = llGetPos();
gClosedRot = llGetRot();
say("new pos: " + (string)gClosedPos);
}
listen(integer channel, string name, key id, string msg)
{
if ( msg == gOpenMsg )
{
state door_opening;
}
}
}
state door_opening
{
on_rez(integer arg) { llResetScript(); }
state_entry()
{
func_debug("door_opening state_entry");
// start opening sound
llLoopSound(gSlideSound, gSlideSoundVolume);
// turn on physics
// doors should never rotate
llSetStatus(STATUS_ROTATE_X|STATUS_ROTATE_Y|STATUS_ROTATE_Z, FALSE);
llSetStatus(STATUS_PHYSICS, TRUE);
// start opening process
llResetTime();
gOpenTimer = 0.0;
llSetTimerEvent(gOpenTimeStep);
}
timer()
{
float time = llGetTime();
if ( time > gOpenTime + gOpenTimeStep)
{
// stop timer interrupts
llSetTimerEvent(0.0);
// door is now open
state door_open;
} else
{
// interpolate proper position
vector pos = gOpenDelta * (time / gOpenTime);
// orient properly
pos *= gClosedRot;
// move there
llMoveToTarget(gClosedPos + pos, gOpenTimeStep * 1.5);
}
}
state_exit()
{
llStopSound();
}
}
state door_open
{
on_rez(integer arg) { llResetScript(); }
state_entry()
{
func_debug("door_open state_entry");
// enforce proper location
llSetStatus(STATUS_PHYSICS, FALSE);
llSetPos(gClosedPos + (gOpenDelta * gClosedRot));
llSetRot(gClosedRot);
}
listen(integer channel, string name, key id, string msg)
{
if ( msg == gCloseMsg )
{
state door_closing;
}
}
}
state door_closing
{
on_rez(integer arg) { llResetScript(); }
state_entry()
{
func_debug("door_closing state_entry");
// audio
llLoopSound(gSlideSound, gSlideSoundVolume);
// turn on physics
// doors should never rotate
llSetStatus(STATUS_ROTATE_X|STATUS_ROTATE_Y|STATUS_ROTATE_Z, FALSE);
llSetStatus(STATUS_PHYSICS, TRUE);
// start opening process
llResetTime();
gOpenTimer = 0.0;
llSetTimerEvent(gOpenTimeStep);
}
timer()
{
float time = llGetTime();
if ( time > gOpenTime + gOpenTimeStep)
{
// stop timer interrupts
llSetTimerEvent(0.0);
// door is now open
state door_closed;
} else
{
// interpolate proper position
vector pos = gOpenDelta * (1.0 - (time / gOpenTime));
// orient properly
pos *= gClosedRot;
// move there
llMoveToTarget(gClosedPos + pos, gOpenTimeStep * 1.5);
}
}
state_exit()
{
// HACKISH: this is also done in the state_entry() of door_closed
// but the moving_end() callback gets called from normal door
// movement hanging over into the state. Evidently state_entry()
// doesn't have to finish before other callbacks happen, or else
// the llSetStatus() doesn't immediately stop physics-based motion
llSetStatus(STATUS_PHYSICS, FALSE);
llSetPos(gClosedPos);
llSetRot(gClosedRot);
// kill closing sound
llStopSound();
}
}