SpriteSheets
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DataServer.Data;
|
||||
using DataServer.Data.Entities;
|
||||
|
||||
namespace Server.Game.Dungeon;
|
||||
|
||||
/// <summary>
|
||||
/// Builds sprite configuration JSON for mobs from the database animation system
|
||||
/// </summary>
|
||||
public class MobSpriteConfigBuilder
|
||||
{
|
||||
private readonly GameDbContext _db;
|
||||
|
||||
public MobSpriteConfigBuilder(GameDbContext db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build sprite config for a mob from its animation assignments
|
||||
/// </summary>
|
||||
public async Task<string?> BuildSpriteConfigAsync(int mobDefinitionId)
|
||||
{
|
||||
// Get all animation assignments for this mob
|
||||
var assignments = await _db.MobAnimationAssignments
|
||||
.Include(a => a.AnimationDefinition)
|
||||
.ThenInclude(d => d!.SpriteSheet)
|
||||
.Where(a => a.MobDefinitionId == mobDefinitionId && a.IsEnabled)
|
||||
.Where(a => a.AnimationDefinition != null && a.AnimationDefinition.IsEnabled)
|
||||
.Where(a => a.AnimationDefinition!.SpriteSheet != null && a.AnimationDefinition.SpriteSheet.IsEnabled)
|
||||
.ToListAsync();
|
||||
|
||||
if (assignments.Count == 0)
|
||||
return null;
|
||||
|
||||
// Build sheets dictionary (deduplicated)
|
||||
var sheets = new Dictionary<string, object>();
|
||||
var sheetKeyMap = new Dictionary<int, string>(); // SpriteSheetId -> key in sheets dict
|
||||
|
||||
foreach (var assignment in assignments)
|
||||
{
|
||||
var sheet = assignment.AnimationDefinition!.SpriteSheet!;
|
||||
if (!sheetKeyMap.ContainsKey(sheet.Id))
|
||||
{
|
||||
var sheetKey = $"sheet_{sheet.Id}";
|
||||
sheetKeyMap[sheet.Id] = sheetKey;
|
||||
sheets[sheetKey] = new
|
||||
{
|
||||
path = sheet.CdnPath,
|
||||
width = sheet.SheetWidth,
|
||||
height = sheet.SheetHeight,
|
||||
frameWidth = sheet.FrameWidth,
|
||||
frameHeight = sheet.FrameHeight
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Build animations dictionary
|
||||
var animations = new Dictionary<string, object>();
|
||||
|
||||
foreach (var assignment in assignments)
|
||||
{
|
||||
var anim = assignment.AnimationDefinition!;
|
||||
var sheetKey = sheetKeyMap[anim.SpriteSheetId];
|
||||
|
||||
// Use override key if specified, otherwise use animation's key
|
||||
var animKey = !string.IsNullOrEmpty(assignment.OverrideKey)
|
||||
? assignment.OverrideKey
|
||||
: anim.Key;
|
||||
|
||||
// Parse frame sequence
|
||||
var frames = ParseFrameSequence(anim.FrameSequence);
|
||||
|
||||
animations[animKey] = new
|
||||
{
|
||||
sheet = sheetKey,
|
||||
frames = frames,
|
||||
fps = anim.Fps,
|
||||
loop = anim.Loop,
|
||||
flipX = anim.FlipX,
|
||||
flipY = anim.FlipY
|
||||
};
|
||||
}
|
||||
|
||||
var config = new
|
||||
{
|
||||
sheets = sheets,
|
||||
animations = animations
|
||||
};
|
||||
|
||||
return JsonSerializer.Serialize(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build sprite configs for multiple mobs efficiently
|
||||
/// </summary>
|
||||
public async Task<Dictionary<int, string?>> BuildSpriteConfigsAsync(IEnumerable<int> mobDefinitionIds)
|
||||
{
|
||||
var result = new Dictionary<int, string?>();
|
||||
|
||||
// Get all assignments for all mobs in one query
|
||||
var allAssignments = await _db.MobAnimationAssignments
|
||||
.Include(a => a.AnimationDefinition)
|
||||
.ThenInclude(d => d!.SpriteSheet)
|
||||
.Where(a => mobDefinitionIds.Contains(a.MobDefinitionId) && a.IsEnabled)
|
||||
.Where(a => a.AnimationDefinition != null && a.AnimationDefinition.IsEnabled)
|
||||
.Where(a => a.AnimationDefinition!.SpriteSheet != null && a.AnimationDefinition.SpriteSheet.IsEnabled)
|
||||
.ToListAsync();
|
||||
|
||||
// Group by mob
|
||||
var grouped = allAssignments.GroupBy(a => a.MobDefinitionId);
|
||||
|
||||
foreach (var group in grouped)
|
||||
{
|
||||
var mobId = group.Key;
|
||||
var assignments = group.ToList();
|
||||
|
||||
if (assignments.Count == 0)
|
||||
{
|
||||
result[mobId] = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Build sheets dictionary
|
||||
var sheets = new Dictionary<string, object>();
|
||||
var sheetKeyMap = new Dictionary<int, string>();
|
||||
|
||||
foreach (var assignment in assignments)
|
||||
{
|
||||
var sheet = assignment.AnimationDefinition!.SpriteSheet!;
|
||||
if (!sheetKeyMap.ContainsKey(sheet.Id))
|
||||
{
|
||||
var sheetKey = $"sheet_{sheet.Id}";
|
||||
sheetKeyMap[sheet.Id] = sheetKey;
|
||||
sheets[sheetKey] = new
|
||||
{
|
||||
path = sheet.CdnPath,
|
||||
width = sheet.SheetWidth,
|
||||
height = sheet.SheetHeight,
|
||||
frameWidth = sheet.FrameWidth,
|
||||
frameHeight = sheet.FrameHeight
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Build animations dictionary
|
||||
var animations = new Dictionary<string, object>();
|
||||
|
||||
foreach (var assignment in assignments)
|
||||
{
|
||||
var anim = assignment.AnimationDefinition!;
|
||||
var sheetKey = sheetKeyMap[anim.SpriteSheetId];
|
||||
var animKey = !string.IsNullOrEmpty(assignment.OverrideKey)
|
||||
? assignment.OverrideKey
|
||||
: anim.Key;
|
||||
|
||||
var frames = ParseFrameSequence(anim.FrameSequence);
|
||||
|
||||
animations[animKey] = new
|
||||
{
|
||||
sheet = sheetKey,
|
||||
frames = frames,
|
||||
fps = anim.Fps,
|
||||
loop = anim.Loop,
|
||||
flipX = anim.FlipX,
|
||||
flipY = anim.FlipY
|
||||
};
|
||||
}
|
||||
|
||||
var config = new { sheets, animations };
|
||||
result[mobId] = JsonSerializer.Serialize(config);
|
||||
}
|
||||
|
||||
// Add null for mobs without assignments
|
||||
foreach (var mobId in mobDefinitionIds)
|
||||
{
|
||||
if (!result.ContainsKey(mobId))
|
||||
result[mobId] = null;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int[] ParseFrameSequence(string frameSequence)
|
||||
{
|
||||
if (string.IsNullOrEmpty(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