How Multiplayer Works
Mokepon features a real-time multiplayer system where players can see each other moving across the map and engage in battles. The game supports both human players and CPU-controlled opponents, creating a dynamic and engaging gameplay experience.The multiplayer server runs on Heroku at
https://mokepon-ed1d40aff3a6.herokuapp.com and handles all real-time game state synchronization.Player Session System
Joining the Game
When a player starts the game, they connect to the server via the/join endpoint:
When the first player joins, the server automatically spawns CPU opponents if none exist, ensuring there are always enemies to battle.
Player Class Structure
Players are represented on the server using aPlayer class:
- id: Unique identifier for each player
- xPercent/yPercent: Normalized position coordinates (0-100% of map size)
- mokepon: The selected Mokepon for this player
- attacks: Array of available attacks
- isCPU: Boolean flag distinguishing CPU from human players
Real-Time Position Updates
Position Broadcasting
The multiplayer system uses a percentage-based position system to ensure consistent rendering across different screen sizes. When a player moves, their position is sent to the server:Position Update Response
The server responds with an array of all other players (enemies), including their positions, Mokepon data, and CPU status. This allows each client to render all visible players on their map.
Safe Spawn Positioning
To prevent players from spawning too close to each other, the server provides a safe position endpoint:- Generates random coordinates between 0-95% of map size
- Checks if position is at least 20% map distance from other players
- Makes up to 20 attempts to find a safe spawn location
- Returns the position and whether it’s confirmed safe
Encounter Mechanics
Collision Detection
Players can encounter each other on the map when they get close enough. The distance calculation uses percentage-based coordinates:Battle Initiation
When players collide on the map:- The client detects the collision using the
isTooCloseToPlayermethod - Battle interface is triggered automatically
- Both players’ attack sequences are loaded from the server
- Battle logic executes based on the rock-paper-scissors style combat system
CPU vs Human Opponents
The multiplayer system treats CPU and human opponents differently in several ways:Human Players
- Full control over movement
- Manual attack selection
- Can leave/rejoin games
- Real-time position updates
CPU Opponents
- Fixed spawn positions
- Automated attack selection
- Persistent until no humans remain
- Regenerate when all players leave
Identifying Opponent Type
The server includes theisCPU flag in position updates, allowing clients to:
- Display different visual indicators for CPU vs human opponents
- Apply different battle logic for CPU encounters
- Show appropriate UI messages (“Challenging CPU” vs “Challenging Player”)
Player Lifecycle
Session Duration
A player’s session lasts from when they join until they disconnect:CPU Regeneration
When all human players disconnect, the server automatically regenerates CPU opponents:This regeneration system ensures that each new player session starts with fresh CPU opponents in different positions with potentially different Mokepons.
Technical Details
Server Configuration
The multiplayer server uses:- Express.js for HTTP handling
- CORS with production/development modes
- Compression for response optimization
- Helmet for security headers
Production Setup
In production, CORS is restricted to the Heroku domain. In development, all origins are allowed for easier testing.
API Endpoints Summary
| Endpoint | Method | Purpose |
|---|---|---|
/join | GET | Create new player session |
/mokepon/:playerId | POST | Assign Mokepon to player |
/mokepon/:playerId/position | POST | Update position & get enemies |
/mokepon/:playerId/safePosition | GET | Get safe spawn location |
/mokepon/:playerId/attacks | GET/POST | Get/set player attacks |
/mokepon/:playerId/cpu-attack | GET | Get CPU’s next attack |
/player/:playerId | DELETE | Remove player from game |