// Debug alert for mobile debugging
if (typeof debugAlert === 'function') {
debugAlert('object.js starting to load');
}
/**
* Open the game controller overlay
*/
function openGameController() {
const overlayContent = document.getElementById('overlayContent');
try {
overlayContent.innerHTML = `
<div style="height: 100%; overflow: auto; padding: 10px;">
<div id="projectOverview"></div>
</div>
`;
renderProjectOverview();
} catch (error) {
overlayContent.innerHTML = `
<div style="padding: 20px; text-align: center;">
<h2>Game Controller</h2>
<p>Error: ${error.message}</p>
<p>Loading basic view...</p>
</div>
`;
renderBasicView();
}
}
/**
* Render basic fallback view
*/
function renderBasicView() {
const container = document.getElementById('projectOverview');
if (!container) return;
container.innerHTML = `
<div style="padding: 15px; background: #1a1a1a; border-radius: 8px; margin-bottom: 20px;">
<h3 style="color: #6cf; margin: 0 0 10px 0;">🎮 Game Object</h3>
<div style="color: #ccc;">
<div>ID: game_001</div>
<div>Name: My Tile Game</div>
<div>Version: 1.0.0</div>
</div>
</div>
<div style="padding: 15px; background: #1a1a1a; border-radius: 8px; margin-bottom: 20px;">
<h3 style="color: #6cf; margin: 0 0 10px 0;">🧱 Tile Groups</h3>
<div style="color: #ccc;">
Groups: ${typeof groups !== 'undefined' && groups ? groups.length : 0}
</div>
</div>
<div style="padding: 15px; background: #1a1a1a; border-radius: 8px;">
<h3 style="color: #6cf; margin: 0 0 10px 0;">🗺️ Tile Maps</h3>
<div style="color: #ccc;">
Maps: ${typeof tilemaps !== 'undefined' && tilemaps ? tilemaps.length : 0}
</div>
</div>
`;
}
/**
* Render the complete project overview in game controller
*/
function renderProjectOverview() {
const container = document.getElementById('projectOverview');
if (!container) return;
container.innerHTML = '';
try {
// Game Object Section
const gameSection = createGameObjectSection();
container.appendChild(gameSection);
// Tile Groups Section
const groupsSection = createTileGroupsSection();
container.appendChild(groupsSection);
// Tilemaps Section
const mapsSection = createTilemapsSection();
container.appendChild(mapsSection);
// Export section
const exportSection = createExportSection();
container.appendChild(exportSection);
} catch (error) {
container.innerHTML = `<div style="color: #f44; padding: 20px;">Error: ${error.message}</div>`;
}
}
/**
* Create Game Object section
*/
function createGameObjectSection() {
const section = document.createElement('div');
section.style.cssText = 'margin-bottom: 20px; padding: 15px; background: #1a1a1a; border-radius: 8px;';
const title = document.createElement('h3');
title.textContent = '🎮 Game Object';
title.style.cssText = 'margin: 0 0 15px 0; color: #6cf; font-size: 18px;';
section.appendChild(title);
// Simple attribute display instead of complex function calls
const mandatoryDiv = document.createElement('div');
mandatoryDiv.innerHTML = `
<div style="margin-bottom: 10px;">
<strong style="color: #f44;">Mandatory:</strong>
<div style="margin: 8px 0; padding: 10px; background: #333; border-radius: 4px; color: #ccc; font-size: 12px;">
<div>ID: game_001</div>
<div>Name: My Tile Game</div>
<div>Version: 1.0.0</div>
</div>
</div>
`;
section.appendChild(mandatoryDiv);
const changeableDiv = document.createElement('div');
changeableDiv.innerHTML = `
<div style="margin-bottom: 10px;">
<strong style="color: #4a4;">Changeable:</strong>
<div style="margin: 8px 0; padding: 10px; background: #333; border-radius: 4px; color: #ccc; font-size: 12px;">
<div>Rules: {win: "reach_exit", lose: "no_health", score: "collect"}</div>
<div>Physics: {gravity: 300, friction: 0.8, bounce: 0.3}</div>
<div>Theme: retro-pixel</div>
</div>
</div>
`;
section.appendChild(changeableDiv);
const optionalDiv = document.createElement('div');
optionalDiv.innerHTML = `
<div style="margin-bottom: 10px;">
<strong style="color: #888;">Optional:</strong>
<div style="margin: 8px 0; padding: 10px; background: #333; border-radius: 4px; color: #ccc; font-size: 12px;">
<div>UI Layout: minimal-hud</div>
<div>Progression Mode: linear</div>
<div>Settings: {difficulty: "normal", mode: "single"}</div>
</div>
</div>
`;
section.appendChild(optionalDiv);
return section;
}
/**
* Create Tile Groups section
*/
function createTileGroupsSection() {
const section = document.createElement('div');
section.style.cssText = 'margin-bottom: 20px; padding: 15px; background: #1a1a1a; border-radius: 8px;';
const title = document.createElement('h3');
title.textContent = '🧱 Tile Groups';
title.style.cssText = 'margin: 0 0 15px 0; color: #6cf; font-size: 18px;';
section.appendChild(title);
if (typeof groups !== 'undefined' && groups && groups.length > 0) {
groups.forEach((group, index) => {
const groupDiv = document.createElement('div');
groupDiv.style.cssText = 'margin-bottom: 20px; padding: 15px; background: #2a2a2a; border-radius: 6px; border-left: 4px solid #6cf;';
groupDiv.innerHTML = `
<h4 style="margin: 0 0 10px 0; color: #fff;">Group ${index + 1}</h4>
<div style="margin-bottom: 10px;">
<strong style="color: #f44;">Mandatory:</strong>
<div style="margin: 8px 0; padding: 10px; background: #333; border-radius: 4px; color: #ccc; font-size: 12px;">
<div>ID: group_${index + 1}</div>
<div>Name: ${group.name || `Tile Group ${index + 1}`}</div>
<div>Atlas Key: ${group.url ? group.url.split('/').pop() : 'none'}</div>
<div>Tile Size: ${group.tiles && group.tiles[0] ? group.tiles[0].size : 32}px</div>
</div>
</div>
<div style="margin-bottom: 10px;">
<strong style="color: #4a4;">Changeable:</strong>
<div style="margin: 8px 0; padding: 10px; background: #333; border-radius: 4px; color: #ccc; font-size: 12px;">
<div>Members: ${group.tiles ? group.tiles.length : 0} tiles</div>
</div>
</div>
<div style="margin-bottom: 10px;">
<strong style="color: #888;">Optional:</strong>
<div style="margin: 8px 0; padding: 10px; background: #333; border-radius: 4px; color: #ccc; font-size: 12px;">
<div>Collidable: true</div>
<div>Interactions: none</div>
<div>AI Profile: idle</div>
</div>
</div>
`;
// Add tile previews
if (group.tiles && group.tiles.length > 0) {
const previewDiv = document.createElement('div');
previewDiv.style.cssText = 'display: flex; gap: 5px; flex-wrap: wrap; margin-top: 10px;';
group.tiles.slice(0, 10).forEach(tile => {
const tileWrapper = document.createElement('div');
tileWrapper.style.cssText = 'position: relative; display: inline-block;';
const canvas = document.createElement('canvas');
canvas.width = 32;
canvas.height = 32;
canvas.style.cssText = 'border: 2px solid #666; border-radius: 4px; background: #000;';
const ctx = canvas.getContext('2d');
const tempCanvas = document.createElement('canvas');
tempCanvas.width = tile.size;
tempCanvas.height = tile.size;
const tempCtx = tempCanvas.getContext('2d');
tempCtx.putImageData(tile.data, 0, 0);
ctx.drawImage(tempCanvas, 0, 0, tile.size, tile.size, 0, 0, 32, 32);
const idBadge = document.createElement('span');
idBadge.textContent = tile.uniqueId;
idBadge.style.cssText = `
position: absolute; bottom: -2px; right: -2px;
background: rgba(0,0,0,0.8); color: #fff;
font-size: 8px; padding: 1px 3px; border-radius: 2px;
border: 1px solid #6cf;
`;
tileWrapper.appendChild(canvas);
tileWrapper.appendChild(idBadge);
previewDiv.appendChild(tileWrapper);
});
groupDiv.appendChild(previewDiv);
}
section.appendChild(groupDiv);
});
} else {
section.innerHTML += '<div style="color: #888; font-style: italic; padding: 10px;">No tile groups created yet. Use the Tile Picker to create groups.</div>';
}
return section;
}
/**
* Create Tilemaps section
*/
function createTilemapsSection() {
const section = document.createElement('div');
section.style.cssText = 'margin-bottom: 20px; padding: 15px; background: #1a1a1a; border-radius: 8px;';
const title = document.createElement('h3');
title.textContent = '🗺️ Tile Maps';
title.style.cssText = 'margin: 0 0 15px 0; color: #6cf; font-size: 18px;';
section.appendChild(title);
if (typeof tilemaps !== 'undefined' && tilemaps && tilemaps.length > 0) {
tilemaps.forEach((tilemap, index) => {
const tilemapDiv = document.createElement('div');
tilemapDiv.style.cssText = 'margin-bottom: 20px; padding: 15px; background: #2a2a2a; border-radius: 6px; border-left: 4px solid #6cf;';
const isActive = typeof currentTilemapIndex !== 'undefined' && currentTilemapIndex === index;
const totalTiles = tilemap.data ? tilemap.data.filter(t => t !== 0).length : 0;
const fillPercent = ((totalTiles / (tilemap.width * tilemap.height)) * 100).toFixed(1);
tilemapDiv.innerHTML = `
<h4 style="margin: 0 0 10px 0; color: ${isActive ? '#6cf' : '#fff'};">${tilemap.name}${isActive ? ' (Active)' : ''}</h4>
<div style="margin-bottom: 10px;">
<strong style="color: #f44;">Mandatory:</strong>
<div style="margin: 8px 0; padding: 10px; background: #333; border-radius: 4px; color: #ccc; font-size: 12px;">
<div>ID: map_${tilemap.id}</div>
<div>Name: ${tilemap.name}</div>
<div>Rows: ${tilemap.height}</div>
<div>Cols: ${tilemap.width}</div>
</div>
</div>
<div style="margin-bottom: 10px;">
<strong style="color: #4a4;">Changeable:</strong>
<div style="margin: 8px 0; padding: 10px; background: #333; border-radius: 4px; color: #ccc; font-size: 12px;">
<div>Start Points: {player: {x: 0, y: 0}}</div>
<div>Layer Index: 1</div>
<div>Background: #2a2a2a</div>
</div>
</div>
<div style="margin-bottom: 10px;">
<strong style="color: #888;">Optional:</strong>
<div style="margin: 8px 0; padding: 10px; background: #333; border-radius: 4px; color: #ccc; font-size: 12px;">
<div>Fill: ${fillPercent}% (${totalTiles} tiles)</div>
<div>Events: []</div>
<div>Progression: {nextMap: "level2"}</div>
</div>
</div>
`;
// Add 2D array if there's data
if (tilemap.data && tilemap.data.length > 0) {
const arrayDiv = document.createElement('div');
arrayDiv.innerHTML = '<strong style="color: #6cf;">Phaser 2D Array:</strong>';
const arrayContainer = document.createElement('div');
arrayContainer.style.cssText = `
background: #1a1a1a; padding: 10px; border-radius: 4px; margin-top: 5px;
font-family: 'Courier New', monospace; font-size: 10px;
color: #ccc; overflow-x: auto; max-height: 150px; overflow-y: auto;
`;
let arrayText = '[\n';
for (let y = 0; y < tilemap.height; y++) {
let row = ' [';
for (let x = 0; x < tilemap.width; x++) {
const index = y * tilemap.width + x;
const tileId = tilemap.data[index] || 0;
row += tileId.toString().padStart(3, ' ');
if (x < tilemap.width - 1) row += ',';
}
row += ']';
if (y < tilemap.height - 1) row += ',';
arrayText += row + '\n';
}
arrayText += ']';
const formattedArray = arrayText.split('\n').map(line => {
return `<div style="line-height: 1.2;">${line}</div>`;
}).join('');
arrayContainer.innerHTML = formattedArray;
arrayDiv.appendChild(arrayContainer);
const copyBtn = document.createElement('button');
copyBtn.textContent = 'Copy Array';
copyBtn.style.cssText = `
margin-top: 5px; background: #444; color: white; border: none;
padding: 4px 8px; border-radius: 4px; cursor: pointer; font-size: 10px;
`;
copyBtn.addEventListener('click', () => {
navigator.clipboard.writeText(arrayText).then(() => {
copyBtn.textContent = 'Copied!';
setTimeout(() => copyBtn.textContent = 'Copy Array', 1000);
});
});
arrayDiv.appendChild(copyBtn);
tilemapDiv.appendChild(arrayDiv);
}
section.appendChild(tilemapDiv);
});
} else {
section.innerHTML += '<div style="color: #888; font-style: italic; padding: 10px;">No tilemaps created yet. Use the Tilemap Editor to create maps.</div>';
}
return section;
}
/**
* Create export section
*/
function createExportSection() {
const section = document.createElement('div');
section.style.cssText = 'padding: 15px; background: #1a1a1a; border-radius: 8px;';
section.innerHTML = `
<h3 style="margin: 0 0 10px 0; color: #6cf; font-size: 18px;">Export Project</h3>
<button onclick="exportCompleteProject()" style="background: #4a4; color: white; border: none; padding: 10px 20px; border-radius: 6px; cursor: pointer; font-size: 14px;">
Export Complete Project
</button>
`;
return section;
}
{ label: 'Progression', value: JSON.stringify({nextMap: 'level2', condition: 'reach_exit'}), editable: true, type: 'json' },
{ label: 'Fill Percentage', value: `${fillPercent}%`, editable: false },
{ label: 'Metadata', value: JSON.stringify({difficulty: 'normal', notes: 'Auto-generated'}), editable: true, type: 'json' }
]);
tilemapDiv.appendChild(optionalDiv);
section.appendChild(tilemapDiv);
});
} else {
const noMaps = document.createElement('div');
noMaps.textContent = 'No tilemaps created yet. Use the Tilemap Editor to create maps.';
noMaps.style.cssText = 'color: #888; font-style: italic; padding: 10px;';
section.appendChild(noMaps);
}
return section;
}
/**
* Export complete project data
*/
function exportCompleteProject() {
const projectData = {
metadata: {
exportDate: new Date().toISOString(),
version: "1.0",
editor: "Tile Map Editor"
},
tileGroups: typeof groups !== 'undefined' ? groups : [],
tilemaps: typeof tilemaps !== 'undefined' ? tilemaps : [],
statistics: calculateProjectStats()
};
// Create downloadable JSON
const dataStr = JSON.stringify(projectData, null, 2);
const dataBlob = new Blob([dataStr], {type: 'application/json'});
const url = URL.createObjectURL(dataBlob);
// Create download link
const link = document.createElement('a');
link.href = url;
link.download = `tilemap-project-${new Date().toISOString().split('T')[0]}.json`;
// Trigger download
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
alert('Project data exported successfully!');
}
/**
* Calculate project statistics
*/
function calculateProjectStats() {
const stats = {
totalGroups: 0,
totalTiles: 0,
totalMaps: 0,
totalPlacedTiles: 0,
sourceImages: 0,
tileSizes: []
};
// Tile group stats
if (typeof groups !== 'undefined' && groups) {
stats.totalGroups = groups.length;
stats.totalTiles = groups.reduce((sum, group) => sum + (group.tiles ? group.tiles.length : 0), 0);
const uniqueUrls = new Set(groups.map(g => g.url).filter(url => url));
stats.sourceImages = uniqueUrls.size;
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);
}
// Tilemap stats
if (typeof tilemaps !== 'undefined' && tilemaps) {
stats.totalMaps = tilemaps.length;
stats.totalPlacedTiles = tilemaps.reduce((sum, map) => {
return sum + (map.data ? map.data.filter(t => t !== 0).length : 0);
}, 0);
}
return stats;
}
// Debug alert for mobile debugging - success
if (typeof debugAlert === 'function') {
debugAlert('object.js loaded successfully');
}