History

coding standard

Rob Kayson committed 9 years ago

Commit a12e8e31 parent 18735583 files · +96 −93Browse files

Showing the changes to scripts/tutorials/entity_scripts (2 of 3 files). Show all

Unified · Split

2 files
  1. scripts/tutorials/entity_scripts/floatingLantern.js +81 −81
  2. scripts/tutorials/entity_scripts/floatingLanternBox.js +11 −9
scripts/tutorials/entity_scripts/floatingLantern.js+81−81
@@ -14,93 +14,93 @@ · 13 unmodified lines
1414 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
1515
1616 (function() {
17− var _this;
17+ var _this;
1818
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};
2323
2424
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+ },
4389
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+ },
4796
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);
84101 }
85− });
86102 }
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+ };
104104
105− return new floatingLantern();
105+ return new floatingLantern();
106106 });
scripts/tutorials/entity_scripts/floatingLanternBox.js+11−9
@@ -21,6 +21,7 @@ · 20 unmodified lines
2121 var LIFETIME = 120;
2222 var RESPAWN_INTERVAL = 1000;
2323 var MAX_LANTERNS = 4;
24+ var SCALE_FACTOR = 1;
2425
2526 var LANTERN = {
2627 type: "Model",
@@ -29,9 +30,9 @@ · 2 unmodified lines
2930 modelURL: LANTERN_MODEL_URL,
3031 script: LANTERN_SCRIPT_URL,
3132 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
3536 },
3637 gravity: {
3738 x: 0,
@@ -57,14 +58,15 @@ · 19 unmodified lines
5758 this.entityID = entityID;
5859 var props = Entities.getEntityProperties(this.entityID);
5960
60− if(props.owningAvatarID === MyAvatar.sessionUUID){
61+ if (props.owningAvatarID === MyAvatar.sessionUUID) {
6162 this.respawnTimer = Script.setInterval(this.spawnAllLanterns.bind(this), RESPAWN_INTERVAL);
6263 }
6364 },
6465
6566 unload: function(entityID) {
66− if(this.respawnTimer)
67− Script.clearInterval(this.respawnTimer);
67+ if (this.respawnTimer) {
68+ Script.clearInterval(this.respawnTimer);
69+ }
6870 },
6971
7072 spawnAllLanterns: function() {
@@ -72,14 +74,14 @@ · 1 unmodified line
7274 var lanternCount = 0;
7375 var nearbyEntities = Entities.findEntities(props.position, props.dimensions.x * 0.75);
7476
75− for(var i = 0; i < nearbyEntities.length; i++) {
77+ for (var i = 0; i < nearbyEntities.length; i++) {
7678 var name = Entities.getEntityProperties(nearbyEntities[i], ["name"]).name;
77− if(name === "Floating Lantern") {
79+ if (name === "Floating Lantern") {
7880 lanternCount++;
7981 }
8082 }
8183
82− while(lanternCount++ < MAX_LANTERNS) {
84+ while (lanternCount++ < MAX_LANTERNS) {
8385 this.spawnLantern();
8486 }
8587 },

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t