swordfight-engine
v1.6.24
Published
A multiplayer sword fighting game engine with character management, round-based combat, and real-time multiplayer support
Downloads
685
Maintainers
Readme
SwordFight Engine
A 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.
Features
- 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
Play SwordFight
- CLI Client - Terminal-based game interface
- Web Client - Browser-based game interface
Installation
npm install swordfight-engineQuick Start
Single Player (Computer Opponent)
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);Multiplayer
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);How It Works
The engine uses an event-driven architecture for communication between the game logic and UI:
- Create a game instance with your character
- Initialize to load character data
- Connect to establish opponent connection
- Listen for game events (
round,victory,defeat, etc.) - Dispatch
inputMoveevents to send your moves
Key Events
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
Development
# Install dependencies
npm install
# Lint code
npm run lint
# Build engine
npm run build
# Build static API
npm run build:api
# Development mode with file watching
npm run devLicense
MIT License
