-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.html
293 lines (255 loc) · 8.97 KB
/
config.html
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
288
289
290
291
292
293
<!doctype html>
<html>
<head>
<title>learningthree.js boiler plate for three.js</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<script src="vendor/Prototype.js"></script>
<script src="vendor/jQuery.js"></script>
<script src="vendor/cannon/Cannon.js"></script>
<script src="vendor/three/Three.js"></script>
<script src="vendor/three/Detector.js"></script>
<!-- https://github.com/mrdoob/stats.js -->
<script src="vendor/three/Stats.js"></script>
<script src="vendor/threex/THREEx.WindowResize.js"></script>
<script src="vendor/threex/THREEx.KeyboardState.js"></script>
<script src="vendor/EventEmitter.js"></script>
<script src="vendor/Underscore.js"></script>
<script src="vendor/dat.gui.min.js"></script>
<script src="js/physics.js"></script>
<script src="js/input.js"></script>
<script src="js/game.js"></script>
<script src="js/component.js"></script>
<script src="js/entity.js"></script>
<script src="js/prefab.js"></script>
<script src="js/interface.js"></script>
<script src="js/components/controller.js"></script>
<script src="js/components/rigidbody.js"></script>
<script src="js/components/follow.js"></script>
<link href="css/main.css" rel="stylesheet"/>
</head>
<body>
<script type="game/interface" name="Config">
<div class="item-nav">
<div data-bind="Player">
<h2 data-property="currency" data-watch></h2>
<button id="sphere" data-click="doThing"><img src="images/sphere.png" width="100%" height="100%"></button> <!--ROW 1-->
<button id="sphere" data-click="makeSpike"><img src="images/spike.png" width="100%" height="100%"></button>
</div>
</div>
</script>
<div id="amoeba">
<div id="interface"></div>
</div>
<script type="text/javascript">
Game.init({
canvas: '#amoeba',
width: window.innerWidth,
height: window.innerHeight,
debug: true,
});
Prefab.define("MenuCamera", function() {
var camera = new Entity.Camera(
45,
window.innerWidth / window.innerHeight,
0.1,
10000
);
var follow = new Follow();
follow.offset = new THREE.Vector3(0, 200, -1); //Fuck you, gimbal.
camera.addComponent(follow);
return camera;
});
var camera = Game.addEntity("MenuCamera");
Prefab.define("ConfigMenu", function() {
var master = new Entity.Transform("ConfigMenu");
return master;
});
var menu = Game.addEntity("ConfigMenu");
Prefab.define("PlayerBase", function() {
var sphere = new Entity.Mesh(
"PlayerBase",
new THREE.SphereGeometry(),
new THREE.MeshLambertMaterial()
);
sphere.userData.currency = 999;
return sphere;
});
Prefab.define("Player", function() {
var base = Prefab.create("PlayerBase");
var rbod = new Rigidbody(0.5, new CANNON.Sphere(50));
var repl = new Replicator([
"position.x",
"position.y",
"position.z"
]);
base.addComponent(rbod);
base.addComponent(repl);
return base;
});
var base = Game.addEntity("PlayerBase");
camera.getComponent('follow').target = base;
Prefab.define("Ground", function() {
var pln = new THREE.PlaneGeometry(1000, 1000, 20, 20)
, gma = new THREE.MeshLambertMaterial()
, gcd = new CANNON.Plane();
var ground = new Entity.Mesh("Ground", pln, gma);
var sbd = new Rigidbody(0, gcd);
sbd._body.quaternion.setFromAxisAngle(
new CANNON.Vec3(1, 0, 0),
-Math.PI / 2
);
ground.addComponent(sbd);
return ground;
});
var ground = Game.addEntity("Ground");
var vertices = _.filter(base.geometry.vertices, function(vert) {
return Math.abs(vert.y - 25) < 0.00001; //what does the < do here? return outside verts?
});
var eligible = [
[base, vertices]
];
Prefab.define("SphereProxy", function() {
var sphere = new Entity.Mesh(
"SphereProxy",
new THREE.SphereGeometry(25),
new THREE.MeshLambertMaterial()
);
var projector = new THREE.Projector();
var controller = new Controller({
'pointer.move': function(x, y) {
var vec = new THREE.Vector3(
(x / window.innerWidth) * 2 - 1,
(y / window.innerHeight) * -2 + 1,
0.5
);
var ray = projector.pickingRay(vec, camera);
var tar = _.head(ray.intersectObject(ground)).point;
tar.add(new THREE.Vector3(0, 25, 0));
var selected = null;
_.each(eligible, function(el) {
var verts = _.sortBy(el[1], function(vert) {
return tar.distanceToSquared(vert);
});
var closest = _.head(verts)
, distance = closest.distanceToSquared(tar);
if (distance < 225) {
if (!selected) {
selected = closest;
}
if (selected && distance < selected.distanceToSquared(tar)) {
selected = closest;
}
}
});
if (selected) {
this.snapped = true;
this.position = selected;
} else {
this.snapped = false;
this.position = tar;
}
},
'pointer.down': function(x, y) {
if (this.snapped) {
this.removeComponent("controller");
}
}
});
sphere.addComponent(controller);
return sphere;
});
Prefab.define("SpikeProxy", function() { //SPIKE
var spike = new Entity.Mesh(
"SpikeProxy",
new THREE.CylinderGeometry(1,25,60,8,1,false),
new THREE.MeshLambertMaterial()
);
var projector = new THREE.Projector();
var controller = new Controller({
'pointer.move': function(x, y) {
var vec = new THREE.Vector3(
(x / window.innerWidth) * 2 - 1,
(y / window.innerHeight) * -2 + 1,
0.5
);
var ray = projector.pickingRay(vec, camera);
var tar = _.head(ray.intersectObject(ground)).point;
tar.add(new THREE.Vector3(0, 25, 0)); //our target verts are 25 up ie mid/radius
var selected = null;
_.each(eligible, function(el) {
var verts = _.sortBy(el[1], function(vert) {
return tar.distanceToSquared(vert);
});
var closest = _.head(verts)
, distance = closest.distanceToSquared(tar);
if (distance < 225) {
if (!selected) {
selected = closest;
}
if (selected && distance < selected.distanceToSquared(tar)) {
selected = closest;
}
}
});
if (selected) {
this.snapped = true;
this.position = selected;
var aMag = Math.sqrt(this.position.x * this.position.x + 0 +
this.position.z * this.position.z)
//SPECIAL NOTE: This next value may change in future
//only hard coded since i couldnt find Playerbase radius
var bMag = Math.sqrt(-43.30127018922193 * -43.30127018922193)
var x = -43.30127018922193/bMag;
var y = 0;//25.000000000000007/aMag;
var z = 0;
var A = new THREE.Vector3(this.position.x,0,this.position.z);
var B = new THREE.Vector3( x,y,z); //stationary vector
bMag /= bMag; //Normalize vector being projected onto
var dot = A.x * B.x + A.y * B.y + A.z * B.z;
var theta = Math.acos(dot / (aMag * bMag));
if(this.position.z < 0)
this.rotation.y = theta * -1; //Rotation works in radians
else
this.rotation.y = theta;
} else {
this.snapped = false;
this.position = tar;
//console.log("X: " + this.position.x + "Y: " + this.position.y);
}
},
'pointer.down': function(x, y) {
if (this.snapped) {
this.removeComponent("controller");
}
}
});
spike.addComponent(controller);
spike.rotation.z = -30;
return spike;
});
var light = new Entity.PointLight("Light", 0xff0000, 1, 1000);
light.position.set(50, 300, 0);
Game.addEntity(light);
var light2 = new Entity.PointLight("Light2", 0x00ff00, 1 , 1000);
light.position.set(-50, 300, 0);
Game.addEntity(light);
var light3 = new Entity.PointLight("Light3", 0x0000ff, 1, 1000);
light.position.set(0, 300, 50);
Game.addEntity(light);
Game.run();
Interface.to("Config", {
Player: base.userData
}, {
doThing: function(e) {
Game.addEntity("SphereProxy");
console.log(e);
console.log(this);
},
makeSpike: function(e) {
Game.addEntity("SpikeProxy");
}
});
</script>
</body>
</html>