History

removed extra actions, creating all entities in create script.

Rob Kayson committed 9 years ago

Commit 2257af41 parent 5ce70de2 files · +76 −157Browse files

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

Unified · Split

scripts/tutorials/entity_scripts/tetherballStick.js+45−156
@@ -17,29 +17,21 @@ · 16 unmodified lines
17(function() {17(function() {
18 var _this;18 var _this;
19 19
20 var NULL_UUID = "{00000000-0000-0000-0000-000000000000}";
21 var LINE_WIDTH = 0.02;20 var LINE_WIDTH = 0.02;
22 var MAX_DISTANCE_MULTIPLIER = 4;
23 var ACTION_DISTANCE = 0.35;
24 var ACTION_TIMESCALE = 0.035;
25 var COLLISION_SOUND_URL = "http://public.highfidelity.io/sounds/Footsteps/FootstepW3Left-12db.wav";21 var COLLISION_SOUND_URL = "http://public.highfidelity.io/sounds/Footsteps/FootstepW3Left-12db.wav";
26 var TIP_OFFSET = 0.26;22 var TIP_OFFSET = 0.26;
27 var LIFETIME = 3600;
28 23
29 tetherballStick = function() {24 tetherballStick = function() {
30 _this = this;25 _this = this;
31 };26 };
32 27
33 tetherballStick.prototype = {28 tetherballStick.prototype = {
34 entityID: NULL_UUID,
35 ballID: NULL_UUID,
36 lineID: NULL_UUID,
37 29
38 getUserData: function(key) {30 getUserData: function() {
39 try {31 try {
40 var stickProps = Entities.getEntityProperties(this.entityID);32 var stickProps = Entities.getEntityProperties(this.entityID);
41 var userData = JSON.parse(stickProps.userData);33 var userData = JSON.parse(stickProps.userData);
42 return userData[key];34 return userData;
43 } catch (e) {35 } catch (e) {
44 print("Error parsing Tetherball Stick UserData in file " +36 print("Error parsing Tetherball Stick UserData in file " +
45 e.fileName + " on line " + e.lineNumber);37 e.fileName + " on line " + e.lineNumber);
@@ -48,51 +40,36 @@ · 2 unmodified lines
48 40
49 preload: function(entityID) {41 preload: function(entityID) {
50 this.entityID = entityID;42 this.entityID = entityID;
51 this.ballID = this.getUserData("ballID");43
44 var userData = this.getUserData();
45 this.ballID = userData.ballID;
46 this.lineID = userData.lineID;
47 this.actionID = userData.actionID;
48 this.maxDistanceBetweenBallAndStick = userData.maxDistanceBetweenBallAndStick;
49
50 // Script.update.connect(this.update);
51 },
52
53 unload: function() {
54 // Script.update.disconnect(this.update);
52 },55 },
53 56
54 update: function(dt) {57 update: function(dt) {
55 _this.drawLine();58 _this.drawLine();
56 },59 },
57 60
58 startEquip: function(id, params) {61 startEquip: function() {
59 this.removeActions();62 Script.update.disconnect(this.update);
60 this.createLine();
61
62 Script.update.connect(this.update);
63 },63 },
64 64
65 continueEquip: function(id, params) {65 continueEquip: function(id, params) {
66 var stickProps = Entities.getEntityProperties(this.entityID);66 this.updateOffsetAction();
67 var ballProps = Entities.getEntityProperties(this.ballID);
68 var tipPosition = this.getTipPosition();
69 var distance = Vec3.distance(tipPosition, ballProps.position);
70 var maxDistance = ACTION_DISTANCE;
71
72 var dVel = Vec3.subtract(ballProps.velocity, stickProps.velocity);
73 var dPos = Vec3.subtract(ballProps.position, stickProps.position);
74 var ballWithinMaxDistance = distance <= maxDistance;
75 var ballMovingCloserToStick = Vec3.dot(dVel, dPos) < 0;
76 var ballAboveStick = ballProps.position.y > tipPosition.y;
77
78 if(this.hasAction()) {
79 if(ballWithinMaxDistance && (ballMovingCloserToStick || ballAboveStick)) {
80 this.removeActions();
81 } else {
82 this.updateOffsetAction();
83 }
84 } else if(!ballWithinMaxDistance && !ballMovingCloserToStick){
85 this.createOffsetAction();
86 }
87
88 this.capBallDistance();67 this.capBallDistance();
68 this.drawLine();
89 },69 },
90 70
91 releaseEquip: function(id, params) {71 releaseEquip: function() {
92 this.deleteLine();72 Script.update.connect(this.update);
93 this.createSpringAction();
94
95 Script.update.disconnect(this.update);
96 },73 },
97 74
98 getTipPosition: function() {75 getTipPosition: function() {
@@ -104,13 +81,10 @@ · 5 unmodified lines
104 return tipPosition;81 return tipPosition;
105 },82 },
106 83
107 getStickFrontPosition: function() {84 updateOffsetAction: function() {
108 var stickProps = Entities.getEntityProperties(this.entityID);85 Entities.updateAction(this.ballID, this.actionID, {
109 var stickFront = Quat.getFront(stickProps.rotation);86 pointToOffsetFrom: this.getTipPosition()
110 var tipPosition = this.getTipPosition();87 });
111 var frontPostion = Vec3.sum(tipPosition, Vec3.multiply(TIP_OFFSET * 0.4, stickFront));
112
113 return frontPostion;
114 },88 },
115 89
116 capBallDistance: function() {90 capBallDistance: function() {
@@ -118,7 +92,7 @@ · 1 unmodified line
118 var ballProps = Entities.getEntityProperties(this.ballID);92 var ballProps = Entities.getEntityProperties(this.ballID);
119 var tipPosition = this.getTipPosition();93 var tipPosition = this.getTipPosition();
120 var distance = Vec3.distance(tipPosition, ballProps.position);94 var distance = Vec3.distance(tipPosition, ballProps.position);
121 var maxDistance = ACTION_DISTANCE * MAX_DISTANCE_MULTIPLIER;95 var maxDistance = this.maxDistanceBetweenBallAndStick;
122 96
123 if(distance > maxDistance) {97 if(distance > maxDistance) {
124 var direction = Vec3.normalize(Vec3.subtract(ballProps.position, tipPosition));98 var direction = Vec3.normalize(Vec3.subtract(ballProps.position, tipPosition));
@@ -129,113 +103,28 @@ · 4 unmodified lines
129 }103 }
130 },104 },
131 105
132 createLine: function() {
133 if(!this.hasLine()) {
134 this.lineID = Entities.addEntity({
135 type: "PolyLine",
136 name: "TetherballStick Line",
137 color: {
138 red: 0,
139 green: 120,
140 blue: 250
141 },
142 textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png",
143 position: this.getTipPosition(),
144 dimensions: {
145 x: 10,
146 y: 10,
147 z: 10
148 },
149 lifetime: this.getUserData("lifetime")
150 });
151 }
152 },
153
154 deleteLine: function() {
155 Entities.deleteEntity(this.lineID);
156 this.lineID = NULL_UUID;
157 },
158
159 hasLine: function() {
160 return this.lineID != NULL_UUID;
161 },
162
163 drawLine: function() {106 drawLine: function() {
164 if(this.hasLine()) {107 var stickProps = Entities.getEntityProperties(this.entityID);
165 var stickProps = Entities.getEntityProperties(this.entityID);108 var tipPosition = this.getTipPosition();
166 var tipPosition = this.getTipPosition();109 var ballProps = Entities.getEntityProperties(this.ballID);
167 var ballProps = Entities.getEntityProperties(this.ballID);110 var cameraQuat = Vec3.multiplyQbyV(Camera.getOrientation(), Vec3.UNIT_NEG_Z);
168 var cameraQuat = Vec3.multiplyQbyV(Camera.getOrientation(), Vec3.UNIT_NEG_Z);111 var linePoints = [];
169 var linePoints = [];112 var normals = [];
170 var normals = [];113 var strokeWidths = [];
171 var strokeWidths = [];114 linePoints.push(Vec3.ZERO);
172 linePoints.push(Vec3.ZERO);115 normals.push(cameraQuat);
173 normals.push(cameraQuat);116 strokeWidths.push(LINE_WIDTH);
174 strokeWidths.push(LINE_WIDTH);117 linePoints.push(Vec3.subtract(ballProps.position, tipPosition));
175 linePoints.push(Vec3.subtract(ballProps.position, tipPosition));118 normals.push(cameraQuat);
176 normals.push(cameraQuat);119 strokeWidths.push(LINE_WIDTH);
177 strokeWidths.push(LINE_WIDTH);120
178 121 var lineProps = Entities.getEntityProperties(this.lineID);
179 var lineProps = Entities.getEntityProperties(this.lineID);122 Entities.editEntity(this.lineID, {
180 Entities.editEntity(this.lineID, {123 linePoints: linePoints,
181 linePoints: linePoints,124 normals: normals,
182 normals: normals,125 strokeWidths: strokeWidths,
183 strokeWidths: strokeWidths,126 position: tipPosition,
184 position: tipPosition,
185 });
186 }
187 },
188
189 createOffsetAction: function() {
190 this.removeActions();
191
192 Entities.addAction("offset", this.ballID, {
193 pointToOffsetFrom: this.getTipPosition(),
194 linearDistance: ACTION_DISTANCE,
195 linearTimeScale: ACTION_TIMESCALE
196 });
197 },
198
199 createSpringAction: function() {
200 this.removeActions();
201
202 Entities.addAction("spring", this.ballID, {
203 targetPosition: this.getTipPosition(),
204 linearTimeScale: ACTION_TIMESCALE
205 });127 });
206 },
207
208 updateOffsetAction: function() {
209 var actionIDs = Entities.getActionIDs(this.ballID);
210 var actionID;
211
212 // Sometimes two offset actions are applied to the ball simultaneously.
213 // Here we ensure that only the most recent action is updated
214 // and the rest are deleted.
215 while(actionIDs.length > 1) {
216 actionID = actionIDs.shift();
217 Entities.deleteAction(this.ballID, actionID);
218 }
219
220 actionID = actionIDs.shift();
221 if(actionID) {
222 Entities.updateAction(this.ballID, actionID, {
223 pointToOffsetFrom: this.getTipPosition()
224 });
225 }
226 },
227
228 removeActions: function() {
229 // Ball should only ever have one action, but sometimes sneaky little actions attach themselves
230 // So we remove all possible actions in this call.
231 var actionIDs = Entities.getActionIDs(this.ballID);
232 for(var i = 0; i < actionIDs.length; i++) {
233 Entities.deleteAction(this.ballID, actionIDs[i]);
234 }
235 },
236
237 hasAction: function() {
238 return Entities.getActionIDs(this.ballID).length > 0;
239 }128 }
240 };129 };
241 130