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
1717 (function() {
1818 var _this;
1919
20− var NULL_UUID = "{00000000-0000-0000-0000-000000000000}";
2120 var LINE_WIDTH = 0.02;
22− var MAX_DISTANCE_MULTIPLIER = 4;
23− var ACTION_DISTANCE = 0.35;
24− var ACTION_TIMESCALE = 0.035;
2521 var COLLISION_SOUND_URL = "http://public.highfidelity.io/sounds/Footsteps/FootstepW3Left-12db.wav";
2622 var TIP_OFFSET = 0.26;
27− var LIFETIME = 3600;
2823
2924 tetherballStick = function() {
3025 _this = this;
3126 };
3227
3328 tetherballStick.prototype = {
34− entityID: NULL_UUID,
35− ballID: NULL_UUID,
36− lineID: NULL_UUID,
3729
38− getUserData: function(key) {
30+ getUserData: function() {
3931 try {
4032 var stickProps = Entities.getEntityProperties(this.entityID);
4133 var userData = JSON.parse(stickProps.userData);
42− return userData[key];
34+ return userData;
4335 } catch (e) {
4436 print("Error parsing Tetherball Stick UserData in file " +
4537 e.fileName + " on line " + e.lineNumber);
@@ -48,51 +40,36 @@ · 2 unmodified lines
4840
4941 preload: function(entityID) {
5042 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);
5255 },
5356
5457 update: function(dt) {
5558 _this.drawLine();
5659 },
5760
58− startEquip: function(id, params) {
59− this.removeActions();
60− this.createLine();
61−
62− Script.update.connect(this.update);
61+ startEquip: function() {
62+ Script.update.disconnect(this.update);
6363 },
6464
6565 continueEquip: function(id, params) {
66− var stickProps = Entities.getEntityProperties(this.entityID);
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−
66+ this.updateOffsetAction();
8867 this.capBallDistance();
68+ this.drawLine();
8969 },
9070
91− releaseEquip: function(id, params) {
92− this.deleteLine();
93− this.createSpringAction();
94−
95− Script.update.disconnect(this.update);
71+ releaseEquip: function() {
72+ Script.update.connect(this.update);
9673 },
9774
9875 getTipPosition: function() {
@@ -104,13 +81,10 @@ · 5 unmodified lines
10481 return tipPosition;
10582 },
10683
107− getStickFrontPosition: function() {
108− var stickProps = Entities.getEntityProperties(this.entityID);
109− var stickFront = Quat.getFront(stickProps.rotation);
110− var tipPosition = this.getTipPosition();
111− var frontPostion = Vec3.sum(tipPosition, Vec3.multiply(TIP_OFFSET * 0.4, stickFront));
112−
113− return frontPostion;
84+ updateOffsetAction: function() {
85+ Entities.updateAction(this.ballID, this.actionID, {
86+ pointToOffsetFrom: this.getTipPosition()
87+ });
11488 },
11589
11690 capBallDistance: function() {
@@ -118,7 +92,7 @@ · 1 unmodified line
11892 var ballProps = Entities.getEntityProperties(this.ballID);
11993 var tipPosition = this.getTipPosition();
12094 var distance = Vec3.distance(tipPosition, ballProps.position);
121− var maxDistance = ACTION_DISTANCE * MAX_DISTANCE_MULTIPLIER;
95+ var maxDistance = this.maxDistanceBetweenBallAndStick;
12296
12397 if(distance > maxDistance) {
12498 var direction = Vec3.normalize(Vec3.subtract(ballProps.position, tipPosition));
@@ -129,113 +103,28 @@ · 4 unmodified lines
129103 }
130104 },
131105
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−
163106 drawLine: function() {
164− if(this.hasLine()) {
165− var stickProps = Entities.getEntityProperties(this.entityID);
166− var tipPosition = this.getTipPosition();
167− var ballProps = Entities.getEntityProperties(this.ballID);
168− var cameraQuat = Vec3.multiplyQbyV(Camera.getOrientation(), Vec3.UNIT_NEG_Z);
169− var linePoints = [];
170− var normals = [];
171− var strokeWidths = [];
172− linePoints.push(Vec3.ZERO);
173− normals.push(cameraQuat);
174− strokeWidths.push(LINE_WIDTH);
175− linePoints.push(Vec3.subtract(ballProps.position, tipPosition));
176− normals.push(cameraQuat);
177− strokeWidths.push(LINE_WIDTH);
178−
179− var lineProps = Entities.getEntityProperties(this.lineID);
180− Entities.editEntity(this.lineID, {
181− linePoints: linePoints,
182− normals: normals,
183− strokeWidths: strokeWidths,
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
107+ var stickProps = Entities.getEntityProperties(this.entityID);
108+ var tipPosition = this.getTipPosition();
109+ var ballProps = Entities.getEntityProperties(this.ballID);
110+ var cameraQuat = Vec3.multiplyQbyV(Camera.getOrientation(), Vec3.UNIT_NEG_Z);
111+ var linePoints = [];
112+ var normals = [];
113+ var strokeWidths = [];
114+ linePoints.push(Vec3.ZERO);
115+ normals.push(cameraQuat);
116+ strokeWidths.push(LINE_WIDTH);
117+ linePoints.push(Vec3.subtract(ballProps.position, tipPosition));
118+ normals.push(cameraQuat);
119+ strokeWidths.push(LINE_WIDTH);
120+
121+ var lineProps = Entities.getEntityProperties(this.lineID);
122+ Entities.editEntity(this.lineID, {
123+ linePoints: linePoints,
124+ normals: normals,
125+ strokeWidths: strokeWidths,
126+ position: tipPosition,
205127 });
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;
239128 }
240129 };
241130