Script: assign to a door prefab. Opens/closes the door over time based on keypress (E by default).
/* <behavior jsname="behavior_door" description="Be a door that can open and close"> <property name="RotationOpen" type="float" default="110.0" /> <property name="Time" type="float" default="500" /> <property name="KeyOpenCloseDoor" type="int" default="69" /> </behavior> */
// script inspired by the CopperCube example script called 'behavior_moveBetweenPoints.js' // at the web page: // http://www.ambiera.com/coppercube/doc/index.html // // this script you can set on an object, like a door prefab, // and it will rotate the door out of the way // over a period of time in milliseconds // when triggered by a key press ('E' by default - no reason) // behavior_door = function() { this.LastTime = null; this.StartTime = null; this.StartRotation = null; this.EndRotation = null; this.State = 0; // 0 = start, closed // 1 = moving, opening // 2 = stopped, open // 3 = moving, closing // (back to start, 0) };
// called every frame. // 'node' is the scene node where this behavior is attached to. // 'timeMs' the current time in milliseconds of the scene. // Returns 'true' if something changed, and 'false' if not. behavior_door.prototype.onAnimate = function(node, timeMs) { // get the time since the last frame if (this.LastTime == null) { // we were never called before, so store the time and cancel this.LastTime = timeMs; this.StartRotation = ccbGetSceneNodeProperty(node, 'Rotation'); this.EndRotation = ccbGetSceneNodeProperty(node, 'Rotation') + this.RotationOpen; return false; } var delta = timeMs - this.LastTime; this.LastTime = timeMs; if (delta > 200) delta = 200; // never do rotations as if there was more than 200 ms of lag..(?) // rotate, maybe var rot = ccbGetSceneNodeProperty(node, 'Rotation');
// closed, but need the current time... if (this.State == 0) { this.StartTime = timeMs; } // opening? if (this.State == 1) { rot.y += ( this.RotationOpen * delta ) / this.Time; ccbSetSceneNodeProperty(node, 'Rotation', rot); if ( (this.StartTime + this.Time) < timeMs ) { // opened - so make sure we're in the END rotation // UGH ccbSetSceneNodeProperty(node, 'Rotation', this.EndRotation); // be done this.State = 2; // done, opened. } return true; }
// opened, but need the current time... if (this.State == 2) { this.StartTime = timeMs; } // closing?? if (this.State == 3) { rot.y -= (this.RotationOpen * delta ) / this.Time; ccbSetSceneNodeProperty(node, 'Rotation', rot); if ( (this.StartTime + this.Time) < timeMs ) { // closed - so make sure we're back to the Start rotation // UGH ccbSetSceneNodeProperty(node, 'Rotation', this.StartRotation); // be done this.State = 0; // done, closed, back to start. } return true; }
return false; }
// parameters: key: key id pressed or left up. // pressed: true if the key was pressed down, false if left up behavior_door.prototype.onKeyEvent = function(key, pressed) { if (this.KeyOpenCloseDoor == key) // configurable as a parameter, 32 = space , 69 is letter E { if (pressed) { // if we're in the middle of opening, leave it alone if (this.State == 1) { return; } // if we're in the middle of closing, leave it alone if (this.State == 3) { return; } if (this.State == 0) { this.State = 1; // start opening return; } if (this.State == 2) { this.State = 3; // start closing return; } // ignore keypresses in other states } else { // if keys weren't pressed, or were, whatever, ignore them. } } }
|