806 lines
32 KiB
Plaintext
806 lines
32 KiB
Plaintext
@page "/admin/dungeonbuilder/rooms/edit"
|
|
@model DungeonBuilder.RoomEditModel
|
|
@using DataServer.Data.Entities
|
|
@using DataServer.Data.Enums
|
|
@{
|
|
ViewData["Title"] = Model.Room.Id == 0 ? "New Room Template" : $"Edit: {Model.Room.Name}";
|
|
var roomTypes = Enum.GetValues<RoomType>();
|
|
}
|
|
|
|
<style>
|
|
.editor-grid {
|
|
display: grid;
|
|
gap: 1px;
|
|
background: #1a1a2e;
|
|
border: 2px solid #374151;
|
|
border-radius: 4px;
|
|
}
|
|
.editor-cell {
|
|
background: #0f0f1a;
|
|
cursor: crosshair;
|
|
position: relative;
|
|
image-rendering: pixelated;
|
|
}
|
|
.editor-cell:hover {
|
|
outline: 2px solid #60a5fa;
|
|
outline-offset: -2px;
|
|
z-index: 10;
|
|
}
|
|
.editor-cell.connection-point {
|
|
outline: 2px solid #22c55e !important;
|
|
outline-offset: -2px;
|
|
}
|
|
.palette-item {
|
|
cursor: pointer;
|
|
border: 2px solid transparent;
|
|
border-radius: 4px;
|
|
background: #1f2937;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
image-rendering: pixelated;
|
|
box-sizing: content-box;
|
|
position: relative;
|
|
}
|
|
.palette-item:hover { border-color: #60a5fa; }
|
|
.palette-item.active { border-color: #fbbf24; background: #374151; }
|
|
.palette-item .walkable-badge {
|
|
position: absolute;
|
|
top: 1px;
|
|
right: 1px;
|
|
width: 8px;
|
|
height: 8px;
|
|
border-radius: 50%;
|
|
}
|
|
.palette-item .walkable-badge.floor { background: #22c55e; }
|
|
.palette-item .walkable-badge.wall { background: #ef4444; }
|
|
.tool-btn {
|
|
padding: 6px 10px;
|
|
border-radius: 6px;
|
|
font-size: 13px;
|
|
cursor: pointer;
|
|
background: #1f2937;
|
|
color: #d1d5db;
|
|
border: 1px solid #374151;
|
|
}
|
|
.tool-btn:hover { background: #374151; }
|
|
.tool-btn.active { background: #4f46e5; color: white; border-color: #4f46e5; }
|
|
.template-info {
|
|
font-size: 10px;
|
|
color: #9ca3af;
|
|
max-width: 100%;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
.mob-select-item {
|
|
padding: 6px 8px;
|
|
background: #1f2937;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
}
|
|
.mob-select-item:hover { background: #374151; }
|
|
.mob-select-item.selected { background: #4f46e5; }
|
|
.tag-chip {
|
|
display: inline-block;
|
|
padding: 2px 8px;
|
|
background: #374151;
|
|
border-radius: 12px;
|
|
font-size: 11px;
|
|
margin: 2px;
|
|
cursor: pointer;
|
|
}
|
|
.tag-chip:hover { background: #4b5563; }
|
|
.tag-chip.active { background: #4f46e5; }
|
|
</style>
|
|
|
|
<div class="mb-4">
|
|
<a href="/admin/dungeonbuilder/rooms" class="text-blue-400 hover:text-blue-300 text-sm">← Back to Rooms</a>
|
|
</div>
|
|
|
|
@if (!string.IsNullOrEmpty(Model.Message))
|
|
{
|
|
<div class="mb-4 p-3 rounded @(Model.IsError ? "bg-red-900/50 text-red-300" : "bg-green-900/50 text-green-300")">
|
|
@Model.Message
|
|
</div>
|
|
}
|
|
|
|
<div class="grid grid-cols-1 xl:grid-cols-4 gap-4">
|
|
<!-- Left Panel -->
|
|
<div class="xl:col-span-1 space-y-4">
|
|
<form method="post" id="roomForm">
|
|
@Html.AntiForgeryToken()
|
|
<input type="hidden" asp-for="Room.Id" />
|
|
<input type="hidden" asp-for="Room.TileGridJson" id="tileGridJson" />
|
|
<input type="hidden" asp-for="Room.ObjectPlacementsJson" id="objectPlacementsJson" />
|
|
<input type="hidden" asp-for="Room.ConnectionPointsJson" id="connectionPointsJson" />
|
|
<input type="hidden" asp-for="Room.MobSpawnsJson" id="mobSpawnsJson" />
|
|
<input type="hidden" asp-for="Room.PlayerSpawnX" id="playerSpawnX" />
|
|
<input type="hidden" asp-for="Room.PlayerSpawnY" id="playerSpawnY" />
|
|
<input type="hidden" asp-for="Room.ExitPortalX" id="exitPortalX" />
|
|
<input type="hidden" asp-for="Room.ExitPortalY" id="exitPortalY" />
|
|
|
|
<div class="card p-4">
|
|
<h3 class="font-semibold text-white mb-3">Room Settings</h3>
|
|
<div class="space-y-3">
|
|
<div>
|
|
<label class="text-xs text-gray-400">Name</label>
|
|
<input type="text" asp-for="Room.Name" class="input w-full text-sm" required />
|
|
</div>
|
|
<div>
|
|
<label class="text-xs text-gray-400">Type</label>
|
|
<select asp-for="Room.Type" class="input w-full text-sm">
|
|
@foreach (var t in roomTypes)
|
|
{
|
|
<option value="@t">@t</option>
|
|
}
|
|
</select>
|
|
</div>
|
|
<div class="grid grid-cols-2 gap-2">
|
|
<div>
|
|
<label class="text-xs text-gray-400">Width</label>
|
|
<input type="number" asp-for="Room.Width" class="input w-full text-sm" min="3" max="50" id="roomWidth" />
|
|
</div>
|
|
<div>
|
|
<label class="text-xs text-gray-400">Height</label>
|
|
<input type="number" asp-for="Room.Height" class="input w-full text-sm" min="3" max="50" id="roomHeight" />
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<label class="text-xs text-gray-400">Biome</label>
|
|
<input type="text" asp-for="Room.Biome" class="input w-full text-sm" placeholder="dungeon" />
|
|
</div>
|
|
<button type="button" onclick="resizeGrid()" class="btn btn-secondary w-full text-sm">Resize Grid</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card p-4">
|
|
<h3 class="font-semibold text-white mb-3">Tools</h3>
|
|
<div class="flex flex-wrap gap-2 mb-3">
|
|
<button type="button" class="tool-btn active" data-tool="paint" onclick="setTool('paint')">🖌️ Paint</button>
|
|
<button type="button" class="tool-btn" data-tool="erase" onclick="setTool('erase')">🧹 Erase</button>
|
|
<button type="button" class="tool-btn" data-tool="fill" onclick="setTool('fill')">🪣 Fill</button>
|
|
</div>
|
|
<div class="border-t border-gray-700 pt-3">
|
|
<label class="text-xs text-gray-400 mb-2 block">Layer</label>
|
|
<div class="flex gap-1">
|
|
<button type="button" class="tool-btn active flex-1 text-xs" data-layer="floor" onclick="setLayer('floor')">Floor</button>
|
|
<button type="button" class="tool-btn flex-1 text-xs" data-layer="wall" onclick="setLayer('wall')">Wall</button>
|
|
<button type="button" class="tool-btn flex-1 text-xs" data-layer="spawn" onclick="setLayer('spawn')">Spawn</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card p-4">
|
|
<h3 class="font-semibold text-white mb-3">Generation</h3>
|
|
<div class="space-y-3">
|
|
<div>
|
|
<label class="text-xs text-gray-400">Weight</label>
|
|
<input type="number" asp-for="Room.Weight" class="input w-full text-sm" min="1" />
|
|
</div>
|
|
<div class="grid grid-cols-2 gap-2">
|
|
<div>
|
|
<label class="text-xs text-gray-400">Min Doors</label>
|
|
<input type="number" asp-for="Room.MinConnections" class="input w-full text-sm" min="1" />
|
|
</div>
|
|
<div>
|
|
<label class="text-xs text-gray-400">Max Doors</label>
|
|
<input type="number" asp-for="Room.MaxConnections" class="input w-full text-sm" min="1" />
|
|
</div>
|
|
</div>
|
|
<div class="flex items-center gap-2">
|
|
<input type="checkbox" asp-for="Room.AllowRotation" class="rounded" />
|
|
<label class="text-sm text-gray-300">Allow Rotation</label>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-primary w-full">
|
|
@(Model.Room.Id == 0 ? "Create Room" : "Save Changes")
|
|
</button>
|
|
</form>
|
|
</div>
|
|
|
|
<!-- Center: Grid -->
|
|
<div class="xl:col-span-2">
|
|
<div class="card p-4">
|
|
<div class="flex items-center justify-between mb-3">
|
|
<h3 class="font-semibold text-white">Grid Editor</h3>
|
|
<div class="flex gap-2">
|
|
<button type="button" onclick="clearLayer()" class="text-xs text-gray-400 hover:text-white">Clear Layer</button>
|
|
<button type="button" onclick="clearAll()" class="text-xs text-red-400 hover:text-red-300">Clear All</button>
|
|
</div>
|
|
</div>
|
|
<div class="overflow-auto bg-gray-900 rounded p-2" style="max-height: 75vh;">
|
|
<div id="editorGrid" class="editor-grid inline-grid"></div>
|
|
</div>
|
|
<div class="mt-2 text-xs text-gray-500 flex justify-between">
|
|
<span id="cursorPos">(-, -)</span>
|
|
<span id="statsInfo">Tiles: 0 | Walls: 0 | Spawns: 0 | Connections: 0</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Right: Palette -->
|
|
<div class="xl:col-span-1 space-y-4">
|
|
<!-- Tile Palette (Floor/Wall) -->
|
|
<div class="card p-4" id="tilePaletteCard">
|
|
<h3 class="font-semibold text-white mb-3">Tiles</h3>
|
|
|
|
<!-- Sheet Selection -->
|
|
<select id="tileSheetSelect" class="input w-full text-sm mb-2" onchange="loadTileTemplates()">
|
|
<option value="">Select TileMap...</option>
|
|
@foreach (var sheet in Model.TileSheets)
|
|
{
|
|
<option value="@sheet.Id"
|
|
data-cdn="@sheet.CdnPath"
|
|
data-cols="@sheet.Columns"
|
|
data-tw="@sheet.TileWidth"
|
|
data-th="@sheet.TileHeight"
|
|
data-sw="@sheet.SheetWidth"
|
|
data-sh="@sheet.SheetHeight">@sheet.Name</option>
|
|
}
|
|
</select>
|
|
|
|
<!-- Tag Filter -->
|
|
<div class="mb-2">
|
|
<input type="text" id="tagFilter" class="input w-full text-sm" placeholder="Filter by tags (comma separated)..." oninput="filterTemplates()" />
|
|
</div>
|
|
|
|
<!-- Common Tags -->
|
|
<div id="commonTags" class="mb-2 flex flex-wrap"></div>
|
|
|
|
<!-- Templates Grid -->
|
|
<div id="tilePalette" class="flex flex-wrap gap-1 max-h-[300px] overflow-auto">
|
|
<div class="text-gray-500 text-xs p-2">Select a TileMap to see templates</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Spawn Palette -->
|
|
<div class="card p-4 hidden" id="spawnPaletteCard">
|
|
<h3 class="font-semibold text-white mb-3">Spawns</h3>
|
|
<div id="spawnPalette" class="flex flex-wrap gap-1 mb-3">
|
|
<div class="palette-item" data-type="spawn" data-spawn="player" title="Player Spawn" onclick="selectPaletteItem(this)" style="width:32px;height:32px;">🧑</div>
|
|
<div class="palette-item" data-type="spawn" data-spawn="exit" title="Exit Portal" onclick="selectPaletteItem(this)" style="width:32px;height:32px;">🌀</div>
|
|
<div class="palette-item" data-type="spawn" data-spawn="connection" title="Connection Point (Doorway)" onclick="selectPaletteItem(this)" style="width:32px;height:32px;">🚪</div>
|
|
<div class="palette-item" data-type="spawn" data-spawn="chest" title="Chest" onclick="selectPaletteItem(this)" style="width:32px;height:32px;">📦</div>
|
|
</div>
|
|
|
|
<h4 class="font-medium text-white text-sm mb-2">Mob Spawns</h4>
|
|
<div class="mb-2">
|
|
<input type="text" id="mobSearch" class="input w-full text-sm" placeholder="Search mobs..." oninput="filterMobs()" />
|
|
</div>
|
|
<div id="mobList" class="max-h-[200px] overflow-auto space-y-1">
|
|
@foreach (var mob in Model.Mobs)
|
|
{
|
|
<div class="mob-select-item" data-mob-id="@mob.Id" data-mob-name="@mob.Name" data-mob-type="@mob.Type" onclick="selectMob(this)">
|
|
<span class="text-sm">@(mob.Type == MobType.Boss ? "💀" : mob.Type == MobType.Elite ? "⭐" : "👾")</span>
|
|
<span class="text-sm text-gray-300">@mob.Name</span>
|
|
<span class="text-xs text-gray-500">(@mob.Type)</span>
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Selected Info -->
|
|
<div class="card p-4">
|
|
<h3 class="font-semibold text-white mb-2">Selected</h3>
|
|
<div id="selectionInfo" class="text-sm text-gray-400">Nothing</div>
|
|
<div id="selectionDetails" class="text-xs text-gray-500 mt-1"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const cdnBase = '@Model.CdnBaseUrl';
|
|
let gw = @Model.Room.Width, gh = @Model.Room.Height;
|
|
let tool = 'paint', layer = 'floor';
|
|
let sel = null, sheet = null;
|
|
|
|
// Data stores - keyed by "x,y"
|
|
let floorData = {}; // Floor tiles (walkable) - { templateId, sheetId, frameIndex, isWalkable }
|
|
let wallData = {}; // Wall tiles (not walkable) - { templateId, sheetId, frameIndex }
|
|
let spawnData = {}; // Spawns - { type, mobId?, mobName? }
|
|
let connectionData = {}; // Connection points (doorways) - { direction }
|
|
|
|
// All tile templates loaded from server (pre-loaded, no API call needed)
|
|
const allTileTemplates = @Html.Raw(Model.TileTemplatesJson);
|
|
let tileTemplates = []; // Filtered for current sheet
|
|
let allTags = new Set();
|
|
|
|
let mouseDown = false;
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
loadData();
|
|
buildGrid();
|
|
});
|
|
|
|
function loadData() {
|
|
try {
|
|
const td = '@Html.Raw(Model.Room.TileGridJson ?? "[]")';
|
|
JSON.parse(td).forEach(t => {
|
|
// Determine if floor or wall based on stored isWalkable flag or template lookup
|
|
const isWalkable = t.isWalkable !== false; // Default to true if not specified
|
|
const key = `${t.x},${t.y}`;
|
|
const data = {
|
|
templateId: t.templateId || 0,
|
|
sheetId: t.sheetId,
|
|
frameIndex: t.frameIndex,
|
|
isWalkable: isWalkable
|
|
};
|
|
if (isWalkable) {
|
|
floorData[key] = data;
|
|
} else {
|
|
wallData[key] = data;
|
|
}
|
|
});
|
|
} catch {}
|
|
try {
|
|
const cd = '@Html.Raw(Model.Room.ConnectionPointsJson ?? "[]")';
|
|
JSON.parse(cd).forEach(c => {
|
|
connectionData[`${c.X},${c.Y}`] = { direction: c.Direction || 'north' };
|
|
});
|
|
} catch {}
|
|
try {
|
|
const md = '@Html.Raw(Model.Room.MobSpawnsJson ?? "[]")';
|
|
JSON.parse(md).forEach(m => {
|
|
spawnData[`${m.X},${m.Y}`] = {
|
|
type: 'mob',
|
|
mobId: m.MobDefinitionId,
|
|
mobName: m.MobName || 'Mob',
|
|
mobType: m.Type || 'Normal'
|
|
};
|
|
});
|
|
} catch {}
|
|
@if (Model.Room.PlayerSpawnX.HasValue) { <text>spawnData['@Model.Room.PlayerSpawnX,@Model.Room.PlayerSpawnY'] = { type: 'player' };</text> }
|
|
@if (Model.Room.ExitPortalX.HasValue) { <text>spawnData['@Model.Room.ExitPortalX,@Model.Room.ExitPortalY'] = { type: 'exit' };</text> }
|
|
}
|
|
|
|
function buildGrid() {
|
|
const g = document.getElementById('editorGrid');
|
|
const cellW = sheet?.tw || 16;
|
|
const cellH = sheet?.th || 16;
|
|
g.style.gridTemplateColumns = `repeat(${gw}, ${cellW}px)`;
|
|
g.innerHTML = '';
|
|
for (let y = 0; y < gh; y++) {
|
|
for (let x = 0; x < gw; x++) {
|
|
const c = document.createElement('div');
|
|
c.className = 'editor-cell';
|
|
c.style.width = cellW + 'px';
|
|
c.style.height = cellH + 'px';
|
|
c.dataset.x = x; c.dataset.y = y;
|
|
c.onmousedown = e => { mouseDown = true; paint(x, y); };
|
|
c.onmouseenter = () => {
|
|
document.getElementById('cursorPos').textContent = `(${x}, ${y})`;
|
|
if (mouseDown) paint(x, y);
|
|
};
|
|
g.appendChild(c);
|
|
render(x, y);
|
|
}
|
|
}
|
|
document.onmouseup = () => mouseDown = false;
|
|
updateStats();
|
|
}
|
|
|
|
function render(x, y) {
|
|
const c = document.querySelector(`.editor-cell[data-x="${x}"][data-y="${y}"]`);
|
|
if (!c) return;
|
|
const k = `${x},${y}`;
|
|
|
|
// Check for floor or wall tile
|
|
const floor = floorData[k];
|
|
const wall = wallData[k];
|
|
const tile = floor || wall;
|
|
|
|
if (tile && sheet && tile.sheetId == sheet.id) {
|
|
const col = tile.frameIndex % sheet.cols, row = Math.floor(tile.frameIndex / sheet.cols);
|
|
c.style.background = `url('${cdnBase}/${sheet.cdn}') ${-col*sheet.tw}px ${-row*sheet.th}px no-repeat`;
|
|
c.style.backgroundSize = `${sheet.sw}px ${sheet.sh}px`;
|
|
} else if (tile) {
|
|
c.style.background = wall ? '#4a1a1a' : '#2d3748';
|
|
} else {
|
|
c.style.background = '#0f0f1a';
|
|
}
|
|
|
|
// Connection point indicator
|
|
c.classList.toggle('connection-point', !!connectionData[k]);
|
|
|
|
// Overlays
|
|
let html = '';
|
|
const sp = spawnData[k];
|
|
if (sp) {
|
|
const icons = { player:'🧑', exit:'🌀', mob:'👾', chest:'📦' };
|
|
const colors = { player:'bg-yellow-500/40', exit:'bg-blue-500/40', mob:'bg-red-500/30', chest:'bg-amber-500/40' };
|
|
let icon = icons[sp.type] || '?';
|
|
html += `<div class="absolute inset-0 flex items-center justify-center ${colors[sp.type]||'bg-gray-500/30'} text-xs">${icon}</div>`;
|
|
}
|
|
if (connectionData[k]) {
|
|
html += `<div class="absolute inset-0 flex items-center justify-center text-green-400 text-xs font-bold">🚪</div>`;
|
|
}
|
|
c.innerHTML = html;
|
|
}
|
|
|
|
function paint(x, y) {
|
|
const k = `${x},${y}`;
|
|
|
|
if (tool === 'paint' && sel) {
|
|
if ((layer === 'floor' || layer === 'wall') && sel.type === 'template') {
|
|
// Remove from both to avoid duplicates
|
|
delete floorData[k];
|
|
delete wallData[k];
|
|
|
|
const data = {
|
|
templateId: sel.templateId,
|
|
sheetId: sel.sheetId,
|
|
frameIndex: sel.frameIndex,
|
|
isWalkable: sel.isWalkable
|
|
};
|
|
|
|
if (sel.isWalkable) {
|
|
floorData[k] = data;
|
|
} else {
|
|
wallData[k] = data;
|
|
}
|
|
} else if (layer === 'spawn') {
|
|
if (sel.type === 'spawn') {
|
|
if (sel.spawn === 'connection') {
|
|
// Toggle connection point
|
|
if (connectionData[k]) {
|
|
delete connectionData[k];
|
|
} else {
|
|
connectionData[k] = { direction: detectEdgeDirection(x, y) };
|
|
}
|
|
} else if (sel.spawn === 'player' || sel.spawn === 'exit') {
|
|
// Only one player/exit spawn
|
|
Object.keys(spawnData).forEach(key => {
|
|
if (spawnData[key].type === sel.spawn) delete spawnData[key];
|
|
});
|
|
spawnData[k] = { type: sel.spawn };
|
|
} else {
|
|
spawnData[k] = { type: sel.spawn };
|
|
}
|
|
} else if (sel.type === 'mob') {
|
|
spawnData[k] = {
|
|
type: 'mob',
|
|
mobId: sel.mobId,
|
|
mobName: sel.mobName,
|
|
mobType: sel.mobType
|
|
};
|
|
}
|
|
}
|
|
} else if (tool === 'erase') {
|
|
if (layer === 'floor') delete floorData[k];
|
|
else if (layer === 'wall') delete wallData[k];
|
|
else if (layer === 'spawn') {
|
|
delete spawnData[k];
|
|
delete connectionData[k];
|
|
}
|
|
} else if (tool === 'fill' && sel?.type === 'template') {
|
|
floodFill(x, y);
|
|
}
|
|
|
|
render(x, y);
|
|
updateStats();
|
|
saveData();
|
|
}
|
|
|
|
function detectEdgeDirection(x, y) {
|
|
// Detect which edge this connection point is on
|
|
if (y === 0) return 'north';
|
|
if (y === gh - 1) return 'south';
|
|
if (x === 0) return 'west';
|
|
if (x === gw - 1) return 'east';
|
|
return 'north'; // Default
|
|
}
|
|
|
|
function floodFill(sx, sy) {
|
|
const sk = `${sx},${sy}`;
|
|
const startTile = floorData[sk] || wallData[sk];
|
|
const startF = startTile?.frameIndex ?? -1;
|
|
const visited = new Set(), queue = [[sx, sy]];
|
|
|
|
while (queue.length && visited.size < 500) {
|
|
const [x, y] = queue.shift();
|
|
const k = `${x},${y}`;
|
|
if (visited.has(k) || x < 0 || x >= gw || y < 0 || y >= gh) continue;
|
|
|
|
const current = floorData[k] || wallData[k];
|
|
if ((current?.frameIndex ?? -1) !== startF) continue;
|
|
|
|
visited.add(k);
|
|
|
|
// Remove from both
|
|
delete floorData[k];
|
|
delete wallData[k];
|
|
|
|
const data = {
|
|
templateId: sel.templateId,
|
|
sheetId: sel.sheetId,
|
|
frameIndex: sel.frameIndex,
|
|
isWalkable: sel.isWalkable
|
|
};
|
|
|
|
if (sel.isWalkable) {
|
|
floorData[k] = data;
|
|
} else {
|
|
wallData[k] = data;
|
|
}
|
|
|
|
render(x, y);
|
|
queue.push([x+1,y],[x-1,y],[x,y+1],[x,y-1]);
|
|
}
|
|
}
|
|
|
|
function setTool(t) {
|
|
tool = t;
|
|
document.querySelectorAll('[data-tool]').forEach(b => b.classList.toggle('active', b.dataset.tool === t));
|
|
}
|
|
|
|
function setLayer(l) {
|
|
layer = l;
|
|
document.querySelectorAll('[data-layer]').forEach(b => b.classList.toggle('active', b.dataset.layer === l));
|
|
document.getElementById('tilePaletteCard').classList.toggle('hidden', l === 'spawn');
|
|
document.getElementById('spawnPaletteCard').classList.toggle('hidden', l !== 'spawn');
|
|
sel = null;
|
|
document.querySelectorAll('.palette-item').forEach(i => i.classList.remove('active'));
|
|
document.querySelectorAll('.mob-select-item').forEach(i => i.classList.remove('selected'));
|
|
document.getElementById('selectionInfo').textContent = 'Nothing';
|
|
document.getElementById('selectionDetails').textContent = '';
|
|
}
|
|
|
|
function loadTileTemplates() {
|
|
const opt = document.getElementById('tileSheetSelect').selectedOptions[0];
|
|
if (!opt?.value) {
|
|
sheet = null;
|
|
tileTemplates = [];
|
|
document.getElementById('tilePalette').innerHTML = '<div class="text-gray-500 text-xs p-2">Select a TileMap</div>';
|
|
document.getElementById('commonTags').innerHTML = '';
|
|
return;
|
|
}
|
|
|
|
sheet = {
|
|
id: +opt.value,
|
|
cdn: opt.dataset.cdn,
|
|
cols: +opt.dataset.cols,
|
|
tw: +opt.dataset.tw,
|
|
th: +opt.dataset.th,
|
|
sw: +opt.dataset.sw,
|
|
sh: +opt.dataset.sh
|
|
};
|
|
|
|
// Rebuild grid with correct tile dimensions
|
|
buildGrid();
|
|
|
|
// Filter templates for this sheet (already loaded, no API call)
|
|
tileTemplates = allTileTemplates.filter(t => t.sheetId === sheet.id);
|
|
|
|
// Collect all tags
|
|
allTags.clear();
|
|
tileTemplates.forEach(t => {
|
|
if (t.tags) {
|
|
t.tags.split(',').map(tag => tag.trim().toLowerCase()).forEach(tag => {
|
|
if (tag) allTags.add(tag);
|
|
});
|
|
}
|
|
});
|
|
|
|
renderCommonTags();
|
|
renderTemplates();
|
|
}
|
|
|
|
function renderCommonTags() {
|
|
const container = document.getElementById('commonTags');
|
|
const sorted = Array.from(allTags).sort();
|
|
container.innerHTML = sorted.slice(0, 15).map(tag =>
|
|
`<span class="tag-chip" data-tag="${tag}" onclick="toggleTagFilter('${tag}')">${tag}</span>`
|
|
).join('');
|
|
}
|
|
|
|
function toggleTagFilter(tag) {
|
|
const input = document.getElementById('tagFilter');
|
|
const current = input.value.split(',').map(t => t.trim().toLowerCase()).filter(t => t);
|
|
const idx = current.indexOf(tag);
|
|
if (idx >= 0) {
|
|
current.splice(idx, 1);
|
|
} else {
|
|
current.push(tag);
|
|
}
|
|
input.value = current.join(', ');
|
|
filterTemplates();
|
|
|
|
// Update chip states
|
|
document.querySelectorAll('.tag-chip').forEach(chip => {
|
|
chip.classList.toggle('active', current.includes(chip.dataset.tag));
|
|
});
|
|
}
|
|
|
|
function filterTemplates() {
|
|
const filter = document.getElementById('tagFilter').value.toLowerCase();
|
|
const filterTags = filter.split(',').map(t => t.trim()).filter(t => t);
|
|
renderTemplates(filterTags);
|
|
}
|
|
|
|
function renderTemplates(filterTags = []) {
|
|
let filtered = tileTemplates;
|
|
|
|
// Filter by current layer (floor vs wall)
|
|
if (layer === 'floor') {
|
|
filtered = filtered.filter(t => t.isWalkable);
|
|
} else if (layer === 'wall') {
|
|
filtered = filtered.filter(t => !t.isWalkable);
|
|
}
|
|
|
|
// Filter by tags
|
|
if (filterTags.length > 0) {
|
|
filtered = filtered.map(t => {
|
|
const templateTags = (t.tags || '').split(',').map(tag => tag.trim().toLowerCase());
|
|
const matchCount = filterTags.filter(ft => templateTags.includes(ft)).length;
|
|
return { ...t, matchCount };
|
|
}).filter(t => t.matchCount > 0)
|
|
.sort((a, b) => b.matchCount - a.matchCount);
|
|
}
|
|
|
|
if (filtered.length === 0) {
|
|
document.getElementById('tilePalette').innerHTML = `<div class="text-gray-500 text-xs p-2">No ${layer} templates found</div>`;
|
|
return;
|
|
}
|
|
|
|
let html = '';
|
|
filtered.forEach(t => {
|
|
// frameIndex is pre-extracted by the server
|
|
const frameIndex = t.frameIndex || 0;
|
|
|
|
const col = frameIndex % sheet.cols;
|
|
const row = Math.floor(frameIndex / sheet.cols);
|
|
|
|
html += `<div class="palette-item flex-col"
|
|
data-type="template"
|
|
data-template-id="${t.id}"
|
|
data-sheet-id="${sheet.id}"
|
|
data-frame="${frameIndex}"
|
|
data-walkable="${t.isWalkable}"
|
|
data-name="${t.name}"
|
|
data-tags="${t.tags || ''}"
|
|
title="${t.name}${t.tags ? ' [' + t.tags + ']' : ''}"
|
|
style="width:${sheet.tw + 8}px; padding:4px;"
|
|
onclick="selectPaletteItem(this)">
|
|
<div style="width:${sheet.tw}px; height:${sheet.th}px; background:url('${cdnBase}/${sheet.cdn}') ${-col*sheet.tw}px ${-row*sheet.th}px no-repeat; background-size:${sheet.sw}px ${sheet.sh}px; position:relative;">
|
|
<div class="walkable-badge ${t.isWalkable ? 'floor' : 'wall'}"></div>
|
|
</div>
|
|
<div class="template-info mt-1" style="width:${sheet.tw}px;">${t.name}</div>
|
|
</div>`;
|
|
});
|
|
|
|
document.getElementById('tilePalette').innerHTML = html;
|
|
}
|
|
|
|
function selectPaletteItem(el) {
|
|
document.querySelectorAll('.palette-item').forEach(i => i.classList.remove('active'));
|
|
document.querySelectorAll('.mob-select-item').forEach(i => i.classList.remove('selected'));
|
|
el.classList.add('active');
|
|
|
|
const type = el.dataset.type;
|
|
if (type === 'template') {
|
|
sel = {
|
|
type: 'template',
|
|
templateId: +el.dataset.templateId,
|
|
sheetId: +el.dataset.sheetId,
|
|
frameIndex: +el.dataset.frame,
|
|
isWalkable: el.dataset.walkable === 'true',
|
|
name: el.dataset.name
|
|
};
|
|
document.getElementById('selectionInfo').textContent = sel.name;
|
|
document.getElementById('selectionDetails').textContent = sel.isWalkable ? '🟢 Walkable (Floor)' : '🔴 Blocking (Wall)';
|
|
} else if (type === 'spawn') {
|
|
sel = { type: 'spawn', spawn: el.dataset.spawn };
|
|
document.getElementById('selectionInfo').textContent = el.title;
|
|
document.getElementById('selectionDetails').textContent = '';
|
|
}
|
|
}
|
|
|
|
function selectMob(el) {
|
|
document.querySelectorAll('.palette-item').forEach(i => i.classList.remove('active'));
|
|
document.querySelectorAll('.mob-select-item').forEach(i => i.classList.remove('selected'));
|
|
el.classList.add('selected');
|
|
|
|
sel = {
|
|
type: 'mob',
|
|
mobId: +el.dataset.mobId,
|
|
mobName: el.dataset.mobName,
|
|
mobType: el.dataset.mobType
|
|
};
|
|
document.getElementById('selectionInfo').textContent = `Mob: ${sel.mobName}`;
|
|
document.getElementById('selectionDetails').textContent = `Type: ${sel.mobType}`;
|
|
}
|
|
|
|
function filterMobs() {
|
|
const search = document.getElementById('mobSearch').value.toLowerCase();
|
|
document.querySelectorAll('.mob-select-item').forEach(el => {
|
|
const name = el.dataset.mobName.toLowerCase();
|
|
el.style.display = name.includes(search) ? '' : 'none';
|
|
});
|
|
}
|
|
|
|
function updateStats() {
|
|
const floors = Object.keys(floorData).length;
|
|
const walls = Object.keys(wallData).length;
|
|
const spawns = Object.keys(spawnData).length;
|
|
const connections = Object.keys(connectionData).length;
|
|
document.getElementById('statsInfo').textContent = `Tiles: ${floors} | Walls: ${walls} | Spawns: ${spawns} | Connections: ${connections}`;
|
|
}
|
|
|
|
function saveData() {
|
|
// Combine floor and wall data into single TileGridJson
|
|
const tiles = [];
|
|
Object.entries(floorData).forEach(([k, v]) => {
|
|
const [x, y] = k.split(',').map(Number);
|
|
tiles.push({ x, y, sheetId: v.sheetId, frameIndex: v.frameIndex, templateId: v.templateId, isWalkable: true });
|
|
});
|
|
Object.entries(wallData).forEach(([k, v]) => {
|
|
const [x, y] = k.split(',').map(Number);
|
|
tiles.push({ x, y, sheetId: v.sheetId, frameIndex: v.frameIndex, templateId: v.templateId, isWalkable: false });
|
|
});
|
|
document.getElementById('tileGridJson').value = JSON.stringify(tiles);
|
|
|
|
// Connection points
|
|
const conns = Object.entries(connectionData).map(([k, v]) => {
|
|
const [x, y] = k.split(',').map(Number);
|
|
return { X: x, Y: y, Direction: v.direction, Width: 1 };
|
|
});
|
|
document.getElementById('connectionPointsJson').value = JSON.stringify(conns);
|
|
|
|
// Mob spawns
|
|
const mobs = [];
|
|
let px = '', py = '', ex = '', ey = '';
|
|
Object.entries(spawnData).forEach(([k, v]) => {
|
|
const [x, y] = k.split(',').map(Number);
|
|
if (v.type === 'player') { px = x; py = y; }
|
|
else if (v.type === 'exit') { ex = x; ey = y; }
|
|
else if (v.type === 'mob') {
|
|
mobs.push({ X: x, Y: y, MobDefinitionId: v.mobId, MobName: v.mobName, Type: v.mobType, Count: 1 });
|
|
}
|
|
else if (v.type === 'chest') {
|
|
// Could add chest spawns here
|
|
}
|
|
});
|
|
document.getElementById('mobSpawnsJson').value = JSON.stringify(mobs);
|
|
document.getElementById('playerSpawnX').value = px;
|
|
document.getElementById('playerSpawnY').value = py;
|
|
document.getElementById('exitPortalX').value = ex;
|
|
document.getElementById('exitPortalY').value = ey;
|
|
|
|
// Empty object placements for now
|
|
document.getElementById('objectPlacementsJson').value = '[]';
|
|
}
|
|
|
|
function resizeGrid() {
|
|
gw = +document.getElementById('roomWidth').value || 10;
|
|
gh = +document.getElementById('roomHeight').value || 10;
|
|
buildGrid();
|
|
}
|
|
|
|
function clearLayer() {
|
|
if (!confirm(`Clear all ${layer}?`)) return;
|
|
if (layer === 'floor') floorData = {};
|
|
else if (layer === 'wall') wallData = {};
|
|
else {
|
|
spawnData = {};
|
|
connectionData = {};
|
|
}
|
|
buildGrid();
|
|
saveData();
|
|
}
|
|
|
|
function clearAll() {
|
|
if (!confirm('Clear everything?')) return;
|
|
floorData = {}; wallData = {}; spawnData = {}; connectionData = {};
|
|
buildGrid();
|
|
saveData();
|
|
}
|
|
|
|
document.getElementById('roomForm').onsubmit = () => saveData();
|
|
</script>
|