-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo2.html
76 lines (67 loc) · 1.98 KB
/
demo2.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Anvil.js Physics Demo</title>
<script src="../build/anvil.js"></script>
</head>
<body>
<canvas id="physicsCanvas" width="800" height="600"></canvas>
<script>
const { Scene, Polygon, Input, SceneManager } = ANVIL;
const physicsCanvas = document.getElementById("physicsCanvas");
const physicsScene = new Scene({
physics: true,
fpsMonitoringEnabled: true,
start: true,
});
// Create and add squares
for (let i = 0; i < 20; i++) {
const square = new Polygon({
points: [[i * 80, 400], [(i + 1) * 80, 400], [(i + 1) * 80, 500], [i * 80, 500]],
backgroundColor: "#0000FF",
physicsEnabled: true,
physicsOptions: { mass: 10 },
});
i++
physicsScene.addObject(square);
}
// create floor
const floor = new Polygon({
points: [[00,600],[600+1000,600],[600+1000,700],[600,700]],
backgroundColor: "black",
physicsEnabled: true,
physicsOptions: {
isStatic: true
}
});
physicsScene.addObject(floor)
// Create player-controlled square
const playerSquare = new Polygon({
points: [[200, 200], [250, 200], [250, 250], [200, 250]],
backgroundColor: "#FFA500",
physicsEnabled: true,
physicsOptions: { mass: 20 },
});
physicsScene.addObject(playerSquare);
// Bind camera to player square
physicsScene.bindCamera(playerSquare);
// Monitor arrow keys for player movement
const moveLeft = new Input("ArrowLeft");
const moveRight = new Input("ArrowRight");
moveLeft.on = () => {
playerSquare.move([-1, 0]);
};
moveRight.on = () => {
playerSquare.move([1, 0]);
};
moveLeft.activate();
moveRight.activate();
const sceneManager = new SceneManager({
initialScene: physicsScene,
canvas: physicsCanvas
})
</script>
</body>
</html>