-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.html
More file actions
138 lines (122 loc) · 3.87 KB
/
Game.html
File metadata and controls
138 lines (122 loc) · 3.87 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Shooting Game</title>
<style>
body { margin: 0; overflow: hidden; background-color: #222; }
canvas { display: block; }
#ui {
position: absolute;
top: 20px;
left: 20px;
color: white;
font-family: Arial, sans-serif;
font-size: 18px;
z-index: 10;
}
</style>
</head>
<body>
<div id="ui">
<p>Weapon: <span id="weapon">Pistol</span></p>
<p>Score: <span id="score">0</span></p>
<p>Health: <span id="health">100</span></p>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
let scene, camera, renderer, controls;
let pistol, rifle;
let currentWeapon = 'pistol';
let score = 0;
let health = 100;
let npcArray = [];
let bullets = [];
let raycaster, mouse, clock;
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Pointer Lock for mouse control
controls = new THREE.PointerLockControls(camera, renderer.domElement);
scene.add(controls.getObject());
clock = new THREE.Clock();
raycaster = new THREE.Raycaster();
mouse = new THREE.Vector2();
createWorld();
createWeapons();
createNPCs();
animate();
}
function createWorld() {
let ground = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
new THREE.MeshBasicMaterial({ color: 0x777777 })
);
ground.rotation.x = - Math.PI / 2;
scene.add(ground);
let light = new THREE.AmbientLight(0x404040, 5);
scene.add(light);
}
function createWeapons() {
pistol = new THREE.Mesh(new THREE.CylinderGeometry(0.1, 0.1, 1), new THREE.MeshBasicMaterial({ color: 0x333333 }));
rifle = new THREE.Mesh(new THREE.CylinderGeometry(0.2, 0.2, 3), new THREE.MeshBasicMaterial({ color: 0x444444 }));
pistol.position.set(0, 1, -3);
rifle.position.set(0, 1, -3);
rifle.visible = false;
scene.add(pistol);
scene.add(rifle);
}
function createNPCs() {
for (let i = 0; i < 5; i++) {
let npc = new THREE.Mesh(
new THREE.BoxGeometry(1, 2, 1),
new THREE.MeshBasicMaterial({ color: 0xFF0000 })
);
npc.position.set(Math.random() * 30 - 15, 1, Math.random() * 30 - 15);
npcArray.push(npc);
scene.add(npc);
}
}
function switchWeapon() {
if (currentWeapon === 'pistol') {
pistol.visible = false;
rifle.visible = true;
currentWeapon = 'rifle';
document.getElementById('weapon').textContent = 'Rifle';
} else {
pistol.visible = true;
rifle.visible = false;
currentWeapon = 'pistol';
document.getElementById('weapon').textContent = 'Pistol';
}
}
function shoot() {
let weapon = currentWeapon === 'pistol' ? pistol : rifle;
raycaster.update(camera.position, camera.rotation);
let intersects = raycaster.intersectObjects(npcArray);
if (intersects.length > 0) {
let npc = intersects[0].object;
npc.material.color.set(0x00FF00); // Mark as eliminated
score += 10;
document.getElementById('score').textContent = score;
}
}
window.addEventListener('click', shoot);
window.addEventListener('keydown', (e) => {
if (e.key === '1') {
switchWeapon();
}
});
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
init();
</script>
</body>
</html>