This commit is contained in:
James B. Pollack 2016-02-25 12:32:06 -08:00
parent 643365bc5a
commit f62777af96
2 changed files with 34 additions and 48 deletions

View file

@ -5,6 +5,7 @@
// Created by James B. Pollack @imgntn on 2/15/2016 // Created by James B. Pollack @imgntn on 2/15/2016
// Copyright 2016 High Fidelity, Inc. // Copyright 2016 High Fidelity, Inc.
// //
// This script creates a maze with a ball that you can tilt to try to get to the end!
// //
// Distributed under the Apache License, Version 2.0. // Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
@ -28,31 +29,31 @@ var BALL_DIMENSIONS = Vec3.multiply(SCALE, {
x: 0.035, x: 0.035,
y: 0.035, y: 0.035,
z: 0.035 z: 0.035
}) });
var BALL_SPAWNER_DIMENSIONS = Vec3.multiply(SCALE, { var BALL_SPAWNER_DIMENSIONS = Vec3.multiply(SCALE, {
x: 0.05, x: 0.05,
y: 0.05, y: 0.05,
z: 0.05 z: 0.05
}) });
var BALL_DETECTOR_DIMENSIONS = Vec3.multiply(SCALE, { var BALL_DETECTOR_DIMENSIONS = Vec3.multiply(SCALE, {
x: 0.1, x: 0.1,
y: 0.1, y: 0.1,
z: 0.1 z: 0.1
}) });
var BALL_COLOR = { var BALL_COLOR = {
red: 255, red: 255,
green: 0, green: 0,
blue: 0 blue: 0
} };
var DEBUG_COLOR = { var DEBUG_COLOR = {
red: 0, red: 0,
green: 255, green: 255,
blue: 0 blue: 0
} };
var center = Vec3.sum(Vec3.sum(MyAvatar.position, { var center = Vec3.sum(Vec3.sum(MyAvatar.position, {
x: 0, x: 0,
@ -116,7 +117,7 @@ var getBallStartLocation = function() {
finalOffset = Vec3.sum(finalOffset, Vec3.multiply(front, BALL_FORWARD_OFFSET)); finalOffset = Vec3.sum(finalOffset, Vec3.multiply(front, BALL_FORWARD_OFFSET));
var location = Vec3.sum(mazeProps.position, finalOffset); var location = Vec3.sum(mazeProps.position, finalOffset);
return location; return location;
} };
var getBallFinishLocation = function() { var getBallFinishLocation = function() {
var mazeProps = Entities.getEntityProperties(tiltMaze); var mazeProps = Entities.getEntityProperties(tiltMaze);
@ -129,12 +130,10 @@ var getBallFinishLocation = function() {
position = Vec3.sum(position, Vec3.multiply(forward, DETECTOR_FORWARD_OFFSET)); position = Vec3.sum(position, Vec3.multiply(forward, DETECTOR_FORWARD_OFFSET));
return position; return position;
} };
var createBall = function(position) { var createBall = function(position) {
print('making ball')
var properties = { var properties = {
name: 'Hifi Tilt Maze Ball', name: 'Hifi Tilt Maze Ball',
type: 'Sphere', type: 'Sphere',
@ -174,7 +173,7 @@ var createBallSpawningAnchor = function() {
}; };
ballSpawningAnchor = Entities.addEntity(properties); ballSpawningAnchor = Entities.addEntity(properties);
} };
var createBallDetector = function() { var createBallDetector = function() {
@ -212,9 +211,9 @@ var createTiltMaze = function(position) {
script: MAZE_SCRIPT script: MAZE_SCRIPT
} }
tiltMaze = Entities.addEntity(properties) tiltMaze = Entities.addEntity(properties);
} };
var createLightAtTheEnd = function() { var createLightAtTheEnd = function() {
@ -223,7 +222,7 @@ var createLightAtTheEnd = function() {
var up = Quat.getUp(mazeProps.rotation); var up = Quat.getUp(mazeProps.rotation);
var down = Vec3.multiply(-1, up); var down = Vec3.multiply(-1, up);
var emitOrientation = Quat.rotationBetween(Vec3.UNIT_NEG_Z, down) var emitOrientation = Quat.rotationBetween(Vec3.UNIT_NEG_Z, down);
var position = getBallFinishLocation(); var position = getBallFinishLocation();
var lightProperties = { var lightProperties = {
@ -242,7 +241,7 @@ var createLightAtTheEnd = function() {
}; };
lightAtTheEnd = Entities.addEntity(lightProperties); lightAtTheEnd = Entities.addEntity(lightProperties);
} };
var createAll = function() { var createAll = function() {
createTiltMaze(center); createTiltMaze(center);
@ -260,7 +259,7 @@ var createAll = function() {
} }
}) })
}) })
} };
createAll(); createAll();

View file

