Persist player overworld location and add world mob spawn editing/runtime payloads
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
|
||||
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 mobsJson = JsonSerializer.Serialize(Model.MobDefinitions.Select(m => new { id = m.Id, name = m.Name }));
|
||||
var tileDefsJson = JsonSerializer.Serialize(Model.TileDefinitions.Select(t => new
|
||||
{
|
||||
id = t.Id,
|
||||
@@ -45,7 +46,7 @@
|
||||
<div class="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 class="text-3xl font-bold text-white">World Editor</h1>
|
||||
<p class="text-gray-400 mt-1">Live chunk editing for tiles, objects, and NPC spawns</p>
|
||||
<p class="text-gray-400 mt-1">Live chunk editing for tiles, objects, NPC spawns, and mob spawns</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -132,6 +133,7 @@
|
||||
<option value="tile">Tile</option>
|
||||
<option value="object">Object</option>
|
||||
<option value="npc">NPC</option>
|
||||
<option value="mob">Mob</option>
|
||||
<option value="erase">Erase</option>
|
||||
</select>
|
||||
</div>
|
||||
@@ -141,6 +143,7 @@
|
||||
<option value="tile">Tile</option>
|
||||
<option value="object">Object</option>
|
||||
<option value="npc">NPC</option>
|
||||
<option value="mob">Mob</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
@@ -186,6 +189,22 @@
|
||||
<label class="label">NPC</label>
|
||||
<select id="npcDefinitionId" class="input"></select>
|
||||
</div>
|
||||
|
||||
<div class="card p-4 space-y-3">
|
||||
<h2 class="text-lg font-semibold text-white">Mob Tool</h2>
|
||||
<label class="label">Mob</label>
|
||||
<select id="mobDefinitionId" class="input"></select>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div>
|
||||
<label class="label">Spawn Count</label>
|
||||
<input id="mobSpawnCount" type="number" class="input" min="1" value="1" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Respawn Sec</label>
|
||||
<input id="mobRespawnSeconds" type="number" class="input" min="0" step="1" value="30" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="xl:col-span-9 space-y-4">
|
||||
@@ -223,6 +242,7 @@
|
||||
const sheets = @Html.Raw(sheetsJson);
|
||||
const objectDefs = @Html.Raw(objectsJson);
|
||||
const npcDefs = @Html.Raw(npcsJson);
|
||||
const mobDefs = @Html.Raw(mobsJson);
|
||||
const tileDefs = @Html.Raw(tileDefsJson);
|
||||
const cdnBaseUrl = '@Model.CdnBaseUrl';
|
||||
|
||||
@@ -232,6 +252,7 @@
|
||||
let currentTiles = new Map();
|
||||
let currentObjects = [];
|
||||
let currentNpcs = [];
|
||||
let currentMobs = [];
|
||||
let eventSource = null;
|
||||
let chunkZoom = 1;
|
||||
const tileDefBySprite = {};
|
||||
@@ -273,6 +294,7 @@
|
||||
|
||||
document.getElementById('objectDefinitionId').innerHTML = objectDefs.map(o => `<option value="${o.id}">${o.name}</option>`).join('');
|
||||
document.getElementById('npcDefinitionId').innerHTML = npcDefs.map(n => `<option value="${n.id}">${n.name}</option>`).join('');
|
||||
document.getElementById('mobDefinitionId').innerHTML = mobDefs.map(m => `<option value="${m.id}">${m.name}</option>`).join('');
|
||||
}
|
||||
|
||||
function refreshRegionSelect(selectedId) {
|
||||
@@ -346,6 +368,7 @@
|
||||
for (const t of data.tiles) preloadSheetImage(t.sheetId);
|
||||
currentObjects = data.objects || [];
|
||||
currentNpcs = data.npcs || [];
|
||||
currentMobs = data.mobs || [];
|
||||
document.getElementById('chunkMeta').textContent = `Chunk ${s.zoneX}:${s.zoneY}:${s.chunkX}:${s.chunkY} v${currentChunk.version} (${currentChunk.width}x${currentChunk.height})`;
|
||||
renderChunk();
|
||||
startStream();
|
||||
@@ -405,15 +428,23 @@
|
||||
else currentNpcs.push({ ...op });
|
||||
} else if (op.type === 'npc_remove') {
|
||||
currentNpcs = currentNpcs.filter(n => n.id !== op.id);
|
||||
} else if (op.type === 'mob_upsert') {
|
||||
const idx = currentMobs.findIndex(m => m.id === op.id);
|
||||
if (idx >= 0) currentMobs[idx] = { ...currentMobs[idx], ...op };
|
||||
else currentMobs.push({ ...op });
|
||||
} else if (op.type === 'mob_remove') {
|
||||
currentMobs = currentMobs.filter(m => m.id !== op.id);
|
||||
} else if (op.type === 'chunk_clear') {
|
||||
currentTiles.clear();
|
||||
currentObjects = [];
|
||||
currentNpcs = [];
|
||||
currentMobs = [];
|
||||
} else if (op.type === 'chunk_deleted') {
|
||||
currentChunk = null;
|
||||
currentTiles.clear();
|
||||
currentObjects = [];
|
||||
currentNpcs = [];
|
||||
currentMobs = [];
|
||||
chunkCtx.clearRect(0, 0, chunkCanvas.width, chunkCanvas.height);
|
||||
document.getElementById('chunkMeta').textContent = 'Chunk deleted';
|
||||
loadZone();
|
||||
@@ -554,6 +585,17 @@
|
||||
chunkCtx.rect(px - Math.max(2, tileSize * 0.2), py - Math.max(2, tileSize * 0.2), Math.max(4, tileSize * 0.4), Math.max(4, tileSize * 0.4));
|
||||
chunkCtx.fill();
|
||||
}
|
||||
for (const m of currentMobs) {
|
||||
const px = m.x * tileSize + tileSize / 2;
|
||||
const py = m.y * tileSize + tileSize / 2;
|
||||
chunkCtx.fillStyle = '#ef4444';
|
||||
chunkCtx.beginPath();
|
||||
chunkCtx.moveTo(px, py - Math.max(3, tileSize * 0.28));
|
||||
chunkCtx.lineTo(px + Math.max(3, tileSize * 0.28), py + Math.max(3, tileSize * 0.28));
|
||||
chunkCtx.lineTo(px - Math.max(3, tileSize * 0.28), py + Math.max(3, tileSize * 0.28));
|
||||
chunkCtx.closePath();
|
||||
chunkCtx.fill();
|
||||
}
|
||||
|
||||
const mode = document.getElementById('paintMode').value;
|
||||
document.getElementById('eraseTargetWrap').classList.toggle('hidden', mode !== 'erase');
|
||||
@@ -636,6 +678,25 @@
|
||||
if (idx >= 0) currentNpcs[idx] = { ...currentNpcs[idx], ...data.op };
|
||||
else currentNpcs.push(data.op);
|
||||
}
|
||||
} else if (mode === 'mob') {
|
||||
const mobDefinitionId = parseInt(document.getElementById('mobDefinitionId').value, 10) || 0;
|
||||
const spawnCount = parseInt(document.getElementById('mobSpawnCount').value, 10) || 1;
|
||||
const respawnSeconds = parseFloat(document.getElementById('mobRespawnSeconds').value) || 30;
|
||||
const data = await postJson('/admin/worldeditor?handler=UpsertMob', {
|
||||
...s,
|
||||
mobDefinitionId,
|
||||
x: pos.x,
|
||||
y: pos.y,
|
||||
rotation: 0,
|
||||
spawnCount,
|
||||
respawnSeconds
|
||||
});
|
||||
if (data.success) {
|
||||
currentChunk.version = data.chunkVersion;
|
||||
const idx = currentMobs.findIndex(m => m.id === data.op.id);
|
||||
if (idx >= 0) currentMobs[idx] = { ...currentMobs[idx], ...data.op };
|
||||
else currentMobs.push(data.op);
|
||||
}
|
||||
} else if (mode === 'erase') {
|
||||
const eraseTarget = document.getElementById('eraseTarget').value;
|
||||
if (eraseTarget === 'tile') {
|
||||
@@ -662,6 +723,15 @@
|
||||
currentNpcs = currentNpcs.filter(n => n.id !== hit.id);
|
||||
}
|
||||
}
|
||||
} else if (eraseTarget === 'mob') {
|
||||
const hit = currentMobs.find(m => m.x === pos.x && m.y === pos.y);
|
||||
if (hit) {
|
||||
const data = await postJson('/admin/worldeditor?handler=RemoveMob', { ...s, id: hit.id });
|
||||
if (data.success) {
|
||||
currentChunk.version = data.chunkVersion;
|
||||
currentMobs = currentMobs.filter(m => m.id !== hit.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -807,13 +877,14 @@
|
||||
currentTiles.clear();
|
||||
currentObjects = [];
|
||||
currentNpcs = [];
|
||||
currentMobs = [];
|
||||
renderChunk();
|
||||
loadZone();
|
||||
}
|
||||
|
||||
async function deleteChunk() {
|
||||
if (!currentChunk) return;
|
||||
if (!confirm('Delete this chunk and all contained tiles/objects/NPC spawns?')) return;
|
||||
if (!confirm('Delete this chunk and all contained tiles/objects/NPC spawns/mob spawns?')) return;
|
||||
const s = selection();
|
||||
const data = await postJson('/admin/worldeditor?handler=DeleteChunk', s);
|
||||
if (!data.success) return;
|
||||
@@ -821,6 +892,7 @@
|
||||
currentTiles.clear();
|
||||
currentObjects = [];
|
||||
currentNpcs = [];
|
||||
currentMobs = [];
|
||||
chunkCtx.clearRect(0, 0, chunkCanvas.width, chunkCanvas.height);
|
||||
document.getElementById('chunkMeta').textContent = 'Chunk deleted';
|
||||
loadZone();
|
||||
|
||||
@@ -26,6 +26,7 @@ public class IndexModel : AdminPageModel
|
||||
public List<TileDefinition> TileDefinitions { get; set; } = new();
|
||||
public List<DungeonObjectDefinition> ObjectDefinitions { get; set; } = new();
|
||||
public List<DungeonNpcDefinition> NpcDefinitions { get; set; } = new();
|
||||
public List<DungeonMobDefinition> MobDefinitions { get; set; } = new();
|
||||
|
||||
public int SelectedRegionId { get; set; }
|
||||
public int SelectedZoneX { get; set; }
|
||||
@@ -82,6 +83,11 @@ public class IndexModel : AdminPageModel
|
||||
.Where(n => n.IsEnabled)
|
||||
.OrderBy(n => n.Name)
|
||||
.ToListAsync();
|
||||
|
||||
MobDefinitions = await _db.DungeonMobDefinitions
|
||||
.Where(m => m.IsEnabled)
|
||||
.OrderBy(m => m.Name)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnGetRegionAsync(int regionId)
|
||||
@@ -282,6 +288,24 @@ public class IndexModel : AdminPageModel
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
var mobs = await _db.WorldChunkMobSpawns
|
||||
.Include(m => m.MobDefinition)
|
||||
.Where(m => m.WorldChunkId == chunk.Id)
|
||||
.OrderBy(m => m.TileY).ThenBy(m => m.TileX)
|
||||
.Select(m => new
|
||||
{
|
||||
id = m.Id,
|
||||
x = m.TileX,
|
||||
y = m.TileY,
|
||||
mobDefinitionId = m.MobDefinitionId,
|
||||
mobName = m.MobDefinition != null ? m.MobDefinition.Name : $"Mob {m.MobDefinitionId}",
|
||||
rotation = m.Rotation,
|
||||
spawnCount = m.SpawnCount,
|
||||
respawnSeconds = m.RespawnSeconds,
|
||||
metadataJson = m.MetadataJson
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
return new JsonResult(new
|
||||
{
|
||||
success = true,
|
||||
@@ -299,7 +323,8 @@ public class IndexModel : AdminPageModel
|
||||
},
|
||||
tiles,
|
||||
objects,
|
||||
npcs
|
||||
npcs,
|
||||
mobs
|
||||
});
|
||||
}
|
||||
|
||||
@@ -443,10 +468,12 @@ public class IndexModel : AdminPageModel
|
||||
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);
|
||||
var mobs = _db.WorldChunkMobSpawns.Where(m => m.WorldChunkId == locate.Chunk.Id);
|
||||
|
||||
_db.WorldChunkTiles.RemoveRange(tiles);
|
||||
_db.WorldChunkObjects.RemoveRange(objects);
|
||||
_db.WorldChunkNpcSpawns.RemoveRange(npcs);
|
||||
_db.WorldChunkMobSpawns.RemoveRange(mobs);
|
||||
|
||||
locate.Chunk.Version++;
|
||||
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
||||
@@ -734,6 +761,121 @@ public class IndexModel : AdminPageModel
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostUpsertMobAsync(
|
||||
int regionId,
|
||||
int zoneX,
|
||||
int zoneY,
|
||||
int chunkX,
|
||||
int chunkY,
|
||||
long? id,
|
||||
int mobDefinitionId,
|
||||
int x,
|
||||
int y,
|
||||
int rotation = 0,
|
||||
int spawnCount = 1,
|
||||
float respawnSeconds = 30,
|
||||
string? metadataJson = null)
|
||||
{
|
||||
var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY);
|
||||
if (!locate.Success || locate.Region == null || locate.Chunk == null)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = locate.Error }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
if (!IsInside(locate.Region.ChunkWidthTiles, locate.Region.ChunkHeightTiles, x, y))
|
||||
{
|
||||
return new JsonResult(new { success = false, error = "tile_out_of_bounds" }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
var mobDef = await _db.DungeonMobDefinitions.FirstOrDefaultAsync(m => m.Id == mobDefinitionId && m.IsEnabled);
|
||||
if (mobDef == null)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = "mob_definition_not_found" }) { StatusCode = 404 };
|
||||
}
|
||||
|
||||
WorldChunkMobSpawn mob;
|
||||
if (id.HasValue)
|
||||
{
|
||||
mob = await _db.WorldChunkMobSpawns.FirstOrDefaultAsync(m => m.Id == id.Value && m.WorldChunkId == locate.Chunk.Id)
|
||||
?? new WorldChunkMobSpawn { WorldChunkId = locate.Chunk.Id };
|
||||
if (mob.Id == 0)
|
||||
{
|
||||
_db.WorldChunkMobSpawns.Add(mob);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mob = new WorldChunkMobSpawn { WorldChunkId = locate.Chunk.Id };
|
||||
_db.WorldChunkMobSpawns.Add(mob);
|
||||
}
|
||||
|
||||
mob.MobDefinitionId = mobDefinitionId;
|
||||
mob.TileX = x;
|
||||
mob.TileY = y;
|
||||
mob.Rotation = rotation;
|
||||
mob.SpawnCount = Math.Clamp(spawnCount, 1, 100);
|
||||
mob.RespawnSeconds = Math.Clamp(respawnSeconds, 0f, 86400f);
|
||||
mob.MetadataJson = string.IsNullOrWhiteSpace(metadataJson) ? null : metadataJson.Trim();
|
||||
|
||||
var fromVersion = locate.Chunk.Version;
|
||||
locate.Chunk.Version++;
|
||||
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
var op = new
|
||||
{
|
||||
type = "mob_upsert",
|
||||
id = mob.Id,
|
||||
x = mob.TileX,
|
||||
y = mob.TileY,
|
||||
mobDefinitionId = mob.MobDefinitionId,
|
||||
mobName = mobDef.Name,
|
||||
rotation = mob.Rotation,
|
||||
spawnCount = mob.SpawnCount,
|
||||
respawnSeconds = mob.RespawnSeconds,
|
||||
metadataJson = mob.MetadataJson
|
||||
};
|
||||
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> OnPostRemoveMobAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY, long id)
|
||||
{
|
||||
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 mob = await _db.WorldChunkMobSpawns.FirstOrDefaultAsync(m => m.Id == id && m.WorldChunkId == locate.Chunk.Id);
|
||||
if (mob != null)
|
||||
{
|
||||
var fromVersion = locate.Chunk.Version;
|
||||
_db.WorldChunkMobSpawns.Remove(mob);
|
||||
locate.Chunk.Version++;
|
||||
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
var op = new { type = "mob_remove", id };
|
||||
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op);
|
||||
}
|
||||
|
||||
return new JsonResult(new
|
||||
{
|
||||
success = true,
|
||||
chunkVersion = locate.Chunk.Version,
|
||||
op = new { type = "mob_remove", id }
|
||||
});
|
||||
}
|
||||
|
||||
private async Task EnsureDefaultRegionAsync()
|
||||
{
|
||||
if (await _db.WorldRegions.AnyAsync())
|
||||
|
||||
Reference in New Issue
Block a user