Add world editor chunk clear/delete, zoom, sprite tile rendering, and tile-definition-driven paint
This commit is contained in:
@@ -30,6 +30,15 @@
|
||||
|
||||
var objectsJson = JsonSerializer.Serialize(Model.ObjectDefinitions.Select(o => new { id = o.Id, name = o.Name }));
|
||||
var npcsJson = JsonSerializer.Serialize(Model.NpcDefinitions.Select(n => new { id = n.Id, name = n.Name }));
|
||||
var tileDefsJson = JsonSerializer.Serialize(Model.TileDefinitions.Select(t => new
|
||||
{
|
||||
id = t.Id,
|
||||
sheetId = t.TileMapSheetId,
|
||||
frameIndex = t.FrameIndex,
|
||||
name = t.Name,
|
||||
type = t.Type.ToString(),
|
||||
isWalkable = t.IsWalkable
|
||||
}));
|
||||
}
|
||||
|
||||
<div class="space-y-6">
|
||||
@@ -74,6 +83,10 @@
|
||||
<button type="button" class="btn btn-primary" onclick="loadChunk()">Load Chunk</button>
|
||||
<button type="button" class="btn btn-secondary" onclick="loadZone()">Load Zone</button>
|
||||
</div>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<button type="button" class="btn btn-secondary" onclick="clearChunk()">Clear Chunk</button>
|
||||
<button type="button" class="btn btn-secondary" onclick="deleteChunk()">Delete Chunk</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card p-4 space-y-3">
|
||||
@@ -155,7 +168,13 @@
|
||||
<div class="card p-4">
|
||||
<div class="flex items-center justify-between mb-2">
|
||||
<h2 class="text-lg font-semibold text-white">Chunk Canvas</h2>
|
||||
<div id="chunkMeta" class="text-sm text-gray-400">No chunk loaded</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<button type="button" class="btn btn-secondary px-2 py-1 text-xs" onclick="adjustZoom(-0.25)">-</button>
|
||||
<input id="zoomLevel" type="range" min="0.5" max="4" step="0.25" value="1" onchange="onZoomChanged()" class="w-28" />
|
||||
<button type="button" class="btn btn-secondary px-2 py-1 text-xs" onclick="adjustZoom(0.25)">+</button>
|
||||
<button type="button" class="btn btn-secondary px-2 py-1 text-xs" onclick="resetZoom()">Reset</button>
|
||||
<div id="chunkMeta" class="text-sm text-gray-400 ml-2">No chunk loaded</div>
|
||||
</div>
|
||||
</div>
|
||||
<canvas id="chunkCanvas" width="768" height="768" class="w-full rounded border border-gray-700 bg-gray-900 cursor-crosshair"></canvas>
|
||||
</div>
|
||||
@@ -169,6 +188,7 @@
|
||||
const sheets = @Html.Raw(sheetsJson);
|
||||
const objectDefs = @Html.Raw(objectsJson);
|
||||
const npcDefs = @Html.Raw(npcsJson);
|
||||
const tileDefs = @Html.Raw(tileDefsJson);
|
||||
const cdnBaseUrl = '@Model.CdnBaseUrl';
|
||||
|
||||
let currentRegion = null;
|
||||
@@ -178,6 +198,9 @@
|
||||
let currentObjects = [];
|
||||
let currentNpcs = [];
|
||||
let eventSource = null;
|
||||
let chunkZoom = 1;
|
||||
const tileDefBySprite = {};
|
||||
const sheetImageCache = new Map();
|
||||
|
||||
const regionCanvas = document.getElementById('regionCanvas');
|
||||
const regionCtx = regionCanvas.getContext('2d');
|
||||
@@ -198,6 +221,10 @@
|
||||
function key(x, y) { return `${x},${y}`; }
|
||||
|
||||
function initSelectors() {
|
||||
for (const def of tileDefs) {
|
||||
tileDefBySprite[`${def.sheetId}:${def.frameIndex}`] = def;
|
||||
}
|
||||
|
||||
const regionSel = document.getElementById('regionId');
|
||||
regionSel.innerHTML = regions.map(r => `<option value="${r.id}">${r.name}</option>`).join('');
|
||||
regionSel.value = '@Model.SelectedRegionId';
|
||||
@@ -272,6 +299,7 @@
|
||||
currentChunk = data.chunk;
|
||||
currentTiles = new Map();
|
||||
for (const t of data.tiles) currentTiles.set(key(t.x, t.y), t);
|
||||
for (const t of data.tiles) preloadSheetImage(t.sheetId);
|
||||
currentObjects = data.objects || [];
|
||||
currentNpcs = data.npcs || [];
|
||||
document.getElementById('chunkMeta').textContent = `Chunk ${s.zoneX}:${s.zoneY}:${s.chunkX}:${s.chunkY} v${currentChunk.version} (${currentChunk.width}x${currentChunk.height})`;
|
||||
@@ -333,6 +361,19 @@
|
||||
else currentNpcs.push({ ...op });
|
||||
} else if (op.type === 'npc_remove') {
|
||||
currentNpcs = currentNpcs.filter(n => n.id !== op.id);
|
||||
} else if (op.type === 'chunk_clear') {
|
||||
currentTiles.clear();
|
||||
currentObjects = [];
|
||||
currentNpcs = [];
|
||||
} else if (op.type === 'chunk_deleted') {
|
||||
currentChunk = null;
|
||||
currentTiles.clear();
|
||||
currentObjects = [];
|
||||
currentNpcs = [];
|
||||
chunkCtx.clearRect(0, 0, chunkCanvas.width, chunkCanvas.height);
|
||||
document.getElementById('chunkMeta').textContent = 'Chunk deleted';
|
||||
loadZone();
|
||||
return;
|
||||
} else {
|
||||
loadChunk();
|
||||
return;
|
||||
@@ -400,7 +441,8 @@
|
||||
chunkCtx.clearRect(0, 0, chunkCanvas.width, chunkCanvas.height);
|
||||
if (!currentChunk) return;
|
||||
|
||||
const tileSize = Math.max(6, Math.floor(Math.min(chunkCanvas.width / currentChunk.width, chunkCanvas.height / currentChunk.height)));
|
||||
const baseTileSize = Math.max(6, Math.floor(Math.min(chunkCanvas.width / currentChunk.width, chunkCanvas.height / currentChunk.height)));
|
||||
const tileSize = Math.max(2, Math.floor(baseTileSize * chunkZoom));
|
||||
const drawW = currentChunk.width * tileSize;
|
||||
const drawH = currentChunk.height * tileSize;
|
||||
chunkCtx.fillStyle = '#111827';
|
||||
@@ -409,14 +451,32 @@
|
||||
for (const t of currentTiles.values()) {
|
||||
const px = t.x * tileSize;
|
||||
const py = t.y * tileSize;
|
||||
if (t.isInvisiblePaywall) {
|
||||
chunkCtx.fillStyle = '#7c2d12';
|
||||
} else if (t.isWalkable) {
|
||||
chunkCtx.fillStyle = '#1d4ed8';
|
||||
const sheet = sheets.find(s => s.id === t.sheetId);
|
||||
const img = sheet ? sheetImageCache.get(sheet.id) : null;
|
||||
const canDrawSprite = !!sheet && !!img && img.complete && sheet.tileW > 0 && sheet.tileH > 0 && sheet.cols > 0;
|
||||
|
||||
if (canDrawSprite) {
|
||||
const col = t.frameIndex % sheet.cols;
|
||||
const row = Math.floor(t.frameIndex / sheet.cols);
|
||||
const sx = col * sheet.tileW;
|
||||
const sy = row * sheet.tileH;
|
||||
chunkCtx.imageSmoothingEnabled = false;
|
||||
chunkCtx.drawImage(img, sx, sy, sheet.tileW, sheet.tileH, px, py, tileSize, tileSize);
|
||||
} else {
|
||||
chunkCtx.fillStyle = '#334155';
|
||||
if (t.isInvisiblePaywall) {
|
||||
chunkCtx.fillStyle = '#7c2d12';
|
||||
} else if (t.isWalkable) {
|
||||
chunkCtx.fillStyle = '#1d4ed8';
|
||||
} else {
|
||||
chunkCtx.fillStyle = '#334155';
|
||||
}
|
||||
chunkCtx.fillRect(px, py, tileSize, tileSize);
|
||||
}
|
||||
|
||||
if (t.isInvisiblePaywall) {
|
||||
chunkCtx.fillStyle = 'rgba(127,29,29,0.45)';
|
||||
chunkCtx.fillRect(px, py, tileSize, tileSize);
|
||||
}
|
||||
chunkCtx.fillRect(px, py, tileSize, tileSize);
|
||||
}
|
||||
|
||||
chunkCtx.strokeStyle = 'rgba(148,163,184,0.16)';
|
||||
@@ -462,7 +522,8 @@
|
||||
const scaleY = chunkCanvas.height / rect.height;
|
||||
const cx = (e.clientX - rect.left) * scaleX;
|
||||
const cy = (e.clientY - rect.top) * scaleY;
|
||||
const tileSize = Math.max(6, Math.floor(Math.min(chunkCanvas.width / currentChunk.width, chunkCanvas.height / currentChunk.height)));
|
||||
const baseTileSize = Math.max(6, Math.floor(Math.min(chunkCanvas.width / currentChunk.width, chunkCanvas.height / currentChunk.height)));
|
||||
const tileSize = Math.max(2, Math.floor(baseTileSize * chunkZoom));
|
||||
const x = Math.floor(cx / tileSize);
|
||||
const y = Math.floor(cy / tileSize);
|
||||
if (x < 0 || y < 0 || x >= currentChunk.width || y >= currentChunk.height) return null;
|
||||
@@ -478,7 +539,8 @@
|
||||
if (mode === 'tile') {
|
||||
const sheetId = parseInt(document.getElementById('sheetId').value, 10) || 0;
|
||||
const frameIndex = parseInt(document.getElementById('frameIndex').value, 10) || 0;
|
||||
const isWalkable = document.getElementById('isWalkable').checked;
|
||||
const def = tileDefBySprite[`${sheetId}:${frameIndex}`];
|
||||
const isWalkable = def ? !!def.isWalkable : document.getElementById('isWalkable').checked;
|
||||
const isInvisiblePaywall = document.getElementById('isInvisiblePaywall').checked;
|
||||
const data = await postJson('/admin/worldeditor?handler=UpsertTile', {
|
||||
...s,
|
||||
@@ -492,7 +554,12 @@
|
||||
if (data.success) {
|
||||
currentChunk.version = data.chunkVersion;
|
||||
currentTiles.set(key(pos.x, pos.y), {
|
||||
x: pos.x, y: pos.y, sheetId, frameIndex, isWalkable, isInvisiblePaywall
|
||||
x: pos.x,
|
||||
y: pos.y,
|
||||
sheetId,
|
||||
frameIndex,
|
||||
isWalkable: data.op?.isWalkable ?? isWalkable,
|
||||
isInvisiblePaywall
|
||||
});
|
||||
}
|
||||
} else if (mode === 'object') {
|
||||
@@ -566,9 +633,21 @@
|
||||
frameIndex = Math.max(0, Math.min(totalFrames - 1, frameIndex));
|
||||
document.getElementById('frameIndex').value = frameIndex;
|
||||
document.getElementById('tileDefLink').href = `/admin/tilemaps/tiles/edit?sheetId=${sheetId}&frameIndex=${frameIndex}`;
|
||||
applySelectedTileDefinition();
|
||||
preloadSheetImage(sheetId);
|
||||
renderSheetPicker();
|
||||
}
|
||||
|
||||
function applySelectedTileDefinition() {
|
||||
const sheetId = parseInt(document.getElementById('sheetId').value, 10) || 0;
|
||||
const frameIndex = parseInt(document.getElementById('frameIndex').value, 10) || 0;
|
||||
const def = tileDefBySprite[`${sheetId}:${frameIndex}`];
|
||||
const walkableBox = document.getElementById('isWalkable');
|
||||
if (def) {
|
||||
walkableBox.checked = !!def.isWalkable;
|
||||
}
|
||||
}
|
||||
|
||||
function resolveSheetImageUrl(sheet) {
|
||||
if (!sheet || !sheet.path) return '';
|
||||
if (sheet.path.startsWith('http://') || sheet.path.startsWith('https://')) return sheet.path;
|
||||
@@ -644,6 +723,65 @@
|
||||
sheetPickerImage.src = imageUrl;
|
||||
}
|
||||
|
||||
function preloadSheetImage(sheetId) {
|
||||
if (!sheetId || sheetImageCache.has(sheetId)) return;
|
||||
const sheet = sheets.find(s => s.id === sheetId);
|
||||
if (!sheet) return;
|
||||
const url = resolveSheetImageUrl(sheet);
|
||||
if (!url) return;
|
||||
const img = new Image();
|
||||
img.onload = () => renderChunk();
|
||||
img.src = url;
|
||||
sheetImageCache.set(sheetId, img);
|
||||
}
|
||||
|
||||
function onZoomChanged() {
|
||||
const value = parseFloat(document.getElementById('zoomLevel').value);
|
||||
chunkZoom = Number.isFinite(value) ? Math.max(0.5, Math.min(4, value)) : 1;
|
||||
renderChunk();
|
||||
}
|
||||
|
||||
function adjustZoom(delta) {
|
||||
const slider = document.getElementById('zoomLevel');
|
||||
const current = parseFloat(slider.value) || 1;
|
||||
const next = Math.max(0.5, Math.min(4, Math.round((current + delta) * 4) / 4));
|
||||
slider.value = String(next);
|
||||
onZoomChanged();
|
||||
}
|
||||
|
||||
function resetZoom() {
|
||||
document.getElementById('zoomLevel').value = '1';
|
||||
onZoomChanged();
|
||||
}
|
||||
|
||||
async function clearChunk() {
|
||||
if (!currentChunk) return;
|
||||
const s = selection();
|
||||
const data = await postJson('/admin/worldeditor?handler=ClearChunk', s);
|
||||
if (!data.success) return;
|
||||
currentChunk.version = data.chunkVersion;
|
||||
currentTiles.clear();
|
||||
currentObjects = [];
|
||||
currentNpcs = [];
|
||||
renderChunk();
|
||||
loadZone();
|
||||
}
|
||||
|
||||
async function deleteChunk() {
|
||||
if (!currentChunk) return;
|
||||
if (!confirm('Delete this chunk and all contained tiles/objects/NPC spawns?')) return;
|
||||
const s = selection();
|
||||
const data = await postJson('/admin/worldeditor?handler=DeleteChunk', s);
|
||||
if (!data.success) return;
|
||||
currentChunk = null;
|
||||
currentTiles.clear();
|
||||
currentObjects = [];
|
||||
currentNpcs = [];
|
||||
chunkCtx.clearRect(0, 0, chunkCanvas.width, chunkCanvas.height);
|
||||
document.getElementById('chunkMeta').textContent = 'Chunk deleted';
|
||||
loadZone();
|
||||
}
|
||||
|
||||
document.getElementById('frameIndex').addEventListener('input', onSheetChanged);
|
||||
sheetPickerCanvas.addEventListener('click', (e) => {
|
||||
const sheetId = parseInt(document.getElementById('sheetId').value, 10) || 0;
|
||||
|
||||
@@ -23,6 +23,7 @@ public class IndexModel : AdminPageModel
|
||||
|
||||
public List<WorldRegion> Regions { get; set; } = new();
|
||||
public List<TileMapSheet> TileSheets { get; set; } = new();
|
||||
public List<TileDefinition> TileDefinitions { get; set; } = new();
|
||||
public List<DungeonObjectDefinition> ObjectDefinitions { get; set; } = new();
|
||||
public List<DungeonNpcDefinition> NpcDefinitions { get; set; } = new();
|
||||
|
||||
@@ -58,6 +59,20 @@ public class IndexModel : AdminPageModel
|
||||
.OrderBy(s => s.Name)
|
||||
.ToListAsync();
|
||||
|
||||
var enabledSheetIds = TileSheets.Select(s => s.Id).ToList();
|
||||
TileDefinitions = await _db.TileDefinitions
|
||||
.Where(t => t.IsEnabled && enabledSheetIds.Contains(t.TileMapSheetId))
|
||||
.Select(t => new TileDefinition
|
||||
{
|
||||
Id = t.Id,
|
||||
TileMapSheetId = t.TileMapSheetId,
|
||||
FrameIndex = t.FrameIndex,
|
||||
Name = t.Name,
|
||||
Type = t.Type,
|
||||
IsWalkable = t.IsWalkable
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
ObjectDefinitions = await _db.DungeonObjects
|
||||
.Where(o => o.IsEnabled)
|
||||
.OrderBy(o => o.Name)
|
||||
@@ -325,7 +340,11 @@ public class IndexModel : AdminPageModel
|
||||
|
||||
tile.SheetId = sheetId;
|
||||
tile.FrameIndex = frameIndex;
|
||||
tile.IsWalkable = isWalkable;
|
||||
var tileDef = await _db.TileDefinitions
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(t => t.IsEnabled && t.TileMapSheetId == sheetId && t.FrameIndex == frameIndex);
|
||||
var effectiveWalkable = tileDef?.IsWalkable ?? isWalkable;
|
||||
tile.IsWalkable = effectiveWalkable;
|
||||
tile.IsInvisiblePaywall = isInvisiblePaywall;
|
||||
|
||||
var fromVersion = locate.Chunk.Version;
|
||||
@@ -340,7 +359,7 @@ public class IndexModel : AdminPageModel
|
||||
y,
|
||||
sheetId,
|
||||
frameIndex,
|
||||
isWalkable,
|
||||
isWalkable = effectiveWalkable,
|
||||
isInvisiblePaywall
|
||||
};
|
||||
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op);
|
||||
@@ -355,6 +374,62 @@ public class IndexModel : AdminPageModel
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostClearChunkAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY)
|
||||
{
|
||||
var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY);
|
||||
if (!locate.Success || locate.Chunk == null)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = locate.Error }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
var fromVersion = locate.Chunk.Version;
|
||||
|
||||
var tiles = _db.WorldChunkTiles.Where(t => t.WorldChunkId == locate.Chunk.Id);
|
||||
var objects = _db.WorldChunkObjects.Where(o => o.WorldChunkId == locate.Chunk.Id);
|
||||
var npcs = _db.WorldChunkNpcSpawns.Where(n => n.WorldChunkId == locate.Chunk.Id);
|
||||
|
||||
_db.WorldChunkTiles.RemoveRange(tiles);
|
||||
_db.WorldChunkObjects.RemoveRange(objects);
|
||||
_db.WorldChunkNpcSpawns.RemoveRange(npcs);
|
||||
|
||||
locate.Chunk.Version++;
|
||||
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
var op = new { type = "chunk_clear" };
|
||||
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op);
|
||||
|
||||
return new JsonResult(new
|
||||
{
|
||||
success = true,
|
||||
chunkVersion = locate.Chunk.Version,
|
||||
fromVersion,
|
||||
toVersion = locate.Chunk.Version,
|
||||
op
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostDeleteChunkAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY)
|
||||
{
|
||||
var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY);
|
||||
if (!locate.Success || locate.Chunk == null)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = locate.Error }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
var fromVersion = locate.Chunk.Version;
|
||||
_db.WorldChunks.Remove(locate.Chunk);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, fromVersion + 1, new { type = "chunk_deleted" });
|
||||
|
||||
return new JsonResult(new
|
||||
{
|
||||
success = true,
|
||||
op = new { type = "chunk_deleted" }
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostRemoveTileAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY, int x, int y)
|
||||
{
|
||||
var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY);
|
||||
|
||||
Reference in New Issue
Block a user