MobAIv2 P2
This commit is contained in:
@@ -27,24 +27,33 @@ public class DungeonSimulation
|
||||
public ConcurrentDictionary<int, DroppedItem> DroppedItems { get; } = new();
|
||||
public ConcurrentDictionary<int, PortalInstance> Portals { get; } = new();
|
||||
public ConcurrentDictionary<int, LootChestInstance> LootChests { get; } = new();
|
||||
public ConcurrentDictionary<int, ProjectileInstance> Projectiles { get; } = new();
|
||||
public ConcurrentDictionary<int, HazardZone> HazardZones { get; } = new();
|
||||
|
||||
// ID generator for loot chests
|
||||
// ID generators
|
||||
private int _nextLootChestId = 1;
|
||||
private int _nextProjectileId = 1;
|
||||
private int _nextHazardId = 1;
|
||||
public int GetNextLootChestId() => _nextLootChestId++;
|
||||
public int GetNextProjectileId() => _nextProjectileId++;
|
||||
public int GetNextHazardId() => _nextHazardId++;
|
||||
|
||||
// Combat tracking
|
||||
private readonly List<DamageEvent> _pendingDamageEvents = new();
|
||||
private readonly List<DeathEvent> _pendingDeathEvents = new();
|
||||
private readonly List<AbilityEvent> _pendingAbilityEvents = new();
|
||||
|
||||
private int _nextDroppedItemId = 1;
|
||||
private readonly Dictionary<int, DungeonMobDefinition> _mobDefinitions;
|
||||
private readonly Dictionary<int, MobAbilityDefinition>? _abilityDefinitions;
|
||||
|
||||
public DungeonSimulation(DungeonLayout layout, int tier, Dictionary<int, DungeonMobDefinition> mobDefinitions, int bracketMin = 1)
|
||||
public DungeonSimulation(DungeonLayout layout, int tier, Dictionary<int, DungeonMobDefinition> mobDefinitions, int bracketMin = 1, Dictionary<int, MobAbilityDefinition>? abilityDefinitions = null)
|
||||
{
|
||||
Layout = layout;
|
||||
Tier = tier;
|
||||
BracketMin = bracketMin;
|
||||
_mobDefinitions = mobDefinitions;
|
||||
_abilityDefinitions = abilityDefinitions;
|
||||
|
||||
SpawnMobs();
|
||||
InitializeChests();
|
||||
@@ -110,7 +119,7 @@ public class DungeonSimulation
|
||||
// Use bracket-aware stat scaling
|
||||
var stats = mobDef.GetStatsForBracket(BracketMin);
|
||||
|
||||
Mobs[mobSpawn.Id] = new MobInstance
|
||||
var mob = new MobInstance
|
||||
{
|
||||
Id = mobSpawn.Id,
|
||||
DefinitionId = mobDef.Id,
|
||||
@@ -121,13 +130,67 @@ public class DungeonSimulation
|
||||
SpawnY = mobSpawn.Y * 32 + 16,
|
||||
Health = stats.MaxHealth,
|
||||
MaxHealth = stats.MaxHealth,
|
||||
Mana = mobDef.BaseMana,
|
||||
MaxMana = mobDef.BaseMana,
|
||||
Stats = stats,
|
||||
State = MobState.Idle,
|
||||
SpriteConfigJson = mobDef.SpriteConfigJson,
|
||||
TintColor = mobDef.TintColor,
|
||||
Scale = mobDef.Scale,
|
||||
Type = mobDef.Type
|
||||
Type = mobDef.Type,
|
||||
Behavior = mobDef.Behavior,
|
||||
PreferredRange = mobDef.PreferredRange,
|
||||
FleeThreshold = mobDef.FleeThreshold,
|
||||
Phases = mobDef.Phases,
|
||||
CurrentAnimation = "idle_down",
|
||||
FacingDirection = "down"
|
||||
};
|
||||
|
||||
// Load abilities for this mob
|
||||
if (_abilityDefinitions != null && mobDef.AbilityAssignments != null)
|
||||
{
|
||||
foreach (var assignment in mobDef.AbilityAssignments.Where(a => a.IsEnabled))
|
||||
{
|
||||
if (_abilityDefinitions.TryGetValue(assignment.AbilityDefinitionId, out var abilityDef))
|
||||
{
|
||||
mob.Abilities.Add(new MobAbilityState
|
||||
{
|
||||
AbilityId = abilityDef.Id,
|
||||
Definition = abilityDef,
|
||||
CooldownRemaining = 0,
|
||||
Priority = assignment.PriorityOverride ?? abilityDef.Priority,
|
||||
Phase = assignment.Phase,
|
||||
MinHpPercent = assignment.MinHpPercent,
|
||||
MaxHpPercent = assignment.MaxHpPercent
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If no abilities loaded, give a basic attack
|
||||
if (mob.Abilities.Count == 0)
|
||||
{
|
||||
mob.Abilities.Add(new MobAbilityState
|
||||
{
|
||||
AbilityId = 0,
|
||||
Definition = new MobAbilityDefinition
|
||||
{
|
||||
Id = 0,
|
||||
Name = "Basic Attack",
|
||||
Key = "basic_attack",
|
||||
AbilityType = MobAbilityType.MeleeAttack,
|
||||
BaseDamage = stats.PhysicalDamage,
|
||||
Range = stats.AttackRange,
|
||||
Cooldown = 1f / stats.AttackSpeed,
|
||||
AnimationKey = "attack",
|
||||
DamageTypeKey = stats.DamageTypeKey
|
||||
},
|
||||
CooldownRemaining = 0,
|
||||
Priority = 1
|
||||
});
|
||||
}
|
||||
|
||||
Mobs[mobSpawn.Id] = mob;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,14 +244,35 @@ public class DungeonSimulation
|
||||
|
||||
_pendingDamageEvents.Clear();
|
||||
_pendingDeathEvents.Clear();
|
||||
_pendingAbilityEvents.Clear();
|
||||
|
||||
// Update all mobs (AI)
|
||||
foreach (var mob in Mobs.Values.Where(m => m.IsAlive))
|
||||
{
|
||||
UpdateMob(mob);
|
||||
MobAI.UpdateMob(mob, this, TickDelta);
|
||||
|
||||
// Process mob attacks if in attacking state
|
||||
if (mob.State == MobState.Attacking && mob.TargetId.HasValue)
|
||||
{
|
||||
float attackInterval = 1f / mob.Stats.AttackSpeed;
|
||||
if (CurrentTick - mob.LastAttackTick >= (long)(attackInterval * TicksPerSecond))
|
||||
{
|
||||
if (Players.TryGetValue(mob.TargetId.Value, out var target) && target.IsAlive)
|
||||
{
|
||||
ProcessMobAbility(mob, target);
|
||||
}
|
||||
mob.LastAttackTick = CurrentTick;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update players (movement, regeneration)
|
||||
// Update projectiles
|
||||
UpdateProjectiles();
|
||||
|
||||
// Update hazard zones
|
||||
UpdateHazardZones();
|
||||
|
||||
// Update players (movement, regeneration, status effects)
|
||||
foreach (var player in Players.Values.Where(p => p.IsAlive))
|
||||
{
|
||||
UpdatePlayer(player);
|
||||
@@ -205,96 +289,415 @@ public class DungeonSimulation
|
||||
{
|
||||
DamageEvents = new List<DamageEvent>(_pendingDamageEvents),
|
||||
DeathEvents = new List<DeathEvent>(_pendingDeathEvents),
|
||||
AbilityEvents = new List<AbilityEvent>(_pendingAbilityEvents),
|
||||
Phase = Phase
|
||||
};
|
||||
}
|
||||
|
||||
private void UpdateMob(MobInstance mob)
|
||||
/// <summary>
|
||||
/// Process a mob's ability/attack against a target
|
||||
/// </summary>
|
||||
private void ProcessMobAbility(MobInstance mob, PlayerInstance target)
|
||||
{
|
||||
// Find closest player
|
||||
PlayerInstance? target = null;
|
||||
float closestDist = float.MaxValue;
|
||||
// Find the best available ability
|
||||
var ability = mob.Abilities
|
||||
.Where(a => a.IsAvailable(mob.HpPercent))
|
||||
.Where(a => a.Phase == 0 || a.Phase == mob.CurrentPhase)
|
||||
.OrderByDescending(a => a.Priority)
|
||||
.FirstOrDefault();
|
||||
|
||||
foreach (var player in Players.Values.Where(p => p.IsAlive))
|
||||
{
|
||||
float dist = Distance(mob.X, mob.Y, player.X, player.Y);
|
||||
if (dist < closestDist)
|
||||
{
|
||||
closestDist = dist;
|
||||
target = player;
|
||||
}
|
||||
}
|
||||
if (ability == null) return;
|
||||
|
||||
if (target == null)
|
||||
{
|
||||
mob.State = MobState.Returning;
|
||||
mob.TargetId = null;
|
||||
MoveToward(mob, mob.SpawnX, mob.SpawnY, mob.Stats.MoveSpeed * 0.5f);
|
||||
return;
|
||||
}
|
||||
float distance = Distance(mob.X, mob.Y, target.X, target.Y);
|
||||
if (ability.Definition.Range > 0 && distance > ability.Definition.Range) return;
|
||||
|
||||
if (closestDist <= mob.Stats.AggroRange)
|
||||
// Put ability on cooldown
|
||||
ability.CooldownRemaining = ability.Definition.Cooldown;
|
||||
|
||||
// Emit ability event for client animation
|
||||
_pendingAbilityEvents.Add(new AbilityEvent
|
||||
{
|
||||
mob.TargetId = target.CharacterId;
|
||||
|
||||
float distFromSpawn = Distance(mob.X, mob.Y, mob.SpawnX, mob.SpawnY);
|
||||
if (distFromSpawn > mob.Stats.LeashRange)
|
||||
{
|
||||
mob.State = MobState.Returning;
|
||||
mob.TargetId = null;
|
||||
MoveToward(mob, mob.SpawnX, mob.SpawnY, mob.Stats.MoveSpeed);
|
||||
mob.Health = Math.Min(mob.MaxHealth, mob.Health + (int)(mob.MaxHealth * 0.02f));
|
||||
return;
|
||||
}
|
||||
|
||||
if (closestDist <= mob.Stats.AttackRange)
|
||||
{
|
||||
mob.State = MobState.Attacking;
|
||||
SourceType = EntityType.Mob,
|
||||
SourceId = mob.Id,
|
||||
AbilityKey = ability.Definition.Key,
|
||||
AnimationKey = ability.Definition.AnimationKey,
|
||||
TargetX = target.X,
|
||||
TargetY = target.Y,
|
||||
TargetId = (int)target.CharacterId
|
||||
});
|
||||
|
||||
// Process ability based on type
|
||||
switch (ability.Definition.AbilityType)
|
||||
{
|
||||
case MobAbilityType.MeleeAttack:
|
||||
case MobAbilityType.Cleave:
|
||||
ProcessMeleeAbility(mob, ability.Definition, target);
|
||||
break;
|
||||
|
||||
float attackInterval = 1f / mob.Stats.AttackSpeed;
|
||||
if (CurrentTick - mob.LastAttackTick >= (long)(attackInterval * TicksPerSecond))
|
||||
{
|
||||
MobAttackPlayer(mob, target);
|
||||
mob.LastAttackTick = CurrentTick;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mob.State = MobState.Chasing;
|
||||
MoveToward(mob, target.X, target.Y, mob.Stats.MoveSpeed);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mob.State = MobState.Idle;
|
||||
mob.TargetId = null;
|
||||
case MobAbilityType.ProjectileSingle:
|
||||
case MobAbilityType.ProjectileAoe:
|
||||
SpawnProjectile(mob, ability.Definition, target);
|
||||
break;
|
||||
|
||||
case MobAbilityType.GroundAoe:
|
||||
case MobAbilityType.ConeAttack:
|
||||
ProcessAoeAbility(mob, ability.Definition, target.X, target.Y);
|
||||
break;
|
||||
|
||||
case MobAbilityType.Poison:
|
||||
case MobAbilityType.Bleed:
|
||||
case MobAbilityType.Burn:
|
||||
ApplyDotToPlayer(mob, ability.Definition, target);
|
||||
break;
|
||||
|
||||
case MobAbilityType.Stun:
|
||||
case MobAbilityType.Slow:
|
||||
case MobAbilityType.Root:
|
||||
case MobAbilityType.Fear:
|
||||
ApplyStatusToPlayer(ability.Definition, target);
|
||||
break;
|
||||
|
||||
case MobAbilityType.Knockback:
|
||||
ApplyKnockback(mob, ability.Definition, target);
|
||||
break;
|
||||
|
||||
case MobAbilityType.Pull:
|
||||
ApplyPull(mob, ability.Definition, target);
|
||||
break;
|
||||
|
||||
case MobAbilityType.HazardZone:
|
||||
SpawnHazardZone(mob, ability.Definition, target.X, target.Y);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void MobAttackPlayer(MobInstance mob, PlayerInstance player)
|
||||
private void ProcessMeleeAbility(MobInstance mob, MobAbilityDefinition ability, PlayerInstance target)
|
||||
{
|
||||
// Use mob's configured damage type key
|
||||
string damageTypeKey = mob.Stats.DamageTypeKey ?? "physical";
|
||||
int baseDamage = mob.Stats.PhysicalDamage + mob.Stats.MagicDamage;
|
||||
int damage = ability.BaseDamage;
|
||||
string damageTypeKey = ability.DamageTypeKey ?? "physical";
|
||||
|
||||
// Calculate damage with resistances
|
||||
var result = CombatCalculator.CalculateMobToPlayerDamage(
|
||||
mob, player, damageTypeKey, baseDamage, player.Resistances);
|
||||
mob, target, damageTypeKey, damage, target.Resistances);
|
||||
|
||||
player.Health -= result.FinalDamage;
|
||||
ApplyDamageToPlayer(mob, target, result.FinalDamage, damageTypeKey, result.WasResisted, result.WasWeakness);
|
||||
|
||||
// Cleave hits other nearby players
|
||||
if (ability.AbilityType == MobAbilityType.Cleave && ability.AoeRadius > 0)
|
||||
{
|
||||
foreach (var other in Players.Values.Where(p => p.IsAlive && p.CharacterId != target.CharacterId))
|
||||
{
|
||||
if (Distance(mob.X, mob.Y, other.X, other.Y) <= ability.AoeRadius)
|
||||
{
|
||||
var otherResult = CombatCalculator.CalculateMobToPlayerDamage(
|
||||
mob, other, damageTypeKey, damage, other.Resistances);
|
||||
ApplyDamageToPlayer(mob, other, otherResult.FinalDamage, damageTypeKey, otherResult.WasResisted, otherResult.WasWeakness);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SpawnProjectile(MobInstance mob, MobAbilityDefinition ability, PlayerInstance target)
|
||||
{
|
||||
var projectile = new ProjectileInstance
|
||||
{
|
||||
Id = GetNextProjectileId(),
|
||||
SourceMobId = mob.Id,
|
||||
AbilityId = ability.Id,
|
||||
X = mob.X,
|
||||
Y = mob.Y,
|
||||
TargetX = target.X,
|
||||
TargetY = target.Y,
|
||||
Speed = ability.ProjectileSpeed,
|
||||
Damage = ability.BaseDamage,
|
||||
DamageTypeKey = ability.DamageTypeKey,
|
||||
AoeRadius = ability.AoeRadius,
|
||||
SpriteKey = ability.ProjectileSprite ?? "projectile_default"
|
||||
};
|
||||
|
||||
// Calculate direction
|
||||
float dx = target.X - mob.X;
|
||||
float dy = target.Y - mob.Y;
|
||||
float dist = MathF.Sqrt(dx * dx + dy * dy);
|
||||
if (dist > 0)
|
||||
{
|
||||
projectile.VelocityX = (dx / dist) * ability.ProjectileSpeed;
|
||||
projectile.VelocityY = (dy / dist) * ability.ProjectileSpeed;
|
||||
}
|
||||
|
||||
Projectiles[projectile.Id] = projectile;
|
||||
}
|
||||
|
||||
private void UpdateProjectiles()
|
||||
{
|
||||
var toRemove = new List<int>();
|
||||
|
||||
foreach (var projectile in Projectiles.Values)
|
||||
{
|
||||
// Move projectile
|
||||
projectile.X += projectile.VelocityX * TickDelta;
|
||||
projectile.Y += projectile.VelocityY * TickDelta;
|
||||
projectile.DistanceTraveled += MathF.Sqrt(
|
||||
projectile.VelocityX * projectile.VelocityX +
|
||||
projectile.VelocityY * projectile.VelocityY) * TickDelta;
|
||||
|
||||
// Check max range
|
||||
if (projectile.DistanceTraveled > 500)
|
||||
{
|
||||
toRemove.Add(projectile.Id);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check collision with players
|
||||
foreach (var player in Players.Values.Where(p => p.IsAlive))
|
||||
{
|
||||
if (Distance(projectile.X, projectile.Y, player.X, player.Y) < 20)
|
||||
{
|
||||
// Hit!
|
||||
if (projectile.AoeRadius > 0)
|
||||
{
|
||||
// AOE explosion
|
||||
ProcessAoeAbilityAtLocation(projectile, projectile.X, projectile.Y);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Single target
|
||||
if (Mobs.TryGetValue(projectile.SourceMobId, out var sourceMob))
|
||||
{
|
||||
var result = CombatCalculator.CalculateMobToPlayerDamage(
|
||||
sourceMob, player, projectile.DamageTypeKey, projectile.Damage, player.Resistances);
|
||||
ApplyDamageToPlayer(sourceMob, player, result.FinalDamage, projectile.DamageTypeKey, result.WasResisted, result.WasWeakness);
|
||||
}
|
||||
}
|
||||
toRemove.Add(projectile.Id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Check wall collision
|
||||
if (!CanMoveTo(projectile.X, projectile.Y))
|
||||
{
|
||||
if (projectile.AoeRadius > 0)
|
||||
{
|
||||
ProcessAoeAbilityAtLocation(projectile, projectile.X, projectile.Y);
|
||||
}
|
||||
toRemove.Add(projectile.Id);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var id in toRemove)
|
||||
{
|
||||
Projectiles.TryRemove(id, out _);
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessAoeAbility(MobInstance mob, MobAbilityDefinition ability, float targetX, float targetY)
|
||||
{
|
||||
foreach (var player in Players.Values.Where(p => p.IsAlive))
|
||||
{
|
||||
if (Distance(targetX, targetY, player.X, player.Y) <= ability.AoeRadius)
|
||||
{
|
||||
var result = CombatCalculator.CalculateMobToPlayerDamage(
|
||||
mob, player, ability.DamageTypeKey, ability.BaseDamage, player.Resistances);
|
||||
ApplyDamageToPlayer(mob, player, result.FinalDamage, ability.DamageTypeKey, result.WasResisted, result.WasWeakness);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessAoeAbilityAtLocation(ProjectileInstance projectile, float x, float y)
|
||||
{
|
||||
if (!Mobs.TryGetValue(projectile.SourceMobId, out var sourceMob)) return;
|
||||
|
||||
foreach (var player in Players.Values.Where(p => p.IsAlive))
|
||||
{
|
||||
if (Distance(x, y, player.X, player.Y) <= projectile.AoeRadius)
|
||||
{
|
||||
var result = CombatCalculator.CalculateMobToPlayerDamage(
|
||||
sourceMob, player, projectile.DamageTypeKey, projectile.Damage, player.Resistances);
|
||||
ApplyDamageToPlayer(sourceMob, player, result.FinalDamage, projectile.DamageTypeKey, result.WasResisted, result.WasWeakness);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyDotToPlayer(MobInstance mob, MobAbilityDefinition ability, PlayerInstance target)
|
||||
{
|
||||
// First apply initial hit if any
|
||||
if (ability.BaseDamage > 0)
|
||||
{
|
||||
var result = CombatCalculator.CalculateMobToPlayerDamage(
|
||||
mob, target, ability.DamageTypeKey, ability.BaseDamage, target.Resistances);
|
||||
ApplyDamageToPlayer(mob, target, result.FinalDamage, ability.DamageTypeKey, result.WasResisted, result.WasWeakness);
|
||||
}
|
||||
|
||||
// Apply DOT status effect
|
||||
string effectType = ability.AbilityType switch
|
||||
{
|
||||
MobAbilityType.Poison => "poison",
|
||||
MobAbilityType.Bleed => "bleed",
|
||||
MobAbilityType.Burn => "burn",
|
||||
_ => "dot"
|
||||
};
|
||||
|
||||
target.StatusEffects.Add(new PlayerStatusEffect
|
||||
{
|
||||
EffectType = effectType,
|
||||
Duration = ability.EffectDuration,
|
||||
TickInterval = ability.DotTickInterval,
|
||||
NextTickIn = ability.DotTickInterval,
|
||||
DamagePerTick = ability.DotDamagePerTick,
|
||||
SourceMobId = mob.Id
|
||||
});
|
||||
}
|
||||
|
||||
private void ApplyStatusToPlayer(MobAbilityDefinition ability, PlayerInstance target)
|
||||
{
|
||||
string effectType = ability.AbilityType switch
|
||||
{
|
||||
MobAbilityType.Stun => "stun",
|
||||
MobAbilityType.Slow => "slow",
|
||||
MobAbilityType.Root => "root",
|
||||
MobAbilityType.Fear => "fear",
|
||||
MobAbilityType.Silence => "silence",
|
||||
_ => "debuff"
|
||||
};
|
||||
|
||||
target.StatusEffects.Add(new PlayerStatusEffect
|
||||
{
|
||||
EffectType = effectType,
|
||||
Duration = ability.EffectDuration,
|
||||
Modifier = ability.SpeedModifier
|
||||
});
|
||||
}
|
||||
|
||||
private void ApplyKnockback(MobInstance mob, MobAbilityDefinition ability, PlayerInstance target)
|
||||
{
|
||||
// Apply damage first
|
||||
if (ability.BaseDamage > 0)
|
||||
{
|
||||
var result = CombatCalculator.CalculateMobToPlayerDamage(
|
||||
mob, target, ability.DamageTypeKey, ability.BaseDamage, target.Resistances);
|
||||
ApplyDamageToPlayer(mob, target, result.FinalDamage, ability.DamageTypeKey, result.WasResisted, result.WasWeakness);
|
||||
}
|
||||
|
||||
// Calculate knockback direction (away from mob)
|
||||
float dx = target.X - mob.X;
|
||||
float dy = target.Y - mob.Y;
|
||||
float dist = MathF.Sqrt(dx * dx + dy * dy);
|
||||
|
||||
if (dist > 0)
|
||||
{
|
||||
float newX = target.X + (dx / dist) * ability.DisplacementDistance;
|
||||
float newY = target.Y + (dy / dist) * ability.DisplacementDistance;
|
||||
|
||||
if (CanMoveTo(newX, target.Y)) target.X = newX;
|
||||
if (CanMoveTo(target.X, newY)) target.Y = newY;
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyPull(MobInstance mob, MobAbilityDefinition ability, PlayerInstance target)
|
||||
{
|
||||
// Calculate pull direction (toward mob)
|
||||
float dx = mob.X - target.X;
|
||||
float dy = mob.Y - target.Y;
|
||||
float dist = MathF.Sqrt(dx * dx + dy * dy);
|
||||
|
||||
if (dist > 0)
|
||||
{
|
||||
float pullDist = Math.Min(ability.DisplacementDistance, dist - 30); // Don't pull on top of mob
|
||||
if (pullDist > 0)
|
||||
{
|
||||
float newX = target.X + (dx / dist) * pullDist;
|
||||
float newY = target.Y + (dy / dist) * pullDist;
|
||||
|
||||
if (CanMoveTo(newX, target.Y)) target.X = newX;
|
||||
if (CanMoveTo(target.X, newY)) target.Y = newY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SpawnHazardZone(MobInstance mob, MobAbilityDefinition ability, float x, float y)
|
||||
{
|
||||
var hazard = new HazardZone
|
||||
{
|
||||
Id = GetNextHazardId(),
|
||||
SourceMobId = mob.Id,
|
||||
X = x,
|
||||
Y = y,
|
||||
Radius = ability.AoeRadius,
|
||||
DamagePerTick = ability.DotDamagePerTick,
|
||||
TickInterval = ability.DotTickInterval,
|
||||
Duration = ability.EffectDuration,
|
||||
NextTickIn = ability.DotTickInterval,
|
||||
DamageTypeKey = ability.DamageTypeKey,
|
||||
VisualKey = ability.VisualEffectKey ?? "hazard_default"
|
||||
};
|
||||
|
||||
HazardZones[hazard.Id] = hazard;
|
||||
}
|
||||
|
||||
private void UpdateHazardZones()
|
||||
{
|
||||
var toRemove = new List<int>();
|
||||
|
||||
foreach (var hazard in HazardZones.Values)
|
||||
{
|
||||
hazard.Duration -= TickDelta;
|
||||
hazard.NextTickIn -= TickDelta;
|
||||
|
||||
if (hazard.Duration <= 0)
|
||||
{
|
||||
toRemove.Add(hazard.Id);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hazard.NextTickIn <= 0)
|
||||
{
|
||||
hazard.NextTickIn = hazard.TickInterval;
|
||||
|
||||
// Damage players in zone
|
||||
MobInstance? sourceMob = null;
|
||||
Mobs.TryGetValue(hazard.SourceMobId, out sourceMob);
|
||||
|
||||
foreach (var player in Players.Values.Where(p => p.IsAlive))
|
||||
{
|
||||
if (Distance(hazard.X, hazard.Y, player.X, player.Y) <= hazard.Radius)
|
||||
{
|
||||
if (sourceMob != null)
|
||||
{
|
||||
var result = CombatCalculator.CalculateMobToPlayerDamage(
|
||||
sourceMob, player, hazard.DamageTypeKey, hazard.DamagePerTick, player.Resistances);
|
||||
ApplyDamageToPlayer(sourceMob, player, result.FinalDamage, hazard.DamageTypeKey, result.WasResisted, result.WasWeakness);
|
||||
}
|
||||
else
|
||||
{
|
||||
ApplyDamageToPlayer(null, player, hazard.DamagePerTick, hazard.DamageTypeKey, false, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var id in toRemove)
|
||||
{
|
||||
HazardZones.TryRemove(id, out _);
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyDamageToPlayer(MobInstance? mob, PlayerInstance player, int damage, string damageTypeKey, bool wasResisted, bool wasWeakness)
|
||||
{
|
||||
player.Health -= damage;
|
||||
player.IsInCombat = true;
|
||||
player.LastCombatTick = CurrentTick;
|
||||
|
||||
_pendingDamageEvents.Add(new DamageEvent
|
||||
{
|
||||
SourceType = EntityType.Mob,
|
||||
SourceId = mob.Id,
|
||||
SourceId = mob?.Id ?? 0,
|
||||
TargetType = EntityType.Player,
|
||||
TargetId = (int)player.CharacterId,
|
||||
Damage = result.FinalDamage,
|
||||
Damage = damage,
|
||||
DamageTypeKey = damageTypeKey,
|
||||
WasResisted = result.WasResisted,
|
||||
WasWeakness = result.WasWeakness
|
||||
WasResisted = wasResisted,
|
||||
WasWeakness = wasWeakness
|
||||
});
|
||||
|
||||
if (player.Health <= 0)
|
||||
@@ -308,32 +711,66 @@ public class DungeonSimulation
|
||||
EntityId = (int)player.CharacterId,
|
||||
EntityName = player.CharacterName,
|
||||
KillerType = EntityType.Mob,
|
||||
KillerId = mob.Id,
|
||||
KillerName = mob.Name
|
||||
KillerId = mob?.Id ?? 0,
|
||||
KillerName = mob?.Name ?? "Hazard"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdatePlayer(PlayerInstance player)
|
||||
{
|
||||
// Process movement input
|
||||
if (player.InputX != 0 || player.InputY != 0)
|
||||
// Update status effects first
|
||||
UpdatePlayerStatusEffects(player);
|
||||
|
||||
// If stunned, can't do anything
|
||||
if (player.IsStunned) return;
|
||||
|
||||
// Process movement input (unless rooted or feared)
|
||||
if (!player.IsRooted && !player.IsFeared)
|
||||
{
|
||||
float speed = player.MoveSpeed * TickDelta;
|
||||
float newX = player.X + player.InputX * speed;
|
||||
float newY = player.Y + player.InputY * speed;
|
||||
|
||||
if (CanMoveTo(newX, player.Y)) player.X = newX;
|
||||
if (CanMoveTo(player.X, newY)) player.Y = newY;
|
||||
|
||||
if (player.InputX != 0 || player.InputY != 0)
|
||||
{
|
||||
player.Rotation = MathF.Atan2(player.InputY, player.InputX);
|
||||
float speedMod = player.GetSpeedModifier();
|
||||
float speed = player.MoveSpeed * speedMod * TickDelta;
|
||||
float newX = player.X + player.InputX * speed;
|
||||
float newY = player.Y + player.InputY * speed;
|
||||
|
||||
if (CanMoveTo(newX, player.Y)) player.X = newX;
|
||||
if (CanMoveTo(player.X, newY)) player.Y = newY;
|
||||
|
||||
if (player.InputX != 0 || player.InputY != 0)
|
||||
{
|
||||
player.Rotation = MathF.Atan2(player.InputY, player.InputX);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process attack
|
||||
if (player.WantsToAttack && player.AttackTargetId.HasValue)
|
||||
// If feared, run away from nearest mob
|
||||
if (player.IsFeared)
|
||||
{
|
||||
var nearestMob = Mobs.Values
|
||||
.Where(m => m.IsAlive)
|
||||
.OrderBy(m => Distance(player.X, player.Y, m.X, m.Y))
|
||||
.FirstOrDefault();
|
||||
|
||||
if (nearestMob != null)
|
||||
{
|
||||
float dx = player.X - nearestMob.X;
|
||||
float dy = player.Y - nearestMob.Y;
|
||||
float dist = MathF.Sqrt(dx * dx + dy * dy);
|
||||
if (dist > 0)
|
||||
{
|
||||
float speed = player.MoveSpeed * 1.2f * TickDelta; // Run faster when feared
|
||||
float newX = player.X + (dx / dist) * speed;
|
||||
float newY = player.Y + (dy / dist) * speed;
|
||||
if (CanMoveTo(newX, player.Y)) player.X = newX;
|
||||
if (CanMoveTo(player.X, newY)) player.Y = newY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process attack (unless stunned or silenced for abilities)
|
||||
if (!player.IsStunned && player.WantsToAttack && player.AttackTargetId.HasValue)
|
||||
{
|
||||
TryPlayerAttack(player, player.AttackTargetId.Value);
|
||||
player.WantsToAttack = false;
|
||||
@@ -349,6 +786,65 @@ public class DungeonSimulation
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdatePlayerStatusEffects(PlayerInstance player)
|
||||
{
|
||||
var expired = new List<PlayerStatusEffect>();
|
||||
|
||||
foreach (var effect in player.StatusEffects)
|
||||
{
|
||||
effect.Duration -= TickDelta;
|
||||
|
||||
// Handle DOT
|
||||
if (effect.DamagePerTick > 0)
|
||||
{
|
||||
effect.NextTickIn -= TickDelta;
|
||||
if (effect.NextTickIn <= 0)
|
||||
{
|
||||
player.Health -= effect.DamagePerTick;
|
||||
player.IsInCombat = true;
|
||||
player.LastCombatTick = CurrentTick;
|
||||
effect.NextTickIn = effect.TickInterval;
|
||||
|
||||
_pendingDamageEvents.Add(new DamageEvent
|
||||
{
|
||||
SourceType = EntityType.Mob,
|
||||
SourceId = effect.SourceMobId,
|
||||
TargetType = EntityType.Player,
|
||||
TargetId = (int)player.CharacterId,
|
||||
Damage = effect.DamagePerTick,
|
||||
DamageTypeKey = effect.EffectType
|
||||
});
|
||||
|
||||
if (player.Health <= 0)
|
||||
{
|
||||
player.Health = 0;
|
||||
player.IsAlive = false;
|
||||
|
||||
string killerName = "DOT";
|
||||
if (Mobs.TryGetValue(effect.SourceMobId, out var mob))
|
||||
killerName = mob.Name;
|
||||
|
||||
_pendingDeathEvents.Add(new DeathEvent
|
||||
{
|
||||
EntityType = EntityType.Player,
|
||||
EntityId = (int)player.CharacterId,
|
||||
EntityName = player.CharacterName,
|
||||
KillerType = EntityType.Mob,
|
||||
KillerId = effect.SourceMobId,
|
||||
KillerName = killerName
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (effect.Duration <= 0)
|
||||
expired.Add(effect);
|
||||
}
|
||||
|
||||
foreach (var e in expired)
|
||||
player.StatusEffects.Remove(e);
|
||||
}
|
||||
|
||||
private void TryPlayerAttack(PlayerInstance player, ulong targetId)
|
||||
{
|
||||
if (!Mobs.TryGetValue((int)targetId, out var mob) || !mob.IsAlive)
|
||||
|
||||
@@ -39,6 +39,11 @@ public class PlayerInstance
|
||||
/// </summary>
|
||||
public Dictionary<string, float> Resistances { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Active status effects on this player
|
||||
/// </summary>
|
||||
public List<PlayerStatusEffect> StatusEffects { get; set; } = new();
|
||||
|
||||
public bool IsAlive { get; set; } = true;
|
||||
public bool IsInCombat { get; set; }
|
||||
|
||||
@@ -56,4 +61,41 @@ public class PlayerInstance
|
||||
public float InputY { get; set; }
|
||||
public bool WantsToAttack { get; set; }
|
||||
public ulong? AttackTargetId { get; set; }
|
||||
|
||||
/// <summary>Check if player is stunned</summary>
|
||||
public bool IsStunned => StatusEffects.Any(e => e.EffectType == "stun" && e.Duration > 0);
|
||||
|
||||
/// <summary>Check if player is rooted</summary>
|
||||
public bool IsRooted => StatusEffects.Any(e => e.EffectType == "root" && e.Duration > 0);
|
||||
|
||||
/// <summary>Check if player is feared</summary>
|
||||
public bool IsFeared => StatusEffects.Any(e => e.EffectType == "fear" && e.Duration > 0);
|
||||
|
||||
/// <summary>Check if player is silenced</summary>
|
||||
public bool IsSilenced => StatusEffects.Any(e => e.EffectType == "silence" && e.Duration > 0);
|
||||
|
||||
/// <summary>Get current speed modifier from status effects</summary>
|
||||
public float GetSpeedModifier()
|
||||
{
|
||||
float modifier = 1.0f;
|
||||
foreach (var effect in StatusEffects.Where(e => e.EffectType == "slow" && e.Duration > 0))
|
||||
{
|
||||
modifier *= effect.Modifier;
|
||||
}
|
||||
return modifier;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A status effect applied to a player
|
||||
/// </summary>
|
||||
public class PlayerStatusEffect
|
||||
{
|
||||
public string EffectType { get; set; } = ""; // poison, bleed, burn, slow, stun, root, fear, silence
|
||||
public float Duration { get; set; }
|
||||
public float TickInterval { get; set; }
|
||||
public float NextTickIn { get; set; }
|
||||
public int DamagePerTick { get; set; }
|
||||
public float Modifier { get; set; } = 1.0f; // For slow, etc.
|
||||
public int SourceMobId { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
namespace Server.Game.Dungeon;
|
||||
|
||||
/// <summary>
|
||||
/// A projectile in flight
|
||||
/// </summary>
|
||||
public class ProjectileInstance
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int SourceMobId { get; set; }
|
||||
public int AbilityId { get; set; }
|
||||
|
||||
public float X { get; set; }
|
||||
public float Y { get; set; }
|
||||
public float VelocityX { get; set; }
|
||||
public float VelocityY { get; set; }
|
||||
public float Speed { get; set; }
|
||||
|
||||
public float TargetX { get; set; }
|
||||
public float TargetY { get; set; }
|
||||
|
||||
public int Damage { get; set; }
|
||||
public string DamageTypeKey { get; set; } = "physical";
|
||||
public float AoeRadius { get; set; }
|
||||
|
||||
public float DistanceTraveled { get; set; }
|
||||
public float MaxRange { get; set; } = 500f;
|
||||
|
||||
public string SpriteKey { get; set; } = "projectile_default";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A persistent damage zone on the ground
|
||||
/// </summary>
|
||||
public class HazardZone
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int SourceMobId { get; set; }
|
||||
|
||||
public float X { get; set; }
|
||||
public float Y { get; set; }
|
||||
public float Radius { get; set; }
|
||||
|
||||
public int DamagePerTick { get; set; }
|
||||
public float TickInterval { get; set; }
|
||||
public float NextTickIn { get; set; }
|
||||
public float Duration { get; set; }
|
||||
|
||||
public string DamageTypeKey { get; set; } = "fire";
|
||||
public string VisualKey { get; set; } = "hazard_default";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event when an ability is used (for client animations)
|
||||
/// </summary>
|
||||
public class AbilityEvent
|
||||
{
|
||||
public EntityType SourceType { get; set; }
|
||||
public int SourceId { get; set; }
|
||||
public string AbilityKey { get; set; } = "";
|
||||
public string AnimationKey { get; set; } = "attack";
|
||||
public float TargetX { get; set; }
|
||||
public float TargetY { get; set; }
|
||||
public int? TargetId { get; set; }
|
||||
}
|
||||
@@ -4,6 +4,7 @@ public class TickResult
|
||||
{
|
||||
public List<DamageEvent> DamageEvents { get; set; } = new();
|
||||
public List<DeathEvent> DeathEvents { get; set; } = new();
|
||||
public List<AbilityEvent> AbilityEvents { get; set; } = new();
|
||||
public DungeonPhase Phase { get; set; }
|
||||
}
|
||||
|
||||
|
||||
+1941
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,22 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SystemRevivalServer.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Mobv2p2 : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>@ViewData["Title"] - Void Scavengers Admin</title>
|
||||
<title>@ViewData["Title"] - Dungeon Divers Admin</title>
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
<script>
|
||||
tailwind.config = {
|
||||
|
||||
Reference in New Issue
Block a user