1060 lines
42 KiB
Plaintext
1060 lines
42 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; }
|
|
.stamp-select-item {
|
|
padding: 6px 8px;
|
|
background: #1f2937;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
}
|
|
.stamp-select-item:hover { background: #374151; }
|
|
.stamp-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 flex-wrap 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="decor1" onclick="setLayer('decor1')">Decor 1</button>
|
|
<button type="button" class="tool-btn flex-1 text-xs" data-layer="decor2" onclick="setLayer('decor2')">Decor 2</button>
|
|
<button type="button" class="tool-btn flex-1 text-xs" data-layer="decor3" onclick="setLayer('decor3')">Decor 3</button>
|
|
<button type="button" class="tool-btn flex-1 text-xs" data-layer="decor4" onclick="setLayer('decor4')">Decor 4</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>
|
|
|
|
<!-- Stamp Palette -->
|
|
<div class="card p-4" id="stampPaletteCard">
|
|
<h3 class="font-semibold text-white mb-3">Stamps</h3>
|
|
<div class="mb-2">
|
|
<input type="text" id="stampSearch" class="input w-full text-sm" placeholder="Search stamps..." oninput="filterStamps()" />
|
|
</div>
|
|
<div id="stampList" class="max-h-[200px] overflow-auto space-y-1">
|
|
@foreach (var stamp in Model.TileStamps)
|
|
{
|
|
<div class="stamp-select-item" data-stamp-id="@stamp.Id" data-stamp-name="@stamp.Name" onclick="selectStamp(this)">
|
|
<span class="text-sm">🗂ï¸</span>
|
|
<span class="text-sm text-gray-300">@stamp.Name</span>
|
|
<span class="text-xs text-gray-500">(@stamp.Width x @stamp.Height)</span>
|
|
</div>
|
|
}
|
|
@if (!Model.TileStamps.Any())
|
|
{
|
|
<div class="text-xs text-gray-500">No stamps created yet.</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;
|
|
let currentSheetId = null; // Currently selected sheet for painting
|
|
|
|
// All loaded sheets (by ID) for rendering tiles from any sheet
|
|
const loadedSheets = {};
|
|
|
|
// Data stores - keyed by "x,y"
|
|
let floorData = {}; // Floor tiles (walkable) - { tileDefId, sheetId, frameIndex, isWalkable }
|
|
let wallData = {}; // Wall tiles (not walkable) - { tileDefId, sheetId, frameIndex }
|
|
let decorData = [{}, {}, {}, {}]; // Four decoration layers
|
|
let spawnData = {}; // Spawns - { type, mobId?, mobName? }
|
|
let connectionData = {}; // Connection points (doorways) - { direction }
|
|
|
|
// All tile definitions loaded from server (pre-loaded, no API call needed)
|
|
const allTileDefs = @Html.Raw(Model.TileDefinitionsJson);
|
|
let tileDefs = []; // Filtered for current sheet
|
|
let allTags = new Set();
|
|
const allStamps = @Html.Raw(Model.TileStampsJson);
|
|
const stampMap = {};
|
|
const templateBySprite = {};
|
|
|
|
let mouseDown = false;
|
|
|
|
// Pre-load all sheet data from the dropdown
|
|
function preloadSheets() {
|
|
document.querySelectorAll('#tileSheetSelect option').forEach(opt => {
|
|
if (opt.value) {
|
|
loadedSheets[+opt.value] = {
|
|
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
|
|
};
|
|
}
|
|
});
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
preloadSheets();
|
|
allTileDefs.forEach(t => {
|
|
if (t.sheetId != null) {
|
|
templateBySprite[`${t.sheetId}:${t.frameIndex}`] = t;
|
|
}
|
|
});
|
|
allStamps.forEach(s => {
|
|
stampMap[s.id] = s;
|
|
});
|
|
loadData();
|
|
buildGrid();
|
|
});
|
|
|
|
function loadData() {
|
|
try {
|
|
const td = '@Html.Raw(Model.Room.TileGridJson ?? "[]")';
|
|
JSON.parse(td).forEach(t => {
|
|
const key = `${t.x},${t.y}`;
|
|
const data = {
|
|
tileDefId: t.tileDefinitionId || t.tileDefId || t.templateId || 0,
|
|
sheetId: t.sheetId,
|
|
frameIndex: t.frameIndex,
|
|
isWalkable: t.isWalkable !== false
|
|
};
|
|
const layerIndex = typeof t.layer === 'number' ? t.layer : 0;
|
|
if (layerIndex >= 1 && layerIndex <= 4) {
|
|
decorData[layerIndex - 1][key] = data;
|
|
} else if (data.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');
|
|
// Scale factor for grid display (2x for better visibility)
|
|
const scale = 2;
|
|
|
|
// Get tile size from current sheet or first available sheet or default
|
|
let tileW = 16, tileH = 16;
|
|
if (currentSheetId && loadedSheets[currentSheetId]) {
|
|
tileW = loadedSheets[currentSheetId].tw;
|
|
tileH = loadedSheets[currentSheetId].th;
|
|
} else {
|
|
// Use first available sheet dimensions
|
|
const firstSheet = Object.values(loadedSheets)[0];
|
|
if (firstSheet) {
|
|
tileW = firstSheet.tw;
|
|
tileH = firstSheet.th;
|
|
}
|
|
}
|
|
|
|
const cellW = tileW * scale;
|
|
const cellH = tileH * scale;
|
|
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}`;
|
|
|
|
// Scale factor for display
|
|
const scale = 2;
|
|
|
|
// Check for floor or wall tile
|
|
const floor = floorData[k];
|
|
const wall = wallData[k];
|
|
const tile = floor || wall;
|
|
|
|
if (tile && tile.sheetId && loadedSheets[tile.sheetId]) {
|
|
const tileSheet = loadedSheets[tile.sheetId];
|
|
const col = tile.frameIndex % tileSheet.cols;
|
|
const row = Math.floor(tile.frameIndex / tileSheet.cols);
|
|
c.style.background = `url('${cdnBase}/${tileSheet.cdn}') ${-col*tileSheet.tw*scale}px ${-row*tileSheet.th*scale}px no-repeat`;
|
|
c.style.backgroundSize = `${tileSheet.sw*scale}px ${tileSheet.sh*scale}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 decorLayers = decorData.map(layer => layer[k]).filter(Boolean);
|
|
decorLayers.forEach(d => {
|
|
if (d.sheetId && loadedSheets[d.sheetId]) {
|
|
const s = loadedSheets[d.sheetId];
|
|
const dc = d.frameIndex % s.cols;
|
|
const dr = Math.floor(d.frameIndex / s.cols);
|
|
html += `<div class="absolute inset-0" style="background:url('${cdnBase}/${s.cdn}') ${-dc*s.tw*scale}px ${-dr*s.th*scale}px no-repeat;background-size:${s.sw*scale}px ${s.sh*scale}px;image-rendering:pixelated;"></div>`;
|
|
}
|
|
});
|
|
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 (sel.type === 'stamp') {
|
|
applyStamp(x, y);
|
|
} else if ((layer === 'floor' || layer === 'wall') && sel.type === 'template') {
|
|
// Remove from both to avoid duplicates
|
|
delete floorData[k];
|
|
delete wallData[k];
|
|
|
|
const data = {
|
|
tileDefId: sel.tileDefId,
|
|
sheetId: sel.sheetId,
|
|
frameIndex: sel.frameIndex,
|
|
isWalkable: sel.isWalkable
|
|
};
|
|
|
|
if (sel.isWalkable) {
|
|
floorData[k] = data;
|
|
} else {
|
|
wallData[k] = data;
|
|
}
|
|
} else if (layer.startsWith('decor') && sel.type === 'template') {
|
|
const decorIdx = getDecorLayerIndex(layer);
|
|
if (decorIdx !== null) {
|
|
decorData[decorIdx][k] = {
|
|
tileDefId: sel.tileDefId,
|
|
sheetId: sel.sheetId,
|
|
frameIndex: sel.frameIndex,
|
|
isWalkable: true
|
|
};
|
|
}
|
|
} 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.startsWith('decor')) {
|
|
const decorIdx = getDecorLayerIndex(layer);
|
|
if (decorIdx !== null) delete decorData[decorIdx][k];
|
|
} else if (layer === 'spawn') {
|
|
delete spawnData[k];
|
|
delete connectionData[k];
|
|
}
|
|
} else if (tool === 'fill' && sel?.type === 'template') {
|
|
if (layer.startsWith('decor')) {
|
|
floodFillDecor(x, y);
|
|
} else {
|
|
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 = {
|
|
tileDefId: sel.tileDefId,
|
|
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 floodFillDecor(sx, sy) {
|
|
const decorIdx = getDecorLayerIndex(layer);
|
|
if (decorIdx === null) return;
|
|
const layerData = decorData[decorIdx];
|
|
const sk = `${sx},${sy}`;
|
|
const startTile = layerData[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 = layerData[k];
|
|
if ((current?.frameIndex ?? -1) !== startF) continue;
|
|
|
|
visited.add(k);
|
|
|
|
layerData[k] = {
|
|
tileDefId: sel.tileDefId,
|
|
sheetId: sel.sheetId,
|
|
frameIndex: sel.frameIndex,
|
|
isWalkable: true
|
|
};
|
|
|
|
render(x, y);
|
|
queue.push([x+1,y],[x-1,y],[x,y+1],[x,y-1]);
|
|
}
|
|
}
|
|
|
|
function applyStamp(x, y) {
|
|
const stamp = stampMap[sel.stampId];
|
|
if (!stamp || !stamp.tiles) return;
|
|
|
|
stamp.tiles.forEach(t => {
|
|
const tx = x + t.x;
|
|
const ty = y + t.y;
|
|
if (tx < 0 || tx >= gw || ty < 0 || ty >= gh) return;
|
|
|
|
const template = templateBySprite[`${t.sheetId}:${t.frameIndex}`];
|
|
if (!template) return;
|
|
|
|
const key = `${tx},${ty}`;
|
|
const data = {
|
|
tileDefId: template.id,
|
|
sheetId: template.sheetId,
|
|
frameIndex: template.frameIndex,
|
|
isWalkable: template.isWalkable
|
|
};
|
|
|
|
if (layer.startsWith('decor')) {
|
|
const decorIdx = getDecorLayerIndex(layer);
|
|
if (decorIdx !== null) {
|
|
decorData[decorIdx][key] = { ...data, isWalkable: true };
|
|
}
|
|
} else if (layer === 'floor' || layer === 'wall') {
|
|
delete floorData[key];
|
|
delete wallData[key];
|
|
if (data.isWalkable) floorData[key] = data;
|
|
else wallData[key] = data;
|
|
}
|
|
|
|
render(tx, ty);
|
|
});
|
|
|
|
updateStats();
|
|
saveData();
|
|
}
|
|
|
|
function setTool(t) {
|
|
tool = t;
|
|
document.querySelectorAll('[data-tool]').forEach(b => b.classList.toggle('active', b.dataset.tool === t));
|
|
}
|
|
|
|
function getDecorLayerIndex(layerKey) {
|
|
switch (layerKey) {
|
|
case 'decor1': return 0;
|
|
case 'decor2': return 1;
|
|
case 'decor3': return 2;
|
|
case 'decor4': return 3;
|
|
default: return null;
|
|
}
|
|
}
|
|
|
|
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');
|
|
document.getElementById('stampPaletteCard').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.querySelectorAll('.stamp-select-item').forEach(i => i.classList.remove('selected'));
|
|
document.getElementById('selectionInfo').textContent = 'Nothing';
|
|
document.getElementById('selectionDetails').textContent = '';
|
|
|
|
// Refresh templates when switching between floor/wall layers
|
|
if (l === 'floor' || l === 'wall') {
|
|
renderTemplates();
|
|
} else if (l.startsWith('decor')) {
|
|
renderTemplates();
|
|
}
|
|
}
|
|
|
|
function loadTileTemplates() {
|
|
const opt = document.getElementById('tileSheetSelect').selectedOptions[0];
|
|
if (!opt?.value) {
|
|
currentSheetId = null;
|
|
tileDefs = [];
|
|
document.getElementById('tilePalette').innerHTML = '<div class="text-gray-500 text-xs p-2">Select a TileMap</div>';
|
|
document.getElementById('commonTags').innerHTML = '';
|
|
return;
|
|
}
|
|
|
|
currentSheetId = +opt.value;
|
|
|
|
// Filter templates for this sheet (already loaded, no API call)
|
|
tileDefs = allTileDefs.filter(t => t.sheetId === currentSheetId);
|
|
|
|
// Collect all tags
|
|
allTags.clear();
|
|
tileDefs.forEach(t => {
|
|
if (t.tags) {
|
|
t.tags.split(',').map(tag => tag.trim().toLowerCase()).forEach(tag => {
|
|
if (tag) allTags.add(tag);
|
|
});
|
|
}
|
|
});
|
|
|
|
renderCommonTags();
|
|
renderTemplates();
|
|
|
|
// Don't rebuild grid - just re-render to show existing tiles
|
|
// Grid dimensions stay the same, tiles from other sheets still display
|
|
for (let y = 0; y < gh; y++) {
|
|
for (let x = 0; x < gw; x++) {
|
|
render(x, y);
|
|
}
|
|
}
|
|
}
|
|
|
|
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 = []) {
|
|
const sheet = currentSheetId ? loadedSheets[currentSheetId] : null;
|
|
if (!sheet) {
|
|
document.getElementById('tilePalette').innerHTML = '<div class="text-gray-500 text-xs p-2">Select a TileMap</div>';
|
|
return;
|
|
}
|
|
|
|
let filtered = tileDefs;
|
|
|
|
// 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);
|
|
} else if (layer.startsWith('decor')) {
|
|
filtered = filtered;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Scale factor for palette display (2x for better visibility)
|
|
const scale = 2;
|
|
const displayW = sheet.tw * scale;
|
|
const displayH = sheet.th * scale;
|
|
|
|
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:${displayW + 12}px; padding:6px;"
|
|
onclick="selectPaletteItem(this)">
|
|
<div style="width:${displayW}px; height:${displayH}px; background:url('${cdnBase}/${sheet.cdn}') ${-col*sheet.tw*scale}px ${-row*sheet.th*scale}px no-repeat; background-size:${sheet.sw*scale}px ${sheet.sh*scale}px; position:relative;">
|
|
<div class="walkable-badge ${t.isWalkable ? 'floor' : 'wall'}"></div>
|
|
</div>
|
|
<div class="template-info mt-1" style="width:${displayW}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'));
|
|
document.querySelectorAll('.stamp-select-item').forEach(i => i.classList.remove('selected'));
|
|
el.classList.add('active');
|
|
|
|
const type = el.dataset.type;
|
|
if (type === 'template') {
|
|
sel = {
|
|
type: 'template',
|
|
tileDefId: +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'));
|
|
document.querySelectorAll('.stamp-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 selectStamp(el) {
|
|
document.querySelectorAll('.palette-item').forEach(i => i.classList.remove('active'));
|
|
document.querySelectorAll('.mob-select-item').forEach(i => i.classList.remove('selected'));
|
|
document.querySelectorAll('.stamp-select-item').forEach(i => i.classList.remove('selected'));
|
|
el.classList.add('selected');
|
|
|
|
const stampId = +el.dataset.stampId;
|
|
sel = { type: 'stamp', stampId };
|
|
document.getElementById('selectionInfo').textContent = `Stamp: ${el.dataset.stampName}`;
|
|
document.getElementById('selectionDetails').textContent = '';
|
|
}
|
|
|
|
function filterStamps() {
|
|
const search = document.getElementById('stampSearch').value.toLowerCase();
|
|
document.querySelectorAll('.stamp-select-item').forEach(el => {
|
|
const name = el.dataset.stampName.toLowerCase();
|
|
el.style.display = name.includes(search) ? '' : 'none';
|
|
});
|
|
}
|
|
|
|
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 decors = decorData.reduce((acc, layer) => acc + Object.keys(layer).length, 0);
|
|
const spawns = Object.keys(spawnData).length;
|
|
const connections = Object.keys(connectionData).length;
|
|
document.getElementById('statsInfo').textContent = `Tiles: ${floors} | Walls: ${walls} | Decor: ${decors} | 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, tileDefinitionId: v.tileDefId, 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, tileDefinitionId: v.tileDefId, isWalkable: false });
|
|
});
|
|
decorData.forEach((layerData, idx) => {
|
|
Object.entries(layerData).forEach(([k, v]) => {
|
|
const [x, y] = k.split(',').map(Number);
|
|
tiles.push({ x, y, sheetId: v.sheetId, frameIndex: v.frameIndex, tileDefinitionId: v.tileDefId, layer: idx + 1, isWalkable: true });
|
|
});
|
|
});
|
|
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 if (layer.startsWith('decor')) {
|
|
const decorIdx = getDecorLayerIndex(layer);
|
|
if (decorIdx !== null) decorData[decorIdx] = {};
|
|
}
|
|
else {
|
|
spawnData = {};
|
|
connectionData = {};
|
|
}
|
|
buildGrid();
|
|
saveData();
|
|
}
|
|
|
|
function clearAll() {
|
|
if (!confirm('Clear everything?')) return;
|
|
floorData = {}; wallData = {}; decorData = [{}, {}, {}, {}]; spawnData = {}; connectionData = {};
|
|
buildGrid();
|
|
saveData();
|
|
}
|
|
|
|
document.getElementById('roomForm').onsubmit = () => saveData();
|
|
</script>
|