forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
364 lines (320 loc) · 13 KB
/
index.html
File metadata and controls
364 lines (320 loc) · 13 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>PlayCanvas UI Example</title>
<script src="../../build/output/playcanvas-latest.js"></script>
<script src="./selection-manager.js"></script>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
<canvas id="application-canvas"></canvas>
<script>
// draw some axes
var drawAxes = function (pos, scale) {
var color = new pc.Color(1,0,0);
var axis = new pc.Vec3();
var end = new pc.Vec3().copy(pos).add(axis.set(scale,0,0));
app.renderLine(pos, end, color);
color.set(0, 1, 0);
end.sub(axis.set(scale,0,0)).add(axis.set(0,scale,0));
app.renderLine(pos, end, color);
color.set(0, 0, 1);
end.sub(axis.set(0,scale,0)).add(axis.set(0,0,scale));
app.renderLine(pos, end, color);
}
</script>
<script>
var canvas = document.getElementById('application-canvas');
var app = new pc.Application(canvas, {
mouse: new pc.Mouse(document.body),
touch: new pc.TouchDevice(document.body)
});
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
// use device pixel ratio
app.graphicsDevice.maxPixelRatio = window.devicePixelRatio;
app.start();
// see selection-manager.js
var selectionManager = new SelectionManager(app, app.graphicsDevice);
selectionManager.attach(app);
// create camera
var c = new pc.Entity();
c.addComponent('camera', {
clearColor: new pc.Color(44/255, 62/255, 80/255),
farClip: 10000
});
c.translate(1,1,1);
c.lookAt(0,0,0);
app.root.addChild(c);
// Load Assets
var assets = [
new pc.Asset("red_button_atlas.png", "texture", {url: "../assets/button/red_button_atlas.png"}),
new pc.Asset('Arial.json', "font", {url: "../assets/arial/Arial.json"}),
new pc.Asset("button.js", "script", {url: "./button.js"}),
new pc.Asset("heart.png", "texture", {url: "../assets/heart.png"}),
new pc.Asset("grey_panel.png", "texture", {url: "../assets/panel/blue_panel.png"})
]
var count =0;
app.assets.on('load', function () {
count++;
if (count === assets.length) {
onLoadComplete();
}
});
for (var i = 0; i < assets.length; i++) {
app.assets.add(assets[i]);
app.assets.load(assets[i]);
}
var buttonAsset = assets[0];
var fontAsset = assets[1];
var heartAsset = assets[3];
var panelAsset = assets[4];
// global ui elements
var menu, panel;
var hud, score, lives;
var fps;
// create a button
// an image element, with button script and selector attached
// and a text element label
var createButton = function (text, x, y, callback) {
var button = new pc.Entity();
button.name = "button";
button.selector = new Selector(button, selectionManager);
button.addComponent('element', {
anchor: new pc.Vec4(0.5,1,0.5,1),
pivot: new pc.Vec2(0.5,1),
width: 256,
height: 64,
type: "image",
rect: [0,0,1,0.25],
textureAsset: buttonAsset
});
button.addComponent('script');
button.script.create('button', {
attributes: {
defaultTexture: buttonAsset,
defaultUvs: [0,0.75,1,0.25],
hoverTexture: buttonAsset,
hoverUvs: [0,0.5,1,0.25],
activatedTexture: buttonAsset,
activatedUvs: [0,0.25,1,0.25],
disabledTexture: buttonAsset,
disabledUvs: [0,0,1,0.25],
}
});
button.element.rect = [0, 0.75, 1, 0.25];
button.setLocalPosition(x, y, 0);
button.script.button.on("press", callback);
var label = new pc.Entity();
label.name = "label";
label.addComponent('element', {
anchor: new pc.Vec4(0.5,0.5,0.5,0.5),
pivot: new pc.Vec2(0.5,0.5),
width: 256,
height: 64,
type: "text",
text: text,
fontAsset: fontAsset,
color: new pc.Color(1,1,1),
opacity: 0.5
});
label.setLocalPosition(0, 8, 0)
button.addChild(label);
button.script.button.on("disabledstate", function (state) {
if (state) {
label.element.color = new pc.Color(0,0,0,0.5);
} else {
label.element.color = new pc.Color(1,1,1,0.5);
}
});
button.selector.on('pointerdown', function () {
if (button.script.button.disabled) return;
label.translateLocal(0,-8,0);
});
button.selector.on('pointerup', function () {
if (button.script.button.disabled) return;
label.translateLocal(0,8,0);
});
button.selector.on('pointerleave', function () {
if (button.script.button.disabled) return;
if (button.script.button.active) {
label.translateLocal(0,8,0);
}
});
return button;
};
// enable the menu, disable the hud
var showMenu = function () {
menu.enabled = true;
hud.enabled = false;
fps.enabled = false;
};
// enable and reset the hud, disable the menu
var showGame = function () {
menu.enabled = false;
hud.enabled = true;
fps.enabled = true;
for (var i = 0; i < lives.length; i++) {
lives[i].enabled = true;
points = 0;
}
}
// toggle annoy mode
function toggleSpin() {
spin = !spin;
}
// create the menu screen, panel and buttons
function createMenu() {
menu = new pc.Entity();
menu.addComponent("screen", {resolution: new pc.Vec2(640,480), screenSpace: true});
menu.screen.scaleMode = "blend";
menu.screen.referenceResolution = new pc.Vec2(1280,720);
// menu.setLocalScale(1/480, 1/480, 1/480);
app.root.addChild(menu);
panel = new pc.Entity();
panel.name = "panel";
panel.addComponent('element', {
anchor: new pc.Vec4(0.5, 0.5, 0.5, 0.5),
pivot: new pc.Vec2(0.5, 0.5),
width: 300,
height: 300,
type: "image",
textureAsset: panelAsset
});
button = createButton("START", 0, -22, function () {
showGame();
});
panel.addChild(button);
button = createButton("ANNOY", 0, -108, function () {
toggleSpin();
});
panel.addChild(button);
button = createButton("DISABLED", 0, -192);
button.script.button.disabled = true;
panel.addChild(button);
menu.addChild(panel);
menu.enabled = false;
};
// create the score text and the lives display
function createHud() {
hud = new pc.Entity();
hud.name = "hud";
hud.addComponent("screen", {resolution: new pc.Vec2(640,480), screenSpace: true});
hud.screen.scaleMode = "blend";
hud.screen.referenceResolution = new pc.Vec2(1280,720);
score = new pc.Entity();
score.addComponent("element", {
anchor: [1,1,1,1],
pivot: [1,1],
type: "text",
text: "0",
fontSize: 64,
fontAsset: fontAsset,
color: new pc.Color(1,1,1)
});
score.translateLocal(-84, -10, 0);
hud.addChild(score);
lives = [];
for (var i = 0; i < 3; i++) {
lives[i] = new pc.Entity();
lives[i].addComponent("element", {
anchor: [1,1,1,1],
pivot: [1,1],
width: 64,
height: 64,
type: "image",
textureAsset: heartAsset
});
lives[i].translateLocal(-10, -64*i-10, 0);
hud.addChild(lives[i]);
}
app.root.addChild(hud);
hud.enabled = false;
}
// create some world space text that displays the current FPS
function createFps() {
fps = new pc.Entity();
fps.addComponent("element", {
anchors: [0,0,0,0],
pivot: [0.5,0],
type: "text",
text: "FPS",
fontSize: 16,
fontAsset: fontAsset,
color: new pc.Color(1,1,1),
opacity: 1
});
fps.setLocalScale(1/64,1/64,1.64);
app.root.addChild(fps);
};
// called when all assets are loaded
function onLoadComplete() {
// use nearest filtering on pixelized textures to prevent leaks
for (var i = 0; i < assets.length; i++) {
if (assets[i].type === "texture") {
assets[i].resource.minFilter = pc.FILTER_NEAREST;
assets[i].resource.magFilter = pc.FILTER_NEAREST;
}
}
// create the entities
createMenu();
createHud();
createFps();
// show main menu
showMenu();
}
var spin = false; // annoy mode
var a = 0; // used for animating
var points = 0; // current score
var averageFps = 60; // current FPS
// update every frame
app.on("update", function (dt) {
// increment angle counter
a += dt;
// draw origin
drawAxes(pc.Vec3.ZERO, 1)
// if hud is active
if (hud && hud.enabled) {
// increment score
points += 10;
score.element.text = points;
// "lose" lives
if (points > 1000) {
lives[2].enabled = false;
}
if (points > 2000) {
lives[1].enabled = false;
}
if (points > 3000) {
lives[0].enabled = false;
showMenu(); // return to main menu
}
// update world space FPS counter
averageFps = pc.math.lerp(averageFps, (1/dt), 0.01); // smooth fps
fps.element.text = "Hello World!\n" + averageFps.toPrecision(3) + "fps";
fps.setLocalPosition(
0.5*Math.sin(2*a),
0.125*Math.sin(3*a),
0.125*Math.sin(5*a)
);
} else {
// if annoy mode is active rotate the panel
if (spin) {
panel.rotateLocal(0, 0, 90*dt);
}
}
});
// hook up window resize events
window.addEventListener("resize", function () {
app.resizeCanvas(canvas.width, canvas.height);
});
</script>
</body>
</html>