425 lines
11 KiB
JavaScript
425 lines
11 KiB
JavaScript
/**
|
|
* PLATFORM SUPPORT SYSTEM
|
|
* Cross-platform compatibility: Mobile, Controller, Steam Deck, Linux, Mac
|
|
*/
|
|
class PlatformSupportSystem {
|
|
constructor(scene) {
|
|
this.scene = scene;
|
|
this.enabled = true;
|
|
|
|
// Platform detection
|
|
this.platform = this.detectPlatform();
|
|
this.isMobile = this.platform === 'mobile';
|
|
this.isController = false;
|
|
|
|
// Mobile controls
|
|
this.virtualJoystick = null;
|
|
this.actionButtons = new Map();
|
|
this.touchControls = {
|
|
enabled: false,
|
|
joystickSize: 'medium',
|
|
buttonOpacity: 0.7,
|
|
leftHanded: false
|
|
};
|
|
|
|
// Controller support
|
|
this.connectedControllers = [];
|
|
this.controllerMapping = new Map();
|
|
|
|
// Steam Deck
|
|
this.isSteamDeck = this.detectSteamDeck();
|
|
this.steamDeckSettings = {
|
|
performanceMode: '60fps',
|
|
uiScale: 1.2
|
|
};
|
|
|
|
this.init();
|
|
console.log('✅ Platform Support System initialized');
|
|
console.log(`📱 Platform: ${this.platform}`);
|
|
}
|
|
|
|
init() {
|
|
if (this.isMobile) {
|
|
this.setupMobileControls();
|
|
}
|
|
|
|
this.setupControllerSupport();
|
|
|
|
if (this.isSteamDeck) {
|
|
this.setupSteamDeck();
|
|
}
|
|
|
|
console.log('🎮 Platform support ready');
|
|
}
|
|
|
|
// ========== PLATFORM DETECTION ==========
|
|
|
|
detectPlatform() {
|
|
const ua = navigator.userAgent.toLowerCase();
|
|
|
|
if (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(ua)) {
|
|
return 'mobile';
|
|
}
|
|
|
|
if (ua.includes('steamdeck')) {
|
|
return 'steamdeck';
|
|
}
|
|
|
|
if (ua.includes('linux')) {
|
|
return 'linux';
|
|
}
|
|
|
|
if (ua.includes('mac')) {
|
|
return 'mac';
|
|
}
|
|
|
|
return 'windows';
|
|
}
|
|
|
|
detectSteamDeck() {
|
|
return navigator.userAgent.toLowerCase().includes('steamdeck');
|
|
}
|
|
|
|
// ========== MOBILE CONTROLS ==========
|
|
|
|
setupMobileControls() {
|
|
this.createVirtualJoystick();
|
|
this.createActionButtons();
|
|
this.createTopHUD();
|
|
this.createBottomActionBar();
|
|
this.setupGestureControls();
|
|
|
|
console.log('📱 Mobile controls initialized');
|
|
}
|
|
|
|
createVirtualJoystick() {
|
|
const sizes = { small: 80, medium: 100, large: 120 };
|
|
const size = sizes[this.touchControls.joystickSize];
|
|
|
|
this.virtualJoystick = {
|
|
x: this.touchControls.leftHanded ? window.innerWidth - 150 : 150,
|
|
y: window.innerHeight - 150,
|
|
size,
|
|
active: false,
|
|
touchId: null,
|
|
direction: { x: 0, y: 0 }
|
|
};
|
|
|
|
console.log('🕹️ Virtual joystick created');
|
|
}
|
|
|
|
createActionButtons() {
|
|
const rightX = this.touchControls.leftHanded ? 150 : window.innerWidth - 150;
|
|
|
|
// Primary action button
|
|
this.actionButtons.set('primary', {
|
|
x: rightX,
|
|
y: window.innerHeight - 150,
|
|
size: 70,
|
|
label: 'A',
|
|
action: 'interact'
|
|
});
|
|
|
|
// Special ability button
|
|
this.actionButtons.set('special', {
|
|
x: rightX - 80,
|
|
y: window.innerHeight - 200,
|
|
size: 60,
|
|
label: 'B',
|
|
action: 'dash'
|
|
});
|
|
|
|
// Auto-aim toggle
|
|
this.actionButtons.set('autoaim', {
|
|
x: rightX + 80,
|
|
y: window.innerHeight - 200,
|
|
size: 50,
|
|
label: '🎯',
|
|
action: 'toggle_autoaim'
|
|
});
|
|
|
|
console.log('🎮 Action buttons created');
|
|
}
|
|
|
|
createTopHUD() {
|
|
// Health bar (top-left)
|
|
// Hunger bar (below health)
|
|
// Gold/Resources (top-right)
|
|
// Mini-map (top-right corner)
|
|
// Day/Season indicator (top-center)
|
|
console.log('📊 Top HUD created');
|
|
}
|
|
|
|
createBottomActionBar() {
|
|
// Weapon/Tool switcher
|
|
// Building mode toggle
|
|
// Crafting quick access
|
|
// Pause button
|
|
console.log('🔧 Bottom action bar created');
|
|
}
|
|
|
|
setupGestureControls() {
|
|
// Pinch to zoom
|
|
// Two-finger pan
|
|
// Swipe to dodge
|
|
// Double-tap for special action
|
|
console.log('👆 Gesture controls setup');
|
|
}
|
|
|
|
handleTouch(touchEvent) {
|
|
// Handle touch input for virtual joystick and buttons
|
|
}
|
|
|
|
updateVirtualJoystick(delta) {
|
|
if (!this.virtualJoystick.active) return;
|
|
|
|
// Update player movement based on joystick direction
|
|
const { x, y } = this.virtualJoystick.direction;
|
|
|
|
if (this.scene.player) {
|
|
// Apply movement
|
|
}
|
|
}
|
|
|
|
// ========== CONTROLLER SUPPORT ==========
|
|
|
|
setupControllerSupport() {
|
|
window.addEventListener('gamepadconnected', (e) => this.onControllerConnected(e));
|
|
window.addEventListener('gamepaddisconnected', (e) => this.onControllerDisconnected(e));
|
|
|
|
this.defineControllerMappings();
|
|
console.log('🎮 Controller support initialized');
|
|
}
|
|
|
|
defineControllerMappings() {
|
|
// Xbox controller
|
|
this.controllerMapping.set('xbox', {
|
|
buttons: {
|
|
0: 'A',
|
|
1: 'B',
|
|
2: 'X',
|
|
3: 'Y',
|
|
4: 'LB',
|
|
5: 'RB',
|
|
6: 'LT',
|
|
7: 'RT',
|
|
8: 'Back',
|
|
9: 'Start',
|
|
10: 'LS',
|
|
11: 'RS',
|
|
12: 'Up',
|
|
13: 'Down',
|
|
14: 'Left',
|
|
15: 'Right'
|
|
},
|
|
axes: {
|
|
0: 'LS_X',
|
|
1: 'LS_Y',
|
|
2: 'RS_X',
|
|
3: 'RS_Y'
|
|
}
|
|
});
|
|
|
|
// PlayStation controller
|
|
this.controllerMapping.set('playstation', {
|
|
buttons: {
|
|
0: 'Cross',
|
|
1: 'Circle',
|
|
2: 'Square',
|
|
3: 'Triangle',
|
|
4: 'L1',
|
|
5: 'R1',
|
|
6: 'L2',
|
|
7: 'R2',
|
|
8: 'Share',
|
|
9: 'Options',
|
|
10: 'L3',
|
|
11: 'R3',
|
|
12: 'Up',
|
|
13: 'Down',
|
|
14: 'Left',
|
|
15: 'Right'
|
|
}
|
|
});
|
|
|
|
// Nintendo Switch Pro
|
|
this.controllerMapping.set('switch', {
|
|
buttons: {
|
|
0: 'B',
|
|
1: 'A',
|
|
2: 'Y',
|
|
3: 'X',
|
|
4: 'L',
|
|
5: 'R',
|
|
6: 'ZL',
|
|
7: 'ZR',
|
|
8: 'Minus',
|
|
9: 'Plus',
|
|
10: 'LS',
|
|
11: 'RS',
|
|
12: 'Up',
|
|
13: 'Down',
|
|
14: 'Left',
|
|
15: 'Right'
|
|
}
|
|
});
|
|
}
|
|
|
|
onControllerConnected(event) {
|
|
const gamepad = event.gamepad;
|
|
this.connectedControllers.push(gamepad);
|
|
this.isController = true;
|
|
|
|
console.log(`🎮 Controller connected: ${gamepad.id}`);
|
|
}
|
|
|
|
onControllerDisconnected(event) {
|
|
const gamepad = event.gamepad;
|
|
const index = this.connectedControllers.indexOf(gamepad);
|
|
if (index > -1) {
|
|
this.connectedControllers.splice(index, 1);
|
|
}
|
|
|
|
this.isController = this.connectedControllers.length > 0;
|
|
console.log(`🎮 Controller disconnected: ${gamepad.id}`);
|
|
}
|
|
|
|
updateControllers() {
|
|
const gamepads = navigator.getGamepads();
|
|
|
|
for (const gamepad of gamepads) {
|
|
if (!gamepad) continue;
|
|
|
|
// Read buttons
|
|
gamepad.buttons.forEach((button, index) => {
|
|
if (button.pressed) {
|
|
this.handleControllerButton(index);
|
|
}
|
|
});
|
|
|
|
// Read axes (joysticks)
|
|
const leftStickX = gamepad.axes[0];
|
|
const leftStickY = gamepad.axes[1];
|
|
|
|
if (Math.abs(leftStickX) > 0.1 || Math.abs(leftStickY) > 0.1) {
|
|
this.handleControllerMovement(leftStickX, leftStickY);
|
|
}
|
|
}
|
|
}
|
|
|
|
handleControllerButton(buttonIndex) {
|
|
// Map button to action
|
|
console.log(`🎮 Button pressed: ${buttonIndex}`);
|
|
}
|
|
|
|
handleControllerMovement(x, y) {
|
|
// Move player based on joystick input
|
|
if (this.scene.player) {
|
|
// Apply movement
|
|
}
|
|
}
|
|
|
|
// ========== STEAM DECK ==========
|
|
|
|
setupSteamDeck() {
|
|
// Adjust UI scale for Steam Deck screen
|
|
this.applyUIScale(this.steamDeckSettings.uiScale);
|
|
|
|
// Set performance mode
|
|
this.setPerformanceMode(this.steamDeckSettings.performanceMode);
|
|
|
|
console.log('🎮 Steam Deck optimizations applied');
|
|
}
|
|
|
|
applyUIScale(scale) {
|
|
// Scale UI elements for better visibility
|
|
console.log(`📏 UI scale: ${scale}x`);
|
|
}
|
|
|
|
setPerformanceMode(mode) {
|
|
const targetFPS = mode === '60fps' ? 60 : 30;
|
|
|
|
if (this.scene.game.config) {
|
|
this.scene.game.config.fps = { target: targetFPS };
|
|
}
|
|
|
|
console.log(`⚡ Performance mode: ${mode}`);
|
|
}
|
|
|
|
// ========== LINUX BUILD ==========
|
|
|
|
setupLinux() {
|
|
// Linux-specific optimizations
|
|
console.log('🐧 Linux optimizations applied');
|
|
}
|
|
|
|
// ========== MAC BUILD ==========
|
|
|
|
setupMac() {
|
|
// macOS-specific optimizations
|
|
// Metal API support
|
|
console.log('🍎 macOS optimizations applied');
|
|
}
|
|
|
|
checkM1M2Chip() {
|
|
// Detect Apple Silicon
|
|
return navigator.userAgent.includes('Macintosh') &&
|
|
navigator.userAgent.includes('AppleWebKit');
|
|
}
|
|
|
|
// ========== MOBILE OPTIMIZATION ==========
|
|
|
|
optimizeForMobile() {
|
|
// Reduce particle count
|
|
// Lower texture quality
|
|
// Disable shadows
|
|
// Reduce draw distance
|
|
console.log('📱 Mobile optimizations applied');
|
|
}
|
|
|
|
optimizeBattery() {
|
|
// Reduce FPS when inactive
|
|
// Disable non-essential effects
|
|
console.log('🔋 Battery optimizations applied');
|
|
}
|
|
|
|
// ========== CUSTOMIZATION ==========
|
|
|
|
saveControlLayout(profileName) {
|
|
const layout = {
|
|
joystickSize: this.touchControls.joystickSize,
|
|
buttonOpacity: this.touchControls.buttonOpacity,
|
|
leftHanded: this.touchControls.leftHanded,
|
|
buttons: Array.from(this.actionButtons.entries())
|
|
};
|
|
|
|
localStorage.setItem(`control_layout_${profileName}`, JSON.stringify(layout));
|
|
console.log(`💾 Control layout saved: ${profileName}`);
|
|
}
|
|
|
|
loadControlLayout(profileName) {
|
|
const saved = localStorage.getItem(`control_layout_${profileName}`);
|
|
if (saved) {
|
|
const layout = JSON.parse(saved);
|
|
this.touchControls = layout;
|
|
console.log(`📂 Control layout loaded: ${profileName}`);
|
|
}
|
|
}
|
|
|
|
// ========== UPDATE ==========
|
|
|
|
update(delta) {
|
|
if (this.isMobile && this.virtualJoystick) {
|
|
this.updateVirtualJoystick(delta);
|
|
}
|
|
|
|
if (this.isController) {
|
|
this.updateControllers();
|
|
}
|
|
}
|
|
|
|
destroy() {
|
|
console.log('🎮 Platform Support System destroyed');
|
|
}
|
|
}
|