-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstep1.html
More file actions
98 lines (90 loc) · 2.86 KB
/
step1.html
File metadata and controls
98 lines (90 loc) · 2.86 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
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Croquet Multiblaster</title>
<style>
html, body {
margin: 0;
height: 100%;
background: #999;
}
#canvas {
background: #000;
object-fit: contain;
max-width: 100%;
max-height: 100%;
}
</style>
<script src="https://unpkg.com/@croquet/croquet@1.0"></script>
</head>
<body>
<canvas id="canvas" width="1000" height="1000"></canvas>
<script>
class Game extends Croquet.Model {
init() {
this.asteroids = new Set();
this.asteroids.add(Asteroid.create());
this.asteroids.add(Asteroid.create());
this.asteroids.add(Asteroid.create());
}
}
Game.register("Game");
class Asteroid extends Croquet.Model {
init() {
this.size = 40;
this.x = Math.random() * 1000;
this.y = Math.random() * 1000;
this.dx = Math.random() * 6 - 3;
this.dy = Math.random() * 6 - 3;
this.a = Math.random() * Math.PI * 2;
this.da = (0.02 + Math.random() * 0.03) * (Math.random() < 0.5 ? 1 : -1);
this.move();
}
move() {
this.x = (this.x + this.dx + 1000) % 1000;
this.y = (this.y + this.dy + 1000) % 1000;
this.a = (this.a + this.da + Math.PI) % Math.PI;
this.future(50).move(); // keep moving every 50 ms
}
}
Asteroid.register("Asteroid");
////////////////////////// VIEW //////////////////////////
class Display extends Croquet.View {
constructor(model) {
super(model);
this.model = model;
this.context = canvas.getContext("2d");
}
// update is called once per render frame
update() {
this.context.clearRect(0, 0, 1000, 1000);
this.context.lineWidth = 3;
this.context.strokeStyle = "white";
for (const asteroid of this.model.asteroids) {
const { x, y, a, size } = asteroid;
this.context.save();
this.context.translate(x, y);
this.context.rotate(a);
this.context.beginPath();
this.context.moveTo(+size, 0);
this.context.lineTo( 0, +size);
this.context.lineTo(-size, 0);
this.context.lineTo( 0, -size);
this.context.closePath();
this.context.stroke();
this.context.restore();
}
}
}
Croquet.Session.join({
apiKey: '1_i65fcn11n7lhrb5n890hs3dhj11hfzfej57pvlrx', // get your own from croquet.io/keys
appId: 'io.croquet.multiblaster-tutorial',
name: Croquet.App.autoSession(),
password: Croquet.App.autoPassword(),
model: Game,
view: Display,
});
</script>
</body>
</html>