Additional tools for Admin portal, and huge NameSpace Revamp
This commit is contained in:
@@ -1,10 +1,29 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Server.Data;
|
||||
using Server.Data.Entities;
|
||||
using DataServer.Data;
|
||||
using DataServer.Data.Entities;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace VoidScavengers.Services.Admin.Pages.Mobs;
|
||||
namespace Server.Services.Admin.Pages.Mobs;
|
||||
|
||||
// Helper class for sprite configuration
|
||||
public class MobSpriteConfig
|
||||
{
|
||||
public Dictionary<string, string> Sprites { get; set; } = new();
|
||||
public Dictionary<string, SpriteAnimation> Animations { get; set; } = new();
|
||||
public int FrameWidth { get; set; } = 64;
|
||||
public int FrameHeight { get; set; } = 64;
|
||||
public int Fps { get; set; } = 8;
|
||||
|
||||
public int GetFrameCount(string key) => Animations.TryGetValue(key, out var anim) ? anim.Frames : 8;
|
||||
public bool IsLooping(string key) => !Animations.TryGetValue(key, out var anim) || anim.Loop;
|
||||
|
||||
public class SpriteAnimation
|
||||
{
|
||||
public int Frames { get; set; } = 8;
|
||||
public bool Loop { get; set; } = true;
|
||||
}
|
||||
}
|
||||
|
||||
public class EditModel : AdminPageModel
|
||||
{
|
||||
@@ -45,6 +64,21 @@ public class EditModel : AdminPageModel
|
||||
return value;
|
||||
return 0f;
|
||||
}
|
||||
|
||||
// Sprite config helper
|
||||
public MobSpriteConfig SpriteConfig
|
||||
{
|
||||
get
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(Mob.SpriteConfigJson)) return new();
|
||||
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
return JsonSerializer.Deserialize<MobSpriteConfig>(Mob.SpriteConfigJson, options) ?? new();
|
||||
}
|
||||
catch { return new(); }
|
||||
}
|
||||
}
|
||||
|
||||
public async Task OnGetAsync(int? id)
|
||||
{
|
||||
@@ -115,6 +149,39 @@ public class EditModel : AdminPageModel
|
||||
}
|
||||
}
|
||||
Mob.ResistancesJson = JsonSerializer.Serialize(resistances);
|
||||
|
||||
// Parse sprite config from form
|
||||
var spriteConfig = new MobSpriteConfig();
|
||||
if (int.TryParse(Request.Form["sprite_frameWidth"], out var fw)) spriteConfig.FrameWidth = fw;
|
||||
if (int.TryParse(Request.Form["sprite_frameHeight"], out var fh)) spriteConfig.FrameHeight = fh;
|
||||
if (int.TryParse(Request.Form["sprite_fps"], out var fps)) spriteConfig.Fps = fps;
|
||||
|
||||
var spriteKeys = Request.Form["sprite_key[]"].ToArray();
|
||||
var spritePaths = Request.Form["sprite_path[]"].ToArray();
|
||||
var spriteFrames = Request.Form["sprite_frames[]"].ToArray();
|
||||
var spriteLoops = Request.Form["sprite_loop[]"].ToArray();
|
||||
|
||||
for (int i = 0; i < spriteKeys.Length && i < spritePaths.Length; i++)
|
||||
{
|
||||
var key = spriteKeys[i]?.Trim();
|
||||
var path = spritePaths[i]?.Trim();
|
||||
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(path))
|
||||
{
|
||||
spriteConfig.Sprites[key] = path;
|
||||
var frames = i < spriteFrames.Length && int.TryParse(spriteFrames[i], out var f) ? f : 8;
|
||||
var loop = spriteLoops.Contains(key) || spriteLoops.Contains("new");
|
||||
spriteConfig.Animations[key] = new MobSpriteConfig.SpriteAnimation { Frames = frames, Loop = loop };
|
||||
}
|
||||
}
|
||||
|
||||
if (spriteConfig.Sprites.Any())
|
||||
{
|
||||
Mob.SpriteConfigJson = JsonSerializer.Serialize(spriteConfig, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
|
||||
}
|
||||
else
|
||||
{
|
||||
Mob.SpriteConfigJson = null;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Mob.Name))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user