Showing the changes to scripts/tutorials/entity_scripts (2 of 3 files). Show all
Unified · Split
| @@ -14,93 +14,93 @@ · 13 unmodified lines | |||
| 14 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | 14 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| 15 | 15 | ||
| 16 | (function() { | 16 | (function() { |
| 17 | var _this; | 17 | var _this; |
| 18 | 18 | ||
| 19 | var SLOW_SPIN_THRESHOLD = 0.1; | 19 | var SLOW_SPIN_THRESHOLD = 0.1; |
| 20 | var ROTATION_COMPLETE_THRESHOLD = 0.01; | 20 | var ROTATION_COMPLETE_THRESHOLD = 0.01; |
| 21 | var ROTATION_SPEED = 0.2; | 21 | var ROTATION_SPEED = 0.2; |
| 22 | var HOME_ROTATION = {x: 0, y: 0, z: 0, w: 0}; | 22 | var HOME_ROTATION = {x: 0, y: 0, z: 0, w: 0}; |
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | floatingLantern = function() { | 25 | floatingLantern = function() { |
| 26 | _this = this; | 26 | _this = this; |
| 27 | this.updateConnected = false; | 27 | this.updateConnected = false; |
| 28 | }; | 28 | }; |
| 29 | 29 | ||
| 30 | floatingLantern.prototype = { | 30 | floatingLantern.prototype = { |
| 31 | 31 | ||
| 32 | preload: function(entityID) { | 32 | preload: function(entityID) { |
| 33 | this.entityID = entityID; | 33 | this.entityID = entityID; |
| 34 | }, | 34 | }, |
| 35 | 35 | ||
| 36 | unload: function(entityID) { | 36 | unload: function(entityID) { |
| 37 | this.disconnectUpdate(); | 37 | this.disconnectUpdate(); |
| 38 | }, | 38 | }, |
| 39 | 39 | ||
| 40 | startNearGrab: function() { | 40 | startNearGrab: function() { |
| 41 | this.disconnectUpdate(); | 41 | this.disconnectUpdate(); |
| 42 | }, | 42 | }, |
| 43 | |||
| 44 | startDistantGrab: function() { | ||
| 45 | this.disconnectUpdate(); | ||
| 46 | }, | ||
| 47 | |||
| 48 | releaseGrab: function() { | ||
| 49 | Entities.editEntity(this.entityID, { | ||
| 50 | gravity: { | ||
| 51 | x: 0, | ||
| 52 | y: 0.5, | ||
| 53 | z: 0 | ||
| 54 | } | ||
| 55 | }); | ||
| 56 | }, | ||
| 57 | |||
| 58 | update: function(dt) { | ||
| 59 | var lanternProps = Entities.getEntityProperties(_this.entityID); | ||
| 60 | |||
| 61 | if (lanternProps && lanternProps.rotation && lanternProps.owningAvatarID === MyAvatar.sessionUUID) { | ||
| 62 | |||
| 63 | var spinningSlowly = ( | ||
| 64 | Math.abs(lanternProps.angularVelocity.x) < SLOW_SPIN_THRESHOLD && | ||
| 65 | Math.abs(lanternProps.angularVelocity.y) < SLOW_SPIN_THRESHOLD && | ||
| 66 | Math.abs(lanternProps.angularVelocity.z) < SLOW_SPIN_THRESHOLD | ||
| 67 | ); | ||
| 68 | |||
| 69 | var rotationComplete = ( | ||
| 70 | Math.abs(lanternProps.rotation.x - HOME_ROTATION.x) < ROTATION_COMPLETE_THRESHOLD && | ||
| 71 | Math.abs(lanternProps.rotation.y - HOME_ROTATION.y) < ROTATION_COMPLETE_THRESHOLD && | ||
| 72 | Math.abs(lanternProps.rotation.z - HOME_ROTATION.z) < ROTATION_COMPLETE_THRESHOLD | ||
| 73 | ); | ||
| 74 | |||
| 75 | if (spinningSlowly && !rotationComplete) { | ||
| 76 | var newRotation = Quat.slerp(lanternProps.rotation, HOME_ROTATION, ROTATION_SPEED * dt); | ||
| 77 | |||
| 78 | Entities.editEntity(_this.entityID, { | ||
| 79 | rotation: newRotation, | ||
| 80 | angularVelocity: { | ||
| 81 | x: 0, | ||
| 82 | y: 0, | ||
| 83 | z: 0 | ||
| 84 | } | ||
| 85 | }); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | }, | ||
| 43 | 89 | ||
| 44 | startDistantGrab: function() { | 90 | connectUpdate: function() { |
| 45 | this.disconnectUpdate(); | 91 | if (!this.updateConnected) { |
| 46 | }, | 92 | this.updateConnected = true; |
| 93 | Script.update.connect(this.update); | ||
| 94 | } | ||
| 95 | }, | ||
| 47 | 96 | ||
| 48 | releaseGrab: function() { | 97 | disconnectUpdate: function() { |
| 49 | Entities.editEntity(this.entityID, { | 98 | if (this.updateConnected) { |
| 50 | gravity: { | 99 | this.updateConnected = false; |
| 51 | x: 0, | 100 | Script.update.disconnect(this.update); |
| 52 | y: 0.5, | ||
| 53 | z: 0 | ||
| 54 | } | ||
| 55 | }); | ||
| 56 | }, | ||
| 57 | |||
| 58 | update: function(dt) { | ||
| 59 | var lanternProps = Entities.getEntityProperties(_this.entityID); | ||
| 60 | |||
| 61 | if(lanternProps && lanternProps.rotation && lanternProps.owningAvatarID === MyAvatar.sessionUUID) { | ||
| 62 | |||
| 63 | var spinningSlowly = ( | ||
| 64 | Math.abs(lanternProps.angularVelocity.x) < SLOW_SPIN_THRESHOLD && | ||
| 65 | Math.abs(lanternProps.angularVelocity.y) < SLOW_SPIN_THRESHOLD && | ||
| 66 | Math.abs(lanternProps.angularVelocity.z) < SLOW_SPIN_THRESHOLD | ||
| 67 | ); | ||
| 68 | |||
| 69 | var rotationComplete = ( | ||
| 70 | Math.abs(lanternProps.rotation.x - HOME_ROTATION.x) < ROTATION_COMPLETE_THRESHOLD && | ||
| 71 | Math.abs(lanternProps.rotation.y - HOME_ROTATION.y) < ROTATION_COMPLETE_THRESHOLD && | ||
| 72 | Math.abs(lanternProps.rotation.z - HOME_ROTATION.z) < ROTATION_COMPLETE_THRESHOLD | ||
| 73 | ); | ||
| 74 | |||
| 75 | if(spinningSlowly && !rotationComplete) { | ||
| 76 | var newRotation = Quat.slerp(lanternProps.rotation, HOME_ROTATION, ROTATION_SPEED * dt); | ||
| 77 | |||
| 78 | Entities.editEntity(_this.entityID, { | ||
| 79 | rotation: newRotation, | ||
| 80 | angularVelocity: { | ||
| 81 | x: 0, | ||
| 82 | y: 0, | ||
| 83 | z: 0 | ||
| 84 | } | 101 | } |
| 85 | }); | ||
| 86 | } | 102 | } |
| 87 | } | 103 | }; |
| 88 | }, | ||
| 89 | |||
| 90 | connectUpdate: function() { | ||
| 91 | if(!this.updateConnected) { | ||
| 92 | this.updateConnected = true; | ||
| 93 | Script.update.connect(this.update); | ||
| 94 | } | ||
| 95 | }, | ||
| 96 | |||
| 97 | disconnectUpdate: function() { | ||
| 98 | if(this.updateConnected) { | ||
| 99 | this.updateConnected = false; | ||
| 100 | Script.update.disconnect(this.update); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | }; | ||
| 104 | 104 | ||
| 105 | return new floatingLantern(); | 105 | return new floatingLantern(); |
| 106 | }); | 106 | }); |
| @@ -21,6 +21,7 @@ · 20 unmodified lines | |||
| 21 | var LIFETIME = 120; | 21 | var LIFETIME = 120; |
| 22 | var RESPAWN_INTERVAL = 1000; | 22 | var RESPAWN_INTERVAL = 1000; |
| 23 | var MAX_LANTERNS = 4; | 23 | var MAX_LANTERNS = 4; |
| 24 | var SCALE_FACTOR = 1; | ||
| 24 | 25 | ||
| 25 | var LANTERN = { | 26 | var LANTERN = { |
| 26 | type: "Model", | 27 | type: "Model", |
| @@ -29,9 +30,9 @@ · 2 unmodified lines | |||
| 29 | modelURL: LANTERN_MODEL_URL, | 30 | modelURL: LANTERN_MODEL_URL, |
| 30 | script: LANTERN_SCRIPT_URL, | 31 | script: LANTERN_SCRIPT_URL, |
| 31 | dimensions: { | 32 | dimensions: { |
| 32 | x: 0.2049, | 33 | x: 0.2049 * SCALE_FACTOR, |
| 33 | y: 0.4, | 34 | y: 0.4 * SCALE_FACTOR, |
| 34 | z: 0.2049 | 35 | z: 0.2049 * SCALE_FACTOR |
| 35 | }, | 36 | }, |
| 36 | gravity: { | 37 | gravity: { |
| 37 | x: 0, | 38 | x: 0, |
| @@ -57,14 +58,15 @@ · 19 unmodified lines | |||
| 57 | this.entityID = entityID; | 58 | this.entityID = entityID; |
| 58 | var props = Entities.getEntityProperties(this.entityID); | 59 | var props = Entities.getEntityProperties(this.entityID); |
| 59 | 60 | ||
| 60 | if(props.owningAvatarID === MyAvatar.sessionUUID){ | 61 | if (props.owningAvatarID === MyAvatar.sessionUUID) { |
| 61 | this.respawnTimer = Script.setInterval(this.spawnAllLanterns.bind(this), RESPAWN_INTERVAL); | 62 | this.respawnTimer = Script.setInterval(this.spawnAllLanterns.bind(this), RESPAWN_INTERVAL); |
| 62 | } | 63 | } |
| 63 | }, | 64 | }, |
| 64 | 65 | ||
| 65 | unload: function(entityID) { | 66 | unload: function(entityID) { |
| 66 | if(this.respawnTimer) | 67 | if (this.respawnTimer) { |
| 67 | Script.clearInterval(this.respawnTimer); | 68 | Script.clearInterval(this.respawnTimer); |
| 69 | } | ||
| 68 | }, | 70 | }, |
| 69 | 71 | ||
| 70 | spawnAllLanterns: function() { | 72 | spawnAllLanterns: function() { |
| @@ -72,14 +74,14 @@ · 1 unmodified line | |||
| 72 | var lanternCount = 0; | 74 | var lanternCount = 0; |
| 73 | var nearbyEntities = Entities.findEntities(props.position, props.dimensions.x * 0.75); | 75 | var nearbyEntities = Entities.findEntities(props.position, props.dimensions.x * 0.75); |
| 74 | 76 | ||
| 75 | for(var i = 0; i < nearbyEntities.length; i++) { | 77 | for (var i = 0; i < nearbyEntities.length; i++) { |
| 76 | var name = Entities.getEntityProperties(nearbyEntities[i], ["name"]).name; | 78 | var name = Entities.getEntityProperties(nearbyEntities[i], ["name"]).name; |
| 77 | if(name === "Floating Lantern") { | 79 | if (name === "Floating Lantern") { |
| 78 | lanternCount++; | 80 | lanternCount++; |
| 79 | } | 81 | } |
| 80 | } | 82 | } |
| 81 | 83 | ||
| 82 | while(lanternCount++ < MAX_LANTERNS) { | 84 | while (lanternCount++ < MAX_LANTERNS) { |
| 83 | this.spawnLantern(); | 85 | this.spawnLantern(); |
| 84 | } | 86 | } |
| 85 | }, | 87 | }, |