From 7a7b16f0a2e8fda5f38e1cbd34295c2983c7ce31 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 7 Oct 2015 18:16:58 -0700 Subject: [PATCH 1/6] wall targets --- .../toys/ping_pong_gun/createPingPongGun.js | 4 +- examples/toys/ping_pong_gun/createTargets.js | 154 ++++++++++++++++++ examples/toys/ping_pong_gun/pingPongGun.js | 8 +- examples/toys/ping_pong_gun/wallTarget.js | 49 ++++++ 4 files changed, 211 insertions(+), 4 deletions(-) create mode 100644 examples/toys/ping_pong_gun/createTargets.js create mode 100644 examples/toys/ping_pong_gun/wallTarget.js diff --git a/examples/toys/ping_pong_gun/createPingPongGun.js b/examples/toys/ping_pong_gun/createPingPongGun.js index cf56d6f790..cfeaba7f4e 100644 --- a/examples/toys/ping_pong_gun/createPingPongGun.js +++ b/examples/toys/ping_pong_gun/createPingPongGun.js @@ -13,8 +13,8 @@ Script.include("../../utilities.js"); var scriptURL = Script.resolvePath('pingPongGun.js'); -var MODEL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/ping_pong_gun.fbx?123' -var COLLISION_HULL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/ping_pong_gun_collision_hull.obj?123'; +var MODEL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/ping_pong_gun.fbx' +var COLLISION_HULL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/ping_pong_gun_collision_hull.obj'; var center = Vec3.sum(Vec3.sum(MyAvatar.position, { x: 0, diff --git a/examples/toys/ping_pong_gun/createTargets.js b/examples/toys/ping_pong_gun/createTargets.js new file mode 100644 index 0000000000..ffc008c7a2 --- /dev/null +++ b/examples/toys/ping_pong_gun/createTargets.js @@ -0,0 +1,154 @@ +// createTargets.js +// +// Script Type: Entity Spawner +// Created by James B. Pollack on 9/30/2015 +// Copyright 2015 High Fidelity, Inc. +// +// This script creates targets that fall down when you shoot them and then automatically reset to their original position. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +/*global MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */ +Script.include("../../utilities.js"); +Script.include("../../libraries/utils.js"); +var scriptURL = Script.resolvePath('wallTarget.js'); + +var MODEL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/target.fbx'; +var COLLISION_HULL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/target_collision_hull.obj'; + +var RESET_DISTANCE = 1; +var TARGET_USER_DATA_KEY = 'hifi-ping_pong_target'; +var NUMBER_OF_TARGETS = 6; +var TARGETS_PER_ROW = 3; + +var TARGET_DIMENSIONS = { + x: 0.03, + y: 0.21, + z: 0.21 +}; + +var VERTICAL_SPACING = 0.3; +var HORIZONTAL_SPACING = TARGET_DIMENSIONS.z + 0.25; +var center = Vec3.sum(Vec3.sum(MyAvatar.position, { + x: 0, + y: 0.5, + z: 0 +}), Vec3.multiply(3, Quat.getFront(Camera.getOrientation()))); + + +var startPosition = { + x: 548.68, + y: 497.30, + z: 509.74 +} + +// var rotation = Quat.fromPitchYawRollDegrees(0, -54, 0.0); + +// var startPosition = center; + +var targetIntervalClearer = Entities.addEntity({ + name: 'Target Interval Clearer - delete me to clear', + type: 'Box', + position: startPosition, + dimensions: { + x: 1, + y: 1, + z: 1 + }, + visible: false, + ignoreForCollisions: true, +}) +var targets = []; + +var originalPositions = []; + +function addTargets() { + var i; + var row = -1; + for (i = 0; i < NUMBER_OF_TARGETS; i++) { + if (i % TARGETS_PER_ROW === 0) { + row++; + } + + var zSpacing = (i % TARGETS_PER_ROW) * HORIZONTAL_SPACING + (row * HORIZONTAL_SPACING / 2); + var position = { + x: startPosition.x, + y: startPosition.y - (row * VERTICAL_SPACING), + z: startPosition.z - zSpacing + }; + + originalPositions.push(position); + var targetProperties = { + name: 'Target', + type: 'Model', + modelURL: MODEL_URL, + shapeType: 'compound', + collisionsWillMove: true, + dimensions: TARGET_DIMENSIONS, + compoundShapeURL: COLLISION_HULL_URL, + position: position, + // rotation:rotation, + script: scriptURL + }; + targets.push(Entities.addEntity(targetProperties)); + } +} + + +function testTargetDistanceFromStart() { + print('TEST TARGET DISTANCE FROM START') + var resetCount = 0; + targets.forEach(function(target, index) { + var currentPosition = Entities.getEntityProperties(target, "position").position; + var originalPosition = originalPositions[index]; + var distance = Vec3.subtract(originalPosition, currentPosition); + var length = Vec3.length(distance); + if (length > RESET_DISTANCE) { + print('SHOULD RESET THIS! at ' + originalPositions[index]) + Entities.deleteEntity(target); + var targetProperties = { + name: 'Target', + type: 'Model', + modelURL: MODEL_URL, + shapeType: 'compound', + collisionsWillMove: true, + dimensions: TARGET_DIMENSIONS, + compoundShapeURL: COLLISION_HULL_URL, + position: originalPositions[index], + // rotation:rotation, + script: scriptURL + }; + + targets[index] = Entities.addEntity(targetProperties); + } + }); +} + + +function deleteEntity(entityID) { + if (entityID === targetIntervalClearer) { + deleteTargets(); + Script.clearInterval(distanceCheckInterval); + Entities.deletingEntity.disconnect(deleteEntity); + } +} + +function deleteTargets() { + while (targets.length > 0) { + Entities.deleteEntity(targets.pop()); + } + Entities.deleteEntity(targetIntervalClearer); +} + +Entities.deletingEntity.connect(deleteEntity); +var distanceCheckInterval = Script.setInterval(testTargetDistanceFromStart, 1000); + +addTargets(); + +function atEnd() { + Script.clearInterval(distanceCheckInterval); + deleteTargets(); +} + +Script.scriptEnding.connect(atEnd); \ No newline at end of file diff --git a/examples/toys/ping_pong_gun/pingPongGun.js b/examples/toys/ping_pong_gun/pingPongGun.js index 1b5973c8bb..1e87eb76ce 100644 --- a/examples/toys/ping_pong_gun/pingPongGun.js +++ b/examples/toys/ping_pong_gun/pingPongGun.js @@ -14,6 +14,7 @@ Script.include("../../libraries/utils.js"); var SHOOTING_SOUND_URL = 'http://hifi-public.s3.amazonaws.com/sounds/ping_pong_gun/pong_sound.wav'; + var PING_PONG_BALL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/ping_pong_ball.fbx'; function PingPongGun() { return; @@ -23,7 +24,7 @@ var RELOAD_THRESHOLD = 0.95; var GUN_TIP_FWD_OFFSET =-0.35; var GUN_TIP_UP_OFFSET = 0.040; - var GUN_FORCE = 9; + var GUN_FORCE = 5; var BALL_RESTITUTION = 0.6; var BALL_LINEAR_DAMPING = 0.4; var BALL_GRAVITY = { @@ -115,7 +116,10 @@ forwardVec = Vec3.multiply(forwardVec, GUN_FORCE); var properties = { - type: 'Sphere', + // type: 'Model', + // modelURL:PING_PONG_BALL_URL, + shapeType:'sphere', + type:'Sphere', color: BALL_COLOR, dimensions: BALL_DIMENSIONS, linearDamping: BALL_LINEAR_DAMPING, diff --git a/examples/toys/ping_pong_gun/wallTarget.js b/examples/toys/ping_pong_gun/wallTarget.js new file mode 100644 index 0000000000..cca35e5d6c --- /dev/null +++ b/examples/toys/ping_pong_gun/wallTarget.js @@ -0,0 +1,49 @@ +// wallTarget.js +// +// Script Type: Entity +// Created by James B. Pollack @imgntn on 9/21/2015 +// Copyright 2015 High Fidelity, Inc. +// +// This script resets an object to its original position when it stops moving after a collision +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +/*global print, MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */ +(function() { + var TARGET_USER_DATA_KEY = 'hifi-ping_pong_target'; + var defaultTargetData = { + originalPosition: null + }; + + var _this; + function Target() { + _this=this; + return; + } + + Target.prototype = { + preload: function(entityID) { + this.entityID = entityID; + var targetData = getEntityCustomData(TARGET_USER_DATA_KEY, entityID, defaultTargetData); + this.originalPosition=targetData.originalPosition; + print('TARGET ORIGINAL POSITION:::'+targetData.originalPosition.x); + }, + collisionWithEntity: function(me, otherEntity) { + Entities.editEntity(me, { + gravity: { + x: 0, + y: -9.8, + z: 0 + }, + velocity: { + x: 0, + y: -0.01, + z: 0 + } + }) + } + }; + + // entity scripts always need to return a newly constructed object of our type + return new Target(); +}); \ No newline at end of file From 6921e0f0222ca0c6b0e0d4fc98cfd72d4710bb0c Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Thu, 8 Oct 2015 11:38:02 -0700 Subject: [PATCH 2/6] Aligned targets on wall in toybox --- examples/toys/ping_pong_gun/createTargets.js | 53 ++++++++++---------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/examples/toys/ping_pong_gun/createTargets.js b/examples/toys/ping_pong_gun/createTargets.js index ffc008c7a2..65ca203599 100644 --- a/examples/toys/ping_pong_gun/createTargets.js +++ b/examples/toys/ping_pong_gun/createTargets.js @@ -29,7 +29,10 @@ var TARGET_DIMENSIONS = { }; var VERTICAL_SPACING = 0.3; -var HORIZONTAL_SPACING = TARGET_DIMENSIONS.z + 0.25; +var NUM_ROWS = 2; +var NUM_COLUMNS = 3; +var spacingVector = {x: 1.4, y: 0, z: -0.93}; +var HORIZONTAL_SPACING = Vec3.multiply(Vec3.normalize(spacingVector), 0.5); var center = Vec3.sum(Vec3.sum(MyAvatar.position, { x: 0, y: 0.5, @@ -64,34 +67,30 @@ var targets = []; var originalPositions = []; function addTargets() { - var i; + var i, rowIndex, columnIndex; var row = -1; - for (i = 0; i < NUMBER_OF_TARGETS; i++) { - if (i % TARGETS_PER_ROW === 0) { - row++; + var rotation = Quat.fromPitchYawRollDegrees(-80, -48, -11); + for (rowIndex = 0; rowIndex < NUM_ROWS; rowIndex++) { + for (columnIndex = 0; columnIndex < NUM_COLUMNS; columnIndex++) { + + var position = Vec3.sum(startPosition, Vec3.multiply(HORIZONTAL_SPACING, columnIndex)); + + originalPositions.push(position); + var targetProperties = { + name: 'Target', + type: 'Model', + modelURL: MODEL_URL, + shapeType: 'compound', + collisionsWillMove: true, + dimensions: TARGET_DIMENSIONS, + compoundShapeURL: COLLISION_HULL_URL, + position: position, + rotation:rotation, + script: scriptURL + }; + targets.push(Entities.addEntity(targetProperties)); } - - var zSpacing = (i % TARGETS_PER_ROW) * HORIZONTAL_SPACING + (row * HORIZONTAL_SPACING / 2); - var position = { - x: startPosition.x, - y: startPosition.y - (row * VERTICAL_SPACING), - z: startPosition.z - zSpacing - }; - - originalPositions.push(position); - var targetProperties = { - name: 'Target', - type: 'Model', - modelURL: MODEL_URL, - shapeType: 'compound', - collisionsWillMove: true, - dimensions: TARGET_DIMENSIONS, - compoundShapeURL: COLLISION_HULL_URL, - position: position, - // rotation:rotation, - script: scriptURL - }; - targets.push(Entities.addEntity(targetProperties)); + startPosition = Vec3.sum(startPosition, {x: 0, y: VERTICAL_SPACING, z: 0}); } } From 62728aa507c8d5378f5f47d18952e86b487eb4de Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 8 Oct 2015 16:16:11 -0700 Subject: [PATCH 3/6] alignments --- examples/toys/ping_pong_gun/createTargets.js | 79 ++++++++++---------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/examples/toys/ping_pong_gun/createTargets.js b/examples/toys/ping_pong_gun/createTargets.js index 65ca203599..eb4eaf70e3 100644 --- a/examples/toys/ping_pong_gun/createTargets.js +++ b/examples/toys/ping_pong_gun/createTargets.js @@ -19,8 +19,8 @@ var COLLISION_HULL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_g var RESET_DISTANCE = 1; var TARGET_USER_DATA_KEY = 'hifi-ping_pong_target'; -var NUMBER_OF_TARGETS = 6; -var TARGETS_PER_ROW = 3; +var NUMBER_OF_TARGETS = 8; +var TARGETS_PER_ROW = 4; var TARGET_DIMENSIONS = { x: 0.03, @@ -28,16 +28,8 @@ var TARGET_DIMENSIONS = { z: 0.21 }; -var VERTICAL_SPACING = 0.3; -var NUM_ROWS = 2; -var NUM_COLUMNS = 3; -var spacingVector = {x: 1.4, y: 0, z: -0.93}; -var HORIZONTAL_SPACING = Vec3.multiply(Vec3.normalize(spacingVector), 0.5); -var center = Vec3.sum(Vec3.sum(MyAvatar.position, { - x: 0, - y: 0.5, - z: 0 -}), Vec3.multiply(3, Quat.getFront(Camera.getOrientation()))); +var VERTICAL_SPACING = 0.5; +var HORIZONTAL_SPACING = TARGET_DIMENSIONS.z + 0.5; var startPosition = { @@ -49,17 +41,21 @@ var startPosition = { // var rotation = Quat.fromPitchYawRollDegrees(0, -54, 0.0); // var startPosition = center; +var rotation = Quat.fromPitchYawRollDegrees(0,-55.25,0); var targetIntervalClearer = Entities.addEntity({ name: 'Target Interval Clearer - delete me to clear', type: 'Box', position: startPosition, - dimensions: { - x: 1, - y: 1, - z: 1 + dimensions: TARGET_DIMENSIONS, + color: { + red: 0, + green: 255, + blue: 0 }, + rotation:rotation, visible: false, + collisionsWillMove: false, ignoreForCollisions: true, }) var targets = []; @@ -67,34 +63,37 @@ var targets = []; var originalPositions = []; function addTargets() { - var i, rowIndex, columnIndex; + var i; var row = -1; - var rotation = Quat.fromPitchYawRollDegrees(-80, -48, -11); - for (rowIndex = 0; rowIndex < NUM_ROWS; rowIndex++) { - for (columnIndex = 0; columnIndex < NUM_COLUMNS; columnIndex++) { - - var position = Vec3.sum(startPosition, Vec3.multiply(HORIZONTAL_SPACING, columnIndex)); - - originalPositions.push(position); - var targetProperties = { - name: 'Target', - type: 'Model', - modelURL: MODEL_URL, - shapeType: 'compound', - collisionsWillMove: true, - dimensions: TARGET_DIMENSIONS, - compoundShapeURL: COLLISION_HULL_URL, - position: position, - rotation:rotation, - script: scriptURL - }; - targets.push(Entities.addEntity(targetProperties)); + for (i = 0; i < NUMBER_OF_TARGETS; i++) { + if (i % TARGETS_PER_ROW === 0) { + row++; } - startPosition = Vec3.sum(startPosition, {x: 0, y: VERTICAL_SPACING, z: 0}); + + var vHat = Quat.getFront(rotation); + var spacer = HORIZONTAL_SPACING * (i % TARGETS_PER_ROW)+(row * HORIZONTAL_SPACING / 2); + var multiplier = Vec3.multiply(spacer, vHat); + var position = Vec3.sum(startPosition, multiplier); + position.y = startPosition.y-(row*VERTICAL_SPACING); + + print('position::: ' + JSON.stringify(position)); + originalPositions.push(position); + var targetProperties = { + name: 'Target', + type: 'Model', + modelURL: MODEL_URL, + shapeType: 'compound', + collisionsWillMove: true, + dimensions: TARGET_DIMENSIONS, + compoundShapeURL: COLLISION_HULL_URL, + position: position, + rotation: rotation, + script: scriptURL + }; + targets.push(Entities.addEntity(targetProperties)); } } - function testTargetDistanceFromStart() { print('TEST TARGET DISTANCE FROM START') var resetCount = 0; @@ -115,7 +114,7 @@ function testTargetDistanceFromStart() { dimensions: TARGET_DIMENSIONS, compoundShapeURL: COLLISION_HULL_URL, position: originalPositions[index], - // rotation:rotation, + rotation: rotation, script: scriptURL }; From ad6fdc813f7013a4388188743178b9d31bce1af8 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 8 Oct 2015 17:50:41 -0700 Subject: [PATCH 4/6] alignment, targets, sounds, master script --- examples/toys/ping_pong_gun/createTargets.js | 25 ++-- examples/toys/ping_pong_gun/wallTarget.js | 28 ++-- unpublishedScripts/masterReset.js | 144 ++++++++++++++++++- 3 files changed, 170 insertions(+), 27 deletions(-) diff --git a/examples/toys/ping_pong_gun/createTargets.js b/examples/toys/ping_pong_gun/createTargets.js index eb4eaf70e3..9851451bc5 100644 --- a/examples/toys/ping_pong_gun/createTargets.js +++ b/examples/toys/ping_pong_gun/createTargets.js @@ -19,16 +19,16 @@ var COLLISION_HULL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_g var RESET_DISTANCE = 1; var TARGET_USER_DATA_KEY = 'hifi-ping_pong_target'; -var NUMBER_OF_TARGETS = 8; -var TARGETS_PER_ROW = 4; +var NUMBER_OF_TARGETS = 6; +var TARGETS_PER_ROW = 3; var TARGET_DIMENSIONS = { - x: 0.03, - y: 0.21, - z: 0.21 + x: 0.06, + y: 0.42, + z: 0.42 }; -var VERTICAL_SPACING = 0.5; +var VERTICAL_SPACING =TARGET_DIMENSIONS.y+ 0.5; var HORIZONTAL_SPACING = TARGET_DIMENSIONS.z + 0.5; @@ -38,9 +38,6 @@ var startPosition = { z: 509.74 } -// var rotation = Quat.fromPitchYawRollDegrees(0, -54, 0.0); - -// var startPosition = center; var rotation = Quat.fromPitchYawRollDegrees(0,-55.25,0); var targetIntervalClearer = Entities.addEntity({ @@ -76,8 +73,8 @@ function addTargets() { var position = Vec3.sum(startPosition, multiplier); position.y = startPosition.y-(row*VERTICAL_SPACING); - print('position::: ' + JSON.stringify(position)); originalPositions.push(position); + var targetProperties = { name: 'Target', type: 'Model', @@ -90,21 +87,23 @@ function addTargets() { rotation: rotation, script: scriptURL }; + targets.push(Entities.addEntity(targetProperties)); } } function testTargetDistanceFromStart() { - print('TEST TARGET DISTANCE FROM START') - var resetCount = 0; targets.forEach(function(target, index) { + var currentPosition = Entities.getEntityProperties(target, "position").position; var originalPosition = originalPositions[index]; var distance = Vec3.subtract(originalPosition, currentPosition); var length = Vec3.length(distance); + if (length > RESET_DISTANCE) { - print('SHOULD RESET THIS! at ' + originalPositions[index]) + Entities.deleteEntity(target); + var targetProperties = { name: 'Target', type: 'Model', diff --git a/examples/toys/ping_pong_gun/wallTarget.js b/examples/toys/ping_pong_gun/wallTarget.js index cca35e5d6c..15b1b55362 100644 --- a/examples/toys/ping_pong_gun/wallTarget.js +++ b/examples/toys/ping_pong_gun/wallTarget.js @@ -10,25 +10,21 @@ // /*global print, MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */ (function() { - var TARGET_USER_DATA_KEY = 'hifi-ping_pong_target'; - var defaultTargetData = { - originalPosition: null - }; - var _this; + function Target() { - _this=this; return; } Target.prototype = { + hasPlayedSound: false, preload: function(entityID) { this.entityID = entityID; - var targetData = getEntityCustomData(TARGET_USER_DATA_KEY, entityID, defaultTargetData); - this.originalPosition=targetData.originalPosition; - print('TARGET ORIGINAL POSITION:::'+targetData.originalPosition.x); + var SOUND_URL = "http://hifi-public.s3.amazonaws.com/sounds/Clay_Pigeon_02.L.wav"; + this.hitSound = SoundCache.getSound(SOUND_URL); }, collisionWithEntity: function(me, otherEntity) { + var position = Entities.getEntityProperties(me, "position").position; Entities.editEntity(me, { gravity: { x: 0, @@ -40,7 +36,19 @@ y: -0.01, z: 0 } - }) + }); + + if (this.hasPlayedSound === false) { + print('PLAY SOUND!!!') + this.audioInjector = Audio.playSound(this.hitSound, { + position: position, + volume: 0.5 + }); + + this.hasPlayedSound = true; + + } + } }; diff --git a/unpublishedScripts/masterReset.js b/unpublishedScripts/masterReset.js index 89f9fd3e1e..f5b6d18744 100644 --- a/unpublishedScripts/masterReset.js +++ b/unpublishedScripts/masterReset.js @@ -21,10 +21,11 @@ var pingPongScriptURL = Script.resolvePath('../examples/toys/ping_pong_gun/pingP var wandScriptURL = Script.resolvePath("../examples/toys/bubblewand/wand.js"); var dollScriptURL = Script.resolvePath("../examples/toys/doll/doll.js"); var lightsScriptURL = Script.resolvePath("../examples/toys/lightSwitch.js"); +var targetsScriptURL = Script.resolvePath('../examples/toys/ping_pong_gun/wallTarget.js'); -MasterReset = function () { +MasterReset = function() { var resetKey = "resetMe"; var GRABBABLE_DATA_KEY = "grabbableKey"; @@ -109,12 +110,14 @@ MasterReset = function () { z: 503.91 }); + createTargets(); + } function deleteAllToys() { var entities = Entities.findEntities(MyAvatar.position, 100); - entities.forEach(function (entity) { + entities.forEach(function(entity) { //params: customKey, id, defaultValue var shouldReset = getEntityCustomData(resetKey, entity, {}).resetMe; if (shouldReset === true) { @@ -299,14 +302,14 @@ MasterReset = function () { function testBallDistanceFromStart() { var resetCount = 0; - collidingBalls.forEach(function (ball, index) { + collidingBalls.forEach(function(ball, index) { var currentPosition = Entities.getEntityProperties(ball, "position").position; var originalPosition = originalBallPositions[index]; var distance = Vec3.subtract(originalPosition, currentPosition); var length = Vec3.length(distance); if (length > RESET_DISTANCE) { - Script.setTimeout(function () { + Script.setTimeout(function() { var newPosition = Entities.getEntityProperties(ball, "position").position; var moving = Vec3.length(Vec3.subtract(currentPosition, newPosition)); if (moving < MINIMUM_MOVE_LENGTH) { @@ -341,6 +344,139 @@ MasterReset = function () { var distanceCheckInterval = Script.setInterval(testBallDistanceFromStart, 1000); } + function createTargets() { + + var MODEL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/target.fbx'; + var COLLISION_HULL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/target_collision_hull.obj'; + + var RESET_DISTANCE = 1; + var TARGET_USER_DATA_KEY = 'hifi-ping_pong_target'; + var NUMBER_OF_TARGETS = 6; + var TARGETS_PER_ROW = 3; + + var TARGET_DIMENSIONS = { + x: 0.06, + y: 0.42, + z: 0.42 + }; + + var VERTICAL_SPACING = TARGET_DIMENSIONS.y + 0.5; + var HORIZONTAL_SPACING = TARGET_DIMENSIONS.z + 0.5; + + + var startPosition = { + x: 548.68, + y: 497.30, + z: 509.74 + } + + var rotation = Quat.fromPitchYawRollDegrees(0, -55.25, 0); + + var targetIntervalClearer = Entities.addEntity({ + name: 'Target Interval Clearer - delete me to clear', + type: 'Box', + position: startPosition, + dimensions: TARGET_DIMENSIONS, + color: { + red: 0, + green: 255, + blue: 0 + }, + rotation: rotation, + visible: false, + collisionsWillMove: false, + ignoreForCollisions: true, + }) + var targets = []; + + var originalPositions = []; + + function addTargets() { + var i; + var row = -1; + for (i = 0; i < NUMBER_OF_TARGETS; i++) { + if (i % TARGETS_PER_ROW === 0) { + row++; + } + + var vHat = Quat.getFront(rotation); + var spacer = HORIZONTAL_SPACING * (i % TARGETS_PER_ROW) + (row * HORIZONTAL_SPACING / 2); + var multiplier = Vec3.multiply(spacer, vHat); + var position = Vec3.sum(startPosition, multiplier); + position.y = startPosition.y - (row * VERTICAL_SPACING); + + originalPositions.push(position); + + var targetProperties = { + name: 'Target', + type: 'Model', + modelURL: MODEL_URL, + shapeType: 'compound', + collisionsWillMove: true, + dimensions: TARGET_DIMENSIONS, + compoundShapeURL: COLLISION_HULL_URL, + position: position, + rotation: rotation, + script: targetsScriptURL + }; + + targets.push(Entities.addEntity(targetProperties)); + } + } + + function testTargetDistanceFromStart() { + targets.forEach(function(target, index) { + + var currentPosition = Entities.getEntityProperties(target, "position").position; + var originalPosition = originalPositions[index]; + var distance = Vec3.subtract(originalPosition, currentPosition); + var length = Vec3.length(distance); + + if (length > RESET_DISTANCE) { + + Entities.deleteEntity(target); + + var targetProperties = { + name: 'Target', + type: 'Model', + modelURL: MODEL_URL, + shapeType: 'compound', + collisionsWillMove: true, + dimensions: TARGET_DIMENSIONS, + compoundShapeURL: COLLISION_HULL_URL, + position: originalPositions[index], + rotation: rotation, + script: targetsScriptURL + }; + + targets[index] = Entities.addEntity(targetProperties); + } + }); + } + + + function deleteEntity(entityID) { + if (entityID === targetIntervalClearer) { + deleteTargets(); + Script.clearInterval(distanceCheckInterval); + Entities.deletingEntity.disconnect(deleteEntity); + } + } + + function deleteTargets() { + while (targets.length > 0) { + Entities.deleteEntity(targets.pop()); + } + Entities.deleteEntity(targetIntervalClearer); + } + + Entities.deletingEntity.connect(deleteEntity); + var distanceCheckInterval = Script.setInterval(testTargetDistanceFromStart, 1000); + + addTargets(); + + } + function createCat(position) { var modelURL = "http://hifi-public.s3.amazonaws.com/ryan/Dark_Cat.fbx"; From 772ea16c71fd042fcd371f14d3647f36698a1ca9 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 8 Oct 2015 17:52:33 -0700 Subject: [PATCH 5/6] cleanup --- examples/toys/ping_pong_gun/createTargets.js | 19 +++++++++++-------- unpublishedScripts/masterReset.js | 8 +++++--- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/examples/toys/ping_pong_gun/createTargets.js b/examples/toys/ping_pong_gun/createTargets.js index 9851451bc5..22329f90f0 100644 --- a/examples/toys/ping_pong_gun/createTargets.js +++ b/examples/toys/ping_pong_gun/createTargets.js @@ -28,7 +28,7 @@ var TARGET_DIMENSIONS = { z: 0.42 }; -var VERTICAL_SPACING =TARGET_DIMENSIONS.y+ 0.5; +var VERTICAL_SPACING = TARGET_DIMENSIONS.y + 0.5; var HORIZONTAL_SPACING = TARGET_DIMENSIONS.z + 0.5; @@ -36,9 +36,9 @@ var startPosition = { x: 548.68, y: 497.30, z: 509.74 -} +}; -var rotation = Quat.fromPitchYawRollDegrees(0,-55.25,0); +var rotation = Quat.fromPitchYawRollDegrees(0, -55.25, 0); var targetIntervalClearer = Entities.addEntity({ name: 'Target Interval Clearer - delete me to clear', @@ -50,11 +50,12 @@ var targetIntervalClearer = Entities.addEntity({ green: 255, blue: 0 }, - rotation:rotation, + rotation: rotation, visible: false, collisionsWillMove: false, ignoreForCollisions: true, -}) +}); + var targets = []; var originalPositions = []; @@ -62,19 +63,21 @@ var originalPositions = []; function addTargets() { var i; var row = -1; + for (i = 0; i < NUMBER_OF_TARGETS; i++) { + if (i % TARGETS_PER_ROW === 0) { row++; } var vHat = Quat.getFront(rotation); - var spacer = HORIZONTAL_SPACING * (i % TARGETS_PER_ROW)+(row * HORIZONTAL_SPACING / 2); + var spacer = HORIZONTAL_SPACING * (i % TARGETS_PER_ROW) + (row * HORIZONTAL_SPACING / 2); var multiplier = Vec3.multiply(spacer, vHat); var position = Vec3.sum(startPosition, multiplier); - position.y = startPosition.y-(row*VERTICAL_SPACING); + position.y = startPosition.y - (row * VERTICAL_SPACING); originalPositions.push(position); - + var targetProperties = { name: 'Target', type: 'Model', diff --git a/unpublishedScripts/masterReset.js b/unpublishedScripts/masterReset.js index f5b6d18744..c7fcd69e30 100644 --- a/unpublishedScripts/masterReset.js +++ b/unpublishedScripts/masterReset.js @@ -368,7 +368,7 @@ MasterReset = function() { x: 548.68, y: 497.30, z: 509.74 - } + }; var rotation = Quat.fromPitchYawRollDegrees(0, -55.25, 0); @@ -386,7 +386,8 @@ MasterReset = function() { visible: false, collisionsWillMove: false, ignoreForCollisions: true, - }) + }); + var targets = []; var originalPositions = []; @@ -395,6 +396,7 @@ MasterReset = function() { var i; var row = -1; for (i = 0; i < NUMBER_OF_TARGETS; i++) { + if (i % TARGETS_PER_ROW === 0) { row++; } @@ -404,7 +406,7 @@ MasterReset = function() { var multiplier = Vec3.multiply(spacer, vHat); var position = Vec3.sum(startPosition, multiplier); position.y = startPosition.y - (row * VERTICAL_SPACING); - + originalPositions.push(position); var targetProperties = { From 1332b8bef699400431228c93edfbe43425599db0 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 8 Oct 2015 17:58:58 -0700 Subject: [PATCH 6/6] remove logging --- examples/toys/ping_pong_gun/wallTarget.js | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/toys/ping_pong_gun/wallTarget.js b/examples/toys/ping_pong_gun/wallTarget.js index 15b1b55362..26e8d320a8 100644 --- a/examples/toys/ping_pong_gun/wallTarget.js +++ b/examples/toys/ping_pong_gun/wallTarget.js @@ -39,7 +39,6 @@ }); if (this.hasPlayedSound === false) { - print('PLAY SOUND!!!') this.audioInjector = Audio.playSound(this.hitSound, { position: position, volume: 0.5