Add aura range evaluation — EnterAuraRange/LeaveAuraRange triggers
- Add EnterAuraRange and LeaveAuraRange to TriggerEventKind enum - Add TriggerMatches cases for OnEnterAuraRangeTrigger / OnLeaveAuraRangeTrigger with AuraOwnership + UnitOwnerMatches (ally/enemy/any) scope filtering - Add EvaluateAuraTransitions (on move), EvaluateAurasOnEnter (on channel), EvaluateAurasOnLeave (on sacrifice/kill) public methods to AbilitySystem - Bidirectional evaluation: moving unit vs. stationary auras + moving unit's own auras vs. stationary units - Hook into GameEngine: MoveUnit, ChannelCard (Spirit), Sacrifice - KillUnit now calls EvaluateAurasOnLeave before board removal Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -697,6 +697,8 @@ public static class AbilitySystem
|
||||
var owner = FindCardOwner(ctx.State, unit.InstanceId);
|
||||
if (owner is null) return;
|
||||
|
||||
// Fire leave-aura triggers before removing from board.
|
||||
EvaluateAurasOnLeave(ctx.State, unit.InstanceId);
|
||||
ctx.State.Board?.RemoveUnit(unit.InstanceId);
|
||||
RemoveFromAllZones(owner, unit, board: null); // board already handled above
|
||||
unit.CurrentHealth = 0;
|
||||
@@ -979,6 +981,14 @@ public static class AbilitySystem
|
||||
occurrence.Kind == TriggerEventKind.Channeled
|
||||
&& CardType(t.CardType, occurrence.CardType)
|
||||
&& Own(t.Ownership, occurrence, ownerId, state),
|
||||
OnEnterAuraRangeTrigger eat =>
|
||||
occurrence.Kind == TriggerEventKind.EnterAuraRange
|
||||
&& Own(eat.AuraOwnership, occurrence, ownerId, state)
|
||||
&& UnitOwnerMatches(eat.UnitOwnership, occurrence.Data?.TargetId, state, ownerId),
|
||||
OnLeaveAuraRangeTrigger lat =>
|
||||
occurrence.Kind == TriggerEventKind.LeaveAuraRange
|
||||
&& Own(lat.AuraOwnership, occurrence, ownerId, state)
|
||||
&& UnitOwnerMatches(lat.UnitOwnership, occurrence.Data?.TargetId, state, ownerId),
|
||||
_ => false,
|
||||
};
|
||||
|
||||
@@ -992,6 +1002,21 @@ public static class AbilitySystem
|
||||
_ => true,
|
||||
};
|
||||
|
||||
private static bool UnitOwnerMatches(
|
||||
string scope, string? unitInstanceId, GameState state, string referenceOwnerId)
|
||||
{
|
||||
if (scope.ToLowerInvariant() == "any" || unitInstanceId is null) return true;
|
||||
foreach (var (ownerId, player) in state.Players)
|
||||
if (player.Field.Any(c => c.InstanceId == unitInstanceId))
|
||||
return scope.ToLowerInvariant() switch
|
||||
{
|
||||
"ally" or "owner" => ownerId == referenceOwnerId,
|
||||
"enemy" => ownerId != referenceOwnerId,
|
||||
_ => true,
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool UnitType(string? required, string? actual)
|
||||
=> required is null or "Unit" or "Any" || required == actual;
|
||||
|
||||
@@ -1253,4 +1278,207 @@ public static class AbilitySystem
|
||||
Source = src,
|
||||
});
|
||||
}
|
||||
|
||||
// ── Aura range evaluation ─────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Fires OnEnterAuraRange / OnLeaveAuraRange triggers when a unit moves from
|
||||
/// <paramref name="from"/> to <paramref name="to"/>.
|
||||
/// Handles both the moving unit crossing into/out of stationary auras, and the
|
||||
/// moving unit's own auras sweeping over stationary units.
|
||||
/// Call after updating the unit's position on the board.
|
||||
/// </summary>
|
||||
public static GameState EvaluateAuraTransitions(
|
||||
GameState state,
|
||||
string movingUnitId,
|
||||
Coord from,
|
||||
Coord to)
|
||||
{
|
||||
// Part 1: stationary aura-bearing units vs. moving unit's position change.
|
||||
foreach (var (auraOwnerId, player) in state.Players)
|
||||
foreach (var auraUnit in player.Field.OfType<FieldCard>()
|
||||
.Where(f => f.Position.HasValue && f.InstanceId != movingUnitId).ToList())
|
||||
{
|
||||
foreach (var def in ParseAbilities(auraUnit.AbilitiesJson)
|
||||
.Where(d => d.Type == AbilityType.Aura && d.AuraRange is not null))
|
||||
{
|
||||
var auraPos = auraUnit.Position!.Value;
|
||||
int min = def.AuraRange!.Min, max = def.AuraRange.Max;
|
||||
FireAuraTransition(state, auraOwnerId, auraUnit.InstanceId, movingUnitId,
|
||||
wasIn: from.ManhattanDistance(auraPos) >= min && from.ManhattanDistance(auraPos) <= max,
|
||||
isIn: to.ManhattanDistance(auraPos) >= min && to.ManhattanDistance(auraPos) <= max);
|
||||
}
|
||||
}
|
||||
|
||||
// Part 2: moving unit's own auras vs. stationary units.
|
||||
var movingUnit = state.Players.Values
|
||||
.SelectMany(p => p.Field.OfType<FieldCard>())
|
||||
.FirstOrDefault(f => f.InstanceId == movingUnitId);
|
||||
if (movingUnit is null) return state;
|
||||
|
||||
string? movingOwnerId = null;
|
||||
foreach (var (oid, pl) in state.Players)
|
||||
if (pl.Field.Any(c => c.InstanceId == movingUnitId)) { movingOwnerId = oid; break; }
|
||||
if (movingOwnerId is null) return state;
|
||||
|
||||
foreach (var def in ParseAbilities(movingUnit.AbilitiesJson)
|
||||
.Where(d => d.Type == AbilityType.Aura && d.AuraRange is not null))
|
||||
{
|
||||
int min = def.AuraRange!.Min, max = def.AuraRange.Max;
|
||||
foreach (var (_, pl) in state.Players)
|
||||
foreach (var other in pl.Field.OfType<FieldCard>()
|
||||
.Where(f => f.Position.HasValue && f.InstanceId != movingUnitId).ToList())
|
||||
{
|
||||
var otherPos = other.Position!.Value;
|
||||
FireAuraTransition(state, movingOwnerId, movingUnitId, other.InstanceId,
|
||||
wasIn: from.ManhattanDistance(otherPos) >= min && from.ManhattanDistance(otherPos) <= max,
|
||||
isIn: to.ManhattanDistance(otherPos) >= min && to.ManhattanDistance(otherPos) <= max);
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fires OnEnterAuraRange triggers for a unit newly placed on the battlefield.
|
||||
/// Handles both existing auras covering the new unit and the new unit's own auras
|
||||
/// covering existing units.
|
||||
/// Call after placing the unit at its initial position.
|
||||
/// </summary>
|
||||
public static GameState EvaluateAurasOnEnter(GameState state, string newUnitId)
|
||||
{
|
||||
var newUnit = state.Players.Values
|
||||
.SelectMany(p => p.Field.OfType<FieldCard>())
|
||||
.FirstOrDefault(f => f.InstanceId == newUnitId);
|
||||
if (newUnit?.Position is null) return state;
|
||||
var newPos = newUnit.Position.Value;
|
||||
|
||||
// Part 1: existing aura units — check if new unit entered their range.
|
||||
foreach (var (auraOwnerId, player) in state.Players)
|
||||
foreach (var auraUnit in player.Field.OfType<FieldCard>()
|
||||
.Where(f => f.Position.HasValue && f.InstanceId != newUnitId).ToList())
|
||||
{
|
||||
foreach (var def in ParseAbilities(auraUnit.AbilitiesJson)
|
||||
.Where(d => d.Type == AbilityType.Aura && d.AuraRange is not null))
|
||||
{
|
||||
int dist = newPos.ManhattanDistance(auraUnit.Position!.Value);
|
||||
if (dist >= def.AuraRange!.Min && dist <= def.AuraRange.Max)
|
||||
FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.EnterAuraRange,
|
||||
ActorId = auraOwnerId,
|
||||
Data = new TriggerEventData { SourceId = auraUnit.InstanceId, TargetId = newUnitId },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Part 2: new unit's own auras — check all existing units.
|
||||
string? newUnitOwnerId = null;
|
||||
foreach (var (oid, pl) in state.Players)
|
||||
if (pl.Field.Any(c => c.InstanceId == newUnitId)) { newUnitOwnerId = oid; break; }
|
||||
if (newUnitOwnerId is null) return state;
|
||||
|
||||
foreach (var def in ParseAbilities(newUnit.AbilitiesJson)
|
||||
.Where(d => d.Type == AbilityType.Aura && d.AuraRange is not null))
|
||||
{
|
||||
foreach (var (_, pl) in state.Players)
|
||||
foreach (var other in pl.Field.OfType<FieldCard>()
|
||||
.Where(f => f.Position.HasValue && f.InstanceId != newUnitId).ToList())
|
||||
{
|
||||
int dist = newPos.ManhattanDistance(other.Position!.Value);
|
||||
if (dist >= def.AuraRange!.Min && dist <= def.AuraRange.Max)
|
||||
FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.EnterAuraRange,
|
||||
ActorId = newUnitOwnerId,
|
||||
Data = new TriggerEventData { SourceId = newUnitId, TargetId = other.InstanceId },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fires OnLeaveAuraRange triggers for a unit about to leave the battlefield.
|
||||
/// Handles both existing auras and the leaving unit's own auras.
|
||||
/// Call before removing the unit from the board.
|
||||
/// </summary>
|
||||
public static GameState EvaluateAurasOnLeave(GameState state, string leavingUnitId)
|
||||
{
|
||||
var leavingUnit = state.Players.Values
|
||||
.SelectMany(p => p.Field.OfType<FieldCard>())
|
||||
.FirstOrDefault(f => f.InstanceId == leavingUnitId);
|
||||
if (leavingUnit?.Position is null) return state;
|
||||
var leavingPos = leavingUnit.Position.Value;
|
||||
|
||||
// Part 1: existing aura units — check if leaving unit was in their range.
|
||||
foreach (var (auraOwnerId, player) in state.Players)
|
||||
foreach (var auraUnit in player.Field.OfType<FieldCard>()
|
||||
.Where(f => f.Position.HasValue && f.InstanceId != leavingUnitId).ToList())
|
||||
{
|
||||
foreach (var def in ParseAbilities(auraUnit.AbilitiesJson)
|
||||
.Where(d => d.Type == AbilityType.Aura && d.AuraRange is not null))
|
||||
{
|
||||
int dist = leavingPos.ManhattanDistance(auraUnit.Position!.Value);
|
||||
if (dist >= def.AuraRange!.Min && dist <= def.AuraRange.Max)
|
||||
FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.LeaveAuraRange,
|
||||
ActorId = auraOwnerId,
|
||||
Data = new TriggerEventData { SourceId = auraUnit.InstanceId, TargetId = leavingUnitId },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Part 2: leaving unit's own auras — fire LeaveAuraRange for each in-range unit.
|
||||
string? leavingOwnerId = null;
|
||||
foreach (var (oid, pl) in state.Players)
|
||||
if (pl.Field.Any(c => c.InstanceId == leavingUnitId)) { leavingOwnerId = oid; break; }
|
||||
if (leavingOwnerId is null) return state;
|
||||
|
||||
foreach (var def in ParseAbilities(leavingUnit.AbilitiesJson)
|
||||
.Where(d => d.Type == AbilityType.Aura && d.AuraRange is not null))
|
||||
{
|
||||
foreach (var (_, pl) in state.Players)
|
||||
foreach (var other in pl.Field.OfType<FieldCard>()
|
||||
.Where(f => f.Position.HasValue && f.InstanceId != leavingUnitId).ToList())
|
||||
{
|
||||
int dist = leavingPos.ManhattanDistance(other.Position!.Value);
|
||||
if (dist >= def.AuraRange!.Min && dist <= def.AuraRange.Max)
|
||||
FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.LeaveAuraRange,
|
||||
ActorId = leavingOwnerId,
|
||||
Data = new TriggerEventData { SourceId = leavingUnitId, TargetId = other.InstanceId },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
private static void FireAuraTransition(
|
||||
GameState state,
|
||||
string auraOwnerId,
|
||||
string auraUnitId,
|
||||
string targetUnitId,
|
||||
bool wasIn,
|
||||
bool isIn)
|
||||
{
|
||||
if (!wasIn && isIn)
|
||||
FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.EnterAuraRange,
|
||||
ActorId = auraOwnerId,
|
||||
Data = new TriggerEventData { SourceId = auraUnitId, TargetId = targetUnitId },
|
||||
});
|
||||
else if (wasIn && !isIn)
|
||||
FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.LeaveAuraRange,
|
||||
ActorId = auraOwnerId,
|
||||
Data = new TriggerEventData { SourceId = auraUnitId, TargetId = targetUnitId },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user