dungeon builder p3
This commit is contained in:
@@ -148,6 +148,7 @@ let stampData = {};
|
||||
|
||||
const loadedSheets = {};
|
||||
const tileDefs = @Html.Raw(Model.TileDefinitionsJson);
|
||||
const tileDefMap = {};
|
||||
|
||||
function preloadSheets() {
|
||||
document.querySelectorAll('#sheetSelect option').forEach(opt => {
|
||||
@@ -167,6 +168,9 @@ function preloadSheets() {
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
preloadSheets();
|
||||
tileDefs.forEach(t => {
|
||||
tileDefMap[`${t.sheetId}:${t.frameIndex}`] = t.id;
|
||||
});
|
||||
loadExisting();
|
||||
buildStampGrid();
|
||||
});
|
||||
@@ -245,20 +249,17 @@ function loadSheet() {
|
||||
|
||||
currentSheetId = +opt.value;
|
||||
const sheet = loadedSheets[currentSheetId];
|
||||
const tiles = tileDefs.filter(t => t.sheetId === currentSheetId);
|
||||
|
||||
const total = sheet.cols * Math.ceil(sheet.sh / sheet.th);
|
||||
let html = '';
|
||||
if (tiles.length === 0) {
|
||||
html = '<div class="text-gray-500 text-xs p-2">No tile definitions for this sheet.</div>';
|
||||
} else {
|
||||
tiles.forEach(t => {
|
||||
const col = t.frameIndex % sheet.cols;
|
||||
const row = Math.floor(t.frameIndex / sheet.cols);
|
||||
html += `<div class="palette-tile" data-frame="${t.frameIndex}" data-tile-id="${t.id}" onclick="selectTile(${t.frameIndex}, ${t.id})"
|
||||
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;"></div>`;
|
||||
});
|
||||
for (let i = 0; i < total; i++) {
|
||||
const col = i % sheet.cols;
|
||||
const row = Math.floor(i / sheet.cols);
|
||||
const tileDefId = tileDefMap[`${currentSheetId}:${i}`] || 0;
|
||||
html += `<div class="palette-tile" data-frame="${i}" data-tile-id="${tileDefId}" onclick="selectTile(${i}, ${tileDefId})"
|
||||
title="Frame ${i}${tileDefId ? '' : ' (no definition)'}"
|
||||
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;"></div>`;
|
||||
}
|
||||
document.getElementById('palette').innerHTML = html;
|
||||
}
|
||||
@@ -268,9 +269,9 @@ function selectTile(frameIndex, tileId) {
|
||||
document.querySelector(`.palette-tile[data-frame="${frameIndex}"]`)?.classList.add('active');
|
||||
|
||||
const sheet = loadedSheets[currentSheetId];
|
||||
selected = { sheetId: currentSheetId, frameIndex, tileDefinitionId: tileId };
|
||||
selected = { sheetId: currentSheetId, frameIndex, tileDefinitionId: tileId || 0 };
|
||||
|
||||
document.getElementById('selectedInfo').textContent = `Frame ${frameIndex}`;
|
||||
document.getElementById('selectedInfo').textContent = `Frame ${frameIndex}${tileId ? '' : ' (no definition)'}`;
|
||||
|
||||
const col = frameIndex % sheet.cols;
|
||||
const row = Math.floor(frameIndex / sheet.cols);
|
||||
|
||||
@@ -297,6 +297,56 @@ else
|
||||
</div>
|
||||
}
|
||||
|
||||
<!-- Tile Definition Setup Modal -->
|
||||
<div id="tileConfigModal" class="fixed inset-0 bg-black/60 z-[1100] hidden items-center justify-center p-4">
|
||||
<div class="bg-gray-900 border border-gray-700 rounded-xl shadow-xl p-4 w-full max-w-xl">
|
||||
<div class="flex items-center justify-between mb-4">
|
||||
<h3 class="text-lg font-semibold text-white">Setup Tile Definition</h3>
|
||||
<button onclick="closeTileConfigModal()" class="text-gray-400 hover:text-white">✕</button>
|
||||
</div>
|
||||
<div class="text-xs text-gray-400 mb-3" id="tileConfigInfo">Frame</div>
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-3">
|
||||
<div>
|
||||
<label class="text-xs text-gray-400">Name</label>
|
||||
<input id="tileConfigName" class="input w-full text-sm" placeholder="Stone Floor" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="text-xs text-gray-400">Type</label>
|
||||
<select id="tileConfigType" class="input w-full text-sm">
|
||||
<option value="Floor">Floor</option>
|
||||
<option value="Wall">Wall</option>
|
||||
<option value="Door">Door</option>
|
||||
<option value="FloorDecor">FloorDecor</option>
|
||||
<option value="WallDecor">WallDecor</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid grid-cols-2 gap-2 mt-3">
|
||||
<label class="flex items-center gap-2 text-sm text-gray-300">
|
||||
<input type="checkbox" id="tileConfigWalkable" class="rounded" /> Walkable
|
||||
</label>
|
||||
<label class="flex items-center gap-2 text-sm text-gray-300">
|
||||
<input type="checkbox" id="tileConfigBlocksLos" class="rounded" /> Blocks LOS
|
||||
</label>
|
||||
<label class="flex items-center gap-2 text-sm text-gray-300">
|
||||
<input type="checkbox" id="tileConfigBlocksProj" class="rounded" /> Blocks Attacks
|
||||
</label>
|
||||
<label class="flex items-center gap-2 text-sm text-gray-300">
|
||||
<input type="checkbox" id="tileConfigInteractable" class="rounded" /> Interactable
|
||||
</label>
|
||||
</div>
|
||||
<div class="mt-4">
|
||||
<label class="text-xs text-gray-400 mb-1 block">Buckets</label>
|
||||
<input type="text" id="bucketFilterModal" class="input w-full text-xs mb-2" placeholder="Filter buckets..." oninput="filterBucketsModal()" />
|
||||
<div id="bucketListModal" class="max-h-48 overflow-auto border border-gray-700 rounded p-2 space-y-1 text-xs"></div>
|
||||
</div>
|
||||
<div class="flex justify-end gap-2 mt-4">
|
||||
<button class="btn btn-ghost" onclick="closeTileConfigModal()">Cancel</button>
|
||||
<button class="btn btn-primary" onclick="saveTileDefinition()">Save</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@section Scripts {
|
||||
<script>
|
||||
const dungeonId = '@Model.Dungeon?.Id';
|
||||
@@ -308,6 +358,7 @@ else
|
||||
let tileSheets = @Html.Raw(Model.TileSheetsJson);
|
||||
let tileDefinitions = @Html.Raw(Model.TileDefinitionsJson);
|
||||
let tileStamps = @Html.Raw(Model.TileStampsJson);
|
||||
let tileBuckets = @Html.Raw(Model.TileBucketsJson);
|
||||
|
||||
let autoRefresh = true, refreshInterval;
|
||||
let zoom = 1.0;
|
||||
@@ -323,6 +374,8 @@ else
|
||||
let expandedSheets = {};
|
||||
const tileDefMap = {};
|
||||
const stampMap = {};
|
||||
const tileDefBySprite = {};
|
||||
let pendingTileConfig = null;
|
||||
|
||||
// ==================== INIT ====================
|
||||
|
||||
@@ -330,6 +383,7 @@ else
|
||||
console.log('[Admin] Init - tileSheets:', tileSheets.length);
|
||||
tileDefinitions.forEach(t => {
|
||||
tileDefMap[t.id] = t;
|
||||
tileDefBySprite[`${t.sheetId}:${t.frameIndex}`] = t;
|
||||
});
|
||||
tileStamps.forEach(s => {
|
||||
try {
|
||||
@@ -530,6 +584,7 @@ else
|
||||
selectedFrameIndex = frameIndex;
|
||||
selectedStampId = 0;
|
||||
renderStampList(document.getElementById('stampFilter')?.value || '');
|
||||
ensureTileConfigured(sheetId, frameIndex);
|
||||
|
||||
const sheet = tileSheets.find(s => s.id === sheetId);
|
||||
const texture = sheetTextures[sheetId];
|
||||
@@ -565,6 +620,123 @@ else
|
||||
// Auto-switch to tile mode
|
||||
setEditMode('tile');
|
||||
}
|
||||
|
||||
function ensureTileConfigured(sheetId, frameIndex) {
|
||||
const def = tileDefBySprite[`${sheetId}:${frameIndex}`];
|
||||
if (!def || !def.bucketIds || def.bucketIds.length === 0) {
|
||||
openTileConfigModal(sheetId, frameIndex, def);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function openTileConfigModal(sheetId, frameIndex, def) {
|
||||
pendingTileConfig = { sheetId, frameIndex };
|
||||
document.getElementById('tileConfigInfo').textContent = `Sheet ${sheetId} - Frame ${frameIndex}`;
|
||||
document.getElementById('tileConfigName').value = def?.name || '';
|
||||
document.getElementById('tileConfigType').value = def?.type || 'Floor';
|
||||
document.getElementById('tileConfigWalkable').checked = def?.isWalkable ?? true;
|
||||
document.getElementById('tileConfigBlocksLos').checked = def?.blocksLineOfSight ?? false;
|
||||
document.getElementById('tileConfigBlocksProj').checked = def?.blocksProjectiles ?? false;
|
||||
document.getElementById('tileConfigInteractable').checked = def?.isInteractable ?? false;
|
||||
renderBucketModal(def?.bucketIds || []);
|
||||
document.getElementById('tileConfigModal').classList.remove('hidden');
|
||||
document.getElementById('tileConfigModal').classList.add('flex');
|
||||
}
|
||||
|
||||
document.addEventListener('change', e => {
|
||||
if (e.target?.id === 'tileConfigType') {
|
||||
const type = e.target.value;
|
||||
if (type === 'Wall' || type === 'WallDecor') {
|
||||
document.getElementById('tileConfigWalkable').checked = false;
|
||||
document.getElementById('tileConfigBlocksLos').checked = true;
|
||||
document.getElementById('tileConfigBlocksProj').checked = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function closeTileConfigModal() {
|
||||
pendingTileConfig = null;
|
||||
document.getElementById('tileConfigModal').classList.add('hidden');
|
||||
document.getElementById('tileConfigModal').classList.remove('flex');
|
||||
}
|
||||
|
||||
function renderBucketModal(selectedIds) {
|
||||
const list = document.getElementById('bucketListModal');
|
||||
const q = (document.getElementById('bucketFilterModal')?.value || '').toLowerCase();
|
||||
const items = tileBuckets.filter(b => !q || (b.name || '').toLowerCase().includes(q));
|
||||
list.innerHTML = items.map(b => `
|
||||
<label class="flex items-center gap-2 text-gray-300">
|
||||
<input type="checkbox" class="bucket-check-modal" value="${b.id}" ${selectedIds.includes(b.id) ? 'checked' : ''} />
|
||||
<span>${b.name}</span>
|
||||
${b.type ? `<span class="text-gray-500 ml-auto">${b.type}</span>` : ''}
|
||||
</label>
|
||||
`).join('');
|
||||
}
|
||||
|
||||
function filterBucketsModal() {
|
||||
const selected = Array.from(document.querySelectorAll('.bucket-check-modal:checked')).map(c => parseInt(c.value));
|
||||
renderBucketModal(selected);
|
||||
}
|
||||
|
||||
async function saveTileDefinition() {
|
||||
if (!pendingTileConfig) return;
|
||||
const name = document.getElementById('tileConfigName').value;
|
||||
const type = document.getElementById('tileConfigType').value;
|
||||
const walkable = document.getElementById('tileConfigWalkable').checked;
|
||||
const blocksLos = document.getElementById('tileConfigBlocksLos').checked;
|
||||
const blocksProj = document.getElementById('tileConfigBlocksProj').checked;
|
||||
const interactable = document.getElementById('tileConfigInteractable').checked;
|
||||
const bucketIds = Array.from(document.querySelectorAll('.bucket-check-modal:checked')).map(c => c.value);
|
||||
const params = new URLSearchParams({
|
||||
id: dungeonId,
|
||||
sheetId: pendingTileConfig.sheetId,
|
||||
frameIndex: pendingTileConfig.frameIndex,
|
||||
name,
|
||||
type,
|
||||
isWalkable: walkable,
|
||||
blocksLineOfSight: blocksLos,
|
||||
blocksProjectiles: blocksProj,
|
||||
isInteractable: interactable
|
||||
});
|
||||
bucketIds.forEach(b => params.append('bucketIds', b));
|
||||
const resp = await fetch(`/admin/dungeons/view?handler=SaveTileDefinition&${params.toString()}`, {
|
||||
method: 'POST',
|
||||
headers: { 'RequestVerificationToken': document.querySelector('[name="__RequestVerificationToken"]').value }
|
||||
});
|
||||
|
||||
let savedId = 0;
|
||||
try {
|
||||
const data = await resp.json();
|
||||
savedId = data.tileDefinitionId || 0;
|
||||
} catch {}
|
||||
|
||||
const def = {
|
||||
id: savedId || 0,
|
||||
sheetId: pendingTileConfig.sheetId,
|
||||
frameIndex: pendingTileConfig.frameIndex,
|
||||
isWalkable: walkable,
|
||||
blocksLineOfSight: blocksLos,
|
||||
blocksProjectiles: blocksProj,
|
||||
isInteractable: interactable,
|
||||
type,
|
||||
bucketIds: bucketIds.map(b => parseInt(b))
|
||||
};
|
||||
// Insert or update local caches
|
||||
const existing = tileDefinitions.find(t => t.sheetId === def.sheetId && t.frameIndex === def.frameIndex);
|
||||
if (existing) {
|
||||
Object.assign(existing, def);
|
||||
def.id = existing.id;
|
||||
} else {
|
||||
tileDefinitions.push(def);
|
||||
}
|
||||
if (def.id) {
|
||||
tileDefMap[def.id] = def;
|
||||
}
|
||||
tileDefBySprite[`${def.sheetId}:${def.frameIndex}`] = def;
|
||||
|
||||
closeTileConfigModal();
|
||||
}
|
||||
|
||||
// ==================== ZOOM ====================
|
||||
|
||||
@@ -641,6 +813,7 @@ else
|
||||
handler = 'SetTile'; params += '&walkable=false';
|
||||
walkableData[y * mapWidth + x] = false;
|
||||
} else if (editMode === 'tile' && selectedSheetId > 0) {
|
||||
if (!ensureTileConfigured(selectedSheetId, selectedFrameIndex)) return;
|
||||
handler = 'PlaceVisualTile';
|
||||
const layer = parseInt(document.getElementById('tileLayer').value);
|
||||
const walkable = document.getElementById('tileWalkable').checked;
|
||||
@@ -686,6 +859,12 @@ else
|
||||
walkable = def.isWalkable;
|
||||
}
|
||||
|
||||
if (!ensureTileConfigured(sheetId, frameIndex)) return;
|
||||
const def = tileDefBySprite[`${sheetId}:${frameIndex}`];
|
||||
if (def) {
|
||||
walkable = def.isWalkable;
|
||||
}
|
||||
|
||||
const params = `id=${dungeonId}&x=${tx}&y=${ty}&sheetId=${sheetId}&frameIndex=${frameIndex}&layer=${layer}&walkable=${walkable}`;
|
||||
await fetch(`/admin/dungeons/view?handler=PlaceVisualTile&${params}`, {
|
||||
method: 'POST', headers: { 'RequestVerificationToken': document.querySelector('[name="__RequestVerificationToken"]').value }
|
||||
|
||||
@@ -29,6 +29,7 @@ public class ViewModel : AdminPageModel
|
||||
public string TileSheetsJson { get; set; } = "[]";
|
||||
public string TileDefinitionsJson { get; set; } = "[]";
|
||||
public string TileStampsJson { get; set; } = "[]";
|
||||
public string TileBucketsJson { get; set; } = "[]";
|
||||
public int VisualTileCount { get; set; }
|
||||
public List<DungeonMobDefinition> MobDefinitions { get; set; } = new();
|
||||
public List<MobAbilityDefinition> AbilityDefinitions { get; set; } = new();
|
||||
@@ -68,16 +69,35 @@ public class ViewModel : AdminPageModel
|
||||
cols = s.Columns
|
||||
}));
|
||||
|
||||
var bucketLinks = await _db.TileDefinitionBuckets.ToListAsync();
|
||||
var bucketByTile = bucketLinks
|
||||
.GroupBy(b => b.TileDefinitionId)
|
||||
.ToDictionary(g => g.Key, g => g.Select(x => x.TileBucketId).ToList());
|
||||
|
||||
var tileDefs = await _db.TileDefinitions
|
||||
.Where(t => t.IsEnabled)
|
||||
.Select(t => new {
|
||||
id = t.Id,
|
||||
sheetId = t.TileMapSheetId,
|
||||
frameIndex = t.FrameIndex,
|
||||
isWalkable = t.IsWalkable
|
||||
})
|
||||
.ToListAsync();
|
||||
TileDefinitionsJson = JsonSerializer.Serialize(tileDefs);
|
||||
|
||||
var tileDefsPayload = tileDefs.Select(t => new {
|
||||
id = t.Id,
|
||||
sheetId = t.TileMapSheetId,
|
||||
frameIndex = t.FrameIndex,
|
||||
name = t.Name,
|
||||
isWalkable = t.IsWalkable,
|
||||
blocksProjectiles = t.BlocksProjectiles,
|
||||
blocksLineOfSight = t.BlocksLineOfSight,
|
||||
isInteractable = t.IsInteractable,
|
||||
type = t.Type.ToString(),
|
||||
bucketIds = bucketByTile.ContainsKey(t.Id) ? bucketByTile[t.Id] : new List<int>()
|
||||
}).ToList();
|
||||
TileDefinitionsJson = JsonSerializer.Serialize(tileDefsPayload);
|
||||
|
||||
var buckets = await _db.TileBuckets
|
||||
.Where(b => b.IsEnabled)
|
||||
.OrderBy(b => b.Name)
|
||||
.Select(b => new { id = b.Id, name = b.Name, type = b.BucketType })
|
||||
.ToListAsync();
|
||||
TileBucketsJson = JsonSerializer.Serialize(buckets);
|
||||
|
||||
var stamps = await _db.TileStamps
|
||||
.Where(s => s.IsEnabled)
|
||||
@@ -172,6 +192,48 @@ public class ViewModel : AdminPageModel
|
||||
{
|
||||
return new JsonResult(new { success = _dungeonHandler.RemoveTile(id, x, y, layer) });
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostSaveTileDefinitionAsync(Guid id, int sheetId, int frameIndex, string? name, string type,
|
||||
bool isWalkable, bool blocksLineOfSight, bool blocksProjectiles, bool isInteractable, int[]? bucketIds)
|
||||
{
|
||||
var tile = await _db.TileDefinitions
|
||||
.FirstOrDefaultAsync(t => t.TileMapSheetId == sheetId && t.FrameIndex == frameIndex);
|
||||
|
||||
if (tile == null)
|
||||
{
|
||||
tile = new TileDefinition
|
||||
{
|
||||
TileMapSheetId = sheetId,
|
||||
FrameIndex = frameIndex,
|
||||
IsEnabled = true
|
||||
};
|
||||
_db.TileDefinitions.Add(tile);
|
||||
}
|
||||
|
||||
tile.Name = name;
|
||||
tile.Type = Enum.TryParse<TileType>(type, out var parsed) ? parsed : TileType.Floor;
|
||||
tile.IsWalkable = (tile.Type == TileType.Wall || tile.Type == TileType.WallDecor) ? false : isWalkable;
|
||||
tile.BlocksLineOfSight = blocksLineOfSight;
|
||||
tile.BlocksProjectiles = blocksProjectiles;
|
||||
tile.IsInteractable = isInteractable;
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
var existing = _db.TileDefinitionBuckets.Where(b => b.TileDefinitionId == tile.Id);
|
||||
_db.TileDefinitionBuckets.RemoveRange(existing);
|
||||
foreach (var bid in (bucketIds ?? Array.Empty<int>()).Distinct())
|
||||
{
|
||||
_db.TileDefinitionBuckets.Add(new TileDefinitionBucket
|
||||
{
|
||||
TileDefinitionId = tile.Id,
|
||||
TileBucketId = bid
|
||||
});
|
||||
}
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return new JsonResult(new { success = true, tileDefinitionId = tile.Id });
|
||||
}
|
||||
|
||||
public IActionResult OnPostFillRect(Guid id, int x1, int y1, int x2, int y2, bool walkable)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user