import { RpgClientEngine, RpgClientEngineHooks, defineModule } from '@rpgjs/client'
const engine: RpgClientEngineHooks = {
async onStart(engine: RpgClientEngine) {
console.log('🎮 Starting RPG Client...')
// Load game configuration
try {
const slug = 'my-game-project'
const response = await fetch(`/api/game/project/${slug}`)
const config = await response.json()
engine.globalConfig = config
engine.gameVersion = config.version
// Initialize client systems
engine.analytics = new GameAnalytics(config.analyticsKey)
engine.errorLogger = new ErrorLogger(config.errorReportingUrl)
console.log('✅ Client initialized successfully')
return true
} catch (error) {
console.error('❌ Failed to initialize client:', error)
return false
}
},
onStep(engine: RpgClientEngine, t: number, dt: number) {
// Update performance metrics
engine.performanceMonitor?.update(dt)
// Update custom systems
engine.particleSystem?.update(dt)
engine.audioManager?.update(dt)
// Auto-save progress every 30 seconds
if (t % 30000 < dt) {
engine.autoSave()
}
},
onInput(engine: RpgClientEngine, { input, playerId }) {
// Handle debug commands
if (engine.debugMode) {
switch (input) {
case 'F3':
engine.gui('debug-info').toggle()
break
case 'F4':
engine.toggleWireframe()
break
}
}
// Handle screenshot
if (input === 'F12') {
engine.takeScreenshot()
}
},
onConnected(engine: RpgClientEngine, socket: any) {
console.log('🌐 Connected to game server')
// Initialize multiplayer features
engine.chat.enable()
engine.voiceChat.initialize()
// Send client capabilities
socket.emit('client-capabilities', {
webGL: engine.renderer.isWebGL,
audioContext: !!window.AudioContext,
gamepadSupport: !!navigator.getGamepads,
touchSupport: 'ontouchstart' in window
})
engine.gui('loading').hide()
engine.gui('hud').show()
},
onDisconnect(engine: RpgClientEngine, reason: any, socket: any) {
console.log('🔌 Disconnected:', reason)
// Disable multiplayer features
engine.chat.disable()
engine.voiceChat.disconnect()
// Show appropriate UI
engine.gui('hud').hide()
engine.gui('disconnected').show({ reason })
},
onConnectError(engine: RpgClientEngine, err: any, socket: any) {
console.error('Connection failed:', err)
engine.gui('connection-error').show({
error: err.message,
onRetry: () => engine.reconnect(),
onOffline: () => engine.startOfflineMode()
})
},
onWindowResize() {
// Responsive design adjustments
const width = window.innerWidth
const height = window.innerHeight
engine.renderer.resize(width, height)
engine.ui.adjustLayout(width, height)
// Mobile optimizations
if (width < 768) {
engine.ui.enableMobileMode()
} else {
engine.ui.disableMobileMode()
}
}
}
export default defineModule({
engine
})