Using HTML + JS (one file) make a 3d FPS shooter game, add guns, random generated map,…
- User
Using HTML + JS (one file) make a 3d FPS shooter game, add guns, random generated map, enemies, working shoot, use babylon.js
- Assistant
Using HTML + JS (one file) make a 3d FPS shooter game, add guns, random generated map, enemies, working shoot, use babylon.js
Certainly! Below is an example of a simple 3D first-person shooter (FPS) game created using Babylon.js. The game features a player with a gun, a randomly generated map (maze), enemies, and a shooting mechanism. All the code is contained within a single HTML file.
Note: This is a basic implementation meant for educational purposes and can be further enhanced.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple 3D FPS Game with Babylon.js</title>
<style>
html, body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
/* Crosshair */
#crosshair {
position: absolute;
left: 50%;
top: 50%;
width: 32px;
height: 32px;
margin-left: -16px;
margin-top: -16px;
background: url('https://i.imgur.com/OQZgBIv.png') no-repeat center center;
pointer-events: none;
}
</style>
<script src="https://cdn.babylonjs.com/babylon.js"></script>
<script src="https://cdn.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
</head>
<body>
<canvas id="renderCanvas"></canvas>
<div id="crosshair"></div>
<script>
// Get the canvas element
var canvas = document.getElementById("renderCanvas");
var engine = new BABYLON.Engine(canvas, true);
// Create the scene
var createScene = function () {
var scene = new BABYLON.Scene(engine);
scene.gravity = new BABYLON.Vector3(0, -0.5, 0);
scene.collisionsEnabled = true;
// Camera (FPS style)
var camera = new BABYLON.UniversalCamera("FPS_Camera", new BABYLON.Vector3(0, 2, 0), scene);
camera.attachControl(canvas, true);
camera.applyGravity = true;
camera.checkCollisions = true;
camera.ellipsoid = new BABYLON.Vector3(1, 1, 1);
// Enable mouse lock (click to lock pointer)
canvas.addEventListener("click", function () {
canvas.requestPointerLock = canvas.requestPointerLock || canvas.msRequestPointerLock || canvas.mozRequestPointerLock || canvas.webkitRequestPointerLock;
if (canvas.requestPointerLock) {
canvas.requestPointerLock();
}
}, false);
// Light
var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
// Ground
var groundSize = 100;
var ground = BABYLON.MeshBuilder.CreateGround("ground", {width: groundSize, height: groundSize}, scene);
ground.checkCollisions = true;
// Maze generation (simple random walls)
var walls = [];
for (var i = -groundSize/2; i < groundSize/2; i += 5) {
for (var j = -groundSize/2; j < groundSize/2; j += 5) {
if (Math.random() > 0.7) {
var box = BABYLON.MeshBuilder.CreateBox("wall", {height: 5, width: 5, depth: 5}, scene);
box.position = new BABYLON.Vector3(i, 2.5, j);
box.checkCollisions = true;
walls.push(box);
}
}
}
// Gun
var gun = BABYLON.MeshBuilder.CreateBox("gun", {height: 0.5, width: 0.5, depth: 1}, scene);
gun.parent = camera;
gun.position = new BABYLON.Vector3(0.5, -0.5, 1);
// Enemies
var enemies = [];
for (var e = 0; e < 10; e++) {
var enemy = BABYLON.MeshBuilder.CreateSphere("enemy", {diameter: 2}, scene);
enemy.position = new BABYLON.Vector3(Math.random() * groundSize - groundSize/2, 1, Math.random() * groundSize - groundSize/2);
enemy.material = new BABYLON.StandardMaterial("enemyMat", scene);
enemy.material.diffuseColor = new BABYLON.Color3(1, 0, 0);
enemies.push(enemy);
}
// Shooting
var canShoot = true;
var shoot = function () {
if (!canShoot) return;
canShoot = false;
setTimeout(function () { canShoot = true; }, 500); // Fire rate
var origin = camera.position.clone();
var forward = camera.getForwardRay().direction;
var ray = new BABYLON.Ray(origin, forward, 100);
// Visualization of the bullet (optional)
var bullet = BABYLON.MeshBuilder.CreateSphere("bullet", {diameter: 0.2}, scene);
bullet.position = origin.add(forward);
bullet.material = new BABYLON.StandardMaterial("bulletMat", scene);
bullet.material.emissiveColor = new BABYLON.Color3(1, 1, 0);
// Animate bullet
var bulletAnimation = scene.beginDirectAnimation(bullet, [new BABYLON.Animation("bulletAnim", "position", 60, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE, false, function () {
bullet.dispose();
})], 0, 60, false, 1, function () {
bullet.dispose();
});
var keys = [];
keys.push({ frame: 0, value: bullet.position });
keys.push({ frame: 60, value: bullet.position.add(forward.scale(100)) });
bulletAnimation.getAnimations()[0].setKeys(keys);
// Check for hit
for (var i = 0; i < enemies.length; i++) {
var enemy = enemies[i];
var hit = BABYLON.Vector3.Distance(bullet.position, enemy.position) < 2;
if (hit) {
enemy.dispose();
enemies.splice(i, 1);
break;
}
}
};
// Input handling
window.addEventListener("mousedown", function () {
shoot();
});
// Game loop
scene.registerBeforeRender(function () {
// Enemy AI (simple movement towards the player)
enemies.forEach(function (enemy) {
var direction = camera.position.subtract(enemy.position).normalize().scale(0.02);
enemy.moveWithCollisions(direction);
});
});
return scene;
};
var scene = createScene();
// Render loop
engine.runRenderLoop(function () {
scene.render();
});
// Resize
window.addEventListener("resize", function () {
engine.resize();
});
</script>
</body>
</html>
Explanation of the Code:
Setup:
Scene Creation:
Scene is created with gravity enabled for basic physics.UniversalCamera is used to simulate an FPS camera, with gravity and collisions enabled.Lighting and Ground:
Randomly Generated Map:
Player's Gun:
Enemies:
Shooting Mechanism:
mousedown event), a shooting function is called.Game Loop:
Controls:
W, A, S, D keys (inherent to UniversalCamera) are used for movement.How to Run the Game:
.html file (e.g., fps_game.html).W, A, S, D keys to move around.Notes for Further Improvements:
Collision Detection Enhancement:
scene.pickWithRay()).Enemy Behavior:
Graphics and Assets:
User Interface:
Performance Optimization:
Enjoy experimenting with this basic FPS game and feel free to expand upon it to create a more complete gaming experience!
Not yet included in a published release.