Admin tools for mob sprites
This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DataServer.Data;
|
||||
using DataServer.Data.Entities;
|
||||
|
||||
namespace Server.Services.Admin.Pages.SpriteSheets;
|
||||
|
||||
public class EditModel : AdminPageModel
|
||||
{
|
||||
private readonly GameDbContext _db;
|
||||
private readonly IConfiguration _config;
|
||||
|
||||
public EditModel(GameDbContext db, IConfiguration config)
|
||||
{
|
||||
_db = db;
|
||||
_config = config;
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
public MobSpriteSheet Sheet { get; set; } = new();
|
||||
|
||||
public List<MobAnimationDefinition> Animations { 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)
|
||||
.OrderBy(a => a.FilePath)
|
||||
.ToListAsync();
|
||||
|
||||
if (id.HasValue)
|
||||
{
|
||||
var sheet = await _db.MobSpriteSheets
|
||||
.Include(s => s.Animations)
|
||||
.FirstOrDefaultAsync(s => s.Id == id.Value);
|
||||
|
||||
if (sheet != null)
|
||||
{
|
||||
Sheet = sheet;
|
||||
Animations = sheet.Animations?.OrderBy(a => a.Key).ToList() ?? new();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Sheet = new MobSpriteSheet
|
||||
{
|
||||
IsEnabled = true
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
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.MobSpriteSheets.Add(Sheet);
|
||||
await _db.SaveChangesAsync();
|
||||
Message = $"Sprite sheet '{Sheet.Name}' created.";
|
||||
return RedirectToPage(new { id = Sheet.Id });
|
||||
}
|
||||
else
|
||||
{
|
||||
var existing = await _db.MobSpriteSheets.FindAsync(Sheet.Id);
|
||||
if (existing == null)
|
||||
{
|
||||
Message = "Sprite sheet not found.";
|
||||
IsError = true;
|
||||
return Page();
|
||||
}
|
||||
|
||||
existing.Name = Sheet.Name;
|
||||
existing.CdnPath = Sheet.CdnPath;
|
||||
existing.SheetWidth = Sheet.SheetWidth;
|
||||
existing.SheetHeight = Sheet.SheetHeight;
|
||||
existing.FrameWidth = Sheet.FrameWidth;
|
||||
existing.FrameHeight = Sheet.FrameHeight;
|
||||
existing.IsEnabled = Sheet.IsEnabled;
|
||||
existing.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
Sheet = existing;
|
||||
Message = "Sprite sheet updated.";
|
||||
|
||||
Animations = await _db.MobAnimationDefinitions
|
||||
.Where(a => a.SpriteSheetId == Sheet.Id)
|
||||
.OrderBy(a => a.Key)
|
||||
.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> OnPostAddAnimationAsync(int sheetId, string key, string? name, string frameSequence, float fps, bool loop, bool flipX, bool flipY)
|
||||
{
|
||||
var animation = new MobAnimationDefinition
|
||||
{
|
||||
SpriteSheetId = sheetId,
|
||||
Key = key,
|
||||
Name = name,
|
||||
FrameSequence = frameSequence,
|
||||
Fps = fps,
|
||||
Loop = loop,
|
||||
FlipX = flipX,
|
||||
FlipY = flipY,
|
||||
IsEnabled = true
|
||||
};
|
||||
|
||||
_db.MobAnimationDefinitions.Add(animation);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return RedirectToPage(new { id = sheetId });
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostUpdateAnimationAsync(int animId, int sheetId, string key, string? name, string frameSequence, float fps, bool loop, bool flipX, bool flipY, bool isEnabled)
|
||||
{
|
||||
var animation = await _db.MobAnimationDefinitions.FindAsync(animId);
|
||||
if (animation != null)
|
||||
{
|
||||
animation.Key = key;
|
||||
animation.Name = name;
|
||||
animation.FrameSequence = frameSequence;
|
||||
animation.Fps = fps;
|
||||
animation.Loop = loop;
|
||||
animation.FlipX = flipX;
|
||||
animation.FlipY = flipY;
|
||||
animation.IsEnabled = isEnabled;
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return RedirectToPage(new { id = sheetId });
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostDeleteAnimationAsync(int animId, int sheetId)
|
||||
{
|
||||
var animation = await _db.MobAnimationDefinitions.FindAsync(animId);
|
||||
if (animation != null)
|
||||
{
|
||||
// Check if assigned to any mobs
|
||||
var assignmentCount = await _db.MobAnimationAssignments
|
||||
.Where(a => a.AnimationDefinitionId == animId)
|
||||
.CountAsync();
|
||||
|
||||
if (assignmentCount > 0)
|
||||
{
|
||||
TempData["Error"] = $"Cannot delete: animation is assigned to {assignmentCount} mob(s).";
|
||||
return RedirectToPage(new { id = sheetId });
|
||||
}
|
||||
|
||||
_db.MobAnimationDefinitions.Remove(animation);
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return RedirectToPage(new { id = sheetId });
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostDuplicateAsync(int id)
|
||||
{
|
||||
var original = await _db.MobSpriteSheets
|
||||
.Include(s => s.Animations)
|
||||
.FirstOrDefaultAsync(s => s.Id == id);
|
||||
|
||||
if (original == null) return RedirectToPage();
|
||||
|
||||
var copy = new MobSpriteSheet
|
||||
{
|
||||
Name = original.Name + " (Copy)",
|
||||
CdnPath = original.CdnPath,
|
||||
SheetWidth = original.SheetWidth,
|
||||
SheetHeight = original.SheetHeight,
|
||||
FrameWidth = original.FrameWidth,
|
||||
FrameHeight = original.FrameHeight,
|
||||
IsEnabled = true,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_db.MobSpriteSheets.Add(copy);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
// Copy animations
|
||||
if (original.Animations != null)
|
||||
{
|
||||
foreach (var anim in original.Animations)
|
||||
{
|
||||
_db.MobAnimationDefinitions.Add(new MobAnimationDefinition
|
||||
{
|
||||
SpriteSheetId = copy.Id,
|
||||
Key = anim.Key,
|
||||
Name = anim.Name,
|
||||
FrameSequence = anim.FrameSequence,
|
||||
Fps = anim.Fps,
|
||||
Loop = anim.Loop,
|
||||
FlipX = anim.FlipX,
|
||||
FlipY = anim.FlipY,
|
||||
IsEnabled = anim.IsEnabled
|
||||
});
|
||||
}
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return RedirectToPage(new { id = copy.Id });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user