awacke1 commited on
Commit
a7617e0
·
1 Parent(s): 43854a2

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +55 -7
index.html CHANGED
@@ -1,7 +1,7 @@
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>
@@ -10,21 +10,25 @@
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: [
@@ -48,7 +52,6 @@
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':
@@ -60,7 +63,6 @@
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':
@@ -69,6 +71,52 @@
69
  break;
70
  }
71
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  }
73
  });
74
  </script>
 
1
  <!DOCTYPE html>
2
  <html>
3
  <head>
4
+ <title>Finite State Machine Demo with 3D Bird Swarm</title>
5
  <script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
6
  </head>
7
  <body>
 
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
  <a-sky src="#skyTexture"></a-sky>
 
14
  <a-plane src="#groundTexture" rotation="-90 0 0" height="100" width="100"></a-plane>
 
15
  <a-entity id="player" position="0 1.5 -5" movement></a-entity>
16
+ <a-entity id="bird-swarm">
17
+ <a-sphere color="#F44336" position="0 1.5 -10" radius="0.5"></a-sphere>
18
+ <a-sphere color="#E91E63" position="0 1.5 -11" radius="0.5"></a-sphere>
19
+ <a-sphere color="#9C27B0" position="0 1.5 -12" radius="0.5"></a-sphere>
20
+ <a-sphere color="#2196F3" position="0 1.5 -13" radius="0.5"></a-sphere>
21
+ <a-sphere color="#4CAF50" position="0 1.5 -14" radius="0.5"></a-sphere>
22
+ </a-entity>
23
  </a-scene>
24
 
25
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/javascript-state-machine/3.0.0/state-machine.min.js"></script>
26
  <script>
27
  AFRAME.registerComponent('movement', {
28
  init: function () {
29
  var el = this.el;
30
  var playerState = 'idle';
31
 
 
32
  var stateMachine = new StateMachine({
33
  init: 'idle',
34
  transitions: [
 
52
  }
53
  });
54
 
 
55
  document.addEventListener('keydown', function (event) {
56
  switch (event.key) {
57
  case 'ArrowUp':
 
63
  }
64
  });
65
 
 
66
  document.addEventListener('keyup', function (event) {
67
  switch (event.key) {
68
  case 'ArrowUp':
 
71
  break;
72
  }
73
  });
74
+
75
+ var birdSwarm = document.querySelector('#bird-swarm');
76
+ var birdState = 'idle';
77
+
78
+ var birdStateMachine = new StateMachine({
79
+ init: 'follow',
80
+ transitions: [
81
+ { name: 'follow', from: 'idle', to: 'follow' },
82
+ { name: 'stop', from: 'follow', to: 'idle' }
83
+ ],
84
+ methods: {
85
+ onEnterFollow: function () {
86
+ birdSwarm.setAttribute('animation', 'property: position; to: 0 1.5 -6; dur: 2000');
87
+ },
88
+ onLeaveFollow: function () {
89
+ birdSwarm.setAttribute('animation', '');
90
+ }
91
+ }
92
+ });
93
+
94
+ function updateBirdPosition () {
95
+ var playerPosition = el.getAttribute('position');
96
+ birdSwarm.setAttribute('position', playerPosition.x + ' ' + playerPosition.y + ' ' + (playerPosition.z - 5));
97
+ }
98
+
99
+ setInterval(function () {
100
+ updateBirdPosition();
101
+ }, 100);
102
+
103
+ document.addEventListener('keydown', function (event) {
104
+ switch (event.key) {
105
+ case 'ArrowUp':
106
+ case 'ArrowDown':
107
+ birdStateMachine.follow();
108
+ break;
109
+ }
110
+ });
111
+
112
+ document.addEventListener('keyup', function (event) {
113
+ switch (event.key) {
114
+ case 'ArrowUp':
115
+ case 'ArrowDown':
116
+ birdStateMachine.stop();
117
+ break;
118
+ }
119
+ });
120
  }
121
  });
122
  </script>