forked from dkushner/amoebadome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
184 lines (162 loc) · 5.35 KB
/
index.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
<!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/components/controller.js"></script>
<script src="js/components/rigidbody.js"></script>
<script src="js/components/follow.js"></script>
<script src="js/components/interface.js"></script>
<link href="css/main.css" rel="stylesheet"/>
</head>
<body>
<div id="amoeba"></div>
<script type="text/javascript">
var Amoeba = new Game({
canvas: '#amoeba',
width: window.innerWidth,
height: window.innerHeight,
debug: true,
}, "scene1");
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 = Amoeba.addEntity("MenuCamera");
Prefab.define("ConfigMenu", function() {
var master = new Entity.Transform("ConfigMenu");
var inter = new Interface({
'sphere': {
target: master,
setup: function(el) {
$(document.createElement('button'))
.text('Sphere')
.addClass('sphere')
.css({
color: '#0000ff'
})
.appendTo($(el))
.click(function() {
Amoeba.addEntity("SphereProxy");
});
}
}
}, [50, 50]);
master.addComponent(inter);
return master;
});
var menu = Amoeba.addEntity("ConfigMenu");
Prefab.define("PlayerBase", function() {
var sphere = new Entity.Mesh(
"PlayerBase",
new THREE.SphereGeometry(),
new THREE.MeshLambertMaterial()
);
return sphere;
});
var base = Amoeba.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 = Amoeba.addEntity("Ground");
var vertices = _.filter(base.geometry.vertices, function(vert) {
return Math.abs(vert.y - 25) < 0.00001;
});
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;
});
var light = new Entity.PointLight("Light", 0xff0000, 1, 1000);
light.position.set(0, 300, 0);
Amoeba.addEntity(light);
Amoeba.run();
</script>
</body>
</html>