-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAIAutoRed1.html
More file actions
259 lines (220 loc) · 10.1 KB
/
Copy pathAIAutoRed1.html
File metadata and controls
259 lines (220 loc) · 10.1 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Red Dead – A-Frame Wild West (First Working Version)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://aframe.io/releases/1.6.0/aframe.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/donmccurdy/aframe-extras@master/dist/aframe-extras.min.js"></script>
<style>
body{margin:0;overflow:hidden;font-family:Courier New,monospace}
#ui,#msg{position:absolute;top:20px;left:20px;color:#fff;background:rgba(0,0,0,.6);padding:12px;border-radius:8px;font-size:18px;z-index:100;pointer-events:none}
#msg{top:auto;bottom:20px;left:20px;color:#ffcc00;max-width:560px}
#crosshair{position:absolute;top:50%;left:50%;width:20px;height:20px;margin:-10px 0 0 -10px;border:2px solid #ffcc00;border-radius:50%;z-index:99}
button{margin-top:8px;padding:8px 16px;background:#8B4513;color:#fff;border:none;border-radius:5px;cursor:pointer;font-weight:bold}
button:hover{background:#A0522D}
</style>
</head>
<body>
<div id="ui">
Health: <span id="health">100</span>% |
Ammo: <span id="ammo">6</span>/6 |
Score: <span id="score">0</span>
</div>
<div id="msg">
<strong>Voice:</strong> "shoot", "reload", "ride", "stop"<br>
<strong>Move:</strong> WASD | <strong>Look:</strong> Mouse | <strong>Shoot:</strong> Click / Voice
<button onclick="startGame()">Start Game</button>
</div>
<div id="crosshair"></div>
<a-scene background="color:#87CEEB" cursor="rayOrigin:mouse" embedded physics="debug:false" style="width:100vw;height:100vh">
<!-- Player -->
<a-entity id="player" camera look-controls="pointerLockEnabled:true"
wasd-controls="acceleration:30" position="0 1.6 5">
<a-entity cursor position="0 0 -0.5"
geometry="primitive:ring;radiusInner:0.02;radiusOuter:0.03"
material="color:#ffcc00;shader:flat"></a-entity>
</a-entity>
<!-- Ground with green grass -->
<a-plane class="ground" position="0 0 0" rotation="-90 0 0"
width="100" height="100" color="#D2B48C"
src="https://cdn.jsdelivr.net/gh/mrdoob/three.js@master/examples/textures/terrain/grasslight-big.jpg"
repeat="10 10" roughness="0.9"></a-plane>
<!-- Lights -->
<a-light type="directional" intensity="0.8" position="10 20 10" castShadow></a-light>
<a-light type="ambient" intensity="0.4"></a-light>
<a-sky color="#87CEEB"></a-sky>
<!-- Cactus 1 -->
<a-entity class="shootable" position="-5 1 -10">
<a-cylinder height="2" radius="0.3" color="#228B22"></a-cylinder>
<a-cylinder height="0.8" radius="0.4" position="0 1.4 0" color="#228B22"></a-cylinder>
<a-box position="0 0.5 0" width="0.1" height="1" depth="0.1" color="#8B4513"></a-box>
</a-entity>
<!-- Cactus 2 -->
<a-entity class="shootable" position="8 1 -15">
<a-cylinder height="1.8" radius="0.25" color="#228B22"></a-cylinder>
<a-cylinder height="0.7" radius="0.35" position="0.5 1.2 0" color="#228B22"></a-cylinder>
<a-cylinder height="0.7" radius="0.35" position="-0.5 1.2 0" color="#228B22"></a-cylinder>
</a-entity>
<!-- Outlaw -->
<a-entity id="outlaw" class="shootable" position="0 1.6 -20">
<a-box depth="0.6" height="1.8" width="0.8" color="#333"></a-box>
<a-cone position="0 1.3 0" radius-bottom="0.4" height="0.6" color="#FDBCB4"></a-cone>
<a-cylinder position="0 0.4 0" radius="0.25" height="0.8" color="#8B4513"></a-cylinder>
<a-sphere position="0 1.6 0.25" radius="0.2" color="#FDBCB4"></a-sphere>
<a-text value="WANTED" color="#FF4500" align="center" position="0 2.3 0" scale="2 2 2"></a-text>
</a-entity>
<!-- Horse -->
<a-entity id="horse" position="-10 0.8 -8">
<a-box depth="2.5" height="1.2" width="1" color="#8B4513" position="0 0.6 0"></a-box>
<a-box depth="0.4" height="1.4" width="0.6" color="#8B4513" position="0.8 0.7 0"></a-box>
<a-box depth="0.4" height="1.4" width="0.6" color="#8B4513" position="-0.8 0.7 0"></a-box>
<a-cylinder rotation="0 0 90" radius="0.3" height="1.2" color="#333" position="1.1 0.3 0"></a-cylinder>
<a-cylinder rotation="0 0 90" radius="0.3" height="1.2" color="#333" position="-1.1 0.3 0"></a-cylinder>
<a-text value="Horse" color="#FFD700" align="center" position="0 1.8 0"></a-text>
</a-entity>
<!-- Sounds -->
<a-assets timeout="30000">
<audio id="gunshot" src="https://cdn.jsdelivr.net/gh/mozvr/aframe-audio-assets@master/gunshot.mp3"></audio>
<audio id="reload" src="https://cdn.jsdelivr.net/gh/mozvr/aframe-audio-assets@master/reload.mp3"></audio>
<audio id="horse" src="https://cdn.jsdelivr.net/gh/mozvr/aframe-audio-assets@master/horse.mp3"></audio>
<audio id="hit" src="https://cdn.jsdelivr.net/gh/mozvr/aframe-audio-assets@master/hit.mp3"></audio>
<audio id="bgmusic" src="https://cdn.jsdelivr.net/gh/mozvr/aframe-audio-assets@master/western.mp3" loop></audio>
</a-assets>
</a-scene>
<script>
let health = 100, ammo = 6, score = 0, riding = false, gameOn = false;
let recognition, horseInterval;
const $ = s => document.querySelector(s);
const healthEl = $('#health'), ammoEl = $('#ammo'), scoreEl = $('#score');
function startGame() {
if (gameOn) return;
gameOn = true;
$('#msg').innerHTML = `Voice commands active – say <b>shoot / reload / ride / stop</b>`;
document.body.addEventListener('click', () => document.body.requestPointerLock());
document.getElementById('bgmusic').play();
// Voice Recognition
if ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window) {
recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
recognition.continuous = true;
recognition.interimResults = false;
recognition.lang = 'en-US';
recognition.onresult = e => {
const cmd = e.results[e.results.length-1][0].transcript.trim().toLowerCase();
handleVoice(cmd);
};
recognition.start();
} else {
alert('Voice not supported – use Chrome/Edge');
}
document.addEventListener('click', shoot);
}
function handleVoice(c) {
if (!gameOn) return;
if (c.includes('shoot')) shoot();
else if (c.includes('reload')) reload();
else if (c.includes('ride') && !riding) mountHorse();
else if (c.includes('stop') && riding) dismountHorse();
}
function shoot() {
if (ammo <= 0) { play('reload'); return; }
ammo--; updateUI(); play('gunshot');
const cam = $('#player').components.camera.camera;
const pos = new THREE.Vector3();
const dir = new THREE.Vector3();
cam.getWorldPosition(pos);
cam.getWorldDirection(dir).multiplyScalar(-1);
const bullet = document.createElement('a-entity');
bullet.setAttribute('position', pos);
bullet.setAttribute('geometry', 'primitive:sphere;radius:0.06');
bullet.setAttribute('material', 'color:#ffcc00;emissive:#ffcc00;emissiveIntensity:0.8');
document.querySelector('a-scene').appendChild(bullet);
const trail = document.createElement('a-entity');
trail.setAttribute('particle-system',
'preset:default; color:#ffcc00,#ff9900,#cccccc; opacity:0.8,0.4; velocityValue:0 0 0; ' +
'accelerationValue:0 -2 0; size:0.2,0.05; lifetime:0.6; particleCount:60');
bullet.appendChild(trail);
const speed = 80, start = performance.now(), maxDist = 120;
const ray = new THREE.Raycaster(pos, dir.clone().normalize());
const move = now => {
const dt = (now - start) / 1000;
const dist = speed * dt;
if (dist > maxDist) { bullet.remove(); return; }
const newPos = pos.clone().add(dir.clone().multiplyScalar(dist));
bullet.setAttribute('position', newPos);
const hits = ray.intersectObjects(
document.querySelectorAll('.shootable').map(e=>e.object3D), true);
if (hits.length && hits[0].distance <= dist) {
hitTarget(hits[0].object.el.closest('.shootable'));
bullet.remove();
return;
}
requestAnimationFrame(move);
};
requestAnimationFrame(move);
}
function hitTarget(el) {
play('hit');
score += 100; updateUI();
const parts = [
{o:new THREE.Vector3(0,0,0), c:'#228B22'},
{o:new THREE.Vector3(0.3,0.4,0), c:'#228B22'},
{o:new THREE.Vector3(-0.3,0.4,0), c:'#228B22'},
{o:new THREE.Vector3(0,0.8,0), c:'#8B4513'}
];
parts.forEach(p => {
const b = document.createElement('a-box');
b.setAttribute('depth',0.4); b.setAttribute('height',0.6); b.setAttribute('width',0.4);
b.setAttribute('material',`color:${p.c}`);
const pos = el.getAttribute('position');
b.setAttribute('position', `${pos.x+p.o.x} ${pos.y+p.o.y} ${pos.z+p.o.z}`);
b.setAttribute('dynamic-body','mass:2');
b.setAttribute('animation',`property:position; to:${pos.x+p.o.x} ${pos.y-5} ${pos.z+p.o.z}; dur:2000; easing:easeInQuad`);
document.querySelector('a-scene').appendChild(b);
setTimeout(()=>b.remove(), 2500);
});
el.remove();
}
function reload() {
play('reload');
setTimeout(()=>{ ammo=6; updateUI(); },1200);
}
function mountHorse() {
if (riding) return;
riding = true; play('horse');
const h = $('#horse'), p = $('#player');
const pos = h.getAttribute('position');
p.setAttribute('position', `${pos.x} 2.5 ${pos.z}`);
const size = 100, margin = 8;
horseInterval = setInterval(()=>{
if (!riding) return;
const cam = $('#player').components.camera.camera;
const dir = new THREE.Vector3();
cam.getWorldDirection(dir).multiplyScalar(-0.25);
const cur = h.object3D.position;
const next = cur.clone().add(dir);
if (Math.abs(next.x) > size/2 - margin) dir.x *= -1;
if (Math.abs(next.z) > size/2 - margin) dir.z *= -1;
h.object3D.position.add(dir);
h.object3D.lookAt(h.object3D.position.clone().add(dir));
p.setAttribute('position', `${h.object3D.position.x} 2.5 ${h.object3D.position.z}`);
},50);
}
function dismountHorse() {
if (!riding) return;
riding = false; clearInterval(horseInterval);
const pos = $('#horse').object3D.position;
$('#player').setAttribute('position', `${pos.x} 1.6 ${pos.z + 2}`);
}
function play(id){ const s=document.getElementById(id); if(s){s.currentTime=0;s.play();} }
function updateUI(){ healthEl.textContent=health; ammoEl.textContent=ammo; scoreEl.textContent=score; }
setTimeout(()=>{
if (document.getElementById('outlaw')) {
health -= 30; updateUI();
if (health<=0){ alert('Game Over!'); location.reload(); }
}
},15000);
updateUI();
</script>
</body>
</html>