// // Carrot Client.js // // Team 8 Hifi Hackathon 2018 // Chang Kayla Sam Lab // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // // get distance to carrot,...if near and triggered, trigger (function() { var _entityID; var _this; var AUDIO_VOLUME_LEVEL = 0.25; var TRIGGER_SOUND = SoundCache.getSound(Script.resolvePath("https://hifi-public.s3.amazonaws.com/sam/2018-oct/sounds/cartoon-squeak_fklEvdNd.mp3")); var TOOFAR_TRIGGER_SOUND = SoundCache.getSound(Script.resolvePath("https://hifi-public.s3.amazonaws.com/sam/2018-oct/sounds/click-blip_fyNxFdNd.mp3")); var WIN_TRIGGER_SOUND = SoundCache.getSound(Script.resolvePath("https://hifi-public.s3.amazonaws.com/sam/2018-oct/sounds/jg-032316-sfx-video-game-beeps-4.mp3")); var NOOP_TRIGGER_SOUND = SoundCache.getSound(Script.resolvePath("https://hifi-public.s3.amazonaws.com/sam/2018-oct/sounds/laser-charge-quick_My3a2IVd.mp3")); var LOST_TRIGGER_SOUND = SoundCache.getSound(Script.resolvePath("https://hifi-public.s3.amazonaws.com/sam/2018-oct/sounds/boing_f1YRp34O.mp3")); var MISS_TRIGGER_SOUND = SoundCache.getSound(Script.resolvePath("https://hifi-public.s3.amazonaws.com/sam/2018-oct/sounds/boing-spring-3_fyW6R2EO.mp3")); var GONG_TIMER_SOUND = SoundCache.getSound(Script.resolvePath("https://hifi-public.s3.amazonaws.com/sam/2018-oct/sounds/chinese-gong-crash_z1sgFyHO.mp3")); var FALL_ANIMATION = Script.resolvePath("animations/Fall.fbx"); var FALL_TIMEOUT_MS = 7000; var START_FRAME = 1; var FRAMES_PER_SECOND= 45; var END_FRAME = 150; var WIN_PARTICLEFX = { "lifetime": 15, "alpha": 0.44999998807907104, "alphaFinish": 0, "alphaStart": 1, "clientOnly": false, "color": { "blue": 27, "green": 223, "red": 245 }, "colorFinish": { "blue": 0, "green": 0, "red": 0, "x": 0, "y": 0, "z": 0 }, "colorStart": { "blue": 0, "green": 213, "red": 255, "x": 255, "y": 213, "z": 0 }, "created": "2018-10-26T20:08:25Z", "dimensions": { "blue": 26.269893646240234, "green": 26.269893646240234, "red": 26.269893646240234, "x": 26.269893646240234, "y": 26.269893646240234, "z": 26.269893646240234 }, "editionNumber": 35, "emitAcceleration": { "blue": 0, "green": 0.10000000149011612, "red": 0, "x": 0, "y": 0.10000000149011612, "z": 0 }, "emitDimensions": { "blue": 1.5, "green": 1.5, "red": 1.5, "x": 1.5, "y": 1.5, "z": 1.5 }, "emitOrientation": { "w": 1, "x": -1.52587890625e-05, "y": -1.52587890625e-05, "z": -1.52587890625e-05 }, "emitRate": 20, "emitSpeed": 0, "itemArtist": "alexia", "lifespan": 14.010000228881836, "maxParticles": 3619, "name": "Stars", "owningAvatarID": "{00000000-0000-0000-0000-000000000000}", "particleRadius": 0.5099999904632568, "polarFinish": 3.1415927410125732, "radiusFinish": 0, "radiusStart": 0, "rotation": { "w": 0.8073700666427612, "x": -7.62939453125e-05, "y": -0.5900511145591736, "z": -7.62939453125e-05 }, "speedSpread": 0, "spinFinish": null, "spinStart": null, "textures": "http://mpassets.highfidelity.com/90307382-86d3-46e8-8be7-1a8da20aa0f6-v1/star.png", "type": "ParticleEffect", "userData": "{\"grabbableKey\":{\"grabbable\":false}}" } var canTrigger = true; function Print(m) { if (true) { print(m) } } function Carrot() { _this = this; } Carrot.prototype = { remotelyCallable: ['triggerWin', 'triggerMiss'], preload: function(entityID) { // runs when this script is refreshed or a // client connects to a domain where this entity is present _this = this; _entityID = entityID; _this.entityID = entityID; _this.position = Entities.getEntityProperties(_this.entityID, 'position').position; _this.winner = JSON.parse(Entities.getEntityProperties(_this.entityID, 'userData').userData).winner; if (_this.winner == -1) { _this.resetPos = JSON.parse(Entities.getEntityProperties(_this.entityID, 'userData').userData).resetPos; } // Print(Entities.getEntityProperties(_this.entityID, 'userData').userData); _this.sessionID = MyAvatar.sessionUUID // Print("Carrot CLient Script loaded" + JSON.stringify(_this.position)) }, unload: function () { // triggered when avatar leaves the domain where entity is present // clear any intervals // clear any listeners // reset anything else that needs to be }, checkTriggerOk: function() { // we would like to check this on server side to avoid cheating... var distance = Vec3.distance(_this.position, MyAvatar.position); return (distance < 2.0); }, clickDownOnEntity: function (entityID, pointerEvent) {}, clickReleaseOnEntity: function (entityID, pointerEvent) {}, holdingClickOnEntity: function (entityID, pointerEvent) {}, mouseMoveOnEntity: function (entityID, pointerEvent) {}, mousePressOnEntity: function (entityID, event) { if (!canTrigger) { return; } if (!_this.checkTriggerOk()) { Print("Carrot Triggered pressed but TOO FAR") _this.playSound(TOOFAR_TRIGGER_SOUND, _this.position, true) return } if (canTrigger) { Print("Carrot Triggered pressed") Print("Sent message" + MyAvatar.sessionUUID) _this.playSound(TRIGGER_SOUND, _this.position, false) Entities.callEntityServerMethod(_this.entityID, 'triggerCarrot', [MyAvatar.sessionUUID]); canTrigger = false; } }, mouseReleaseOnEntity: function (entityID, event) {}, mouseDoublePressOffEntity: function (pointerEvent) {}, hoverEnterEntity: function (entityID, pointerEvent) {}, hoverLeaveEntity: function (entityID, pointerEvent) {}, hoverOverEntity: function (entityID, pointerEvent) {}, triggerWin: function() { Print("I triggered that carrot!!!"); if (_this.winner == 1) { var props = WIN_PARTICLEFX props.position = _this.position Entities.addEntity(WIN_PARTICLEFX) Print("THis is the THe Golden Carrot!!!!!!!!!!!!!!!!!!!!!!!!"); _this.position = Vec3.sum(_this.position, {x: 0, y: 1.0, z: 0}) Entities.editEntity(_this.entityID, { position: _this.position, angularVelocity: {x: 1.3, y: 2, z: 1}, dimensions: {x: 1, y: 2, z: 1}, modelURL: "https://hifi-public.s3.amazonaws.com/sam/2018-oct/models/carrot%20gold%20winner.fbx" } ) _this.playSound(WIN_TRIGGER_SOUND, _this.position) } else if (_this.winner == -1) { Print("THis a Losing Carrot ....... "); _this.position = Vec3.sum(_this.position, {x: 0, y: 0.7, z: 0}) Entities.editEntity(_this.entityID, { position: _this.position, dynamic:true, velocity:{"x":0,"y":1,"z":0}, gravity:{"x":0,"y":-9,"z":0}, angularVelocity: {x: 0.3, y: 0.8, z: 1}, modelURL: "https://hifi-public.s3.amazonaws.com/sam/2018-oct/models/beet-fbx.fbx" } ) _this.playSound(LOST_TRIGGER_SOUND, _this.position) MyAvatar.position = _this.resetPos // MyAvatar.overrideAnimation(FALL_ANIMATION, FRAMES_PER_SECOND, 0, START_FRAME, END_FRAME); // Script.setTimeout(function() { // MyAvatar.restoreAnimation(); // }, FALL_TIMEOUT_MS); } else { _this.position = Vec3.sum(_this.position, {x: 0, y: 0.5, z: 0}) Entities.editEntity(_this.entityID, { position: _this.position, dynamic:true, velocity:{"x":0,"y":0.8,"z":0}, gravity:{"x":0,"y":-9,"z":0}, } ) _this.playSound(NOOP_TRIGGER_SOUND, _this.position) } }, triggerMiss: function() { Print("I MISSED that carrot :("); _this.playSound(MISS_TRIGGER_SOUND, _this.position) }, playSound: function(sound, position, local) { if (sound.downloaded) { Audio.playSound(sound, { position: position, localOnly: (local === undefined) ? false : local, volume: AUDIO_VOLUME_LEVEL }); } }, /* fallDown: function(entityID, mouseEvent) { MyAvatar.overrideAnimation(FALL_ANIMATION, FRAMES_PER_SECOND, 0, START_FRAME, END_FRAME); Script.setTimeout(function() { MyAvatar.restoreAnimation(); }, FALL_TIMEOUT_MS); },*/ }; return new Carrot(); });