awacke1 commited on
Commit
3aa6634
·
1 Parent(s): c884218

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +192 -157
index.html CHANGED
@@ -1,167 +1,202 @@
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>
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
- <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 class="bird" color="#F44336" position="0 1.5 -10" radius="0.5"></a-sphere>
18
- <a-sphere class="bird" color="#E91E63" position="0 1.5 -11" radius="0.5"></a-sphere>
19
- <a-sphere class="bird" color="#9C27B0" position="0 1.5 -12" radius="0.5"></a-sphere>
20
- <a-sphere class="bird" color="#2196F3" position="0 1.5 -13" radius="0.5"></a-sphere>
21
- <a-sphere class="bird" 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: [
35
- { name: 'moveForward', from: ['idle', 'moveBackward'], to: 'moveForward' },
36
- { name: 'moveBackward', from: ['idle', 'moveForward'], to: 'moveBackward' },
37
- { name: 'stop', from: ['moveForward', 'moveBackward'], to: 'idle' }
38
- ],
39
- methods: {
40
- onEnterMoveForward: function () {
41
- el.setAttribute('animation', 'property: position; to: 0 1.5 -2; dur: 2000');
42
- },
43
- onEnterMoveBackward: function () {
44
- el.setAttribute('animation', 'property: position; to: 0 1.5 -8; dur: 2000');
45
- },
46
- onLeaveMoveForward: function () {
47
- el.setAttribute('animation', '');
48
- },
49
- onLeaveMoveBackward: function () {
50
- el.setAttribute('animation', '');
51
- }
52
- }
53
- });
54
-
55
- document.addEventListener('keydown', function (event) {
56
- switch (event.key) {
57
- case 'ArrowUp':
58
- stateMachine.moveForward();
59
- break;
60
- case 'ArrowDown':
61
- stateMachine.moveBackward();
62
- break;
63
- }
64
- });
65
-
66
- document.addEventListener('keyup', function (event) {
67
- switch (
68
- case 'ArrowUp':
69
- case 'ArrowDown':
70
- stateMachine.stop();
71
- break;
72
- }
73
- });
74
-
75
- var birdSwarm = document.querySelector('#bird-swarm');
76
- var birdState = 'idle';
77
- var birdInterval;
78
-
79
- var birdStateMachine = new StateMachine({
80
- init: 'follow',
81
- transitions: [
82
- { name: 'follow', from: 'idle', to: 'follow' },
83
- { name: 'stop', from: 'follow', to: 'idle' },
84
- { name: 'click', from: 'follow', to: 'click' },
85
- { name: 'release', from: 'click', to: 'follow' }
86
- ],
87
- methods: {
88
- onEnterFollow: function () {
89
- birdInterval = setInterval(function () {
90
- var birds = document.querySelectorAll('.bird');
91
- var birdPosition = birdSwarm.getAttribute('position');
92
- var randomX = Math.random() * 0.4 - 0.2;
93
- var randomY = Math.random() * 0.4 - 0.2;
94
- var randomZ = Math.random() * 0.4 - 0.2;
95
- birds.forEach(function (bird) {
96
- bird.setAttribute('animation', 'property: position; to: ' + (birdPosition.x + randomX) + ' ' + (birdPosition.y + randomY) + ' ' + (birdPosition.z + randomZ) + '; dur: 1000');
97
- });
98
- }, 1000);
99
- },
100
- onLeaveFollow: function () {
101
- clearInterval(birdInterval);
102
- var birds = document.querySelectorAll('.bird');
103
- birds.forEach(function (bird) {
104
- bird.setAttribute('animation', '');
105
- });
106
- },
107
- onClick: function (event, from, to) {
108
- var target = event.target;
109
- target.setAttribute('animation__click', 'property: position; to: 0 2.5 -5; dur: 1000');
110
- },
111
- onRelease: function (event, from, to) {
112
- var target = event.target;
113
- target.removeAttribute('animation__click');
114
- var birdInterval = setInterval(function () {
115
- var birdPosition = target.getAttribute('position');
116
- var randomX = Math.random() * 0.4 - 0.2;
117
- var randomY = Math.random() * 0.4 - 0.2;
118
- var randomZ = Math.random() * 0.4 - 0.2;
119
- target.setAttribute('animation', 'property: position; to: ' + (birdPosition.x + randomX) + ' ' + (birdPosition.y + randomY) + ' ' + (birdPosition.z + randomZ) + '; dur: 1000');
120
- }, 1000);
121
- }
122
- }
123
- });
124
-
125
- function updateBirdPosition () {
126
- var playerPosition = el.getAttribute('position');
127
- birdSwarm.setAttribute('position', playerPosition.x + ' ' + playerPosition.y + ' ' + (playerPosition.z - 5));
128
- }
129
 
