forked from github/game-off-2013
-
Notifications
You must be signed in to change notification settings - Fork 1
/
strawman.js
287 lines (241 loc) · 9.79 KB
/
strawman.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"use strict";
define(['settings','spitball'], function(settings, spitball) {
function StrawModel() {
var points = new THREE.SplineCurve3([
new THREE.Vector3(0, settings.strawLength/2, 0),
new THREE.Vector3(0, -settings.strawLength, 0),
]);
// points, ?, radius, facets, ? ?
var geometry = new THREE.TubeGeometry(points, 12,settings.strawRadius,16, false, false);
geometry.applyMatrix(
new THREE.Matrix4()
.makeTranslation( 0, 0, 0 ) );
geometry.applyMatrix(
new THREE.Matrix4()
.makeRotationFromQuaternion(
new THREE.Quaternion()
.setFromAxisAngle(
new THREE.Vector3( 1, 0, 0),
-Math.PI/2 )));
var material = new THREE.MeshPhongMaterial({
color: 0xFFFFFF,
side: THREE.DoubleSide,
});
var mesh = new THREE.Mesh( geometry, material );
var container = new THREE.Object3D();
container.add(mesh);
return container;
}
function LauncherModel() {
var points = new THREE.SplineCurve3([
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, -settings.launcherLength, 0),
]);
// points, ?, radius, facets, ? ?
var geometry = new THREE.TubeGeometry( points, 12, settings.strawRadius, 16, false, false);
geometry.applyMatrix(
new THREE.Matrix4()
.makeTranslation( 0, 0, 0 ) );
geometry.applyMatrix(
new THREE.Matrix4()
.makeRotationFromQuaternion(
new THREE.Quaternion()
.setFromAxisAngle(
new THREE.Vector3( 1, 0, 0),
-Math.PI/2 )));
geometry.applyMatrix(
new THREE.Matrix4()
.makeTranslation( 0, 0, 0 ) );
var material = new THREE.MeshPhongMaterial({
color: 0xFFFFFF,
side: THREE.DoubleSide,
});
var mesh = new THREE.Mesh( geometry, material );
var container = new THREE.Object3D();
container.add( mesh );
var jointGeometry = new THREE.SphereGeometry( settings.launcherRadius*1.05, 25 );
var jointMaterial = new THREE.MeshPhongMaterial( { color:0xFFFFFF, side:THREE.DoubleSide } );
var jointMesh = new THREE.Mesh( jointGeometry, jointMaterial );
container.add( jointMesh );
var eyeGeometry = new THREE.SphereGeometry( settings.launcherRadius, 50 );
var eyeMaterial = new THREE.MeshNormalMaterial( { color:0xFFFFFF } );
var eyeMesh = new THREE.Mesh( eyeGeometry, eyeMaterial );
eyeMesh.position.z = settings.launcherLength*0.9;
container.add( eyeMesh );
return container;
}
function StrawmanObject() {
var model = new THREE.Object3D();
model.matrixAutoUpdate = false;
var strawModel = new StrawModel();
model.add( strawModel );
strawModel.rotation.y = -Math.PI;
var launcherModel = new LauncherModel();
model.add( launcherModel );
launcherModel.rotation.y = -Math.PI;
launcherModel.position.z = -settings.strawLength;
var tracker = new THREE.Object3D();
launcherModel.add( tracker );
var strawTip = new THREE.Object3D();
strawTip.position.z = settings.launcherLength;
launcherModel.add( strawTip );
strawModel.position.z = settings.strawLength+settings.launcherLength-20;
launcherModel.position.z = settings.launcherLength-20;
var uprightQuaternion = new THREE.Quaternion()
.setFromEuler( launcherModel.rotation );
function transform(m) {
model.matrix.fromArray(m);
model.matrixWorldNeedsUpdate = true;
}
function errorTerm(a) { return Math.random()*a - a/2; }
var targetPosition = new THREE.Vector3();
var m = new THREE.Matrix4();
var errorVector;
function setTarget( target ) {
var cloned = target.clone();
errorVector = new THREE.Vector3( errorTerm(25), errorTerm(25), errorTerm(25) );
cloned.position.add( errorVector );
cloned.updateMatrix();
m.getInverse(model.matrix).multiply(cloned.matrix);
targetPosition.getPositionFromMatrix(m);
lookAtTarget().start();
}
function lookAtTarget( time ) {
tracker.lookAt( targetPosition );
var targetQuaternion = new THREE.Quaternion()
.setFromEuler( tracker.rotation );
var initialQuaternion = new THREE.Quaternion()
.setFromEuler( launcherModel.rotation );
return new TWEEN.Tween( { fraction:0.0 } )
.to( {fraction:1.0}, time ? time : 250*30 )
.easing( TWEEN.Easing.Bounce.Out)
.onUpdate( function () {
launcherModel.setRotationFromQuaternion(
initialQuaternion.slerp(
targetQuaternion,
this.fraction
)
);
});
}
function fire() {
return new spitball.Spitball( getStrawTip(), errorVector );
}
function withdraw() {
var currentQuaternion = new THREE.Quaternion()
.setFromEuler( launcherModel.rotation );
return new TWEEN.Tween( {z:strawModel.position.z, lz:launcherModel.position.z, fraction:0.0 } )
.to( { z:settings.strawLength+settings.launcherLength, lz:settings.launcherLength, fraction:1.0 }, 150 )
.easing( TWEEN.Easing.Linear.None )
.onUpdate( function() {
strawModel.position.z = this.z;
launcherModel.position.z = this.lz;
launcherModel.setRotationFromQuaternion(
currentQuaternion.slerp(
uprightQuaternion,
this.fraction
)
);
});
}
function insert() {
return new TWEEN.Tween( {z:strawModel.position.z, lz:launcherModel.position.z } )
.to( { z:0, lz:-settings.strawLength }, 1500 )
.easing( TWEEN.Easing.Elastic.Out )
.onUpdate( function() {
strawModel.position.z = this.z;
launcherModel.position.z = this.lz;
});
}
function spin() {
return new TWEEN.Tween( {y:launcherModel.rotation.y, x:launcherModel.rotation.x } )
.to( { y:Math.random()*Math.PI, x:Math.random()*Math.PI }, 350 )
.easing( TWEEN.Easing.Linear.None )
.onUpdate( function() {
launcherModel.rotation.y = this.y;
launcherModel.rotation.x = this.x;
});
}
function setPosition( position ) {
strawModel.position.x = position.x;
strawModel.position.y = position.y;
launcherModel.position.x = position.x;
launcherModel.position.y = position.y;
}
function getStrawTip() {
var position = new THREE.Vector3().getPositionFromMatrix( strawTip.matrixWorld );
return position;
}
return {
model: model,
transform: transform,
pickables: [],
setTarget: setTarget,
fire: fire,
spin: spin,
setPosition:setPosition,
insert:insert,
withdraw: withdraw,
};
}
function Strawman() {
var spinTween = undefined;
var currentCoordinate = undefined;
var withdrawn = true;
function bump( coordinate ) {
withdrawStrawman( function() {
coordinate.puzzle.object.bump();
addStrawman( coordinate, coordinate.puzzle.object.getHolePosition() );
coordinate.filter.strawman.insert()
.onStart( function(){
withdrawn = false;
currentCoordinate = coordinate;
})
.start();
});
}
function addStrawman( coordinate, position ) {
coordinate.filter.add( coordinate.puzzle.id, coordinate.filter.strawman );
coordinate.filter.strawman.setPosition( position );
}
function removeStrawman() {
currentCoordinate.filter.remove( currentCoordinate.puzzle.id, currentCoordinate.filter.strawman );
}
function spinStrawman() {
if( !currentCoordinate ) {
return;
}
var target = new THREE.Object3D();
currentCoordinate.filter.strawman.setTarget( target );
}
function withdrawStrawman( callback ) {
if( spinTween ) {
spinTween.stop();
}
if( withdrawn ) {
callback();
}
else {
currentCoordinate.filter.strawman.withdraw()
.onComplete( function() {
removeStrawman();
withdrawn = true;
callback();
})
.start();
}
}
function getCurrentCoordinate() {
return currentCoordinate;
}
return {
bump: bump,
spin: spinStrawman,
getCurrentCoordinate: getCurrentCoordinate,
};
}
return {
StrawmanObject:StrawmanObject,
Strawman:Strawman,
};
});