-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.html
72 lines (70 loc) · 2.31 KB
/
example.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
<canvas style="outline: black 3px solid;" id="canvas" width="500" height="500"></canvas>
<script src="script.js"></script>
<script>
class Obstacle extends Entity {
}
const canvas = document.getElementById("canvas");
const scene = new Scene(canvas);
const player = new Entity(
new EntityData()
.setX(0)
.setY(0)
.setModel(new SquareModel(20, 20))
);
const text = new Entity(
new EntityData()
.setX(canvas.width - 50)
.setY(30)
.setModel(
new TextModel(10, 10)
.setText("")
.setFont("Impact")
.setColor("rgba(0, 255, 0, 0.5)")
.setPixels(16)
.setMaxWidth(100)
)
);
scene.addEntity(player, 1);
scene.addEntity(new Obstacle(
new EntityData()
.setX(50)
.setY(50)
.setModel(new SquareModel(20, 20))
));
scene.addEntity(text);
setInterval(() => text.model.setText(scene.fps + " FPS"), 100);
let heldKeys = {};
addEventListener("keydown", key => {
heldKeys[key.key] = true;
if(key.key === " " && player.clientX) {
player.lookAt(new Vector2(player.clientX, player.clientY));
player.addMotion(player.getDirection().multiply(150, 150));
player.angle = 0;
}
});
addEventListener("keyup", key => {
delete heldKeys[key.key];
});
addEventListener("mousemove", ev => {
const {clientX, clientY} = ev;
player.clientX = clientX;
player.clientY = clientY;
});
addEventListener("blur", () => heldKeys = {});
player.on("onMove", ev => {
ev.entity.x = ev.to.x;
if (ev.entity.getCollidingEntities(scene, [Obstacle]).length > 0) ev.to.x = ev.from.x;
ev.entity.y = ev.to.y;
if (ev.entity.getCollidingEntities(scene, [Obstacle]).length > 0) ev.to.y = ev.from.y;
})
setInterval(() => {
let dx = 0;
let dy = 0;
if (heldKeys["w"]) dy--;
if (heldKeys["a"]) dx--;
if (heldKeys["s"]) dy++;
if (heldKeys["d"]) dx++;
player.move(dx, dy);
player.preventBorder(scene);
});
</script>