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