230 lines
7.7 KiB
C#
230 lines
7.7 KiB
C#
using DataServer.Data.Entities;
|
|
using DataServer.Data.Enums;
|
|
using GameServer.Dungeon;
|
|
|
|
namespace GameServer.World;
|
|
|
|
/// <summary>
|
|
/// Tracks mob spawn points in a zone, handles respawn timers, and creates MobInstance objects.
|
|
/// Each spawn point can have multiple mobs (SpawnCount) that respawn independently.
|
|
/// </summary>
|
|
public sealed class WorldMobSpawnTracker
|
|
{
|
|
private readonly Dictionary<int, DungeonMobDefinition> _mobDefinitions;
|
|
private readonly List<SpawnPoint> _spawnPoints = new();
|
|
private int _nextMobId = 1;
|
|
private readonly Random _rng = new();
|
|
|
|
public WorldMobSpawnTracker(Dictionary<int, DungeonMobDefinition> mobDefinitions)
|
|
{
|
|
_mobDefinitions = mobDefinitions;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register a mob spawn point from chunk data.
|
|
/// Returns any initially spawned mobs.
|
|
/// </summary>
|
|
public List<MobInstance> RegisterSpawnPoint(WorldChunkMobSpawnData data, int chunkWorldOffsetX, int chunkWorldOffsetY)
|
|
{
|
|
if (!_mobDefinitions.TryGetValue(data.MobDefinitionId, out var mobDef))
|
|
return new List<MobInstance>();
|
|
|
|
var spawnPoint = new SpawnPoint
|
|
{
|
|
SpawnDataId = data.Id,
|
|
MobDefinitionId = data.MobDefinitionId,
|
|
WorldX = chunkWorldOffsetX + (data.TileX * 32) + 16,
|
|
WorldY = chunkWorldOffsetY + (data.TileY * 32) + 16,
|
|
SpawnCount = Math.Max(1, data.SpawnCount),
|
|
RespawnSeconds = Math.Max(5f, data.RespawnSeconds),
|
|
ActiveMobIds = new List<int>(),
|
|
RespawnTimers = new List<float>()
|
|
};
|
|
|
|
_spawnPoints.Add(spawnPoint);
|
|
|
|
// Spawn initial mobs
|
|
var mobs = new List<MobInstance>();
|
|
for (int i = 0; i < spawnPoint.SpawnCount; i++)
|
|
{
|
|
var mob = CreateMob(mobDef, spawnPoint);
|
|
if (mob != null)
|
|
{
|
|
spawnPoint.ActiveMobIds.Add(mob.Id);
|
|
mobs.Add(mob);
|
|
}
|
|
}
|
|
|
|
return mobs;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notify that a mob has died. Start respawn timer for its spawn point.
|
|
/// </summary>
|
|
public void OnMobDied(int mobId)
|
|
{
|
|
foreach (var sp in _spawnPoints)
|
|
{
|
|
int idx = sp.ActiveMobIds.IndexOf(mobId);
|
|
if (idx >= 0)
|
|
{
|
|
sp.ActiveMobIds.RemoveAt(idx);
|
|
sp.RespawnTimers.Add(sp.RespawnSeconds);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update respawn timers and return any newly spawned mobs.
|
|
/// </summary>
|
|
public List<MobInstance> Update(float deltaTime)
|
|
{
|
|
var newMobs = new List<MobInstance>();
|
|
|
|
foreach (var sp in _spawnPoints)
|
|
{
|
|
for (int i = sp.RespawnTimers.Count - 1; i >= 0; i--)
|
|
{
|
|
sp.RespawnTimers[i] -= deltaTime;
|
|
if (sp.RespawnTimers[i] <= 0)
|
|
{
|
|
sp.RespawnTimers.RemoveAt(i);
|
|
|
|
if (!_mobDefinitions.TryGetValue(sp.MobDefinitionId, out var mobDef))
|
|
continue;
|
|
|
|
var mob = CreateMob(mobDef, sp);
|
|
if (mob != null)
|
|
{
|
|
sp.ActiveMobIds.Add(mob.Id);
|
|
newMobs.Add(mob);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return newMobs;
|
|
}
|
|
|
|
private MobInstance? CreateMob(DungeonMobDefinition mobDef, SpawnPoint sp)
|
|
{
|
|
var stats = mobDef.GetStatsForBracket(1); // World mobs use bracket 1 (base stats)
|
|
int id = _nextMobId++;
|
|
|
|
// Randomize spawn position slightly around the spawn point
|
|
float offsetX = ((float)_rng.NextDouble() - 0.5f) * 64f;
|
|
float offsetY = ((float)_rng.NextDouble() - 0.5f) * 64f;
|
|
float spawnX = sp.WorldX + offsetX;
|
|
float spawnY = sp.WorldY + offsetY;
|
|
|
|
var mob = new MobInstance
|
|
{
|
|
Id = id,
|
|
DefinitionId = mobDef.Id,
|
|
Name = mobDef.Name,
|
|
X = spawnX,
|
|
Y = spawnY,
|
|
SpawnX = sp.WorldX,
|
|
SpawnY = sp.WorldY,
|
|
Health = stats.MaxHealth,
|
|
MaxHealth = stats.MaxHealth,
|
|
Mana = mobDef.BaseMana,
|
|
MaxMana = mobDef.BaseMana,
|
|
Stats = stats,
|
|
State = MobState.Idle,
|
|
StateTime = (float)(_rng.NextDouble() * 3f),
|
|
SpriteConfigJson = mobDef.SpriteConfigJson,
|
|
TintColor = mobDef.TintColor,
|
|
Scale = mobDef.Scale,
|
|
Type = mobDef.Type,
|
|
Behavior = mobDef.Behavior,
|
|
PreferredRange = mobDef.PreferredRange,
|
|
FleeThreshold = mobDef.FleeThreshold,
|
|
Phases = mobDef.Phases,
|
|
CurrentAnimation = "idle_down",
|
|
FacingDirection = "down"
|
|
};
|
|
|
|
// Initialize abilities
|
|
InitializeMobAbilities(mob, mobDef, stats);
|
|
|
|
return mob;
|
|
}
|
|
|
|
private void InitializeMobAbilities(MobInstance mob, DungeonMobDefinition mobDef, DungeonMobCombatStats stats)
|
|
{
|
|
mob.Abilities.Clear();
|
|
|
|
// Load abilities from assignments (same pattern as DungeonSimulation)
|
|
if (mobDef.AbilityAssignments != null)
|
|
{
|
|
foreach (var assignment in mobDef.AbilityAssignments.Where(a => a.IsEnabled))
|
|
{
|
|
var abilityDef = assignment.AbilityDefinition;
|
|
if (abilityDef == null) continue;
|
|
|
|
mob.Abilities.Add(new MobAbilityState
|
|
{
|
|
AbilityId = abilityDef.Id,
|
|
Definition = abilityDef,
|
|
ResolvedAnimationKey = abilityDef.AnimationKey,
|
|
CooldownRemaining = 0f,
|
|
Priority = assignment.PriorityOverride ?? abilityDef.Priority,
|
|
Phase = assignment.Phase,
|
|
MinHpPercent = assignment.MinHpPercent,
|
|
MaxHpPercent = assignment.MaxHpPercent
|
|
});
|
|
}
|
|
}
|
|
|
|
// Ensure at least a basic melee attack
|
|
bool hasBasicMelee = mob.Abilities.Any(a => a.Definition.AbilityType == MobAbilityType.MeleeAttack);
|
|
if (!hasBasicMelee)
|
|
{
|
|
mob.Abilities.Add(new MobAbilityState
|
|
{
|
|
Definition = new MobAbilityDefinition
|
|
{
|
|
Name = "Melee Attack",
|
|
AbilityType = MobAbilityType.MeleeAttack,
|
|
BaseDamage = stats.PhysicalDamage > 0 ? stats.PhysicalDamage : 10,
|
|
Range = stats.AttackRange > 0 ? stats.AttackRange : 40f,
|
|
Cooldown = stats.AttackSpeed > 0 ? 1f / stats.AttackSpeed : 1.5f,
|
|
ManaCost = 0,
|
|
AnimationKey = "attack"
|
|
},
|
|
CooldownRemaining = 0f
|
|
});
|
|
}
|
|
}
|
|
|
|
public int SpawnPointCount => _spawnPoints.Count;
|
|
public int TotalActiveMobs => _spawnPoints.Sum(sp => sp.ActiveMobIds.Count);
|
|
|
|
private class SpawnPoint
|
|
{
|
|
public long SpawnDataId { get; set; }
|
|
public int MobDefinitionId { get; set; }
|
|
public float WorldX { get; set; }
|
|
public float WorldY { get; set; }
|
|
public int SpawnCount { get; set; }
|
|
public float RespawnSeconds { get; set; }
|
|
public List<int> ActiveMobIds { get; set; } = new();
|
|
public List<float> RespawnTimers { get; set; } = new();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serializable mob spawn data extracted from CachedChunk's MobDtos.
|
|
/// </summary>
|
|
public sealed class WorldChunkMobSpawnData
|
|
{
|
|
public long Id { get; set; }
|
|
public int MobDefinitionId { get; set; }
|
|
public int TileX { get; set; }
|
|
public int TileY { get; set; }
|
|
public int SpawnCount { get; set; } = 1;
|
|
public float RespawnSeconds { get; set; } = 30f;
|
|
}
|