Update index.html
Browse files- index.html +73 -16
index.html
CHANGED
@@ -1,19 +1,76 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
</html>
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
+
<head>
|
4 |
+
<title>Finite State Machine Demo</title>
|
5 |
+
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
|
6 |
+
</head>
|
7 |
+
<body>
|
8 |
+
<a-scene>
|
9 |
+
<a-assets>
|
10 |
+
<img id="groundTexture" src="https://cdn.aframe.io/a-painter/images/floor.jpg">
|
11 |
+
<img id="skyTexture" src="https://cdn.aframe.io/a-painter/images/sky.jpg">
|
12 |
+
</a-assets>
|
13 |
+
|
14 |
+
<a-sky src="#skyTexture"></a-sky>
|
15 |
+
|
16 |
+
<a-plane src="#groundTexture" rotation="-90 0 0" height="100" width="100"></a-plane>
|
17 |
+
|
18 |
+
<a-entity id="player" position="0 1.5 -5" movement></a-entity>
|
19 |
+
</a-scene>
|
20 |
+
|
21 |
+
<script>
|
22 |
+
AFRAME.registerComponent('movement', {
|
23 |
+
init: function () {
|
24 |
+
var el = this.el;
|
25 |
+
var playerState = 'idle';
|
26 |
+
|
27 |
+
// Set up the state machine
|
28 |
+
var stateMachine = new StateMachine({
|
29 |
+
init: 'idle',
|
30 |
+
transitions: [
|
31 |
+
{ name: 'moveForward', from: ['idle', 'moveBackward'], to: 'moveForward' },
|
32 |
+
{ name: 'moveBackward', from: ['idle', 'moveForward'], to: 'moveBackward' },
|
33 |
+
{ name: 'stop', from: ['moveForward', 'moveBackward'], to: 'idle' }
|
34 |
+
],
|
35 |
+
methods: {
|
36 |
+
onEnterMoveForward: function () {
|
37 |
+
el.setAttribute('animation', 'property: position; to: 0 1.5 -2; dur: 2000');
|
38 |
+
},
|
39 |
+
onEnterMoveBackward: function () {
|
40 |
+
el.setAttribute('animation', 'property: position; to: 0 1.5 -8; dur: 2000');
|
41 |
+
},
|
42 |
+
onLeaveMoveForward: function () {
|
43 |
+
el.setAttribute('animation', '');
|
44 |
+
},
|
45 |
+
onLeaveMoveBackward: function () {
|
46 |
+
el.setAttribute('animation', '');
|
47 |
+
}
|
48 |
+
}
|
49 |
+
});
|
50 |
+
|
51 |
+
// Listen for keydown events to trigger state changes
|
52 |
+
document.addEventListener('keydown', function (event) {
|
53 |
+
switch (event.key) {
|
54 |
+
case 'ArrowUp':
|
55 |
+
stateMachine.moveForward();
|
56 |
+
break;
|
57 |
+
case 'ArrowDown':
|
58 |
+
stateMachine.moveBackward();
|
59 |
+
break;
|
60 |
+
}
|
61 |
+
});
|
62 |
+
|
63 |
+
// Listen for keyup events to stop the player movement
|
64 |
+
document.addEventListener('keyup', function (event) {
|
65 |
+
switch (event.key) {
|
66 |
+
case 'ArrowUp':
|
67 |
+
case 'ArrowDown':
|
68 |
+
stateMachine.stop();
|
69 |
+
break;
|
70 |
+
}
|
71 |
+
});
|
72 |
+
}
|
73 |
+
});
|
74 |
+
</script>
|
75 |
+
</body>
|
76 |
</html>
|