- Introduction
- Demo
- Features
- Technical Overview
- Detailed Implementation
- What Works
- Current Limitations
- Installation and Setup
- How to Play
- Potential Enhancements
- Contributing
- License
- Acknowledgments
This project is a demonstration of a 3D First-Person Shooter (FPS) maze game created using HTML, CSS, JavaScript, and the Three.js library. It showcases what's possible in browser-based 3D gaming and serves as a learning resource for those interested in game development with web technologies.
The most notable aspect of this project is its rapid development time. Using GPT-O1, OpenAI's latest language model with advanced reasoning capabilities, I was able to create this complex demonstration in less than 40 minutes.
Try the demo here: https://mohdmahmodi.com/fps/
- Procedurally generated 3D mazes
- First-person shooter mechanics
- Advanced enemy AI with optimized A* pathfinding
- Two types of power-ups: Walk Through Walls and Fast Shooting
- Progressive difficulty with increasing maze size and enemy count
- Minimap for navigation with toggle for full view
- Performance optimization techniques
- Health system with visual health bar
- Score tracking and round management
- Smooth level transitions with visual effects
This demonstration is built entirely in JavaScript using Three.js for 3D rendering. Key components include:
- Three.js scene setup with main gameplay and minimap scenes
- Procedural maze generation using a depth-first search algorithm
- Custom player controls for first-person movement and camera
- Optimized A* pathfinding for enemy AI using a Min-Heap (Priority Queue)
- Basic collision detection using bounding boxes and spheres
- Power-up system with two types of power-ups
- Health system with visual representation
- Performance optimizations for smooth rendering
- User interface elements for game state and player feedback
The maze is generated using a depth-first search algorithm:
- Initialize a 2D array representing the maze, filled with walls.
- Start from a random cell (usually (1,1)) and mark it as a path.
- Push the starting cell onto a stack.
- While the stack is not empty:
- Pop a cell from the stack.
- Check its neighbors (up, right, down, left) that are two steps away.
- If a neighbor hasn't been visited:
- Remove the wall between the current cell and the chosen neighbor.
- Mark the neighbor as visited.
- Push the neighbor onto the stack.
- This process creates a perfect maze with no loops or isolated areas.
The maze size increases with each round, providing progressively more challenging levels.
Player movement and camera controls are implemented using custom JavaScript:
- Mouse movement is captured to control the camera's pitch and yaw.
- Keyboard input (W, A, S, D or arrow keys) is used for movement.
- The player's position is updated based on the input and delta time.
- Collision detection is performed after movement to prevent wall clipping.
Shooting mechanics are implemented with a cooldown system and bullet management.
Enemies use an optimized A* pathfinding algorithm to navigate the maze:
- The maze is represented as a grid of nodes.
- Each node stores its position, whether it's walkable, and pathfinding metadata (f, g, h costs).
- The A* algorithm uses a Min-Heap as a priority queue for efficiency.
- Paths are recalculated periodically or when the player moves significantly.
- Enemies move along the calculated path, updating their position each frame.
Enemies have simple animations for walking and a basic attack system when in close proximity to the player.
The game uses two Three.js WebGLRenderer instances:
- Main scene renderer:
- Uses a perspective camera for the player's view.
- Renders the 3D maze, enemies, bullets, and power-ups.
- Minimap renderer:
- Uses an orthographic camera for a top-down view.
- Renders a simplified version of the maze and entity positions.
- Can be toggled between a local view and a full maze view.
Both renderers update every frame in the main game loop.
Collision detection uses a simple sphere-box intersection test:
- Create a bounding sphere around the player.
- For each wall in the maze, create a bounding box.
- Check for intersections between the player's sphere and wall boxes.
- If a collision is detected, reverse the most recent movement.
Similar collision detection is used for bullet-enemy interactions.
The game features two types of power-ups:
- Walk Through Walls: Allows the player to pass through maze walls temporarily.
- Fast Shooting: Increases the player's shooting speed for a limited time.
Power-ups are represented as 3D objects in the maze and on the minimap. When collected, they provide temporary effects that modify game mechanics.
The player has a health system with the following features:
- Visual health bar displayed on the screen.
- Health decreases when attacked by enemies.
- Color-coded health bar (green, yellow, red) based on remaining health.
- Game over when health reaches zero.
The game includes several UI elements:
- Health bar: Visual representation of player health.
- Scoreboard: Displays current round and remaining enemies.
- Power-up display: Shows active power-ups and their remaining duration.
- FPS counter: Displays current frames per second.
- Crosshair: Indicates the center of the screen for aiming.
- Message system: Displays temporary messages for game events.
- Black overlay: Used for smooth transitions between levels.
The game uses a fixed time step game loop:
- The target frame rate is set to 60 FPS.
- Each frame, the game state is updated based on the elapsed time.
- Rendering occurs after state updates.
- FPS is calculated and displayed for performance monitoring.
Performance optimizations include:
- Using
MeshBasicMaterial
instead of more complex materials. - Disabling shadows and antialiasing.
- Limiting the number of active bullets.
- Reduced geometry for enemies and bullets.
- Throttling certain updates (e.g., pathfinding) to occur less frequently.
- Maze Generation: Successfully creates random, solvable mazes for each level.
- First-Person Controls: Smooth movement and camera controls in a first-person perspective.
- Enemy AI: Enemies navigate the maze and chase the player using optimized pathfinding.
- Shooting Mechanics: Players can shoot bullets that collide with and eliminate enemies.
- Power-Up System: Two types of power-ups that temporarily modify game mechanics.
- Minimap: Toggleable minimap that shows player and enemy positions, with full maze view option.
- Progressive Difficulty: Increasing maze size and enemy count as rounds progress.
- Health System: Functional health tracking with visual representation.
- User Interface: Informative UI elements including health bar, round information, and FPS counter.
- Level Transitions: Smooth transitions between levels with visual effects.
-
Collision Detection:
- Players may occasionally clip through walls, especially at corners.
- The sphere-box collision method is simple but not always precise.
-
Enemy Behavior:
- Enemies may get stuck in certain maze configurations.
- Attack mechanics are basic and may feel inconsistent.
-
Performance:
- Frame rate may drop in later rounds with larger mazes and more enemies.
- No level-of-detail system for optimizing rendering of distant objects.
-
Audio:
- Currently, there is no sound implementation.
-
Browser Compatibility:
- Relies on modern JavaScript features and WebGL, may not work in older browsers.
-
Mobile Support:
- Not optimized for touch controls or mobile screens.
-
Save System:
- No way to save progress or high scores between sessions.
-
Variety:
- Limited enemy types and power-ups may lead to repetitive gameplay.
-
Clone the repository:
git clone https://github.com/yourusername/3d-fps-maze-game.git
-
Navigate to the project directory:
cd 3d-fps-maze-game
-
Open
index.html
in a modern web browser that supports WebGL.
Note: Running through a local web server is recommended to avoid potential CORS issues.
- Click on the game window to enable mouse controls.
- Use W/A/S/D or arrow keys to move.
- Move the mouse to look around.
- Left-click to shoot.
- Press 'M' to toggle minimap size.
- Walk over power-ups to activate them:
- Blue power-up: Walk Through Walls
- Red power-up: Fast Shooting
- Eliminate enemies to progress to the next round.
- Monitor your health bar and avoid enemy attacks.
- The game ends when your health reaches zero.
- Improved collision detection using more sophisticated algorithms.
- Enhanced enemy AI with varied behaviors and difficulty levels.
- Additional weapon types and power-ups.
- Implement audio for sound effects and background music.
- Optimization for mobile devices and touch controls.
- Save system for storing progress and high scores.
- More varied level designs and objectives.
- Multiplayer functionality.
- Advanced graphics with dynamic lighting and shadows.
- Integration with a backend for leaderboards and user accounts.
This project is primarily a demonstration, but suggestions for improvements are welcome. If you'd like to contribute:
- Fork the repository
- Create a feature branch (
git checkout -b feature/YourFeature
) - Commit your changes (
git commit -m 'Add YourFeature'
) - Push to the branch (
git push origin feature/YourFeature
) - Open a Pull Request
This project is open source and available under the MIT License.
- Developed by: Mohd Mahmodi
- Contact: [email protected]
- X / Twitter: @mohdmahmodi
- GPT-O1 by OpenAI for development assistance
- Three.js library for 3D rendering
This project demonstrates the potential of AI-assisted rapid prototyping in game development and serves as a starting point for those interested in browser-based 3D games.