-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
336 lines (295 loc) · 11.8 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<!DOCTYPE html>
<html>
<head>
<title>Staying Alive</title>
<meta charset="UTF-8">
<!-- output -->
<script src="lib/dat.gui.min.js"></script>
<script src="lib/hsl.js"></script>
<script src="sprite.js"></script>
<script src="animatedsprite.js"></script>
<script src="audio.js"></script>
<script src="canvasresizer.js"></script>
<script src="loadingbar.js"></script>
<!--<script src="particle.js"></script>-->
<script src="canvasui.js"></script>
<!-- input -->
<script src="lib/mousetrap.js"></script>
<script src="lib/mousetrap-global-bind.js"></script>
<script src="gamepad.js"></script>
<script src="inputmapper.js"></script>
<script src="mainloop.js"></script>
<script src="vec.js"></script>
<script src="util2d.js"></script>
<script src="utiljs.js"></script>
<script src="utilgl/shader.js"></script>
<script src="utilgl/texture.js"></script>
<script src="utilgl.js"></script>
<script src="tilemap.js"></script>
<script src="softbodyrenderer.js"></script>
<script src="physics.js"></script>
<script src="squishycreature.js"></script>
<script>
'use strict';
var Game = function(resizer, physics) {
this.resizer = resizer;
this.physics = physics;
this.canvas = resizer.getCanvas();
this.gl = glUtils.initGl(this.canvas, {}, 4);
var gl = this.gl;
this.glManager = glStateManager(gl);
gl.disable(gl.CULL_FACE);
gl.disable(gl.DEPTH_TEST);
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.enableVertexAttribArray(1);
SoftBodyRenderer.loadShaders(gl);
SquishyCreature.initRenderers(gl);
Sprite.gl = gl;
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
var that = this;
this.canvasUI = new CanvasUI({
element: this.canvas,
getCanvasPositionFromEvent: function(event) {
return that.getWorldPosition(event);
}
});
this.time = 0;
this.background = new Sprite('background.jpg', Sprite.loadAsGLTexture(gl));
this.initCreature();
this.draggedParticle = null;
this.canvasUI.down = function(vec) {
if (that.draggedParticle != null) {
that.setDraggedParticleForce(null, 0);
}
CanvasUI.prototype.down.apply(that.canvasUI, arguments);
var veinInfo = that.squishyCreature.getNearestVeinEnding(vec, 35);
var part = that.physics.getNearestParticle(vec, 60);
if (veinInfo) {
that.draggedParticle = veinInfo;
} else {
that.draggedParticle = part;
}
};
this.canvasUI.release = function() {
CanvasUI.prototype.release.apply(that.canvasUI, arguments);
if (that.draggedParticle != null) {
if (!(that.draggedParticle instanceof Particle)) {
that.attachVeinIfPossible(that.draggedParticle);
}
that.setDraggedParticleForce(null, 0);
that.draggedParticle = null;
}
};
// Canvas to use for debug visualization, possibly UI
this.canvas2D = document.createElement('canvas');
this.canvas2D.width = this.canvas.width;
this.canvas2D.height = this.canvas.height;
this.ctx2D = this.canvas2D.getContext('2d');
this.canvas2DTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, this.canvas2DTexture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
this.hasAttached = false;
};
Game.prototype.initCreature = function() {
this.physics = new GamePhysics();
this.squishyCreature = new SquishyCreature({gl: this.gl, physics: this.physics});
this.draggedParticle = null;
};
var drawText = function(ctx, str, x, y, flipped) {
ctx.save();
ctx.translate(x, y);
if (flipped) {
ctx.scale(1, -1);
}
ctx.font = 'bold 15px monospace';
ctx.lineWidth = 4;
ctx.strokeStyle = '#000';
ctx.strokeText(str, 0, 0);
ctx.fillText(str, 0, 0);
ctx.restore();
}
Game.prototype.render = function(ctx) {
// CanvasResizer passes a wrapped 2D context to use here when run in FIXED_COORDINATE_SYSTEM mode,
// where ctx.canvas.width/height are set to the coordinate system width/height.
// Otherwise the context initialized here is used.
if (ctx === undefined) {
ctx = this.gl;
}
var gl = ctx;
/*gl.clearColor(0.2, 0.2, 0.2, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);*/
if (this.background.loaded) {
this.glManager.useQuadPositionBuffer();
this.glManager.useQuadTexCoordBuffer();
this.glManager.drawQuad(SoftBodyRenderer.shader, {uWorldTransform: [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1], uTex: this.background.texture});
};
this.squishyCreature.render(this.getWorldTransform(ctx.canvas), this.getHilightedSlot());
// Render HUD stuff
var worldTransform = this.getWorldTransform(this.ctx2D.canvas);
this.ctx2D.save();
this.ctx2D.clearRect(0, 0, this.canvas2D.width, this.canvas2D.height);
this.ctx2D.fillStyle = '#fff';
var transX = this.ctx2D.canvas.width * 0.5;
var transY = this.ctx2D.canvas.height * 0.5;
this.ctx2D.translate(transX, transY);
var scaleX = worldTransform[0] * this.ctx2D.canvas.width * 0.5;
var scaleY = worldTransform[5] * this.ctx2D.canvas.height * 0.5;
this.ctx2D.scale(scaleX, -scaleY);
//TODO: It'd be nice to just have a list of things to loop through, here
this.physics.renderHUD(this.ctx2D);
if (this.draggedParticle != null && !(this.draggedParticle instanceof Particle)) {
if (this.isDraggedVeinAttached()) {
drawText(this.ctx2D, 'Pull hard to detach vein', 0, 0, true);
} else {
drawText(this.ctx2D, 'Release vein over a slot to connect', 0, 0, true);
}
} else if (!this.hasAttached && this.time > 20) {
drawText(this.ctx2D, 'Drag veins by their ends to detach and attach them', 0, 0, true);
} else {
this.squishyCreature.renderHUD(this.ctx2D);
}
this.ctx2D.restore();
// Credits and info
this.ctx2D.textAlign = 'center';
this.ctx2D.fillStyle = '#fff';
drawText(this.ctx2D, 'Staying Alive - Ludum Dare #33 Game by Olli Etuaho, Zachary Laster, Sakari Leppä, Valtteri Heinonen and Anastasia Diatlova', this.ctx2D.canvas.width * 0.5, 700);
gl.bindTexture(gl.TEXTURE_2D, this.canvas2DTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this.canvas2D);
this.glManager.useQuadPositionBuffer();
this.glManager.useQuadTexCoordBuffer();
this.glManager.drawQuad(SoftBodyRenderer.shader, {uWorldTransform: [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1], uTex: this.canvas2DTexture});
return ctx;
};
Game.prototype.getWorldTransform = function(canvas) {
var scale = 0.0016;
var scaleX = scale;
var scaleY = scale * canvas.width / canvas.height;
var translateX = 0.0;
var translateY = 0.0 * scaleY / scaleX;
var worldTransform = [
scaleX, 0, 0, 0,
0, scaleY, 0, 0,
0, 0, 1, 0,
translateX, translateY, 0, 1
];
return worldTransform;
};
Game.prototype.getWorldPosition = function(event) {
var canvasPos = this.resizer.getCanvasPosition(event);
var glNormPos = new Vec2(canvasPos.x / this.resizer.canvas.width * 2.0 - 1.0, -canvasPos.y / this.resizer.canvas.height * 2.0 + 1.0);
var worldTransform = this.getWorldTransform(this.resizer.canvas);
var glToWorldTransform = glUtils.matrixInverse(worldTransform);
// This assumes that the world transform only has scale and translation.
glNormPos.x = glNormPos.x * glToWorldTransform[0] + glToWorldTransform[12];
glNormPos.y = glNormPos.y * glToWorldTransform[5] + glToWorldTransform[13];
return glNormPos;
};
Game.prototype.getDraggedParticleAsParticle = function() {
var part = null;
if (this.draggedParticle != null) {
if (this.draggedParticle instanceof Particle) {
part = this.draggedParticle;
} else {
part = this.draggedParticle.vein.mesh.positions[this.draggedParticle.posIndex].particle;
}
}
return part;
};
Game.prototype.setDraggedParticleForce = function(forceTarget, force) {
var part = this.getDraggedParticleAsParticle();
if (part != null) {
part.externalForceTarget = forceTarget;
part.externalForce = force;
}
};
Game.prototype.isDraggedVeinAttached = function() {
if (this.draggedParticle != null && !(this.draggedParticle instanceof Particle)) {
if (this.draggedParticle.vein.isAttachedFrom(this.draggedParticle.posIndex)) {
return true;
}
}
return false;
};
Game.prototype.getHilightedSlot = function() {
if (this.draggedParticle != null && !(this.draggedParticle instanceof Particle)) {
if (this.draggedParticle.vein.isAttachedFrom(this.draggedParticle.posIndex)) {
return null;
}
var pos = this.getDraggedParticleAsParticle().state.position;
var slot = this.squishyCreature.getNearestFreeVeinSlot(pos, 60);
if (slot) {
return slot;
}
}
return null;
};
Game.prototype.attachVeinIfPossible = function(veinInfo) {
// Check if already attached
if (veinInfo.vein.isAttachedFrom(veinInfo.posIndex)) {
return;
}
var pos = this.getDraggedParticleAsParticle().state.position;
var slot = this.squishyCreature.getNearestFreeVeinSlot(pos, 60);
if (slot) {
slot.attachVein(veinInfo.vein, veinInfo.posIndex);
this.hasAttached = true;
}
};
Game.prototype.update = function(deltaTime) {
this.time += deltaTime;
this.physics.update(deltaTime);
if (this.draggedParticle != null) {
var cursor = new CVec(this.canvasUI.cursorX, this.canvasUI.cursorY);
var dist = this.getDraggedParticleAsParticle().state.position.distance(cursor);
this.setDraggedParticleForce(cursor, Math.sqrt(dist) + 10);
}
this.squishyCreature.update(deltaTime);
};
var DEV_MODE = (window.location.href.indexOf("?devMode") != -1);
Game.music = new Audio('alive_labsounds');
var game;
var start = function() {
var DEBUG_MAIN_LOOP = DEV_MODE && true; // Set to true to allow fast-forwarding main loop with 'f'
Audio.muteAll(DEV_MODE && false); // Set to true if sounds annoy developers
dat.instance = new dat.GUI();
//if (!DEV_MODE) dat.GUI.toggleHide();
var resizer = new CanvasResizer({mode: CanvasResizer.Mode.FIXED_RESOLUTION_INTERPOLATED, width: 1280, height: 720});
game = new Game(resizer);
initGUI();
startMainLoop([resizer, game], {debugMode: DEBUG_MAIN_LOOP});
Game.music.playSingular(true);
};
var initGUI = function() {
SquishyCreature.debug['Show veins'] = false;
SquishyCreature.debug['Show organs'] = true;
var datinst = dat.instance;
var f1 = datinst.addFolder('Organs');
f1.add(SquishyCreature.debug, 'Show veins');
f1.add(SquishyCreature.debug, 'Show organs');
datinst.add({'Scramble veins':function() { game.squishyCreature.scrambleVeins(); game.hasAttached = true; }}, 'Scramble veins');
datinst.add({'Reset':function() { game.initCreature(); }}, 'Reset');
GamePhysics.debug.springs = {};
GamePhysics.debug.particles = {};
GamePhysics.debug.springs.show = false;
GamePhysics.debug.springs.color = '#0f0';
GamePhysics.debug.particles.show = false;
if (DEV_MODE) {
GamePhysics.debug.particles.show = true;
var datinst = dat.instance;
var gui = datinst.addFolder('Physics');
var f1 = gui.addFolder('Springs');
f1.add(GamePhysics.debug.springs, 'show');
f1.addColor(GamePhysics.debug.springs, 'color');
var f2 = gui.addFolder('Particles');
f2.add(GamePhysics.debug.particles, 'show');
}
};
</script>
</head>
<body onload="start()" style="background: black;">
</body>
</html>