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