お待たせしました。以下に、指定されたすべての機能を持つ3DのFPSゲームのコードを、一つのHTMLファイルにまとめて提供します。このコードは、Three.jsとCannon.jsのライブラリを使用しており、外部ライブラリはCDN経由で読み込んでいます。
html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>3D FPSゲーム</title>
<style>
body { margin: 0; overflow: hidden; }
#ui {
position: absolute; top: 10px; left: 10px; color: #fff; font-family: sans-serif;
}
#ui div { margin-bottom: 5px; }
#game-over {
display: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0, 0, 0, 0.8); color: #fff; font-size: 48px; text-align: center; padding-top: 200px;
}
#restart-btn {
margin-top: 20px; padding: 10px 20px; font-size: 24px; cursor: pointer;
}
</style>
</head>
<body>
<div id="ui">
<div>スコア: <span id="score">0</span></div>
<div>ヘルス: <span id="health">100</span></div>
<div>弾薬: <span id="ammo">30</span></div>
</div>
<div id="game-over">
ゲームオーバー<br>
<button id="restart-btn">再スタート</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/three@0.155/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.155/examples/js/controls/PointerLockControls.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cannon-es@0.20.0/dist/cannon-es.js"></script>
<script>
let scene, camera, renderer, controls;
let world;
let objects = [];
let bullets = [];
let enemies = [];
let score = 0;
let health = 100;
let ammo = 30;
let gameOver = false;
init();
animate();
function init() {
// シーンとカメラの設定
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 1, 1000);
camera.position.y = 1.6; // 視点の高さ
// レンダラーの設定
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// ポインタロックコントロール
controls = new THREE.PointerLockControls(camera, document.body);
document.body.addEventListener('click', () => {
controls.lock();
}, false);
// 物理エンジンの設定
world = new CANNON.World({
gravity: new CANNON.Vec3(0, -9.82, 0) // 重力
});
// 地面の作成
const floorGeometry = new THREE.PlaneGeometry(100, 100);
const floorMaterial = new THREE.MeshPhongMaterial({color: 0x999999});
const floorMesh = new THREE.Mesh(floorGeometry, floorMaterial);
floorMesh.rotation.x = -Math.PI / 2;
scene.add(floorMesh);
const floorBody = new CANNON.Body({
mass: 0, // 静的な物体
shape: new CANNON.Plane(),
quaternion: new CANNON.Quaternion().setFromEuler(-Math.PI / 2, 0, 0)
});
world.addBody(floorBody);
// ライトの追加
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5, 10, 7.5);
scene.add(light);
// イベントリスナー
window.addEventListener('resize', onWindowResize, false);
document.addEventListener('mousedown', onMouseDown, false);
// 敵の生成
for (let i = 0; i < 5; i++) {
spawnEnemy();
}
}
function spawnEnemy() {
const enemyGeometry = new THREE.BoxGeometry(1, 2, 1);
const enemyMaterial = new THREE.MeshStandardMaterial({color: 0xff0000});
const enemyMesh = new THREE.Mesh(enemyGeometry, enemyMaterial);
enemyMesh.position.set(Math.random() * 50 - 25, 1, Math.random() * 50 - 25);
scene.add(enemyMesh);
const enemyBody = new CANNON.Body({
mass: 5,
shape: new CANNON.Box(new CANNON.Vec3(0.5, 1, 0.5)),
position: new CANNON.Vec3(enemyMesh.position.x, enemyMesh.position.y, enemyMesh.position.z)
});
world.addBody(enemyBody);
enemies.push({mesh: enemyMesh, body: enemyBody});
}
function onWindowResize() {
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function onMouseDown(event) {
if (ammo <= 0 || gameOver) return;
ammo--;
document.getElementById('ammo').textContent = ammo;
// 弾丸の生成
const bulletGeometry = new THREE.SphereGeometry(0.1, 8, 8);
const bulletMaterial = new THREE.MeshBasicMaterial({color: 0xffff00});
const bulletMesh = new THREE.Mesh(bulletGeometry, bulletMaterial);
bulletMesh.position.copy(controls.getObject().position);
scene.add(bulletMesh);
const bulletBody = new CANNON.Body({
mass: 0.1,
shape: new CANNON.Sphere(0.1),
position: new CANNON.Vec3().copy(bulletMesh.position)
});
const shootDirection = new THREE.Vector3();
controls.getDirection(shootDirection);
bulletBody.velocity.set(
shootDirection.x * 20,
shootDirection.y * 20,
shootDirection.z * 20
);
world.addBody(bulletBody);
bullets.push({mesh: bulletMesh, body: bulletBody});
}
function animate() {
requestAnimationFrame(animate);
if (!gameOver) {
// 物理エンジンの更新
world.step(1/60);
// 弾丸の更新
bullets.forEach((bullet, index) => {
bullet.mesh.position.copy(bullet.body.position);
// 敵との衝突判定
enemies.forEach((enemy, enemyIndex) => {
const distance = bullet.mesh.position.distanceTo(enemy.mesh.position);
if (distance < 1) {
scene.remove(bullet.mesh);
world.removeBody(bullet.body);
bullets.splice(index, 1);
scene.remove(enemy.mesh);
world.removeBody(enemy.body);
enemies.splice(enemyIndex, 1);
score += 100;
document.getElementById('score').textContent = score;
// 新たな敵を生成
spawnEnemy();
}
});
});
// 敵の追跡AI
enemies.forEach((enemy) => {
const direction = new THREE.Vector3();
direction.subVectors(controls.getObject().position, enemy.mesh.position).normalize();
enemy.body.velocity.set(direction.x * 2, 0, direction.z * 2);
enemy.mesh.position.copy(enemy.body.position);
// プレイヤーとの衝突判定
const distance = enemy.mesh.position.distanceTo(controls.getObject().position);
if (distance < 1.5) {
health -= 1;
document.getElementById('health').textContent = health;
if (health <= 0) {
gameOver = true;
document.getElementById('game-over').style.display = 'block';
}
}
});
// プレイヤーの移動
controls.update();
}
renderer.render(scene, camera);
}
// 再スタートボタンの動作
document.getElementById('restart-btn').addEventListener('click', () => {
location.reload();
});
</script>
</body>
</html>
このコードを実行するには、ローカルサーバーで実行するか、セキュリティ設定を緩和する必要があります。ブラウザで直接開く場合、一部の機能が制限される可能性があります。