130
- setInterval(function () {
131
- updateBirdPosition();
132
- }, 100);
133
-
134
- document.addEventListener('keydown', function (event) {
135
- switch (event.key) {
136
- case 'ArrowUp':
137
- case 'ArrowDown':
138
- birdStateMachine.follow();
139
- break;
140
- }
141
- });
142
-
143
- document.addEventListener('keyup', function (event) {
144
- switch (event.key) {
145
- case 'ArrowUp':
146
- case 'ArrowDown':
147
- birdStateMachine.stop();
148
- break;
149
- }
150
- });
151
-
152
- var birds = document.querySelectorAll('.bird');
153
- birds.forEach(function (bird) {
154
- bird.addEventListener('mousedown', function (event) {
155
- birdStateMachine.click
156
- }, false);
157
-
158
- bird.addEventListener('mouseup', function (event) {
159
- birdStateMachine.release(event);
160
- }, false);
161
- });
162
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  }
164
- });
165
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  </body>
167
- </html>
 
1
  <!DOCTYPE html>
2
  <html>
3
  <head>
 
4
  <script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
5
  </head>
6
  <body>
7
  <a-scene>
8
+ <!-- Camera -->
9
+ <a-entity camera look-controls position="0 2 6"></a-entity>
10
+
11
+ <!-- Field -->
12
+ <a-box color="green" position="0 -0.5 -10" width="16" height="0.5" depth="26"></a-box>
13
+
14
+ <!-- Ball -->
15
+ <a-sphere id="ball" color="white" radius="0.2" position="0 0.2 0"></a-sphere>
16
+
17
+ <!-- Particle Systems -->
18
+ <!-- Team 1 -->
19
+ <a-entity particle-system="color: #FFC107; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="-2 0.5 -3"></a-entity>
20
+ <a-entity particle-system="color: #FFC107; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="2 0.5 -3"></a-entity>
21
+ <a-entity particle-system="color: #FFC107; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="-2 0.5 -7"></a-entity>
22
+ <a-entity particle-system="color: #FFC107; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="2 0.5 -7"></a-entity>
23
+
24
+ <!-- Team 2 -->
25
+ <a-entity particle-system="color: #2196F3; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="-2 0.5 3"></a-entity>
26
+ <a-entity particle-system="color: #2196F3; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="2 0.5 3"></a-entity>
27
+ <a-entity particle-system="color: #2196F3; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="-2 0.5 7"></a-entity>
28
+ <a-entity particle-system="color: #2196F3; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="2 0.5 7"></a-entity>
29
+
30
+ <!-- Script to move the ball -->
31
+ <script>
32
+ // Get the ball entity
33
+ var ball = document.querySelector("#ball");
34
+
35
+ // Set the ball's initial velocity
36
+ var ballVelocity = new THREE.Vector3(0, 0, 0);
37
+
38
+ // Set the ball's mass
39
+ var ballMass = 1;
40
+
41
+ // Set the ball's friction coefficient
42
+ var ballFriction = 0.01;
43
+
44
+ // Set the ball's restitution coefficient
45
+ var ballRestitution = 0.9;
46
+
47
+ // Set the ball's position update interval
48
+ var ballInterval = 20; // milliseconds
49
+
50
+ // Set the ball's initial position
51
+ var ballPosition = new THREE.Vector3(0, 0.2, 0);
52
+
53
+ // Set the ball's size
54
+ var ballSize = 0.2;
55
+
56
+ // Set the ball's goal width and height
57
+ var goalWidth = 5;
58
+ var goalHeight = 3;
59
+
60
+ // Set the ball's movement speed
61
+ var ballSpeed = 0.2;
62
+
63
+ // Set the ball's maximum velocity
64
+ var ballMaxVelocity = 10;
65
+
66
+ // Set the ball's reset position
67
+ var resetPosition = new THREE.Vector3(0, 0.2, 0);
68
+
69
+ // Set the ball's collision radius
70
+ var ballRadius = 0.4;
71
+
72
+ // Set the ball's collision offset
73
+ var ballOffset = new THREE.Vector3(0, ballRadius, 0);
74
+
75
+ // Set the ball's collision detection
76
+ var ballCollisionDetection = true;
77
+
78
+ // Set the ball's collision response
79
+ var ballCollisionResponse = true;
80
+
81
+ // Set the ball's collision event listener
82
+ ball.addEventListener("collide", function(event) {
83
+ // Get the collider object
84
+ var collider = event.detail.body.el;
85
+
86
+ // Get the collider's velocity
87
+ var colliderVelocity = collider.getAttribute("particle-system").velocity;
88
+
89
+ // Set the ball's velocity to the collider's velocity
90
+ ballVelocity.copy(colliderVelocity);
91
+ });
92
+
93
+ // Set the ball's movement direction
94
+ var ballDirection = new THREE.Vector3(0, 0, 0);
95
+
96
+ // Set the ball's goal detection
97
+ function detectGoal() {
98
+ if (ballPosition.x < -goalWidth / 2 || ballPosition.x > goalWidth / 2 || ballPosition.z < -goalHeight / 2 || ballPosition.z > goalHeight / 2) {
99
+ resetGame();
100
+ }
101
+ }
102
+
103
+ // Set the ball's movement logic
104
+ function moveBall() {
105
+ // Update the ball's position
106
+ ballPosition.add(ballDirection);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
+ // Apply friction to the ball's velocity
109
+ ballVelocity.multiplyScalar(1 - ballFriction);
110
+
111
+ // Apply gravity to the ball's velocity
112
+ ballVelocity.add(new THREE.Vector3(0, -9.8 * ballMass / 1000, 0));
113
+
114
+ // Apply the ball's velocity to its position
115
+ ballPosition.add(ballVelocity);
116
+
117
+ // Apply the ball's position to the ball entity
118
+ ball.setAttribute("position", ballPosition);
119
+
120
+ // Detect if the ball has gone into a goal
121
+ detectGoal();
122
+
123
+ // Update the ball's position every interval
124
+ setTimeout(moveBall, ballInterval);
125
+ }
126
+
127
+ // Set the ball's reset logic
128
+ function resetGame() {
129
+ // Reset the ball's position
130
+ ballPosition.copy(resetPosition);
131
+ ball.setAttribute("position", ballPosition);
132
+
133
+ // Reset the ball's velocity
134
+ ballVelocity.set(0, 0, 0);
135
+
136
+ // Reset the ball's direction
137
+ ballDirection.set(0, 0, 0);
138
+ }
139
+
140
+ // Set the ball's collision detection
141
+ function detectCollisions() {
142
+ // Get all the particle system entities
143
+ var particleSystems = document.querySelectorAll("a-entity[particle-system]");
144
+
145
+ // Loop through each particle system entity
146
+ for (var i = 0; i < particleSystems.length; i++) {
147
+ // Get the particle system's position
148
+ var particleSystemPosition = particleSystems[i].getAttribute("position");
149
+
150
+ // Get the particle system's velocity
151
+ var particleSystemVelocity = particleSystems[i].getAttribute("particle-system").velocity;
152
+
153
+ // Get the particle system's direction
154
+ var particleSystemDirection = new THREE.Vector3(particleSystemVelocity.x, 0, particleSystemVelocity.z);
155
+
156
+ // Get the particle system's radius
157
+ var particleSystemRadius = particleSystems[i].getAttribute("particle-system").size * particleSystems[i].getAttribute("particle-system").particleCount;
158
+
159
+ // Get the particle system's offset
160
+ var particleSystemOffset = new THREE.Vector3(0, particleSystemRadius, 0);
161
+
162
+ // Get the particle system's collision detection
163
+ var particleSystemCollisionDetection = particleSystems[i].getAttribute("particle-system").collisionDetection;
164
+
165
+ // Get the particle system's collision response
166
+ var particleSystemCollisionResponse = particleSystems[i].getAttribute("particle-system").collisionResponse;
167
+
168
+ // Get the particle system's collision event listener
169
+ var particleSystemCollisionListener = particleSystems[i].getAttribute("particle-system").collisionListener;
170
+
171
+ // Calculate the distance between the particle system and the ball
172
+ var distance = particleSystemPosition.distanceTo(ballPosition);
173
+
174
+ // Check if the particle system is colliding with the ball
175
+ if (distance < particleSystemRadius + ballRadius) {
176
+ // Set the ball's direction to the particle system's direction
177
+ ballDirection.copy(particleSystemDirection);
178
+
179
+ // Emit a collision event on the particle system entity
180
+ if (particleSystemCollisionListener) {
181
+ particleSystems[i].emit("collision");
182
+ }
183
  }
184
+ }
185
+
186
+ // Check for collisions every interval
187
+ setTimeout(detectCollisions, 20);
188
+ }
189
+
190
+ // Set the ball's movement direction to the ball's speed
191
+ ballDirection.set(0, 0, -ballSpeed);
192
+
193
+ // Start the ball's movement logic
194
+ moveBall();
195
+
196
+ // Start the ball's collision detection
197
+ detectCollisions();
198
+ </script>
199
+ </a-scene>
200
+
201
  </body>
202
+ </html>