# WebRTC Multiplayer Setup

The game now supports WebRTC multiplayer for WebAssembly (browser) builds!

## How It Works

The `NetworkManager` automatically detects the platform:
- **Native builds** (Windows, Linux, Mac): Uses **ENet** (traditional UDP networking)
- **Web builds**: Uses **WebRTC** (peer-to-peer browser networking)

## Important Note About WebRTC

WebRTC requires a **signaling mechanism** to establish connections between peers. This is different from ENet which uses direct IP:port connections.

### Current Implementation Status

✅ **Done:**
- Auto-detection of web platform
- WebRTCMultiplayerPeer creation for host and client
- Basic connection setup
- **STUN server configured** (`stun:ruinborn.thefirstboss.com:3578`) for NAT traversal

⚠️ **Requires Additional Setup:**
- **Signaling server** to exchange connection info between peers
- Session Description Protocol (SDP) offer/answer exchange
- ICE candidate exchange

## Quick Setup Options

### Option 1: Use a Public Signaling Server (Easiest)

Use a service like:
- [peerjs-server](https://github.com/peers/peerjs-server)
- [Matchbox](https://github.com/johanhelsing/matchbox) (Godot-specific)
- Roll your own simple WebSocket server

### Option 2: Manual Connection (For Testing)

For local testing, you can manually exchange connection strings:
1. Host creates a game
2. Host copies their connection info
3. Joiner pastes host's connection info
4. Joiner generates their own connection info
5. Host pastes joiner's connection info

### Option 3: Implement Signaling Server (Recommended for Production)

Create a simple WebSocket signaling server that:
1. Maintains a list of active lobbies
2. Relays SDP offers/answers between peers
3. Relays ICE candidates between peers

## Example: Using Matchbox (Recommended)

Matchbox is a simple signaling server specifically for Godot WebRTC:

```bash
# Install matchbox server
cargo install matchbox_server

# Run the server
matchbox_server
```

Then update the code to connect to your matchbox server when in web mode.

## Testing WebRTC Locally

1. **Export for HTML5**:
   - Project -> Export -> Add -> HTML5 (WebAssembly)
   - Export the project

2. **Run a local web server**:
   ```bash
   # Python 3
   python -m http.server 8000
   
   # Or use any web server
   ```

3. **Open in browsers**:
   - Host: `http://localhost:8000`
   - Client: `http://localhost:8000` (in another browser tab/window)

4. **Connect**:
   - One browser hosts
   - Other browser joins
   - (Requires signaling server to be running)

## Alternative: WebSocket Multiplayer

If WebRTC setup is too complex, you can use WebSocketMultiplayerPeer instead:

```gdscript
# In network_manager.gd, replace WebRTCMultiplayerPeer with:
var peer = WebSocketMultiplayerPeer.new()

# For host:
peer.create_server(port)

# For client:
peer.create_client("ws://your-server:port")
```

WebSocket is simpler but requires a dedicated server (can't be P2P like WebRTC).

## Production Deployment

For production, you'll need:
1. A signaling server (for WebRTC) or game server (for WebSocket)
2. HTTPS hosting (required for WebRTC in browsers)
3. Proper CORS configuration
4. STUN/TURN servers for NAT traversal (optional but recommended)

## Current Status

The game will:
- ✅ Detect web platform automatically
- ✅ Create WebRTC peers
- ⚠️ Require signaling server setup to actually connect

**To complete the setup**, implement one of the options above based on your needs.

## Quick Recommendation

**For Testing**: Use Matchbox (Option 3)
**For Production**: Implement a proper signaling server with lobby/matchmaking

## Resources

- [Godot WebRTC Documentation](https://docs.godotengine.org/en/stable/tutorials/networking/webrtc.html)
- [Matchbox Server](https://github.com/johanhelsing/matchbox)
- [WebRTC for Godot Tutorial](https://docs.godotengine.org/en/stable/tutorials/networking/high_level_multiplayer.html#webrtc)
