tilemap
This commit is contained in:
@@ -152,6 +152,13 @@
|
||||
Sprite Sheets
|
||||
</a>
|
||||
|
||||
<a href="/admin/tilemaps" class="sidebar-link @(ViewContext.RouteData.Values["page"]?.ToString()?.StartsWith("/TileMaps") == true ? "active" : "")" onclick="closeMobileMenu()">
|
||||
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 5a1 1 0 011-1h4a1 1 0 011 1v4a1 1 0 01-1 1H5a1 1 0 01-1-1V5zm10 0a1 1 0 011-1h4a1 1 0 011 1v4a1 1 0 01-1 1h-4a1 1 0 01-1-1V5zM4 15a1 1 0 011-1h4a1 1 0 011 1v4a1 1 0 01-1 1H5a1 1 0 01-1-1v-4zm10 0a1 1 0 011-1h4a1 1 0 011 1v4a1 1 0 01-1 1h-4a1 1 0 01-1-1v-4z"/>
|
||||
</svg>
|
||||
TileMaps
|
||||
</a>
|
||||
|
||||
<a href="/admin/assets" class="sidebar-link @(ViewContext.RouteData.Values["page"]?.ToString()?.StartsWith("/Assets") == true ? "active" : "")" onclick="closeMobileMenu()">
|
||||
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/>
|
||||
|
||||
@@ -0,0 +1,473 @@
|
||||
@page "/admin/tilemaps/edit"
|
||||
@model TileMaps.EditModel
|
||||
@using DataServer.Data.Entities
|
||||
@{
|
||||
ViewData["Title"] = Model.Sheet.Id == 0 ? "New TileMap Sheet" : $"Edit: {Model.Sheet.Name}";
|
||||
var tileTypes = Enum.GetValues<TileType>();
|
||||
}
|
||||
|
||||
<div class="mb-6">
|
||||
<a href="/admin/tilemaps" class="text-blue-400 hover:text-blue-300">← Back to TileMap Sheets</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-2 gap-6">
|
||||
<!-- Sheet Settings -->
|
||||
<div class="card">
|
||||
<h2 class="text-xl font-semibold text-white mb-4">TileMap Sheet Settings</h2>
|
||||
|
||||
<form method="post">
|
||||
@Html.AntiForgeryToken()
|
||||
<input type="hidden" asp-for="Sheet.Id" />
|
||||
|
||||
<div class="space-y-4">
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-300 mb-1">Name</label>
|
||||
<input type="text" asp-for="Sheet.Name" class="input w-full" placeholder="Dungeon Stone Tiles" />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-300 mb-1">CDN Path</label>
|
||||
<div class="flex gap-2">
|
||||
<input type="text" asp-for="Sheet.CdnPath" class="input flex-1" placeholder="textures/tilemaps/dungeon.png" />
|
||||
<button type="button" onclick="showCdnBrowser()" class="btn btn-secondary">Browse</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-300 mb-1">Tags (comma-separated)</label>
|
||||
<input type="text" asp-for="Sheet.Tags" class="input w-full" placeholder="dungeon, stone, dark" />
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-300 mb-1">Sheet Width (px)</label>
|
||||
<input type="number" asp-for="Sheet.SheetWidth" class="input w-full" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-300 mb-1">Sheet Height (px)</label>
|
||||
<input type="number" asp-for="Sheet.SheetHeight" class="input w-full" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-300 mb-1">Tile Width (px)</label>
|
||||
<input type="number" asp-for="Sheet.TileWidth" class="input w-full" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-300 mb-1">Tile Height (px)</label>
|
||||
<input type="number" asp-for="Sheet.TileHeight" class="input w-full" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<input type="checkbox" asp-for="Sheet.IsEnabled" class="rounded" />
|
||||
<label asp-for="Sheet.IsEnabled" class="text-gray-300">Enabled</label>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary w-full">
|
||||
@(Model.Sheet.Id == 0 ? "Create TileMap Sheet" : "Save Changes")
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- Preview -->
|
||||
<div class="card">
|
||||
<h2 class="text-xl font-semibold text-white mb-4">Preview</h2>
|
||||
|
||||
@if (!string.IsNullOrEmpty(Model.Sheet.CdnPath))
|
||||
{
|
||||
<div class="bg-gray-900 rounded-lg p-4 overflow-auto max-h-96">
|
||||
<div class="relative inline-block" style="image-rendering: pixelated;">
|
||||
<img src="@Model.CdnBaseUrl/@Model.Sheet.CdnPath"
|
||||
alt="@Model.Sheet.Name"
|
||||
id="sheetPreview"
|
||||
class="max-w-none" />
|
||||
<!-- Grid overlay will be drawn by JavaScript -->
|
||||
<canvas id="gridOverlay" class="absolute top-0 left-0 pointer-events-none"></canvas>
|
||||
</div>
|
||||
</div>
|
||||
<p class="text-sm text-gray-400 mt-2">
|
||||
Grid: @Model.Sheet.Columns columns × @Model.Sheet.Rows rows = @Model.Sheet.TotalTiles tiles
|
||||
</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="bg-gray-900 rounded-lg p-8 text-center text-gray-500">
|
||||
Select a CDN path to preview the tilemap
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (Model.Sheet.Id > 0)
|
||||
{
|
||||
<!-- Tile Editor Section -->
|
||||
<div class="card mt-6">
|
||||
<div class="flex items-center justify-between mb-4">
|
||||
<h2 class="text-xl font-semibold text-white">Tile Definitions</h2>
|
||||
<div class="flex gap-2">
|
||||
<form method="post" asp-page-handler="AutoGenerate" class="inline">
|
||||
<input type="hidden" name="sheetId" value="@Model.Sheet.Id" />
|
||||
<select name="defaultType" class="input">
|
||||
<option value="0">Floor (walkable)</option>
|
||||
<option value="1">Wall (blocked)</option>
|
||||
</select>
|
||||
<button type="submit" class="btn btn-secondary">Auto-Generate All</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p class="text-gray-400 mb-4">Click on a tile to define its properties. Defined tiles: @Model.TileDefinitions.Count / @Model.Sheet.TotalTiles</p>
|
||||
|
||||
<!-- Interactive Tile Grid -->
|
||||
<div class="bg-gray-900 rounded-lg p-4 overflow-auto">
|
||||
<div class="relative inline-block" id="tileGridContainer" style="image-rendering: pixelated;">
|
||||
@if (!string.IsNullOrEmpty(Model.Sheet.CdnPath))
|
||||
{
|
||||
<img src="@Model.CdnBaseUrl/@Model.Sheet.CdnPath"
|
||||
alt="@Model.Sheet.Name"
|
||||
id="tileGridImage"
|
||||
class="max-w-none" />
|
||||
|
||||
<!-- Clickable tile overlay -->
|
||||
<div id="tileClickOverlay" class="absolute top-0 left-0"
|
||||
style="width: @(Model.Sheet.SheetWidth)px; height: @(Model.Sheet.SheetHeight)px;">
|
||||
@for (int row = 0; row < Model.Sheet.Rows; row++)
|
||||
{
|
||||
@for (int col = 0; col < Model.Sheet.Columns; col++)
|
||||
{
|
||||
var frameIndex = row * Model.Sheet.Columns + col;
|
||||
var tileDef = Model.TileDefinitions.FirstOrDefault(t => t.FrameIndex == frameIndex);
|
||||
var hasDefinition = tileDef != null;
|
||||
var tileColor = hasDefinition ? GetTileTypeColor(tileDef!.Type) : "transparent";
|
||||
|
||||
<div class="absolute cursor-pointer border border-gray-700/50 hover:border-yellow-400 hover:bg-yellow-400/20 transition-all"
|
||||
style="left: @(col * Model.Sheet.TileWidth)px; top: @(row * Model.Sheet.TileHeight)px; width: @(Model.Sheet.TileWidth)px; height: @(Model.Sheet.TileHeight)px; background-color: @tileColor;"
|
||||
onclick="openTileEditor(@frameIndex, @(tileDef?.Id ?? 0))"
|
||||
title="Frame @frameIndex @(hasDefinition ? $"({tileDef!.Type})" : "(undefined)")">
|
||||
@if (hasDefinition)
|
||||
{
|
||||
<span class="absolute bottom-0 right-0 text-[8px] text-white bg-black/50 px-0.5">@frameIndex</span>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Tile Type Legend -->
|
||||
<div class="mt-4 flex flex-wrap gap-2">
|
||||
<span class="text-sm text-gray-400">Legend:</span>
|
||||
<span class="px-2 py-0.5 text-xs rounded" style="background-color: rgba(34, 197, 94, 0.3);">Floor</span>
|
||||
<span class="px-2 py-0.5 text-xs rounded" style="background-color: rgba(239, 68, 68, 0.3);">Wall</span>
|
||||
<span class="px-2 py-0.5 text-xs rounded" style="background-color: rgba(168, 85, 247, 0.3);">Door</span>
|
||||
<span class="px-2 py-0.5 text-xs rounded" style="background-color: rgba(59, 130, 246, 0.3);">Water</span>
|
||||
<span class="px-2 py-0.5 text-xs rounded" style="background-color: rgba(249, 115, 22, 0.3);">Lava</span>
|
||||
<span class="px-2 py-0.5 text-xs rounded" style="background-color: rgba(234, 179, 8, 0.3);">Spawn</span>
|
||||
</div>
|
||||
|
||||
<!-- Defined Tiles Table -->
|
||||
@if (Model.TileDefinitions.Count > 0)
|
||||
{
|
||||
<div class="mt-6">
|
||||
<h3 class="text-lg font-semibold text-white mb-3">Defined Tiles</h3>
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full text-sm">
|
||||
<thead>
|
||||
<tr class="text-left text-gray-400 border-b border-gray-700">
|
||||
<th class="p-2">Frame</th>
|
||||
<th class="p-2">Preview</th>
|
||||
<th class="p-2">Name</th>
|
||||
<th class="p-2">Type</th>
|
||||
<th class="p-2">Tags</th>
|
||||
<th class="p-2">Properties</th>
|
||||
<th class="p-2">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var tile in Model.TileDefinitions)
|
||||
{
|
||||
var col = tile.FrameIndex % Model.Sheet.Columns;
|
||||
var row = tile.FrameIndex / Model.Sheet.Columns;
|
||||
|
||||
<tr class="border-b border-gray-800 hover:bg-gray-800/50">
|
||||
<td class="p-2 text-gray-300">@tile.FrameIndex</td>
|
||||
<td class="p-2">
|
||||
<div class="w-8 h-8 bg-gray-900 overflow-hidden" style="image-rendering: pixelated;">
|
||||
<div style="width: @(Model.Sheet.TileWidth)px; height: @(Model.Sheet.TileHeight)px; background-image: url('@Model.CdnBaseUrl/@Model.Sheet.CdnPath'); background-position: -@(col * Model.Sheet.TileWidth)px -@(row * Model.Sheet.TileHeight)px; transform: scale(@(32.0 / Model.Sheet.TileWidth));"></div>
|
||||
</div>
|
||||
</td>
|
||||
<td class="p-2 text-gray-300">@(tile.Name ?? "-")</td>
|
||||
<td class="p-2">
|
||||
<span class="px-2 py-0.5 text-xs rounded" style="background-color: @GetTileTypeColor(tile.Type);">@tile.Type</span>
|
||||
</td>
|
||||
<td class="p-2 text-gray-400 text-xs">@(tile.Tags ?? "-")</td>
|
||||
<td class="p-2 text-xs">
|
||||
@if (tile.IsWalkable) { <span class="text-green-400">Walk</span> }
|
||||
@if (tile.BlocksProjectiles) { <span class="text-red-400 ml-1">NoProj</span> }
|
||||
@if (tile.BlocksLineOfSight) { <span class="text-yellow-400 ml-1">NoLOS</span> }
|
||||
</td>
|
||||
<td class="p-2">
|
||||
<button onclick="openTileEditor(@tile.FrameIndex, @tile.Id)" class="text-blue-400 hover:text-blue-300 mr-2">Edit</button>
|
||||
<form method="post" asp-page-handler="DeleteTile" class="inline">
|
||||
<input type="hidden" name="tileId" value="@tile.Id" />
|
||||
<input type="hidden" name="sheetId" value="@Model.Sheet.Id" />
|
||||
<button type="submit" class="text-red-400 hover:text-red-300" onclick="return confirm('Delete?')">Delete</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
<!-- Tile Editor Modal -->
|
||||
<div id="tileEditorModal" class="fixed inset-0 bg-black/50 z-50 hidden items-center justify-center">
|
||||
<div class="bg-gray-900 rounded-xl shadow-xl p-6 w-full max-w-md mx-4 border border-gray-700">
|
||||
<h3 class="text-lg font-semibold text-white mb-4">Edit Tile <span id="modalFrameIndex"></span></h3>
|
||||
|
||||
<form method="post" asp-page-handler="SaveTile">
|
||||
@Html.AntiForgeryToken()
|
||||
<input type="hidden" name="sheetId" value="@Model.Sheet.Id" />
|
||||
<input type="hidden" name="frameIndex" id="editFrameIndex" />
|
||||
|
||||
<div class="space-y-4">
|
||||
<!-- Tile Preview -->
|
||||
<div class="flex justify-center">
|
||||
<div id="modalTilePreview" class="bg-gray-800 border border-gray-600" style="image-rendering: pixelated;"></div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-300 mb-1">Name (optional)</label>
|
||||
<input type="text" name="name" id="editName" class="input w-full" placeholder="Stone Floor A" />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-300 mb-1">Type</label>
|
||||
<select name="type" id="editType" class="input w-full">
|
||||
@foreach (var t in tileTypes)
|
||||
{
|
||||
<option value="@((int)t)">@t</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-300 mb-1">Tags (comma-separated)</label>
|
||||
<input type="text" name="tags" id="editTags" class="input w-full" placeholder="stone, cracked, floor" />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-300 mb-1">Render Layer</label>
|
||||
<select name="renderLayer" id="editRenderLayer" class="input w-full">
|
||||
<option value="0">0 - Ground</option>
|
||||
<option value="1">1 - Floor Decor</option>
|
||||
<option value="2">2 - Walls</option>
|
||||
<option value="3">3 - Ceiling/Overlay</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-3 gap-4">
|
||||
<label class="flex items-center gap-2 text-gray-300">
|
||||
<input type="checkbox" name="isWalkable" id="editWalkable" value="true" class="rounded" />
|
||||
Walkable
|
||||
</label>
|
||||
<label class="flex items-center gap-2 text-gray-300">
|
||||
<input type="checkbox" name="blocksProjectiles" id="editBlocksProj" value="true" class="rounded" />
|
||||
Blocks Proj
|
||||
</label>
|
||||
<label class="flex items-center gap-2 text-gray-300">
|
||||
<input type="checkbox" name="blocksLineOfSight" id="editBlocksLOS" value="true" class="rounded" />
|
||||
Blocks LOS
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex gap-2 mt-6">
|
||||
<button type="submit" class="btn btn-primary flex-1">Save Tile</button>
|
||||
<button type="button" onclick="closeTileEditor()" class="btn btn-secondary flex-1">Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- CDN Browser Modal -->
|
||||
<div id="cdnBrowserModal" class="fixed inset-0 bg-black/50 z-50 hidden items-center justify-center">
|
||||
<div class="bg-gray-900 rounded-xl shadow-xl p-6 w-full max-w-2xl mx-4 border border-gray-700 max-h-[80vh] overflow-hidden flex flex-col">
|
||||
<h3 class="text-lg font-semibold text-white mb-4">Select TileMap from CDN</h3>
|
||||
<div class="flex-1 overflow-y-auto grid grid-cols-2 md:grid-cols-3 gap-3">
|
||||
@foreach (var asset in Model.CdnImages)
|
||||
{
|
||||
<button type="button" onclick="selectCdnImage('@asset.DownloadPath')" class="p-2 bg-gray-800 rounded-lg hover:bg-gray-700 text-left">
|
||||
<img src="@Model.CdnBaseUrl/@asset.DownloadPath" alt="@asset.FilePath" class="w-full h-20 object-contain mb-1" style="image-rendering: pixelated;" />
|
||||
<p class="text-xs text-gray-400 truncate">@asset.FilePath</p>
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
<div class="flex justify-end pt-4">
|
||||
<button type="button" onclick="closeCdnBrowser()" class="btn btn-secondary">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@section Scripts {
|
||||
<script>
|
||||
// Tile definitions data from server
|
||||
const tileDefinitions = @Html.Raw(System.Text.Json.JsonSerializer.Serialize(Model.TileDefinitions.ToDictionary(t => t.FrameIndex)));
|
||||
const sheetWidth = @Model.Sheet.SheetWidth;
|
||||
const sheetHeight = @Model.Sheet.SheetHeight;
|
||||
const tileWidth = @Model.Sheet.TileWidth;
|
||||
const tileHeight = @Model.Sheet.TileHeight;
|
||||
const columns = @Model.Sheet.Columns;
|
||||
const cdnBaseUrl = '@Model.CdnBaseUrl';
|
||||
const cdnPath = '@Model.Sheet.CdnPath';
|
||||
|
||||
function openTileEditor(frameIndex, existingId) {
|
||||
document.getElementById('editFrameIndex').value = frameIndex;
|
||||
document.getElementById('modalFrameIndex').textContent = '#' + frameIndex;
|
||||
|
||||
// Calculate tile position for preview
|
||||
const col = frameIndex % columns;
|
||||
const row = Math.floor(frameIndex / columns);
|
||||
|
||||
// Set preview
|
||||
const preview = document.getElementById('modalTilePreview');
|
||||
preview.style.width = (tileWidth * 2) + 'px';
|
||||
preview.style.height = (tileHeight * 2) + 'px';
|
||||
preview.style.backgroundImage = `url('${cdnBaseUrl}/${cdnPath}')`;
|
||||
preview.style.backgroundPosition = `-${col * tileWidth * 2}px -${row * tileHeight * 2}px`;
|
||||
preview.style.backgroundSize = `${sheetWidth * 2}px ${sheetHeight * 2}px`;
|
||||
|
||||
// Load existing data if available
|
||||
const existing = tileDefinitions[frameIndex];
|
||||
if (existing) {
|
||||
document.getElementById('editName').value = existing.Name || '';
|
||||
document.getElementById('editType').value = existing.Type;
|
||||
document.getElementById('editTags').value = existing.Tags || '';
|
||||
document.getElementById('editRenderLayer').value = existing.RenderLayer;
|
||||
document.getElementById('editWalkable').checked = existing.IsWalkable;
|
||||
document.getElementById('editBlocksProj').checked = existing.BlocksProjectiles;
|
||||
document.getElementById('editBlocksLOS').checked = existing.BlocksLineOfSight;
|
||||
} else {
|
||||
// Defaults for new tile
|
||||
document.getElementById('editName').value = '';
|
||||
document.getElementById('editType').value = '0';
|
||||
document.getElementById('editTags').value = '';
|
||||
document.getElementById('editRenderLayer').value = '0';
|
||||
document.getElementById('editWalkable').checked = true;
|
||||
document.getElementById('editBlocksProj').checked = false;
|
||||
document.getElementById('editBlocksLOS').checked = false;
|
||||
}
|
||||
|
||||
document.getElementById('tileEditorModal').classList.remove('hidden');
|
||||
document.getElementById('tileEditorModal').classList.add('flex');
|
||||
}
|
||||
|
||||
function closeTileEditor() {
|
||||
document.getElementById('tileEditorModal').classList.add('hidden');
|
||||
document.getElementById('tileEditorModal').classList.remove('flex');
|
||||
}
|
||||
|
||||
function showCdnBrowser() {
|
||||
document.getElementById('cdnBrowserModal').classList.remove('hidden');
|
||||
document.getElementById('cdnBrowserModal').classList.add('flex');
|
||||
}
|
||||
|
||||
function closeCdnBrowser() {
|
||||
document.getElementById('cdnBrowserModal').classList.add('hidden');
|
||||
document.getElementById('cdnBrowserModal').classList.remove('flex');
|
||||
}
|
||||
|
||||
function selectCdnImage(path) {
|
||||
document.getElementById('Sheet_CdnPath').value = path;
|
||||
closeCdnBrowser();
|
||||
}
|
||||
|
||||
// Draw grid overlay on preview
|
||||
function drawGridOverlay() {
|
||||
const img = document.getElementById('sheetPreview');
|
||||
const canvas = document.getElementById('gridOverlay');
|
||||
if (!img || !canvas) return;
|
||||
|
||||
canvas.width = img.naturalWidth;
|
||||
canvas.height = img.naturalHeight;
|
||||
|
||||
const ctx = canvas.getContext('2d');
|
||||
ctx.strokeStyle = 'rgba(255, 255, 255, 0.3)';
|
||||
ctx.lineWidth = 1;
|
||||
|
||||
// Vertical lines
|
||||
for (let x = tileWidth; x < canvas.width; x += tileWidth) {
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x, 0);
|
||||
ctx.lineTo(x, canvas.height);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
// Horizontal lines
|
||||
for (let y = tileHeight; y < canvas.height; y += tileHeight) {
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(0, y);
|
||||
ctx.lineTo(canvas.width, y);
|
||||
ctx.stroke();
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize when image loads
|
||||
const previewImg = document.getElementById('sheetPreview');
|
||||
if (previewImg) {
|
||||
previewImg.onload = drawGridOverlay;
|
||||
if (previewImg.complete) drawGridOverlay();
|
||||
}
|
||||
|
||||
// Close modals on escape
|
||||
document.addEventListener('keydown', e => {
|
||||
if (e.key === 'Escape') {
|
||||
closeTileEditor();
|
||||
closeCdnBrowser();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
}
|
||||
|
||||
@functions {
|
||||
string GetTileTypeColor(TileType type)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
TileType.Floor => "rgba(34, 197, 94, 0.3)",
|
||||
TileType.Wall => "rgba(239, 68, 68, 0.3)",
|
||||
TileType.Door => "rgba(168, 85, 247, 0.3)",
|
||||
TileType.Water => "rgba(59, 130, 246, 0.3)",
|
||||
TileType.Lava => "rgba(249, 115, 22, 0.3)",
|
||||
TileType.Pit => "rgba(31, 41, 55, 0.5)",
|
||||
TileType.Stairs => "rgba(236, 72, 153, 0.3)",
|
||||
TileType.PlayerSpawn => "rgba(234, 179, 8, 0.4)",
|
||||
TileType.MobSpawn => "rgba(234, 179, 8, 0.3)",
|
||||
TileType.BossSpawn => "rgba(220, 38, 38, 0.4)",
|
||||
TileType.ChestSpawn => "rgba(251, 191, 36, 0.4)",
|
||||
TileType.Trigger => "rgba(139, 92, 246, 0.3)",
|
||||
TileType.Transition => "rgba(14, 165, 233, 0.3)",
|
||||
TileType.Secret => "rgba(163, 163, 163, 0.3)",
|
||||
TileType.Trap => "rgba(239, 68, 68, 0.4)",
|
||||
TileType.FloorDecor => "rgba(34, 197, 94, 0.2)",
|
||||
TileType.WallDecor => "rgba(239, 68, 68, 0.2)",
|
||||
_ => "transparent"
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DataServer.Data;
|
||||
using DataServer.Data.Entities;
|
||||
|
||||
namespace Server.Services.Admin.Pages.TileMaps;
|
||||
|
||||
public class EditModel : AdminPageModel
|
||||
{
|
||||
private readonly GameDbContext _db;
|
||||
private readonly IConfiguration _config;
|
||||
|
||||
public EditModel(GameDbContext db, IConfiguration config)
|
||||
{
|
||||
_db = db;
|
||||
_config = config;
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
public TileMapSheet Sheet { get; set; } = new();
|
||||
|
||||
public List<TileDefinition> TileDefinitions { get; set; } = new();
|
||||
public List<ClientAsset> CdnImages { get; set; } = new();
|
||||
public string CdnBaseUrl { get; set; } = "";
|
||||
|
||||
public string? Message { get; set; }
|
||||
public bool IsError { get; set; }
|
||||
|
||||
public async Task OnGetAsync(int? id)
|
||||
{
|
||||
CdnBaseUrl = _config["Cdn:BaseUrl"] ?? "https://assets.aeternalisgames.com";
|
||||
CdnImages = await _db.ClientAssets
|
||||
.Where(a => a.AssetType == ClientAssetType.Texture && a.IsEnabled)
|
||||
.Where(a => a.FilePath.StartsWith("textures/tilemaps/") || a.FilePath.Contains("tilemap"))
|
||||
.OrderBy(a => a.FilePath)
|
||||
.ToListAsync();
|
||||
|
||||
// Also get all textures as fallback
|
||||
if (CdnImages.Count == 0)
|
||||
{
|
||||
CdnImages = await _db.ClientAssets
|
||||
.Where(a => a.AssetType == ClientAssetType.Texture && a.IsEnabled)
|
||||
.OrderBy(a => a.FilePath)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
if (id.HasValue)
|
||||
{
|
||||
var sheet = await _db.TileMapSheets
|
||||
.Include(s => s.Tiles)
|
||||
.FirstOrDefaultAsync(s => s.Id == id.Value);
|
||||
|
||||
if (sheet != null)
|
||||
{
|
||||
Sheet = sheet;
|
||||
TileDefinitions = sheet.Tiles?.OrderBy(t => t.FrameIndex).ToList() ?? new();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Sheet = new TileMapSheet
|
||||
{
|
||||
IsEnabled = true,
|
||||
TileWidth = 32,
|
||||
TileHeight = 32
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAsync()
|
||||
{
|
||||
CdnBaseUrl = _config["Cdn:BaseUrl"] ?? "https://assets.aeternalisgames.com";
|
||||
|
||||
try
|
||||
{
|
||||
if (Sheet.Id == 0)
|
||||
{
|
||||
Sheet.CreatedAt = DateTime.UtcNow;
|
||||
Sheet.UpdatedAt = DateTime.UtcNow;
|
||||
_db.TileMapSheets.Add(Sheet);
|
||||
await _db.SaveChangesAsync();
|
||||
Message = $"TileMap sheet '{Sheet.Name}' created.";
|
||||
return RedirectToPage(new { id = Sheet.Id });
|
||||
}
|
||||
else
|
||||
{
|
||||
var existing = await _db.TileMapSheets.FindAsync(Sheet.Id);
|
||||
if (existing == null)
|
||||
{
|
||||
Message = "TileMap sheet not found.";
|
||||
IsError = true;
|
||||
return Page();
|
||||
}
|
||||
|
||||
existing.Name = Sheet.Name;
|
||||
existing.CdnPath = Sheet.CdnPath;
|
||||
existing.Tags = Sheet.Tags;
|
||||
existing.SheetWidth = Sheet.SheetWidth;
|
||||
existing.SheetHeight = Sheet.SheetHeight;
|
||||
existing.TileWidth = Sheet.TileWidth;
|
||||
existing.TileHeight = Sheet.TileHeight;
|
||||
existing.IsEnabled = Sheet.IsEnabled;
|
||||
existing.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
Sheet = existing;
|
||||
Message = "TileMap sheet updated.";
|
||||
|
||||
TileDefinitions = await _db.TileDefinitions
|
||||
.Where(t => t.TileMapSheetId == Sheet.Id)
|
||||
.OrderBy(t => t.FrameIndex)
|
||||
.ToListAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Message = $"Error: {ex.Message}";
|
||||
IsError = true;
|
||||
}
|
||||
|
||||
CdnImages = await _db.ClientAssets
|
||||
.Where(a => a.AssetType == ClientAssetType.Texture && a.IsEnabled)
|
||||
.OrderBy(a => a.FilePath)
|
||||
.ToListAsync();
|
||||
|
||||
return Page();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostSaveTileAsync(int sheetId, int frameIndex, string? name, int type, string? tags, bool isWalkable, bool blocksProjectiles, bool blocksLineOfSight, int renderLayer)
|
||||
{
|
||||
var existing = await _db.TileDefinitions
|
||||
.FirstOrDefaultAsync(t => t.TileMapSheetId == sheetId && t.FrameIndex == frameIndex);
|
||||
|
||||
if (existing != null)
|
||||
{
|
||||
existing.Name = name;
|
||||
existing.Type = (TileType)type;
|
||||
existing.Tags = tags;
|
||||
existing.IsWalkable = isWalkable;
|
||||
existing.BlocksProjectiles = blocksProjectiles;
|
||||
existing.BlocksLineOfSight = blocksLineOfSight;
|
||||
existing.RenderLayer = renderLayer;
|
||||
}
|
||||
else
|
||||
{
|
||||
_db.TileDefinitions.Add(new TileDefinition
|
||||
{
|
||||
TileMapSheetId = sheetId,
|
||||
FrameIndex = frameIndex,
|
||||
Name = name,
|
||||
Type = (TileType)type,
|
||||
Tags = tags,
|
||||
IsWalkable = isWalkable,
|
||||
BlocksProjectiles = blocksProjectiles,
|
||||
BlocksLineOfSight = blocksLineOfSight,
|
||||
RenderLayer = renderLayer,
|
||||
IsEnabled = true
|
||||
});
|
||||
}
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
return RedirectToPage(new { id = sheetId });
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostDeleteTileAsync(int tileId, int sheetId)
|
||||
{
|
||||
var tile = await _db.TileDefinitions.FindAsync(tileId);
|
||||
if (tile != null)
|
||||
{
|
||||
_db.TileDefinitions.Remove(tile);
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
return RedirectToPage(new { id = sheetId });
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAutoGenerateAsync(int sheetId, int defaultType)
|
||||
{
|
||||
var sheet = await _db.TileMapSheets.FindAsync(sheetId);
|
||||
if (sheet == null) return RedirectToPage();
|
||||
|
||||
// Generate tile definitions for all frames that don't have one
|
||||
var existingFrames = await _db.TileDefinitions
|
||||
.Where(t => t.TileMapSheetId == sheetId)
|
||||
.Select(t => t.FrameIndex)
|
||||
.ToListAsync();
|
||||
|
||||
var totalTiles = sheet.TotalTiles;
|
||||
for (int i = 0; i < totalTiles; i++)
|
||||
{
|
||||
if (!existingFrames.Contains(i))
|
||||
{
|
||||
_db.TileDefinitions.Add(new TileDefinition
|
||||
{
|
||||
TileMapSheetId = sheetId,
|
||||
FrameIndex = i,
|
||||
Type = (TileType)defaultType,
|
||||
IsWalkable = defaultType == 0, // Floor is walkable
|
||||
IsEnabled = true
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
return RedirectToPage(new { id = sheetId });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
@page "/admin/tilemaps"
|
||||
@model TileMaps.IndexModel
|
||||
@{
|
||||
ViewData["Title"] = "TileMap Sheets";
|
||||
}
|
||||
|
||||
<div class="flex items-center justify-between mb-6">
|
||||
<div>
|
||||
<h1 class="text-2xl font-bold text-white">TileMap Sheets</h1>
|
||||
<p class="text-gray-400">Manage tilemap sprite sheets for dungeon generation</p>
|
||||
</div>
|
||||
<a href="/admin/tilemaps/edit" class="btn btn-primary">+ New TileMap Sheet</a>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
@foreach (var sheet in Model.Sheets)
|
||||
{
|
||||
<div class="card">
|
||||
<div class="flex justify-between items-start mb-3">
|
||||
<div>
|
||||
<h3 class="text-lg font-semibold text-white">@sheet.Name</h3>
|
||||
<p class="text-sm text-gray-400">@sheet.CdnPath</p>
|
||||
</div>
|
||||
<span class="px-2 py-1 text-xs rounded @(sheet.IsEnabled ? "bg-green-900 text-green-300" : "bg-red-900 text-red-300")">
|
||||
@(sheet.IsEnabled ? "Enabled" : "Disabled")
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<!-- Preview -->
|
||||
<div class="mb-3 bg-gray-900 rounded-lg p-2 flex justify-center">
|
||||
<img src="@Model.CdnBaseUrl/@sheet.CdnPath"
|
||||
alt="@sheet.Name"
|
||||
class="max-h-32 object-contain"
|
||||
style="image-rendering: pixelated;" />
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-2 gap-2 text-sm mb-3">
|
||||
<div>
|
||||
<span class="text-gray-500">Sheet Size:</span>
|
||||
<span class="text-gray-300">@sheet.SheetWidth x @sheet.SheetHeight</span>
|
||||
</div>
|
||||
<div>
|
||||
<span class="text-gray-500">Tile Size:</span>
|
||||
<span class="text-gray-300">@sheet.TileWidth x @sheet.TileHeight</span>
|
||||
</div>
|
||||
<div>
|
||||
<span class="text-gray-500">Grid:</span>
|
||||
<span class="text-gray-300">@sheet.Columns x @sheet.Rows (@sheet.TotalTiles tiles)</span>
|
||||
</div>
|
||||
<div>
|
||||
<span class="text-gray-500">Defined:</span>
|
||||
<span class="text-gray-300">@(sheet.Tiles?.Count ?? 0) tiles</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (!string.IsNullOrEmpty(sheet.Tags))
|
||||
{
|
||||
<div class="flex flex-wrap gap-1 mb-3">
|
||||
@foreach (var tag in sheet.Tags.Split(',', StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
<span class="px-2 py-0.5 bg-blue-900/50 text-blue-300 text-xs rounded">@tag.Trim()</span>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="flex gap-2">
|
||||
<a href="/admin/tilemaps/edit?id=@sheet.Id" class="btn btn-secondary flex-1 text-center">Edit</a>
|
||||
<form method="post" asp-page-handler="Delete" class="flex-1">
|
||||
<input type="hidden" name="id" value="@sheet.Id" />
|
||||
<button type="submit" class="btn bg-red-600 hover:bg-red-700 w-full"
|
||||
onclick="return confirm('Delete this tilemap sheet?')">Delete</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
@if (Model.Sheets.Count == 0)
|
||||
{
|
||||
<div class="card text-center py-12">
|
||||
<p class="text-gray-400 mb-4">No tilemap sheets yet.</p>
|
||||
<a href="/admin/tilemaps/edit" class="btn btn-primary">Create Your First TileMap Sheet</a>
|
||||
</div>
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DataServer.Data;
|
||||
using DataServer.Data.Entities;
|
||||
|
||||
namespace Server.Services.Admin.Pages.TileMaps;
|
||||
|
||||
public class IndexModel : AdminPageModel
|
||||
{
|
||||
private readonly GameDbContext _db;
|
||||
private readonly IConfiguration _config;
|
||||
|
||||
public IndexModel(GameDbContext db, IConfiguration config)
|
||||
{
|
||||
_db = db;
|
||||
_config = config;
|
||||
}
|
||||
|
||||
public List<TileMapSheet> Sheets { get; set; } = new();
|
||||
public string CdnBaseUrl { get; set; } = "";
|
||||
|
||||
public async Task OnGetAsync()
|
||||
{
|
||||
CdnBaseUrl = _config["Cdn:BaseUrl"] ?? "https://assets.aeternalisgames.com";
|
||||
Sheets = await _db.TileMapSheets
|
||||
.Include(s => s.Tiles)
|
||||
.OrderBy(s => s.Name)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostDeleteAsync(int id)
|
||||
{
|
||||
var sheet = await _db.TileMapSheets.FindAsync(id);
|
||||
if (sheet != null)
|
||||
{
|
||||
// Check if used in any room templates
|
||||
// For now just delete - add check later
|
||||
_db.TileMapSheets.Remove(sheet);
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
return RedirectToPage();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user