@ -1,17 +1,16 @@
// //
// maze.js // maze.js
// //
// Script Type: Entity
// //
// Created by James B. Pollack @imgntn on 2/15/2016 // Created by James B. Pollack @imgntn on 2/15/2016
// Copyright 2016 High Fidelity, Inc. // Copyright 2016 High Fidelity, Inc.
// //
//
// This script resets a ball to its original position when the ball enters it. // This script resets a ball to its original position when the ball enters it.
// //
// Distributed under the Apache License, Version 2.0. // Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
(function() { (function() {
Script.include('../../../../libraries/utils.js'); Script.include('../../../../libraries/utils.js');
@ -41,13 +40,13 @@
x: 0.05, x: 0.05,
y: 0.05, y: 0.05,
z: 0.05 z: 0.05
}) });
var BALL_COLOR = { var BALL_COLOR = {
red: 255, red: 255,
green: 0, green: 0,
blue: 0 blue: 0
} };
var _this; var _this;
@ -97,7 +96,7 @@
if (this.ballLocked === true) { if (this.ballLocked === true) {
return; return;
} }
// print('making ball')
var properties = { var properties = {
name: 'Hifi Tilt Maze Ball', name: 'Hifi Tilt Maze Ball',
type: 'Sphere', type: 'Sphere',
@ -112,18 +111,17 @@
density: BALL_DENSITY, density: BALL_DENSITY,
color: BALL_COLOR, color: BALL_COLOR,
dimensions: BALL_DIMENSIONS dimensions: BALL_DIMENSIONS
}; };
this.ball = Entities.addEntity(properties); this.ball = Entities.addEntity(properties);
}, },
destroyBall: function() { destroyBall: function() {
var res = Entities.findEntities(MyAvatar.position, 10); var results = Entities.findEntities(MyAvatar.position, 10);
res.forEach(function(r) { results.forEach(function(result) {
var props = Entities.getEntityProperties(r, ['name']); var props = Entities.getEntityProperties(result, ['name']);
var isAMazeBall = props.name.indexOf('Maze Ball'); var isAMazeBall = props.name.indexOf('Maze Ball');
if (isAMazeBall > -1 && r === _this.ball) { if (isAMazeBall > -1 && result === _this.ball) {
Entities.deleteEntity(r); Entities.deleteEntity(result);
} }
}) })
}, },
@ -131,40 +129,34 @@
if (this.ballLocked === true) { if (this.ballLocked === true) {
return; return;
} }
// print('test ball distance')
var userData = Entities.getEntityProperties(this.entityID, 'userData').userData; var userData = Entities.getEntityProperties(this.entityID, 'userData').userData;
var data = null; var data = null;
try { try {
data = JSON.parse(userData) data = JSON.parse(userData);
} catch (e) { } catch (e) {
print('error parsing json in maze userdata') // print('error parsing json in maze userdata')
} }
if (data === null) { if (data === null) {
print('data is null in userData') // print('data is null in userData')
return; return;
} }
// print('DATA IS:'+JSON.stringify(data))
var ballPosition; var ballPosition;
if (this.ball === null) { if (this.ball === null) {
this.ball = data.tiltMaze.firstBall; this.ball = data.tiltMaze.firstBall;
ballPosition = Entities.getEntityProperties(data.tiltMaze.firstBall, 'position').position; ballPosition = Entities.getEntityProperties(data.tiltMaze.firstBall, 'position').position;
} else { } else {
ballPosition = Entities.getEntityProperties(this.ball, 'position').position ballPosition = Entities.getEntityProperties(this.ball, 'position').position;
} }
var ballSpawnerPosition = Entities.getEntityProperties(data.tiltMaze.ballSpawner, 'position').position; var ballSpawnerPosition = Entities.getEntityProperties(data.tiltMaze.ballSpawner, 'position').position;
var separation = Vec3.distance(ballPosition, ballSpawnerPosition); var separation = Vec3.distance(ballPosition, ballSpawnerPosition);
// print('separation from ball:' + separation)
if (separation > BALL_DISTANCE_THRESHOLD) { if (separation > BALL_DISTANCE_THRESHOLD) {
// print('BALL TOO FAR MAKE A NEW ONE')
this.destroyBall(); this.destroyBall();
this.createBall(); this.createBall();
} else {
} }
}, },
testWinDistance: function() { testWinDistance: function() {
@ -175,12 +167,12 @@
var userData = Entities.getEntityProperties(this.entityID, 'userData').userData; var userData = Entities.getEntityProperties(this.entityID, 'userData').userData;
var data = null; var data = null;
try { try {
data = JSON.parse(userData) data = JSON.parse(userData);
} catch (e) { } catch (e) {
print('error parsing json in maze userdata') // print('error parsing json in maze userdata')
} }
if (data === null) { if (data === null) {
print('data is null in userData') // print('data is null in userData')
return; return;
} }
@ -189,25 +181,20 @@
this.ball = data.tiltMaze.firstBall; this.ball = data.tiltMaze.firstBall;
ballPosition = Entities.getEntityProperties(data.tiltMaze.firstBall, 'position').position; ballPosition = Entities.getEntityProperties(data.tiltMaze.firstBall, 'position').position;
} else { } else {
ballPosition = Entities.getEntityProperties(this.ball, 'position').position ballPosition = Entities.getEntityProperties(this.ball, 'position').position;
} }
var ballDetectorPosition = Entities.getEntityProperties(data.tiltMaze.detector, 'position').position; var ballDetectorPosition = Entities.getEntityProperties(data.tiltMaze.detector, 'position').position;
var separation = Vec3.distance(ballPosition, ballDetectorPosition); var separation = Vec3.distance(ballPosition, ballDetectorPosition);
// print('separation from win:' + separation)
if (separation < BALL_DETECTOR_THRESHOLD) { if (separation < BALL_DETECTOR_THRESHOLD) {
// print('BALL IS IN DETECTOR, MAKE A NEW ONE')
this.ballLocked = true; this.ballLocked = true;
this.destroyBall(); this.destroyBall();
this.playVictorySound(); this.playVictorySound();
Script.setTimeout(function() { Script.setTimeout(function() {
_this.ballLocked = false; _this.ballLocked = false;
_this.createBall() _this.createBall();
}, 1500) }, 1500)
} else { }
}
}, },
playVictorySound: function() { playVictorySound: function() {
var position = Entities.getEntityProperties(this.entityID, "position").position; var position = Entities.getEntityProperties(this.entityID, "position").position;