SpriteSheets

This commit is contained in:
dingo
2025-12-27 20:24:49 -05:00
parent c16dc1259d
commit 326110ada8
5 changed files with 2395 additions and 12 deletions
+19 -10
View File
@@ -39,6 +39,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
// Mob definitions cache
private Dictionary<int, DungeonMobDefinition> _mobDefinitions = new();
private Dictionary<int, string?> _mobSpriteConfigs = new(); // MobDefinitionId -> SpriteConfig JSON
private bool _initialized = false;
// Tick management
@@ -238,20 +239,28 @@ public sealed partial class DungeonHandler : IClientMessageHandler
_log.LogCritical("Dungeon Mobs Table EMPTY");
}
// Debug: Log which mobs have SpriteConfigJson
_mobDefinitions = mobs.ToDictionary(m => m.Id);
// Build sprite configs from animation assignments
var spriteConfigBuilder = new MobSpriteConfigBuilder(db);
_mobSpriteConfigs = await spriteConfigBuilder.BuildSpriteConfigsAsync(mobs.Select(m => m.Id));
// Log sprite config status
foreach (var mob in mobs)
{
if (!string.IsNullOrEmpty(mob.SpriteConfigJson))
if (_mobSpriteConfigs.TryGetValue(mob.Id, out var config) && !string.IsNullOrEmpty(config))
{
_log.LogInformation("Mob {Name} has SpriteConfigJson: {Json}", mob.Name, mob.SpriteConfigJson.Substring(0, Math.Min(100, mob.SpriteConfigJson.Length)));
_log.LogInformation("Mob {Name} has new-format SpriteConfig ({Length} chars)", mob.Name, config.Length);
}
else if (!string.IsNullOrEmpty(mob.SpriteConfigJson))
{
_log.LogInformation("Mob {Name} has legacy SpriteConfigJson: {Json}", mob.Name, mob.SpriteConfigJson.Substring(0, Math.Min(100, mob.SpriteConfigJson.Length)));
}
else
{
_log.LogInformation("Mob {Name} has NO SpriteConfigJson", mob.Name);
_log.LogInformation("Mob {Name} has NO sprite config", mob.Name);
}
}
_mobDefinitions = mobs.ToDictionary(m => m.Id);
}
public async Task HandleAsync(int opCode, IConnection connection, ReadOnlyMemory<byte> payload, CancellationToken ct)
@@ -658,7 +667,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
int seed = Random.Shared.Next();
var layout = DungeonGenerator.Generate(seed, tier);
var simulation = new DungeonSimulation(layout, tier, _mobDefinitions, bracketMin);
var simulation = new DungeonSimulation(layout, tier, _mobDefinitions, bracketMin, null, _mobSpriteConfigs);
instance = new DungeonInstance
{
@@ -2077,7 +2086,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
int seed = Random.Shared.Next();
var layout = DungeonGenerator.Generate(seed, tier);
var simulation = new DungeonSimulation(layout, tier, _mobDefinitions, bracketMin);
var simulation = new DungeonSimulation(layout, tier, _mobDefinitions, bracketMin, null, _mobSpriteConfigs);
var instance = new DungeonInstance
{
@@ -2293,7 +2302,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
try
{
var layout = DungeonGenerator.Generate(persisted.Seed, persisted.Tier);
var simulation = new DungeonSimulation(layout, persisted.Tier, _mobDefinitions, persisted.LevelBracketMin);
var simulation = new DungeonSimulation(layout, persisted.Tier, _mobDefinitions, persisted.LevelBracketMin, null, _mobSpriteConfigs);
// NOTE: We intentionally do NOT restore mob state here.
// When a dungeon is restored (all players left), ALL mobs respawn fresh.
@@ -2365,7 +2374,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
// Create a fresh simulation with the same parameters
var layout = DungeonGenerator.Generate(instance.Seed, instance.Tier);
var freshSimulation = new DungeonSimulation(layout, instance.Tier, _mobDefinitions, instance.LevelBracketMin);
var freshSimulation = new DungeonSimulation(layout, instance.Tier, _mobDefinitions, instance.LevelBracketMin, null, _mobSpriteConfigs);
// Clear and rebuild mobs, keeping those with loot
instance.Simulation.Mobs.Clear();
+21 -2
View File
@@ -46,14 +46,22 @@ public class DungeonSimulation
private int _nextDroppedItemId = 1;
private readonly Dictionary<int, DungeonMobDefinition> _mobDefinitions;
private readonly Dictionary<int, MobAbilityDefinition>? _abilityDefinitions;
private readonly Dictionary<int, string?>? _spriteConfigs;
public DungeonSimulation(DungeonLayout layout, int tier, Dictionary<int, DungeonMobDefinition> mobDefinitions, int bracketMin = 1, Dictionary<int, MobAbilityDefinition>? abilityDefinitions = null)
public DungeonSimulation(
DungeonLayout layout,
int tier,
Dictionary<int, DungeonMobDefinition> mobDefinitions,
int bracketMin = 1,
Dictionary<int, MobAbilityDefinition>? abilityDefinitions = null,
Dictionary<int, string?>? spriteConfigs = null)
{
Layout = layout;
Tier = tier;
BracketMin = bracketMin;
_mobDefinitions = mobDefinitions;
_abilityDefinitions = abilityDefinitions;
_spriteConfigs = spriteConfigs;
SpawnMobs();
InitializeChests();
@@ -180,6 +188,17 @@ public class DungeonSimulation
// Use bracket-aware stat scaling
var stats = mobDef.GetStatsForBracket(BracketMin);
// Get sprite config - prefer new format from _spriteConfigs, fallback to legacy SpriteConfigJson
string? spriteConfig = null;
if (_spriteConfigs != null && _spriteConfigs.TryGetValue(mobDef.Id, out var newConfig) && !string.IsNullOrEmpty(newConfig))
{
spriteConfig = newConfig;
}
else
{
spriteConfig = mobDef.SpriteConfigJson;
}
var mob = new MobInstance
{
Id = mobSpawn.Id,
@@ -196,7 +215,7 @@ public class DungeonSimulation
Stats = stats,
State = MobState.Idle,
StateTime = (float)(rng.NextDouble() * 3f), // Randomize so mobs don't all start moving at once
SpriteConfigJson = mobDef.SpriteConfigJson,
SpriteConfigJson = spriteConfig,
TintColor = mobDef.TintColor,
Scale = mobDef.Scale,
Type = mobDef.Type,
+195
View File
@@ -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();
}
}
File diff suppressed because it is too large Load Diff
+22
View File
@@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SystemRevivalServer.Migrations
{
/// <inheritdoc />
public partial class spritesheets : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}