Switch NPC editor to mob-style animation assignment selection
This commit is contained in:
@@ -24,36 +24,19 @@ public class EditModel : AdminPageModel
|
||||
public List<DungeonNpcDialogTree> DialogTrees { get; set; } = new();
|
||||
public List<DamageResistanceType> ResistanceTypes { get; set; } = new();
|
||||
public List<MobAbilityDefinition> AllAbilities { get; set; } = new();
|
||||
public List<MobSpriteSheet> AllSpriteSheets { get; set; } = new();
|
||||
public List<MobAnimationDefinition> AllAnimations { get; set; } = new();
|
||||
public List<int> SelectedAbilityIds { get; set; } = new();
|
||||
public List<NpcSpriteRow> SpriteRows { get; set; } = new();
|
||||
public NpcSpriteConfig SpriteConfig { get; set; } = new();
|
||||
public List<NpcAnimationSelection> SelectedAnimations { get; set; } = new();
|
||||
|
||||
public bool IsNew => Npc.Id == 0;
|
||||
public string? Message { get; set; }
|
||||
public bool IsError { get; set; }
|
||||
|
||||
public class NpcSpriteRow
|
||||
public class NpcAnimationSelection
|
||||
{
|
||||
public string Key { get; set; } = "";
|
||||
public int? SheetId { get; set; }
|
||||
public int Frames { get; set; } = 8;
|
||||
public bool Loop { get; set; } = true;
|
||||
}
|
||||
|
||||
public class NpcSpriteConfig
|
||||
{
|
||||
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 class SpriteAnimation
|
||||
{
|
||||
public int Frames { get; set; } = 8;
|
||||
public bool Loop { get; set; } = true;
|
||||
}
|
||||
public int AnimationDefinitionId { get; set; }
|
||||
public string? OverrideKey { get; set; }
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
}
|
||||
|
||||
public async Task OnGetAsync(int? id)
|
||||
@@ -116,9 +99,10 @@ public class EditModel : AdminPageModel
|
||||
Npc.Key = (Npc.Key ?? "").Trim();
|
||||
Npc.Description ??= "";
|
||||
Npc.TintColor = string.IsNullOrWhiteSpace(Npc.TintColor) ? "#ffffff" : Npc.TintColor.Trim();
|
||||
|
||||
ParseAndSetResistancesFromForm();
|
||||
ParseAndSetAbilityIdsFromForm();
|
||||
ParseAndSetSpriteConfigFromForm();
|
||||
ParseAndSetAnimationsFromForm();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Npc.Name))
|
||||
{
|
||||
@@ -130,9 +114,7 @@ public class EditModel : AdminPageModel
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Npc.Key))
|
||||
{
|
||||
Npc.Key = Npc.Name
|
||||
.ToLowerInvariant()
|
||||
.Replace(" ", "_");
|
||||
Npc.Key = Npc.Name.ToLowerInvariant().Replace(" ", "_");
|
||||
}
|
||||
|
||||
try
|
||||
@@ -228,9 +210,12 @@ public class EditModel : AdminPageModel
|
||||
.OrderBy(a => a.Name)
|
||||
.ToListAsync();
|
||||
|
||||
AllSpriteSheets = await _db.MobSpriteSheets
|
||||
.Where(s => s.IsEnabled)
|
||||
.OrderBy(s => s.Name)
|
||||
AllAnimations = await _db.MobAnimationDefinitions
|
||||
.Include(a => a.SpriteSheet)
|
||||
.Where(a => a.IsEnabled)
|
||||
.Where(a => a.SpriteSheet != null && a.SpriteSheet.IsEnabled)
|
||||
.OrderBy(a => a.SpriteSheet!.Name)
|
||||
.ThenBy(a => a.Key)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
@@ -256,42 +241,81 @@ public class EditModel : AdminPageModel
|
||||
Npc.AbilityDefinitionIdsJson = "[]";
|
||||
}
|
||||
|
||||
SelectedAnimations = ParseAnimationsFromSpriteConfig(Npc.SpriteConfigJson);
|
||||
}
|
||||
|
||||
private List<NpcAnimationSelection> ParseAnimationsFromSpriteConfig(string? spriteConfigJson)
|
||||
{
|
||||
var selected = new List<NpcAnimationSelection>();
|
||||
if (string.IsNullOrWhiteSpace(spriteConfigJson)) return selected;
|
||||
|
||||
try
|
||||
{
|
||||
SpriteConfig = string.IsNullOrWhiteSpace(Npc.SpriteConfigJson)
|
||||
? new NpcSpriteConfig()
|
||||
: (JsonSerializer.Deserialize<NpcSpriteConfig>(Npc.SpriteConfigJson!, new JsonSerializerOptions
|
||||
using var doc = JsonDocument.Parse(spriteConfigJson);
|
||||
var root = doc.RootElement;
|
||||
|
||||
if (!root.TryGetProperty("sheets", out var sheetsElem) || !root.TryGetProperty("animations", out var animationsElem))
|
||||
return selected;
|
||||
|
||||
foreach (var animProp in animationsElem.EnumerateObject())
|
||||
{
|
||||
var animKey = animProp.Name;
|
||||
var animObj = animProp.Value;
|
||||
if (!animObj.TryGetProperty("sheet", out var sheetRefElem)) continue;
|
||||
var sheetRef = sheetRefElem.GetString();
|
||||
if (string.IsNullOrWhiteSpace(sheetRef)) continue;
|
||||
if (!sheetsElem.TryGetProperty(sheetRef, out var sheetObj)) continue;
|
||||
if (!sheetObj.TryGetProperty("path", out var pathElem)) continue;
|
||||
var path = pathElem.GetString();
|
||||
if (string.IsNullOrWhiteSpace(path)) continue;
|
||||
|
||||
var frames = animObj.TryGetProperty("frames", out var framesElem)
|
||||
? framesElem.EnumerateArray().Select(x => x.GetInt32()).ToArray()
|
||||
: Array.Empty<int>();
|
||||
var fps = animObj.TryGetProperty("fps", out var fpsElem) ? fpsElem.GetSingle() : 8f;
|
||||
var loop = animObj.TryGetProperty("loop", out var loopElem) && loopElem.GetBoolean();
|
||||
var flipX = animObj.TryGetProperty("flipX", out var flipXElem) && flipXElem.GetBoolean();
|
||||
var flipY = animObj.TryGetProperty("flipY", out var flipYElem) && flipYElem.GetBoolean();
|
||||
|
||||
var match = AllAnimations.FirstOrDefault(a =>
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
}) ?? new NpcSpriteConfig());
|
||||
var sheet = a.SpriteSheet;
|
||||
if (sheet == null) return false;
|
||||
if (!string.Equals(sheet.CdnPath, path, StringComparison.OrdinalIgnoreCase)) return false;
|
||||
if (Math.Abs(a.Fps - fps) > 0.01f) return false;
|
||||
if (a.Loop != loop || a.FlipX != flipX || a.FlipY != flipY) return false;
|
||||
var aFrames = ParseFrameSequence(a.FrameSequence);
|
||||
return aFrames.SequenceEqual(frames);
|
||||
});
|
||||
|
||||
if (match == null)
|
||||
{
|
||||
match = AllAnimations.FirstOrDefault(a =>
|
||||
a.SpriteSheet != null &&
|
||||
string.Equals(a.Key, animKey, StringComparison.OrdinalIgnoreCase) &&
|
||||
string.Equals(a.SpriteSheet.CdnPath, path, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
if (match != null)
|
||||
{
|
||||
selected.Add(new NpcAnimationSelection
|
||||
{
|
||||
AnimationDefinitionId = match.Id,
|
||||
OverrideKey = string.Equals(animKey, match.Key, StringComparison.OrdinalIgnoreCase) ? null : animKey,
|
||||
IsEnabled = true
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
SpriteConfig = new NpcSpriteConfig();
|
||||
// Keep empty if invalid JSON
|
||||
}
|
||||
|
||||
SpriteRows = new List<NpcSpriteRow>();
|
||||
foreach (var kv in SpriteConfig.Sprites)
|
||||
{
|
||||
var frames = 8;
|
||||
var loop = true;
|
||||
if (SpriteConfig.Animations.TryGetValue(kv.Key, out var anim))
|
||||
{
|
||||
frames = anim.Frames;
|
||||
loop = anim.Loop;
|
||||
}
|
||||
|
||||
var matchingSheet = AllSpriteSheets.FirstOrDefault(s =>
|
||||
string.Equals(s.CdnPath, kv.Value, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
SpriteRows.Add(new NpcSpriteRow
|
||||
{
|
||||
Key = kv.Key,
|
||||
SheetId = matchingSheet?.Id,
|
||||
Frames = frames,
|
||||
Loop = loop
|
||||
});
|
||||
}
|
||||
return selected
|
||||
.GroupBy(x => new { x.AnimationDefinitionId, Key = (x.OverrideKey ?? "").Trim().ToLowerInvariant() })
|
||||
.Select(g => g.First())
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private void ParseAndSetResistancesFromForm()
|
||||
@@ -323,56 +347,101 @@ public class EditModel : AdminPageModel
|
||||
Npc.AbilityDefinitionIdsJson = JsonSerializer.Serialize(list);
|
||||
}
|
||||
|
||||
private void ParseAndSetSpriteConfigFromForm()
|
||||
private void ParseAndSetAnimationsFromForm()
|
||||
{
|
||||
var spriteKeys = Request.Form["sprite_key[]"].ToArray();
|
||||
var spriteSheetIds = Request.Form["sprite_sheet_id[]"].ToArray();
|
||||
var spriteFrames = Request.Form["sprite_frames[]"].ToArray();
|
||||
var spriteLoops = Request.Form["sprite_loop[]"].ToArray();
|
||||
var ids = Request.Form["npc_anim_id[]"].ToArray();
|
||||
var overrides = Request.Form["npc_anim_override_key[]"].ToArray();
|
||||
var enabledValues = Request.Form["npc_anim_enabled[]"].ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var config = new NpcSpriteConfig();
|
||||
if (int.TryParse(Request.Form["sprite_frameWidth"], out var fw)) config.FrameWidth = fw;
|
||||
if (int.TryParse(Request.Form["sprite_frameHeight"], out var fh)) config.FrameHeight = fh;
|
||||
if (int.TryParse(Request.Form["sprite_fps"], out var fps)) config.Fps = fps;
|
||||
|
||||
for (var i = 0; i < spriteKeys.Length && i < spriteSheetIds.Length; i++)
|
||||
var selected = new List<NpcAnimationSelection>();
|
||||
for (var i = 0; i < ids.Length; i++)
|
||||
{
|
||||
var key = (spriteKeys[i] ?? "").Trim();
|
||||
if (string.IsNullOrWhiteSpace(key)) continue;
|
||||
if (!int.TryParse(spriteSheetIds[i], out var sheetId)) continue;
|
||||
var sheet = AllSpriteSheets.FirstOrDefault(s => s.Id == sheetId);
|
||||
if (sheet == null) continue;
|
||||
if (!int.TryParse(ids[i], out var animId) || animId <= 0) continue;
|
||||
var isEnabled = enabledValues.Contains(i.ToString());
|
||||
var ov = i < overrides.Length ? overrides[i]?.Trim() : null;
|
||||
if (string.IsNullOrWhiteSpace(ov)) ov = null;
|
||||
|
||||
var frames = 8;
|
||||
if (i < spriteFrames.Length && int.TryParse(spriteFrames[i], out var rowFrames))
|
||||
selected.Add(new NpcAnimationSelection
|
||||
{
|
||||
frames = Math.Max(1, rowFrames);
|
||||
}
|
||||
|
||||
var loop = spriteLoops.Contains(i.ToString());
|
||||
config.Sprites[key] = sheet.CdnPath;
|
||||
config.Animations[key] = new NpcSpriteConfig.SpriteAnimation
|
||||
{
|
||||
Frames = frames,
|
||||
Loop = loop
|
||||
};
|
||||
AnimationDefinitionId = animId,
|
||||
OverrideKey = ov,
|
||||
IsEnabled = isEnabled
|
||||
});
|
||||
}
|
||||
|
||||
if (config.Sprites.Count == 0)
|
||||
selected = selected
|
||||
.GroupBy(x => new { x.AnimationDefinitionId, Key = (x.OverrideKey ?? "").Trim().ToLowerInvariant() })
|
||||
.Select(g => g.First())
|
||||
.ToList();
|
||||
|
||||
SelectedAnimations = selected;
|
||||
|
||||
var enabled = selected.Where(s => s.IsEnabled).ToList();
|
||||
if (enabled.Count == 0)
|
||||
{
|
||||
SpriteConfig = new NpcSpriteConfig();
|
||||
SpriteRows = new List<NpcSpriteRow>();
|
||||
Npc.SpriteConfigJson = null;
|
||||
return;
|
||||
}
|
||||
|
||||
SpriteConfig = config;
|
||||
Npc.SpriteConfigJson = JsonSerializer.Serialize(config, new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
});
|
||||
var byId = AllAnimations.Where(a => a.SpriteSheet != null).ToDictionary(a => a.Id, a => a);
|
||||
|
||||
// rebuild display rows for round-trip on validation failure
|
||||
ParseNpcJsonFields();
|
||||
var sheets = new Dictionary<string, object>();
|
||||
var sheetKeyById = new Dictionary<int, string>();
|
||||
var animations = new Dictionary<string, object>();
|
||||
|
||||
foreach (var sel in enabled)
|
||||
{
|
||||
if (!byId.TryGetValue(sel.AnimationDefinitionId, out var anim)) continue;
|
||||
var sheet = anim.SpriteSheet;
|
||||
if (sheet == null) continue;
|
||||
|
||||
if (!sheetKeyById.TryGetValue(sheet.Id, out var sheetKey))
|
||||
{
|
||||
sheetKey = $"sheet_{sheet.Id}";
|
||||
sheetKeyById[sheet.Id] = sheetKey;
|
||||
sheets[sheetKey] = new
|
||||
{
|
||||
path = sheet.CdnPath,
|
||||
width = sheet.SheetWidth,
|
||||
height = sheet.SheetHeight,
|
||||
frameWidth = sheet.FrameWidth,
|
||||
frameHeight = sheet.FrameHeight
|
||||
};
|
||||
}
|
||||
|
||||
var key = !string.IsNullOrWhiteSpace(sel.OverrideKey) ? sel.OverrideKey! : anim.Key;
|
||||
animations[key] = new
|
||||
{
|
||||
sheet = sheetKey,
|
||||
frames = ParseFrameSequence(anim.FrameSequence),
|
||||
fps = anim.Fps,
|
||||
loop = anim.Loop,
|
||||
flipX = anim.FlipX,
|
||||
flipY = anim.FlipY
|
||||
};
|
||||
}
|
||||
|
||||
if (animations.Count == 0)
|
||||
{
|
||||
Npc.SpriteConfigJson = null;
|
||||
return;
|
||||
}
|
||||
|
||||
Npc.SpriteConfigJson = JsonSerializer.Serialize(new
|
||||
{
|
||||
sheets,
|
||||
animations
|
||||
});
|
||||
}
|
||||
|
||||
private static int[] ParseFrameSequence(string frameSequence)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(frameSequence))
|
||||
return new[] { 0 };
|
||||
|
||||
return frameSequence
|
||||
.Split(',')
|
||||
.Select(s => int.TryParse(s.Trim(), out var n) ? n : 0)
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user