Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Example script - opening/closing a door

tyoud1
Registered User
Quote
2015-10-25 09:10:42

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.
}
}
}



tyoud1
Registered User
Quote
2015-10-25 09:20:07

Not enough space to add this:

* BUG: race condition near the end of the open/close cycle. If you press E fast near the end, you can sometimes confuse the door. I have some commented out code "// UGH" which was my attempt to make it firmly start and stop the model at particular place when done.

* Question: is there an easier way to do this?

* Future features?

1) how to add a thing that checks range to a player's camera
1b) and maybe facing too, so that you have to be able to see the door to open it (not have it be behind you)

2) and show an icon, like of a little door opening, center screen when it would be willing to repsond to clicks

3) and play sounds, maybe one sound to open and another on close (default is that if blank, the close sound is the same as the open sound or something, maybe null able not sure).

4) keys - make it so that its openable if you have a key in inventory?
4b) or you can enter a combination value or something


adeb
Registered User
Quote
2015-10-25 11:02:03

this is easy way without script

you can test and download by link

http://adebgamesoft.be/ccb/openc...


adeb
Registered User
Quote
2015-10-25 11:05:13

but the door close automatically when you leave door area


kurtmel
Registered User
Quote
2016-02-17 16:19:12

wrote:
this is easy way without script

you can test and download by link

http://adebgamesoft.be/ccb/openc...


Adeb, your link is broken, any way to post your methods or code here in the forum instead? Thanks!!


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internationa?" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact