A multiplayer sword fighting game engine with character management, round-based combat, and real-time multiplayer support
npm install swordfight-engineA JavaScript-based multiplayer sword fighting game engine featuring round-based combat with a unique "book swapping" mechanic where combat outcomes are determined by the defender's vulnerability tables.
- 8 Playable Characters with unique speed profiles and strategies
- Event-driven Architecture for easy UI integration
- Multiplayer Support via WebSocket or Cloudflare Durable Objects
- Computer Opponent for single-player gameplay
- Dual Build System: Full version (115KB) with bundled data, or lite version (17KB) that uses the API
- Static API with pre-computed outcomes for custom client development
- CLI Client - Terminal-based game interface
- Web Client - Browser-based game interface
``bash`
npm install swordfight-engine
`javascript
import { Game } from 'swordfight-engine';
// Create and initialize game
const game = new Game('computer', 'fighter');
await game.initialize();
await game.connect();
// Listen for game events
document.addEventListener('round', (event) => {
const { myRoundData, opponentsRoundData } = event.detail;
// Handle round results
});
// Send moves
const moveEvent = new CustomEvent('inputMove', {
detail: { move: 'attack-high' }
});
document.dispatchEvent(moveEvent);
`
`javascript
import { Game, CharacterLoader } from 'swordfight-engine/lite';
import { DurableObjectTransport } from 'swordfight-engine/transports';
// Configure API endpoint for lite version
CharacterLoader.setApiBase('https://api.swordfight.me');
// Create and initialize game
const game = new Game('room-123', 'fighter');
await game.initialize();
// Connect with transport
const transport = new DurableObjectTransport(game, {
serverUrl: 'wss://swordfight.your-username.workers.dev'
});
await game.connect(transport);
`
The engine uses an event-driven architecture for communication between the game logic and UI:
1. Create a game instance with your character
2. Initialize to load character data
3. Connect to establish opponent connection
4. Listen for game events (round, victory, defeat, etc.)inputMove
5. Dispatch events to send your moves
- round - Round completed with combat resultsmove
- - Your move was receivedopponentsMove
- - Opponent's move was receivedname
- - Opponent information receivedvictory
- - You won the gamedefeat
- - You lost the game
`bashInstall dependencies
npm install
MIT License