📜
variables.js
Back
📝 Javascript ⚡ Executable Ctrl+S: Save • Ctrl+R: Run • Ctrl+F: Find
// Debug alert for mobile debugging if (typeof debugAlert === 'function') { debugAlert('variables.js loaded'); } // ======================================== // FILE MANAGEMENT VARIABLES // ======================================== let selectedImage = null; let selectedImageName = null; let selectedImageDiv = null; let selectedTileSize = null; // ======================================== // TILE PICKER VARIABLES // ======================================== let groups = [{ id: 0, url: null, tiles: [] }]; let currentGroup = 0; let nextUniqueId = 1; // start at 1 (0 = "no object") // ======================================== // TILEMAP VARIABLES // ======================================== let tilemaps = [ { id: 0, name: "Map 1", width: 20, height: 15, tileSize: 32, data: [] } ]; let currentTilemapIndex = 0; let nextTilemapId = 1; let selectedMapTile = null; let activePaletteGroup = 0; // ======================================== // GAME OBJECT VARIABLES // ======================================== let gameObject = { // Mandatory fields id: 'game_001', name: 'My Tile Game', version: '1.0.0', // Changeable fields rules: { win: 'reach_exit', lose: 'no_health', score: 'collect' }, physics: { gravity: 300, friction: 0.8, bounce: 0.3 }, theme: 'retro-pixel', // Optional fields uiLayout: 'minimal-hud', progressionMode: 'linear', settings: { difficulty: 'normal', mode: 'single' } }; // ======================================== // UTILITY FUNCTIONS FOR VARIABLE ACCESS // ======================================== /** * Get current tilemap helper */ function getCurrentTilemap() { return tilemaps[currentTilemapIndex]; } /** * Get current group helper */ function getCurrentGroup() { return groups[currentGroup]; } /** * Reset all variables to initial state */ function resetAllVariables() { // Reset file variables selectedImage = null; selectedImageName = null; selectedImageDiv = null; selectedTileSize = null; // Reset tile picker variables groups = [{ id: 0, url: null, tiles: [] }]; currentGroup = 0; nextUniqueId = 1; // Reset tilemap variables tilemaps = [ { id: 0, name: "Map 1", width: 20, height: 15, tileSize: 32, data: [] } ]; currentTilemapIndex = 0; nextTilemapId = 1; selectedMapTile = null; activePaletteGroup = 0; // Reset game object to defaults gameObject = { id: 'game_001', name: 'My Tile Game', version: '1.0.0', rules: { win: 'reach_exit', lose: 'no_health', score: 'collect' }, physics: { gravity: 300, friction: 0.8, bounce: 0.3 }, theme: 'retro-pixel', uiLayout: 'minimal-hud', progressionMode: 'linear', settings: { difficulty: 'normal', mode: 'single' } }; } /** * Get project statistics */ function getProjectStats() { const stats = { totalGroups: groups.length, totalTiles: groups.reduce((sum, group) => sum + (group.tiles ? group.tiles.length : 0), 0), totalMaps: tilemaps.length, totalPlacedTiles: tilemaps.reduce((sum, map) => { return sum + (map.data ? map.data.filter(t => t !== 0).length : 0); }, 0), sourceImages: 0, tileSizes: [] }; // Calculate unique source images const uniqueUrls = new Set(groups.map(g => g.url).filter(url => url)); stats.sourceImages = uniqueUrls.size; // Calculate tile sizes used const sizes = new Set(); groups.forEach(group => { if (group.tiles) { group.tiles.forEach(tile => { if (tile.size) sizes.add(tile.size); }); } }); stats.tileSizes = Array.from(sizes).sort((a, b) => a - b); return stats; } /** * Export all project data */ function exportProjectData() { return { metadata: { exportDate: new Date().toISOString(), version: "1.0", editor: "Tile Game Editor" }, gameObject: gameObject, tileGroups: groups, tilemaps: tilemaps, statistics: getProjectStats() }; } /** * Import project data */ function importProjectData(projectData) { try { if (projectData.gameObject) { gameObject = projectData.gameObject; } if (projectData.tileGroups) { groups = projectData.tileGroups; // Update nextUniqueId to avoid conflicts let maxId = 0; groups.forEach(group => { if (group.tiles) { group.tiles.forEach(tile => { if (tile.uniqueId > maxId) maxId = tile.uniqueId; }); } }); nextUniqueId = maxId + 1; } if (projectData.tilemaps) { tilemaps = projectData.tilemaps; // Update nextTilemapId to avoid conflicts let maxMapId = 0; tilemaps.forEach(map => { if (map.id > maxMapId) maxMapId = map.id; }); nextTilemapId = maxMapId + 1; } return true; } catch (error) { console.error('Import failed:', error); return false; } } // Debug alert for mobile debugging - success if (typeof debugAlert === 'function') { debugAlert('variables.js loaded successfully'); }