script
Script {
directOutput TRUE
field SFNode boards USE BOARDS
field SFNode main USE MAIN_SCRIPT
field SFNode text USE SELECTION_TEXT
url "javascript:
function initialize() {
var children = boards.children;
for(i=0;i<children.length;i++) {
Browser.addRoute(children[i],'text_changed',
text,'set_string');
Browser.addRoute(children[i],'topTexture_set',
main,'topTexture_set');
Browser.addRoute(children[i],'baseTexture_set',
main,'baseTexture_set');
}
}
"
}
]
}
Group {
children [
Shape {
appearance Appearance {
material Material {
diffuseColor 1 1 1
emissiveColor 0.5 0.5 0.5
}
}
geometry DEF ELEVATION ElevationGrid {
normalPerVertex TRUE
solid TRUE
creaseAngle 1
xDimension 32
zDimension 200
xSpacing 1
zSpacing 1
height [ ]
}
}
DEF BOARDER Boarder {
board DEF BOARD Snowboard {
}
boot Boot {
textureUrl "local/skate/airwalk_freeride.gif"
}
}
ROUTE MAIN_SCRIPT.topTexture_changed TO BOARD.topTexture
ROUTE MAIN_SCRIPT.baseTexture_changed TO BOARD.baseTexture
ROUTE MAIN_SCRIPT.startGame TO BOARDER.bindViewpoint
DEF TIMER TimeSensor {
enabled FALSE
loop TRUE
cycleInterval 10
}
ROUTE MAIN_SCRIPT.startGame TO TIMER.enabled
DEF SCRIPT Script {
directOutput TRUE
eventIn SFTime tick
eventOut SFVec3f position_changed
eventOut SFRotation orientation_changed
field SFVec3f speed 0 0 0
field SFVec3f position 16 100 195
field SFRotation orientation 0 0 1 0
field SFTime oldTick 0
field SFRotation rotA 0 0 1 0
field SFRotation rotB 0 0 1 0
field SFVec3f defDirection 0 0 1
field SFVec3f direction 0 0 1
field SFVec3f yAxis 0 1 0
field SFVec3f up 0 1 0
field SFVec3f nose 0 0 0
field SFVec3f tail 0 0 0
field MFFloat height []
field SFNode elevation USE ELEVATION
field SFInt32 oldMask 0
field SFNode me USE SCRIPT
eventIn SFNode onEvent
field SFBool upk FALSE
field SFBool down FALSE
field SFBool left FALSE
field SFBool right FALSE
field SFBool space FALSE
field SFInt32 k_upk 1004 ##38
field SFInt32 k_down 1005 ##40
field SFInt32 k_left 1006 ##37
field SFInt32 k_right 1007 ##39
field SFInt32 k_space 32
url "javascript:
function connectKeyboard() {
// tell what events
oldMask = Browser.eventMask;
Browser.eventMask = ((1<<5) | (1<<6)); // KeyDown Only...
// add event observer
Browser.addRoute(Browser,'event_changed',me,'onEvent');
}
function disconnectKeyboard()
{
// remove event observer
Browser.deleteRoute(Browser,'event_changed',me,'onEvent');
Browser.eventMask = oldMask;
}
function onEvent(e,t) {
if (e.type == 'keydown') {
if(e.keyCode == k_left) {
left = TRUE;
e.returnValue = false;
} else if(e.keyCode == k_upk) {
upk = TRUE;
e.returnValue = false;
} else if(e.keyCode == k_right) {
right = TRUE;
e.returnValue = false;
} else if(e.keyCode == k_down) {
down = TRUE;
e.returnValue = false;
} else if(e.keyCode == k_space) {
space = TRUE;
e.returnValue = false;
}
} else if (e.type == 'keyup') {
if(e.keyCode == k_left) {
left = FALSE;
e.returnValue = false;
} else if(e.keyCode == k_upk) {
upk = FALSE;
e.returnValue = false;
} else if(e.keyCode == k_right) {
right = FALSE;
e.returnValue = false;
} else if(e.keyCode == k_down) {
down = FALSE;
e.returnValue = false;
} else if(e.keyCode == k_space) {
space = FALSE;
e.returnValue = false;
}
} else {
Browser.print('Unknown event : '+e.type);
}
}
function initialize() {
connectKeyboard();
var i = 0;
var j = 0;
var p = 0;
var y = 0;
for (i=0;i<200;i++) {
for (j=0;j<32;j++) {
height[p] = y + (Math.random()-0.5)/3;
p = p + 1;
}
y = y + 0.5;
if(i==50) y = y +3;
}
elevation.height = height;
}
function tick(v,t) {
if(oldTick == 0) {
oldTick = v;
}
while (oldTick < v) {
update();
oldTick = oldTick + 0.05;
}
}
function update() {
if(position[2] <= 0) return;
position[0] = position[0] + speed[0];
position[1] = position[1] + speed[1];
position[2] = position[2] + speed[2];
var h = getHeight(position[0],position[2]);
if(h>position[1]) {
position[1] = h;
speed[1] = 0;
}
position_changed = position;
var realNose = (orientation.multVec(new SFVec3f(0,0,-0.4))).add(position);
nose[0] = realNose[0];
nose[2] = realNose[2];
nose[1] = getHeight(nose[0],nose[2]);
var realTail = (orientation.multVec(new SFVec3f(0,0,0.4))).add(position);
tail[0] = realTail[0];
tail[2] = realTail[2];
tail[1] = getHeight(tail[0],tail[2]);
var dl = Math.sqrt(direction[0]*direction[0]+direction[2]*direction[2]);
var accel = 0.01 + (direction.dot(yAxis) * dl)/200;
if(realTail[1] < realNose[1]) accel = -accel;
speed[0] = speed[0]- (direction[0]*accel)/3;
speed[1] = speed[1] - 0.10; // gravity
speed[2] = speed[2]- (direction[2]*accel)/3;
//Browser.print('[spd] '+ speed);
// Do somphin with rotation only,
// and keep speed constant when jumpin...
// Also do 'fall' check when landing...
if(realNose[1]>(nose[1]+0.2)) {// 'flying', don't adjust orientation
direction = realTail.subtract(realNose);
} else {
direction = tail.subtract(nose);
}
if(left) {
var S = Math.sqrt(speed[0]*speed[0]+speed[2]*speed[2]) * 0.99;
direction = (new SFRotation(0,1,0,0.1)).multVec(direction);
var L = Math.sqrt(direction[0]*direction[0]+direction[2]*direction[2]);
if(Math.atan(speed.dot(direction)) > 0) { // reverse
speed[0] = S*(direction[0]/L);
speed[2] = S*(direction[2]/L);
} else {
speed[0] = -S*(direction[0]/L);
speed[2] = -S*(direction[2]/L);
}
}
if(right) {
var S = Math.sqrt(speed[0]*speed[0]+speed[2]*speed[2]) * 0.99;
direction = (new SFRotation(0,1,0,-0.1)).multVec(direction);
var L = Math.sqrt(direction[0]*direction[0]+direction[2]*direction[2]);
if(Math.atan(speed.dot(direction)) > 0) { // reverse
speed[0] = S*(direction[0]/L);
speed[2] = S*(direction[2]/L);
} else {
speed[0] = -S*(direction[0]/L);
speed[2] = -S*(direction[2]/L);
}
}
if(upk) {
direction = (new SFRotation(1,0,0,-0.2)).multVec(direction);
}
if(down) {
direction = (new SFRotation(1,0,0,0.2)).multVec(direction);
}
rotA = new SFRotation(defDirection,
direction);
up = rotA.multVec(yAxis);
rotB = new SFRotation(
direction.cross(up),
direction.cross(yAxis));
orientation = rotA.multiply(rotB);
orientation_changed = orientation;
}
function getHeight(x,z) {
if(x<0 || x>=31) return 0;
if(z<0 || z>=199) return 0;
var xfl = Math.floor(x);
var zfl = Math.floor(z);
var i = xfl + zfl*32;
var hxz = height[i];
var hx1z = height[i+1];
var hxz1 = height[i+32];
var hx1z1= height[i+33];
var hz0 = (hxz+((hx1z-hxz)*(x-xfl)));
var hz1 = (hxz1+((hx1z1-hxz1)*(x-xfl)));
var h = hz0 + ((hz1-hz0)*(z-zfl));
return h;
}
"
}
ROUTE TIMER.time_changed TO SCRIPT.tick
ROUTE SCRIPT.position_changed TO BOARDER.position
ROUTE SCRIPT.orientation_changed TO BOARDER.orientation
]
}
]
}
ROUTE MAIN_SCRIPT.setMainSwitch TO MAIN_SWITCH.set_whichChoice
ROUTE GO_BUTTON_TOUCH.touchTime TO MAIN_SCRIPT.goButtonPressed