Commit pending working-tree changes (local edits, line endings)

This commit is contained in:
cthangover committed 2026-10-21 12:31:47 +02:00
1 parent 589ee46395
commit 65e632a66f
19 files changed
+1710 -1710

No files matched your search

+101 -101
View File
@@ -1,101 +1,101 @@
using Cthangover.CardBattle.UI;
using Cthangover.Core.Battle.Actions;
using Cthangover.Core.Characters;
using Godot;
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Base class for animated battle actions that move, rotate, and scale card controls
/// through a sequence of visual phases. Subclasses implement <c>DoInternalAction</c> to advance
/// a frame-by-frame animation state machine driven by <see cref="CardBattleCore"/>'s
/// per-frame polling loop (<c>while (!DoAction()) await frame;</c>).
/// Captures source/target positions and rotations in <see cref="DoStart"/> so that
/// <see cref="DoEnd"/> can restore them regardless of what the animation did.
/// </summary>
public abstract class AbstractBattleAction : IBattleAction
{
/// <summary>Seconds timestamp of the current phase start (used for phase progress); -1 before the first phase.</summary>
protected double Timestamp { get; set; } = -1;
/// <summary>Playback speed multiplier applied to all phase durations.</summary>
protected float Speed { get; set; }
/// <summary>The action definition whose stats drive the animation.</summary>
protected ActionCharacter Action { get; set; }
/// <summary>Acting card node.</summary>
protected CharacterCardNode Source { get; set; }
/// <summary>Receiving card node.</summary>
protected CharacterCardNode Target { get; set; }
/// <summary>Visual control of the source card (null-safe abort when freed mid-animation).</summary>
protected Control SourceControl { get; set; }
/// <summary>Source position captured in <see cref="DoStart"/> for restoration in <see cref="DoEnd"/>.</summary>
protected Vector2 SourcePos { get; set; }
/// <summary>Source rotation captured in <see cref="DoStart"/>.</summary>
protected float SourceRot { get; set; }
/// <summary>Visual control of the target card.</summary>
protected Control TargetControl { get; set; }
/// <summary>Target position captured in <see cref="DoStart"/>.</summary>
protected Vector2 TargetPos { get; set; }
/// <summary>Target rotation captured in <see cref="DoStart"/>.</summary>
protected float TargetRot { get; set; }
/// <summary>
/// Advances the animation by one frame. Called in a loop by <see cref="CardBattleCore.RunEnemyTurn"/>
/// with <c>await root.ToSignal(frame)</c> between calls. Returns <c>true</c> when the animation
/// has completed all phases and the action result has been applied.
/// Returns <c>true</c> early if any required node became null (card was destroyed mid-animation).
/// </summary>
/// <returns><c>true</c> when the animation is finished or cannot proceed.</returns>
public bool DoAction()
{
if (Source == null || Target == null || Action == null || SourceControl == null || TargetControl == null)
return true;
return DoInternalAction();
}
/// <summary>Frame tick of the animation; implementors advance phases and return true when finished.</summary>
protected abstract bool DoInternalAction();
/// <summary>Phase setup called once before the first <see cref="DoInternalAction"/> tick.</summary>
protected abstract void DoInternalStart();
/// <summary>Cleanup called once after completion — restore captured positions/rotations here.</summary>
protected abstract void DoInternalEnd();
protected AbstractBattleAction(CharacterCardNode source, CharacterCardNode target, ActionCharacter action, float speed = 1)
{
Source = source;
Target = target;
Action = action;
Speed = speed;
}
/// <summary>
/// Captures the current positions, rotations, and <see cref="Control"/> references of both source
/// and target cards before the animation begins, then delegates to the subclass
/// <see cref="DoInternalStart"/> for any additional setup (e.g. resetting timestamps).
/// </summary>
public void DoStart()
{
if (Source == null || Target == null || Action == null)
return;
SourceControl = Source.GetControlNode();
SourcePos = SourceControl.GlobalPosition;
SourceRot = SourceControl.Rotation;
TargetControl = Target.GetControlNode();
TargetPos = TargetControl.GlobalPosition;
TargetRot = TargetControl.Rotation;
DoInternalStart();
}
/// <summary>
/// Restores source and target card controls to their original positions, rotations, and visual state
/// captured during <see cref="DoStart"/>. Delegates specific cleanup to <see cref="DoInternalEnd"/>.
/// </summary>
public void DoEnd()
{
if (Source == null || Target == null || Action == null)
return;
DoInternalEnd();
}
}
}
using Cthangover.CardBattle.UI;
using Cthangover.Core.Battle.Actions;
using Cthangover.Core.Characters;
using Godot;
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Base class for animated battle actions that move, rotate, and scale card controls
/// through a sequence of visual phases. Subclasses implement <c>DoInternalAction</c> to advance
/// a frame-by-frame animation state machine driven by <see cref="CardBattleCore"/>'s
/// per-frame polling loop (<c>while (!DoAction()) await frame;</c>).
/// Captures source/target positions and rotations in <see cref="DoStart"/> so that
/// <see cref="DoEnd"/> can restore them regardless of what the animation did.
/// </summary>
public abstract class AbstractBattleAction : IBattleAction
{
/// <summary>Seconds timestamp of the current phase start (used for phase progress); -1 before the first phase.</summary>
protected double Timestamp { get; set; } = -1;
/// <summary>Playback speed multiplier applied to all phase durations.</summary>
protected float Speed { get; set; }
/// <summary>The action definition whose stats drive the animation.</summary>
protected ActionCharacter Action { get; set; }
/// <summary>Acting card node.</summary>
protected CharacterCardNode Source { get; set; }
/// <summary>Receiving card node.</summary>
protected CharacterCardNode Target { get; set; }
/// <summary>Visual control of the source card (null-safe abort when freed mid-animation).</summary>
protected Control SourceControl { get; set; }
/// <summary>Source position captured in <see cref="DoStart"/> for restoration in <see cref="DoEnd"/>.</summary>
protected Vector2 SourcePos { get; set; }
/// <summary>Source rotation captured in <see cref="DoStart"/>.</summary>
protected float SourceRot { get; set; }
/// <summary>Visual control of the target card.</summary>
protected Control TargetControl { get; set; }
/// <summary>Target position captured in <see cref="DoStart"/>.</summary>
protected Vector2 TargetPos { get; set; }
/// <summary>Target rotation captured in <see cref="DoStart"/>.</summary>
protected float TargetRot { get; set; }
/// <summary>
/// Advances the animation by one frame. Called in a loop by <see cref="CardBattleCore.RunEnemyTurn"/>
/// with <c>await root.ToSignal(frame)</c> between calls. Returns <c>true</c> when the animation
/// has completed all phases and the action result has been applied.
/// Returns <c>true</c> early if any required node became null (card was destroyed mid-animation).
/// </summary>
/// <returns><c>true</c> when the animation is finished or cannot proceed.</returns>
public bool DoAction()
{
if (Source == null || Target == null || Action == null || SourceControl == null || TargetControl == null)
return true;
return DoInternalAction();
}
/// <summary>Frame tick of the animation; implementors advance phases and return true when finished.</summary>
protected abstract bool DoInternalAction();
/// <summary>Phase setup called once before the first <see cref="DoInternalAction"/> tick.</summary>
protected abstract void DoInternalStart();
/// <summary>Cleanup called once after completion — restore captured positions/rotations here.</summary>
protected abstract void DoInternalEnd();
protected AbstractBattleAction(CharacterCardNode source, CharacterCardNode target, ActionCharacter action, float speed = 1)
{
Source = source;
Target = target;
Action = action;
Speed = speed;
}
/// <summary>
/// Captures the current positions, rotations, and <see cref="Control"/> references of both source
/// and target cards before the animation begins, then delegates to the subclass
/// <see cref="DoInternalStart"/> for any additional setup (e.g. resetting timestamps).
/// </summary>
public void DoStart()
{
if (Source == null || Target == null || Action == null)
return;
SourceControl = Source.GetControlNode();
SourcePos = SourceControl.GlobalPosition;
SourceRot = SourceControl.Rotation;
TargetControl = Target.GetControlNode();
TargetPos = TargetControl.GlobalPosition;
TargetRot = TargetControl.Rotation;
DoInternalStart();
}
/// <summary>
/// Restores source and target card controls to their original positions, rotations, and visual state
/// captured during <see cref="DoStart"/>. Delegates specific cleanup to <see cref="DoInternalEnd"/>.
/// </summary>
public void DoEnd()
{
if (Source == null || Target == null || Action == null)
return;
DoInternalEnd();
}
}
}
+26 -26
View File
@@ -1,26 +1,26 @@
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Phases of the animated battle action state machine used by <see cref="PhysicsAttackAction"/>
/// and <see cref="PhysicsDefenceAction"/>. Each tick, the action evaluates the current phase,
/// interpolates card positions/rotations/scales, and transitions to the next phase when its
/// duration elapses. The <c>Recover</c> phase signals completion by returning <c>true</c> from <c>DoInternalAction</c>.
/// </summary>
public enum ActionPhase
{
/// <summary>Initial delay before any movement.</summary>
Wait,
/// <summary>Source card rotates toward target and begins slight movement.</summary>
Prepare,
/// <summary>Source card travels from its position to the target along a movement pattern.</summary>
MoveForAttack,
/// <summary>Damage/effect is applied to the target card at the point of impact.</summary>
AttackImpact,
/// <summary>Target card shows knockback, squashing, and shaking reaction.</summary>
TargetReaction,
/// <summary>Source card returns toward its original position.</summary>
MoveForBack,
/// <summary>All cards smoothly return to their original transforms.</summary>
Recover
}
}
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Phases of the animated battle action state machine used by <see cref="PhysicsAttackAction"/>
/// and <see cref="PhysicsDefenceAction"/>. Each tick, the action evaluates the current phase,
/// interpolates card positions/rotations/scales, and transitions to the next phase when its
/// duration elapses. The <c>Recover</c> phase signals completion by returning <c>true</c> from <c>DoInternalAction</c>.
/// </summary>
public enum ActionPhase
{
/// <summary>Initial delay before any movement.</summary>
Wait,
/// <summary>Source card rotates toward target and begins slight movement.</summary>
Prepare,
/// <summary>Source card travels from its position to the target along a movement pattern.</summary>
MoveForAttack,
/// <summary>Damage/effect is applied to the target card at the point of impact.</summary>
AttackImpact,
/// <summary>Target card shows knockback, squashing, and shaking reaction.</summary>
TargetReaction,
/// <summary>Source card returns toward its original position.</summary>
MoveForBack,
/// <summary>All cards smoothly return to their original transforms.</summary>
Recover
}
}
+403 -403
View File
@@ -1,403 +1,403 @@
using Cthangover.Core.Battle;
using Cthangover.Core.Battle.Actions;
using Cthangover.Core.UI.Theming;
using Cthangover.Core.Utils;
using Cthangover.TurnBattle;
using Cthangover.Core.Characters;
using Cthangover.CardBattle.UI;
using Godot;
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Animated battle action that moves the source card toward an enemy target, applies damage/defence
/// via <see cref="ActionExecutorHub"/>, and animates the target's knockback/squash reaction.
/// The source card follows one of five randomly-selected <see cref="MovementPattern"/> trajectories
/// (direct, arc, zigzag, spiral, dash). This is the visual counterpart of <see cref="PhysicsDamageActionCard"/>
/// — it handles the presentation while the executor handles the stat changes.
/// Created by <see cref="CardBattleCore.CreateAnimatedAction"/> for enemy turns.
/// </summary>
public class PhysicsAttackAction : AbstractBattleAction
{
/// <summary>
/// Pre-defined movement trajectory patterns for the source card's approach to the target.
/// Selected randomly at construction to add visual variety to enemy attack animations.
/// </summary>
public enum MovementPattern
{
Direct,
Arc,
Zigzag,
Spiral,
Dash
}
private ActionPhase phase = ActionPhase.Wait;
private float attackRotation;
private bool damageApplied;
private bool stunEffectSpawned;
private Vector2 targetOriginalPos;
private Vector2 targetOriginalScale;
private Vector2 knockbackDirection;
private float targetReactionIntensity = 0.3f;
private Vector2 knockbackPos;
private float shake;
private Vector2 squashScale;
private float waitDuration = 0.2f;
private float prepareDuration = 0.3f;
private float moveForAttackDuration = 0.4f;
private float attackImpactDuration = 0.3f;
private float targetReactionDuration = 0.4f;
private float moveForBackDuration = 0.5f;
private float recoverDuration = 0.3f;
private MovementPattern movementPattern = MovementPattern.Direct;
private Vector2 movementControlPoint;
/// <summary>
/// Creates an attack animation from <paramref name="source"/> to <paramref name="target"/>
/// using the stats defined in <paramref name="action"/>. The <see cref="MovementPattern"/> is
/// randomly selected on construction to make enemy attacks visually distinct.
/// <paramref name="speed"/> acts as a multiplier on all phase durations.
/// </summary>
public PhysicsAttackAction(CharacterCardNode source, CharacterCardNode target, ActionCharacter action, float speed = 1)
: base(source, target, action, speed)
{
movementPattern = (MovementPattern)GD.RandRange(0, 5);
}
/// <summary>Resets the phase clock and starts the attack sequence from the Wait phase.</summary>
protected override void DoInternalStart()
{
Timestamp = Godot.Time.GetTicksUsec() / 1_000_000.0;
damageApplied = false;
stunEffectSpawned = false;
phase = ActionPhase.Wait;
targetOriginalScale = TargetControl.Scale;
}
private float GetPhaseProgress()
{
float elapsed = (float)(Godot.Time.GetTicksUsec() / 1_000_000.0 - Timestamp) * Speed * (float)Godot.Engine.TimeScale;
float duration = GetCurrentPhaseDuration();
return Mathf.Clamp(elapsed / duration, 0f, 1f);
}
private float GetCurrentPhaseDuration()
{
switch (phase)
{
case ActionPhase.Wait: return waitDuration;
case ActionPhase.Prepare: return prepareDuration;
case ActionPhase.MoveForAttack: return moveForAttackDuration;
case ActionPhase.AttackImpact: return attackImpactDuration;
case ActionPhase.TargetReaction: return targetReactionDuration;
case ActionPhase.MoveForBack: return moveForBackDuration;
case ActionPhase.Recover: return recoverDuration;
default: return 1f;
}
}
private void ResetTimestamp()
{
Timestamp = Godot.Time.GetTicksUsec() / 1_000_000.0;
}
/// <summary>Advances the multi-phase attack: dash in, impact, damage/stun application, return and recover.</summary>
protected override bool DoInternalAction()
{
float progress = GetPhaseProgress();
switch (phase)
{
case ActionPhase.Wait:
if (progress >= 1f)
{
phase = ActionPhase.Prepare;
ResetTimestamp();
targetOriginalPos = TargetControl.GlobalPosition;
targetOriginalScale = TargetControl.Scale;
knockbackDirection = (TargetControl.GlobalPosition - SourceControl.GlobalPosition).Normalized();
InitializeMovementPattern();
}
break;
case ActionPhase.Prepare:
{
float prepareProgress = EaseOutQuad(Mathf.Min(progress, 1f));
Vector2 preparePosition = GetMovementPosition(prepareProgress * 0.1f);
SourceControl.GlobalPosition = SourcePos.Lerp(preparePosition, prepareProgress);
SourceControl.Rotation = Mathf.Lerp(SourceRot, TargetRot, prepareProgress * 0.3f);
if (progress >= 1f)
{
phase = ActionPhase.MoveForAttack;
ResetTimestamp();
}
break;
}
case ActionPhase.MoveForAttack:
{
float attackProgress = EaseInOutQuad(Mathf.Min(progress, 1f));
Vector2 targetPosition = GetMovementPosition(attackProgress);
SourceControl.GlobalPosition = targetPosition;
SourceControl.Rotation = Mathf.Lerp(SourceRot, TargetRot, attackProgress);
if (progress >= 1f)
{
phase = ActionPhase.AttackImpact;
ResetTimestamp();
attackRotation = (float)GD.RandRange(-35f, 35f) * Mathf.Pi / 180f;
}
break;
}
case ActionPhase.AttackImpact:
{
float impactProgress = EaseInOutQuad(Mathf.Min(progress * 3f, 1f));
SourceControl.Rotation = Mathf.Lerp(TargetRot, attackRotation, impactProgress);
if (!damageApplied && progress >= 0.3f)
{
damageApplied = true;
var result = ActionExecutorHub.Instance.Execute(Action, Source.Card, Target.Card);
Source.UpdateInfo();
Target.UpdateInfo();
if (result.Result)
{
LogAttackResult(result);
}
else
{
GameLogger.Log("CARD_ACT",
$"PhysicsAttackAction: failed to execute action {Action.Name} (source={Source.Card.Name} target={Target.Card.Name})",
LogLevel.Error);
}
}
if (progress >= 1f)
{
phase = ActionPhase.TargetReaction;
ResetTimestamp();
}
break;
}
case ActionPhase.TargetReaction:
{
float reactionProgress = EaseInOutQuad(Mathf.Min(progress, 1f));
bool isStun = Action != null && Action.GetInt("Turn", 0) > 1;
float currentKnockback = targetReactionIntensity;
if (isStun)
currentKnockback *= 2.5f;
knockbackPos = targetOriginalPos + knockbackDirection * currentKnockback;
TargetControl.GlobalPosition = targetOriginalPos.Lerp(knockbackPos, reactionProgress);
squashScale = targetOriginalScale;
float currentSquash = isStun ? 0.35f : 0.2f;
squashScale.X *= 1f - reactionProgress * currentSquash;
squashScale.Y *= 1f + reactionProgress * (currentSquash * 0.5f);
TargetControl.Scale = squashScale;
float currentShakeMultiplier = isStun ? 1.8f : 1f;
shake = Mathf.Sin(reactionProgress * Mathf.Pi * (isStun ? 6f : 4f)) * (1f - reactionProgress) * 0.05f * currentShakeMultiplier;
TargetControl.Rotation = shake * 30f * Mathf.Pi / 180f;
if (progress >= 1f)
{
phase = ActionPhase.MoveForBack;
ResetTimestamp();
}
break;
}
case ActionPhase.MoveForBack:
{
float backProgress = EaseOutQuad(Mathf.Min(progress, 1f));
Vector2 backPosition = GetMovementPosition(1f - progress);
SourceControl.GlobalPosition = backPosition.Lerp(SourcePos, backProgress);
SourceControl.Rotation = Mathf.Lerp(attackRotation, SourceRot, backProgress);
TargetControl.GlobalPosition = knockbackPos.Lerp(targetOriginalPos, backProgress);
TargetControl.Scale = squashScale.Lerp(targetOriginalScale, backProgress);
float currentShake = Mathf.Sin(backProgress * Mathf.Pi) * shake * (1f - backProgress);
TargetControl.Rotation = Mathf.Lerp(currentShake * 30f * Mathf.Pi / 180f, TargetRot, backProgress);
if (progress >= 1f)
{
phase = ActionPhase.Recover;
ResetTimestamp();
}
break;
}
case ActionPhase.Recover:
{
float recoverProgress = EaseOutQuad(Mathf.Min(progress, 1f));
SourceControl.GlobalPosition = SourceControl.GlobalPosition.Lerp(SourcePos, recoverProgress);
SourceControl.Rotation = Mathf.Lerp(SourceControl.Rotation, SourceRot, recoverProgress);
TargetControl.GlobalPosition = TargetControl.GlobalPosition.Lerp(TargetPos, recoverProgress);
TargetControl.Rotation = Mathf.Lerp(TargetControl.Rotation, TargetRot, recoverProgress);
TargetControl.Scale = TargetControl.Scale.Lerp(targetOriginalScale, recoverProgress);
if (progress >= 1f)
return true;
break;
}
default:
return true;
}
return false;
}
/// <summary>Restores the target's captured position, scale and rotation after the attack animation.</summary>
protected override void DoInternalEnd()
{
if (SourceControl != null)
{
SourceControl.GlobalPosition = SourcePos;
SourceControl.Rotation = SourceRot;
}
if (TargetControl != null)
{
TargetControl.GlobalPosition = TargetPos;
TargetControl.Rotation = TargetRot;
TargetControl.Scale = targetOriginalScale;
}
}
/// <summary>
/// Mirrors the resolved action onto the UI: floating damage / absorbed
/// shield numbers, a colour pulse on the target card and journal rows, so
/// an AI attack reads the same as the player's own attack. Stun actions
/// (a "Turn" above 1) flash the electric zap instead of damage numbers.
/// </summary>
private void LogAttackResult(ChangedAttributes result)
{
var targetName = TranslationServer.Translate(Target.Card.Name).ToString();
var isStun = Action != null && Action.GetInt("Turn", 0) > 1;
var defenceLost = -result.Target.Defence;
if (defenceLost > 0)
{
ShowDamageBehaviour.SpawnDefence(defenceLost, TargetControl, TargetControl.GlobalPosition);
BattleLogHub.Log(
string.Format(Translate("battle/log_shield_absorb"), targetName, defenceLost),
UI.BattleLogKind.Shield);
}
if (isStun)
{
Target.PlayZap();
BattleLogHub.Log(
string.Format(Translate("battle/log_stun"), targetName, Action.GetInt("Turn", 0)),
UI.BattleLogKind.Status);
return;
}
if (result.Target.Damage > 0)
{
ShowDamageBehaviour.SpawnDamage(result.Target.Damage, TargetControl, TargetControl.GlobalPosition);
Target.Flash(UiTokens.DangerText);
BattleLogHub.Log(
string.Format(Translate("battle/log_damage"),
Translate(Action.Name),
TranslationServer.Translate(Source.Card.Name).ToString(),
targetName,
result.Target.Damage),
UI.BattleLogKind.Damage);
}
}
private static string Translate(string key)
=> TranslationServer.Translate(key).ToString();
private void InitializeMovementPattern()
{
Vector2 direction = (TargetPos - SourcePos).Normalized();
Vector2 perpendicular = new Vector2(-direction.Y, direction.X);
switch (movementPattern)
{
case MovementPattern.Arc:
movementControlPoint = SourcePos + direction * 0.5f + perpendicular * 2f;
break;
case MovementPattern.Zigzag:
movementControlPoint = SourcePos + direction * 0.5f + perpendicular * 1.5f;
break;
case MovementPattern.Spiral:
movementControlPoint = TargetPos + perpendicular * 1.5f;
break;
case MovementPattern.Dash:
movementControlPoint = SourcePos + direction * 0.3f;
break;
default:
movementControlPoint = TargetPos;
break;
}
}
private Vector2 GetMovementPosition(float progress)
{
switch (movementPattern)
{
case MovementPattern.Direct:
return SourcePos.Lerp(TargetPos, progress);
case MovementPattern.Arc:
return SourcePos.Lerp(movementControlPoint, progress)
.Lerp(movementControlPoint.Lerp(TargetPos, progress), progress);
case MovementPattern.Zigzag:
if (progress < 0.5f)
return SourcePos.Lerp(movementControlPoint, progress * 2f);
else
return movementControlPoint.Lerp(TargetPos, (progress - 0.5f) * 2f);
case MovementPattern.Spiral:
{
Vector2 toTarget = TargetPos - SourcePos;
float angle = progress * Mathf.Pi * 2f;
float radius = (1f - progress) * 1.5f;
Vector2 spiralOffset = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * radius;
return SourcePos.Lerp(TargetPos, progress) + spiralOffset;
}
case MovementPattern.Dash:
if (progress < 0.3f)
return SourcePos.Lerp(movementControlPoint, progress / 0.3f);
else if (progress < 0.7f)
return movementControlPoint;
else
return movementControlPoint.Lerp(TargetPos, (progress - 0.7f) / 0.3f);
default:
return SourcePos.Lerp(TargetPos, progress);
}
}
private float EaseOutQuad(float t)
{
return 1f - (1f - t) * (1f - t);
}
private float EaseInOutQuad(float t)
{
return t < 0.5f ? 2f * t * t : 1f - Mathf.Pow(-2f * t + 2f, 2f) / 2f;
}
}
}
using Cthangover.Core.Battle;
using Cthangover.Core.Battle.Actions;
using Cthangover.Core.UI.Theming;
using Cthangover.Core.Utils;
using Cthangover.TurnBattle;
using Cthangover.Core.Characters;
using Cthangover.CardBattle.UI;
using Godot;
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Animated battle action that moves the source card toward an enemy target, applies damage/defence
/// via <see cref="ActionExecutorHub"/>, and animates the target's knockback/squash reaction.
/// The source card follows one of five randomly-selected <see cref="MovementPattern"/> trajectories
/// (direct, arc, zigzag, spiral, dash). This is the visual counterpart of <see cref="PhysicsDamageActionCard"/>
/// — it handles the presentation while the executor handles the stat changes.
/// Created by <see cref="CardBattleCore.CreateAnimatedAction"/> for enemy turns.
/// </summary>
public class PhysicsAttackAction : AbstractBattleAction
{
/// <summary>
/// Pre-defined movement trajectory patterns for the source card's approach to the target.
/// Selected randomly at construction to add visual variety to enemy attack animations.
/// </summary>
public enum MovementPattern
{
Direct,
Arc,
Zigzag,
Spiral,
Dash
}
private ActionPhase phase = ActionPhase.Wait;
private float attackRotation;
private bool damageApplied;
private bool stunEffectSpawned;
private Vector2 targetOriginalPos;
private Vector2 targetOriginalScale;
private Vector2 knockbackDirection;
private float targetReactionIntensity = 0.3f;
private Vector2 knockbackPos;
private float shake;
private Vector2 squashScale;
private float waitDuration = 0.2f;
private float prepareDuration = 0.3f;
private float moveForAttackDuration = 0.4f;
private float attackImpactDuration = 0.3f;
private float targetReactionDuration = 0.4f;
private float moveForBackDuration = 0.5f;
private float recoverDuration = 0.3f;
private MovementPattern movementPattern = MovementPattern.Direct;
private Vector2 movementControlPoint;
/// <summary>
/// Creates an attack animation from <paramref name="source"/> to <paramref name="target"/>
/// using the stats defined in <paramref name="action"/>. The <see cref="MovementPattern"/> is
/// randomly selected on construction to make enemy attacks visually distinct.
/// <paramref name="speed"/> acts as a multiplier on all phase durations.
/// </summary>
public PhysicsAttackAction(CharacterCardNode source, CharacterCardNode target, ActionCharacter action, float speed = 1)
: base(source, target, action, speed)
{
movementPattern = (MovementPattern)GD.RandRange(0, 5);
}
/// <summary>Resets the phase clock and starts the attack sequence from the Wait phase.</summary>
protected override void DoInternalStart()
{
Timestamp = Godot.Time.GetTicksUsec() / 1_000_000.0;
damageApplied = false;
stunEffectSpawned = false;
phase = ActionPhase.Wait;
targetOriginalScale = TargetControl.Scale;
}
private float GetPhaseProgress()
{
float elapsed = (float)(Godot.Time.GetTicksUsec() / 1_000_000.0 - Timestamp) * Speed * (float)Godot.Engine.TimeScale;
float duration = GetCurrentPhaseDuration();
return Mathf.Clamp(elapsed / duration, 0f, 1f);
}
private float GetCurrentPhaseDuration()
{
switch (phase)
{
case ActionPhase.Wait: return waitDuration;
case ActionPhase.Prepare: return prepareDuration;
case ActionPhase.MoveForAttack: return moveForAttackDuration;
case ActionPhase.AttackImpact: return attackImpactDuration;
case ActionPhase.TargetReaction: return targetReactionDuration;
case ActionPhase.MoveForBack: return moveForBackDuration;
case ActionPhase.Recover: return recoverDuration;
default: return 1f;
}
}
private void ResetTimestamp()
{
Timestamp = Godot.Time.GetTicksUsec() / 1_000_000.0;
}
/// <summary>Advances the multi-phase attack: dash in, impact, damage/stun application, return and recover.</summary>
protected override bool DoInternalAction()
{
float progress = GetPhaseProgress();
switch (phase)
{
case ActionPhase.Wait:
if (progress >= 1f)
{
phase = ActionPhase.Prepare;
ResetTimestamp();
targetOriginalPos = TargetControl.GlobalPosition;
targetOriginalScale = TargetControl.Scale;
knockbackDirection = (TargetControl.GlobalPosition - SourceControl.GlobalPosition).Normalized();
InitializeMovementPattern();
}
break;
case ActionPhase.Prepare:
{
float prepareProgress = EaseOutQuad(Mathf.Min(progress, 1f));
Vector2 preparePosition = GetMovementPosition(prepareProgress * 0.1f);
SourceControl.GlobalPosition = SourcePos.Lerp(preparePosition, prepareProgress);
SourceControl.Rotation = Mathf.Lerp(SourceRot, TargetRot, prepareProgress * 0.3f);
if (progress >= 1f)
{
phase = ActionPhase.MoveForAttack;
ResetTimestamp();
}
break;
}
case ActionPhase.MoveForAttack:
{
float attackProgress = EaseInOutQuad(Mathf.Min(progress, 1f));
Vector2 targetPosition = GetMovementPosition(attackProgress);
SourceControl.GlobalPosition = targetPosition;
SourceControl.Rotation = Mathf.Lerp(SourceRot, TargetRot, attackProgress);
if (progress >= 1f)
{
phase = ActionPhase.AttackImpact;
ResetTimestamp();
attackRotation = (float)GD.RandRange(-35f, 35f) * Mathf.Pi / 180f;
}
break;
}
case ActionPhase.AttackImpact:
{
float impactProgress = EaseInOutQuad(Mathf.Min(progress * 3f, 1f));
SourceControl.Rotation = Mathf.Lerp(TargetRot, attackRotation, impactProgress);
if (!damageApplied && progress >= 0.3f)
{
damageApplied = true;
var result = ActionExecutorHub.Instance.Execute(Action, Source.Card, Target.Card);
Source.UpdateInfo();
Target.UpdateInfo();
if (result.Result)
{
LogAttackResult(result);
}
else
{
GameLogger.Log("CARD_ACT",
$"PhysicsAttackAction: failed to execute action {Action.Name} (source={Source.Card.Name} target={Target.Card.Name})",
LogLevel.Error);
}
}
if (progress >= 1f)
{
phase = ActionPhase.TargetReaction;
ResetTimestamp();
}
break;
}
case ActionPhase.TargetReaction:
{
float reactionProgress = EaseInOutQuad(Mathf.Min(progress, 1f));
bool isStun = Action != null && Action.GetInt("Turn", 0) > 1;
float currentKnockback = targetReactionIntensity;
if (isStun)
currentKnockback *= 2.5f;
knockbackPos = targetOriginalPos + knockbackDirection * currentKnockback;
TargetControl.GlobalPosition = targetOriginalPos.Lerp(knockbackPos, reactionProgress);
squashScale = targetOriginalScale;
float currentSquash = isStun ? 0.35f : 0.2f;
squashScale.X *= 1f - reactionProgress * currentSquash;
squashScale.Y *= 1f + reactionProgress * (currentSquash * 0.5f);
TargetControl.Scale = squashScale;
float currentShakeMultiplier = isStun ? 1.8f : 1f;
shake = Mathf.Sin(reactionProgress * Mathf.Pi * (isStun ? 6f : 4f)) * (1f - reactionProgress) * 0.05f * currentShakeMultiplier;
TargetControl.Rotation = shake * 30f * Mathf.Pi / 180f;
if (progress >= 1f)
{
phase = ActionPhase.MoveForBack;
ResetTimestamp();
}
break;
}
case ActionPhase.MoveForBack:
{
float backProgress = EaseOutQuad(Mathf.Min(progress, 1f));
Vector2 backPosition = GetMovementPosition(1f - progress);
SourceControl.GlobalPosition = backPosition.Lerp(SourcePos, backProgress);
SourceControl.Rotation = Mathf.Lerp(attackRotation, SourceRot, backProgress);
TargetControl.GlobalPosition = knockbackPos.Lerp(targetOriginalPos, backProgress);
TargetControl.Scale = squashScale.Lerp(targetOriginalScale, backProgress);
float currentShake = Mathf.Sin(backProgress * Mathf.Pi) * shake * (1f - backProgress);
TargetControl.Rotation = Mathf.Lerp(currentShake * 30f * Mathf.Pi / 180f, TargetRot, backProgress);
if (progress >= 1f)
{
phase = ActionPhase.Recover;
ResetTimestamp();
}
break;
}
case ActionPhase.Recover:
{
float recoverProgress = EaseOutQuad(Mathf.Min(progress, 1f));
SourceControl.GlobalPosition = SourceControl.GlobalPosition.Lerp(SourcePos, recoverProgress);
SourceControl.Rotation = Mathf.Lerp(SourceControl.Rotation, SourceRot, recoverProgress);
TargetControl.GlobalPosition = TargetControl.GlobalPosition.Lerp(TargetPos, recoverProgress);
TargetControl.Rotation = Mathf.Lerp(TargetControl.Rotation, TargetRot, recoverProgress);
TargetControl.Scale = TargetControl.Scale.Lerp(targetOriginalScale, recoverProgress);
if (progress >= 1f)
return true;
break;
}
default:
return true;
}
return false;
}
/// <summary>Restores the target's captured position, scale and rotation after the attack animation.</summary>
protected override void DoInternalEnd()
{
if (SourceControl != null)
{
SourceControl.GlobalPosition = SourcePos;
SourceControl.Rotation = SourceRot;
}
if (TargetControl != null)
{
TargetControl.GlobalPosition = TargetPos;
TargetControl.Rotation = TargetRot;
TargetControl.Scale = targetOriginalScale;
}
}
/// <summary>
/// Mirrors the resolved action onto the UI: floating damage / absorbed
/// shield numbers, a colour pulse on the target card and journal rows, so
/// an AI attack reads the same as the player's own attack. Stun actions
/// (a "Turn" above 1) flash the electric zap instead of damage numbers.
/// </summary>
private void LogAttackResult(ChangedAttributes result)
{
var targetName = TranslationServer.Translate(Target.Card.Name).ToString();
var isStun = Action != null && Action.GetInt("Turn", 0) > 1;
var defenceLost = -result.Target.Defence;
if (defenceLost > 0)
{
ShowDamageBehaviour.SpawnDefence(defenceLost, TargetControl, TargetControl.GlobalPosition);
BattleLogHub.Log(
string.Format(Translate("battle/log_shield_absorb"), targetName, defenceLost),
UI.BattleLogKind.Shield);
}
if (isStun)
{
Target.PlayZap();
BattleLogHub.Log(
string.Format(Translate("battle/log_stun"), targetName, Action.GetInt("Turn", 0)),
UI.BattleLogKind.Status);
return;
}
if (result.Target.Damage > 0)
{
ShowDamageBehaviour.SpawnDamage(result.Target.Damage, TargetControl, TargetControl.GlobalPosition);
Target.Flash(UiTokens.DangerText);
BattleLogHub.Log(
string.Format(Translate("battle/log_damage"),
Translate(Action.Name),
TranslationServer.Translate(Source.Card.Name).ToString(),
targetName,
result.Target.Damage),
UI.BattleLogKind.Damage);
}
}
private static string Translate(string key)
=> TranslationServer.Translate(key).ToString();
private void InitializeMovementPattern()
{
Vector2 direction = (TargetPos - SourcePos).Normalized();
Vector2 perpendicular = new Vector2(-direction.Y, direction.X);
switch (movementPattern)
{
case MovementPattern.Arc:
movementControlPoint = SourcePos + direction * 0.5f + perpendicular * 2f;
break;
case MovementPattern.Zigzag:
movementControlPoint = SourcePos + direction * 0.5f + perpendicular * 1.5f;
break;
case MovementPattern.Spiral:
movementControlPoint = TargetPos + perpendicular * 1.5f;
break;
case MovementPattern.Dash:
movementControlPoint = SourcePos + direction * 0.3f;
break;
default:
movementControlPoint = TargetPos;
break;
}
}
private Vector2 GetMovementPosition(float progress)
{
switch (movementPattern)
{
case MovementPattern.Direct:
return SourcePos.Lerp(TargetPos, progress);
case MovementPattern.Arc:
return SourcePos.Lerp(movementControlPoint, progress)
.Lerp(movementControlPoint.Lerp(TargetPos, progress), progress);
case MovementPattern.Zigzag:
if (progress < 0.5f)
return SourcePos.Lerp(movementControlPoint, progress * 2f);
else
return movementControlPoint.Lerp(TargetPos, (progress - 0.5f) * 2f);
case MovementPattern.Spiral:
{
Vector2 toTarget = TargetPos - SourcePos;
float angle = progress * Mathf.Pi * 2f;
float radius = (1f - progress) * 1.5f;
Vector2 spiralOffset = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * radius;
return SourcePos.Lerp(TargetPos, progress) + spiralOffset;
}
case MovementPattern.Dash:
if (progress < 0.3f)
return SourcePos.Lerp(movementControlPoint, progress / 0.3f);
else if (progress < 0.7f)
return movementControlPoint;
else
return movementControlPoint.Lerp(TargetPos, (progress - 0.7f) / 0.3f);
default:
return SourcePos.Lerp(TargetPos, progress);
}
}
private float EaseOutQuad(float t)
{
return 1f - (1f - t) * (1f - t);
}
private float EaseInOutQuad(float t)
{
return t < 0.5f ? 2f * t * t : 1f - Mathf.Pow(-2f * t + 2f, 2f) / 2f;
}
}
}
+56 -56
View File
@@ -1,56 +1,56 @@
using Cthangover.Core.Cards.StatusEffect;
using Cthangover.Core.Characters;
using Godot;
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Executor that applies physical damage from an attack card to a target.
/// Calculates damage as <c>card_attack_multiplier × user_attack_stat</c>, reduces it by
/// the target's current defence value (which is consumed in the process), and applies the
/// remainder to the target's health. Invokes <c>OnTakeDamage</c>/<c>OnDealDamage</c> hooks
/// on both characters' <see cref="StatusEffectQueue"/> so that damage-modifying status effects
/// can intercept and adjust the values before they are applied.
/// Registered in <see cref="CardBattleActionProvider"/> under <c>"physics/attack"</c>.
/// </summary>
public class PhysicsDamageActionCard : ActionBase
{
/// <summary>
/// Unique identifier registered in <see cref="CardBattleActionProvider"/> as <c>"physics/attack"</c>.
/// </summary>
public override string ID => "PhysicsDamageActionCard";
/// <summary>
/// Resolves the attack: deducts action points from <paramref name="user"/> via <c>CheckRequiredAndUsePoint</c>,
/// computes raw damage from the card's attack multiplier and the user's attack stat,
/// applies status-effect damage hooks, consumes <paramref name="target"/>'s defence shield first,
/// then subtracts remaining damage from health. Returns a <see cref="ChangedAttributes"/> with
/// the actual damage and defence-loss deltas for the floating text display.
/// </summary>
public override ChangedAttributes Execute(ActionCharacter actionCard, Character user, Character target)
{
if (target == null || user == null || !CheckRequiredAndUsePoint(actionCard, user))
return new ChangedAttributes { Result = false };
var damageDelta = Mathf.RoundToInt(actionCard.GetFloat("Attack", 1f) * user.Attributes.Attack.Value);
StatusEffectService.Of(target).OnTakeDamage(user, ref damageDelta);
StatusEffectService.Of(user).OnDealDamage(target, ref damageDelta);
var defenceDelta = 0;
var ignoreArmor = user.HasPassiveEffect("IgnoreArmor");
if (!ignoreArmor && target.Attributes.Defence.Value > 0)
{
defenceDelta = target.Attributes.Defence.Value >= damageDelta ? damageDelta : target.Attributes.Defence.Value;
damageDelta -= defenceDelta;
}
target.Attributes.Defence.Value -= defenceDelta;
target.Attributes.Health.Value -= damageDelta;
return new ChangedAttributes { Result = true, Target = { Damage = damageDelta, Defence = -defenceDelta}};
}
}
}
using Cthangover.Core.Cards.StatusEffect;
using Cthangover.Core.Characters;
using Godot;
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Executor that applies physical damage from an attack card to a target.
/// Calculates damage as <c>card_attack_multiplier × user_attack_stat</c>, reduces it by
/// the target's current defence value (which is consumed in the process), and applies the
/// remainder to the target's health. Invokes <c>OnTakeDamage</c>/<c>OnDealDamage</c> hooks
/// on both characters' <see cref="StatusEffectQueue"/> so that damage-modifying status effects
/// can intercept and adjust the values before they are applied.
/// Registered in <see cref="CardBattleActionProvider"/> under <c>"physics/attack"</c>.
/// </summary>
public class PhysicsDamageActionCard : ActionBase
{
/// <summary>
/// Unique identifier registered in <see cref="CardBattleActionProvider"/> as <c>"physics/attack"</c>.
/// </summary>
public override string ID => "PhysicsDamageActionCard";
/// <summary>
/// Resolves the attack: deducts action points from <paramref name="user"/> via <c>CheckRequiredAndUsePoint</c>,
/// computes raw damage from the card's attack multiplier and the user's attack stat,
/// applies status-effect damage hooks, consumes <paramref name="target"/>'s defence shield first,
/// then subtracts remaining damage from health. Returns a <see cref="ChangedAttributes"/> with
/// the actual damage and defence-loss deltas for the floating text display.
/// </summary>
public override ChangedAttributes Execute(ActionCharacter actionCard, Character user, Character target)
{
if (target == null || user == null || !CheckRequiredAndUsePoint(actionCard, user))
return new ChangedAttributes { Result = false };
var damageDelta = Mathf.RoundToInt(actionCard.GetFloat("Attack", 1f) * user.Attributes.Attack.Value);
StatusEffectService.Of(target).OnTakeDamage(user, ref damageDelta);
StatusEffectService.Of(user).OnDealDamage(target, ref damageDelta);
var defenceDelta = 0;
var ignoreArmor = user.HasPassiveEffect("IgnoreArmor");
if (!ignoreArmor && target.Attributes.Defence.Value > 0)
{
defenceDelta = target.Attributes.Defence.Value >= damageDelta ? damageDelta : target.Attributes.Defence.Value;
damageDelta -= defenceDelta;
}
target.Attributes.Defence.Value -= defenceDelta;
target.Attributes.Health.Value -= damageDelta;
return new ChangedAttributes { Result = true, Target = { Damage = damageDelta, Defence = -defenceDelta}};
}
}
}
+231 -231
View File
@@ -1,231 +1,231 @@
using Cthangover.Core.Battle;
using Cthangover.Core.Battle.Actions;
using Cthangover.Core.UI.Theming;
using Cthangover.Core.Characters;
using Cthangover.TurnBattle;
using Cthangover.CardBattle.UI;
using Godot;
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Animated battle action that plays a self-buff or ally-support animation: the source card
/// briefly rotates toward the target, applies a defence boost via <see cref="ActionExecutorHub"/>,
/// and shows a subtle block/stagger reaction on the target. Used for both <c>ToSelf</c> and
/// <c>ToAlias</c> action types. This is the visual counterpart of <see cref="PhysicsDefenceActionCard"/>.
/// Created by <see cref="CardBattleCore.CreateAnimatedAction"/> for enemy self-buff/support turns.
/// </summary>
public class PhysicsDefenceAction : AbstractBattleAction
{
private ActionPhase phase = ActionPhase.Prepare;
private float defendRotation;
private bool effectApplied;
private Vector2 targetOriginalScale;
private Color targetOriginalColor;
private Vector2 blockScale;
private Vector2 blockKnockback;
private Color blockColor;
private float prepareDuration = 0.3f;
private float attackImpactDuration = 0.4f;
private float targetReactionDuration = 0.3f;
private float moveForBackDuration = 0.4f;
private float recoverDuration = 0.3f;
/// <summary>
/// Creates a defence/support animation. Both <paramref name="source"/> and <paramref name="target"/>
/// can be the same card (for self-buffs) or different (for ally support).
/// <paramref name="speed"/> multiplies all phase durations.
/// </summary>
public PhysicsDefenceAction(CharacterCardNode source, CharacterCardNode target, ActionCharacter action, float speed = 1)
: base(source, target, action, speed) { }
/// <summary>Resets the phase clock and captures the target's original scale and colour.</summary>
protected override void DoInternalStart()
{
Timestamp = Godot.Time.GetTicksUsec() / 1_000_000.0;
effectApplied = false;
phase = ActionPhase.Prepare;
targetOriginalScale = TargetControl.Scale;
targetOriginalColor = TargetControl.Modulate;
}
private float GetPhaseProgress()
{
float elapsed = (float)(Godot.Time.GetTicksUsec() / 1_000_000.0 - Timestamp) * Speed * (float)Godot.Engine.TimeScale;
float duration = GetCurrentPhaseDuration();
return Mathf.Clamp(elapsed / duration, 0f, 1f);
}
private float GetCurrentPhaseDuration()
{
switch (phase)
{
case ActionPhase.Prepare: return prepareDuration;
case ActionPhase.AttackImpact: return attackImpactDuration;
case ActionPhase.TargetReaction: return targetReactionDuration;
case ActionPhase.MoveForBack: return moveForBackDuration;
case ActionPhase.Recover: return recoverDuration;
default: return 1f;
}
}
private void ResetTimestamp()
{
Timestamp = Godot.Time.GetTicksUsec() / 1_000_000.0;
}
/// <summary>Advances the guard animation: approach, impact, defence stat application, return and recover.</summary>
protected override bool DoInternalAction()
{
float progress = GetPhaseProgress();
switch (phase)
{
case ActionPhase.Prepare:
{
float prepareProgress = EaseOutQuad(Mathf.Min(progress, 1f));
SourceControl.Rotation = Mathf.Lerp(SourceRot, TargetRot, prepareProgress);
if (progress >= 1f)
{
phase = ActionPhase.AttackImpact;
ResetTimestamp();
defendRotation = (float)GD.RandRange(-15f, 15f) * Mathf.Pi / 180f;
targetOriginalScale = TargetControl.Scale;
targetOriginalColor = TargetControl.Modulate;
}
break;
}
case ActionPhase.AttackImpact:
{
float impactProgress = EaseInOutQuad(Mathf.Min(progress * 2f, 1f));
SourceControl.Rotation = Mathf.Lerp(TargetRot, defendRotation, impactProgress);
blockScale = targetOriginalScale * (1f + impactProgress * 0.1f);
TargetControl.Scale = blockScale;
blockColor = targetOriginalColor;
blockColor.G = Mathf.Min(1f, blockColor.G + impactProgress * 0.3f);
blockColor.B = Mathf.Min(1f, blockColor.B + impactProgress * 0.2f);
TargetControl.Modulate = blockColor;
if (!effectApplied && progress >= 0.4f)
{
effectApplied = true;
var result = ActionExecutorHub.Instance.Execute(Action, Source.Card, Target.Card);
Source.UpdateInfo();
Target.UpdateInfo();
if (result.Result && result.Target.Defence > 0)
{
ShowDamageBehaviour.SpawnDefence(result.Target.Defence, TargetControl, TargetControl.GlobalPosition);
Target.Flash(UiTokens.Info);
BattleLogHub.Log(
string.Format(Translate("battle/log_shield_gain"),
TranslationServer.Translate(Target.Card.Name).ToString(),
result.Target.Defence),
UI.BattleLogKind.Buff);
}
}
if (progress >= 1f)
{
phase = ActionPhase.TargetReaction;
ResetTimestamp();
}
break;
}
case ActionPhase.TargetReaction:
{
float reactionProgress = EaseInOutQuad(Mathf.Min(progress * 1.5f, 1f));
blockKnockback = TargetPos + (TargetPos - SourcePos).Normalized() * 0.1f;
TargetControl.GlobalPosition = TargetPos.Lerp(blockKnockback, reactionProgress * 0.5f);
TargetControl.Scale = blockScale.Lerp(targetOriginalScale, reactionProgress);
if (progress >= 1f)
{
phase = ActionPhase.MoveForBack;
ResetTimestamp();
}
break;
}
case ActionPhase.MoveForBack:
{
float backProgress = EaseOutQuad(Mathf.Min(progress, 1f));
SourceControl.GlobalPosition = TargetPos.Lerp(SourcePos, backProgress);
SourceControl.Rotation = Mathf.Lerp(defendRotation, SourceRot, backProgress);
TargetControl.GlobalPosition = TargetControl.GlobalPosition.Lerp(TargetPos, backProgress);
TargetControl.Scale = TargetControl.Scale.Lerp(targetOriginalScale, backProgress);
Color currentColor = TargetControl.Modulate;
TargetControl.Modulate = currentColor.Lerp(targetOriginalColor, backProgress);
if (progress >= 1f)
{
phase = ActionPhase.Recover;
ResetTimestamp();
}
break;
}
case ActionPhase.Recover:
{
float recoverProgress = EaseOutQuad(Mathf.Min(progress, 1f));
SourceControl.GlobalPosition = SourceControl.GlobalPosition.Lerp(SourcePos, recoverProgress);
SourceControl.Rotation = Mathf.Lerp(SourceControl.Rotation, SourceRot, recoverProgress);
TargetControl.Rotation = Mathf.Lerp(TargetControl.Rotation, TargetRot, recoverProgress);
if (progress >= 1f)
return true;
break;
}
default:
return true;
}
return false;
}
/// <summary>Restores the target's captured visual state after the defence animation.</summary>
protected override void DoInternalEnd()
{
if (SourceControl != null)
{
SourceControl.GlobalPosition = SourcePos;
SourceControl.Rotation = SourceRot;
}
if (TargetControl != null)
{
TargetControl.GlobalPosition = TargetPos;
TargetControl.Rotation = TargetRot;
TargetControl.Scale = targetOriginalScale;
TargetControl.Modulate = targetOriginalColor;
}
}
private float EaseOutQuad(float t)
{
return 1f - (1f - t) * (1f - t);
}
private float EaseInOutQuad(float t)
{
return t < 0.5f ? 2f * t * t : 1f - Mathf.Pow(-2f * t + 2f, 2f) / 2f;
}
private static string Translate(string key)
=> TranslationServer.Translate(key).ToString();
}
}
using Cthangover.Core.Battle;
using Cthangover.Core.Battle.Actions;
using Cthangover.Core.UI.Theming;
using Cthangover.Core.Characters;
using Cthangover.TurnBattle;
using Cthangover.CardBattle.UI;
using Godot;
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Animated battle action that plays a self-buff or ally-support animation: the source card
/// briefly rotates toward the target, applies a defence boost via <see cref="ActionExecutorHub"/>,
/// and shows a subtle block/stagger reaction on the target. Used for both <c>ToSelf</c> and
/// <c>ToAlias</c> action types. This is the visual counterpart of <see cref="PhysicsDefenceActionCard"/>.
/// Created by <see cref="CardBattleCore.CreateAnimatedAction"/> for enemy self-buff/support turns.
/// </summary>
public class PhysicsDefenceAction : AbstractBattleAction
{
private ActionPhase phase = ActionPhase.Prepare;
private float defendRotation;
private bool effectApplied;
private Vector2 targetOriginalScale;
private Color targetOriginalColor;
private Vector2 blockScale;
private Vector2 blockKnockback;
private Color blockColor;
private float prepareDuration = 0.3f;
private float attackImpactDuration = 0.4f;
private float targetReactionDuration = 0.3f;
private float moveForBackDuration = 0.4f;
private float recoverDuration = 0.3f;
/// <summary>
/// Creates a defence/support animation. Both <paramref name="source"/> and <paramref name="target"/>
/// can be the same card (for self-buffs) or different (for ally support).
/// <paramref name="speed"/> multiplies all phase durations.
/// </summary>
public PhysicsDefenceAction(CharacterCardNode source, CharacterCardNode target, ActionCharacter action, float speed = 1)
: base(source, target, action, speed) { }
/// <summary>Resets the phase clock and captures the target's original scale and colour.</summary>
protected override void DoInternalStart()
{
Timestamp = Godot.Time.GetTicksUsec() / 1_000_000.0;
effectApplied = false;
phase = ActionPhase.Prepare;
targetOriginalScale = TargetControl.Scale;
targetOriginalColor = TargetControl.Modulate;
}
private float GetPhaseProgress()
{
float elapsed = (float)(Godot.Time.GetTicksUsec() / 1_000_000.0 - Timestamp) * Speed * (float)Godot.Engine.TimeScale;
float duration = GetCurrentPhaseDuration();
return Mathf.Clamp(elapsed / duration, 0f, 1f);
}
private float GetCurrentPhaseDuration()
{
switch (phase)
{
case ActionPhase.Prepare: return prepareDuration;
case ActionPhase.AttackImpact: return attackImpactDuration;
case ActionPhase.TargetReaction: return targetReactionDuration;
case ActionPhase.MoveForBack: return moveForBackDuration;
case ActionPhase.Recover: return recoverDuration;
default: return 1f;
}
}
private void ResetTimestamp()
{
Timestamp = Godot.Time.GetTicksUsec() / 1_000_000.0;
}
/// <summary>Advances the guard animation: approach, impact, defence stat application, return and recover.</summary>
protected override bool DoInternalAction()
{
float progress = GetPhaseProgress();
switch (phase)
{
case ActionPhase.Prepare:
{
float prepareProgress = EaseOutQuad(Mathf.Min(progress, 1f));
SourceControl.Rotation = Mathf.Lerp(SourceRot, TargetRot, prepareProgress);
if (progress >= 1f)
{
phase = ActionPhase.AttackImpact;
ResetTimestamp();
defendRotation = (float)GD.RandRange(-15f, 15f) * Mathf.Pi / 180f;
targetOriginalScale = TargetControl.Scale;
targetOriginalColor = TargetControl.Modulate;
}
break;
}
case ActionPhase.AttackImpact:
{
float impactProgress = EaseInOutQuad(Mathf.Min(progress * 2f, 1f));
SourceControl.Rotation = Mathf.Lerp(TargetRot, defendRotation, impactProgress);
blockScale = targetOriginalScale * (1f + impactProgress * 0.1f);
TargetControl.Scale = blockScale;
blockColor = targetOriginalColor;
blockColor.G = Mathf.Min(1f, blockColor.G + impactProgress * 0.3f);
blockColor.B = Mathf.Min(1f, blockColor.B + impactProgress * 0.2f);
TargetControl.Modulate = blockColor;
if (!effectApplied && progress >= 0.4f)
{
effectApplied = true;
var result = ActionExecutorHub.Instance.Execute(Action, Source.Card, Target.Card);
Source.UpdateInfo();
Target.UpdateInfo();
if (result.Result && result.Target.Defence > 0)
{
ShowDamageBehaviour.SpawnDefence(result.Target.Defence, TargetControl, TargetControl.GlobalPosition);
Target.Flash(UiTokens.Info);
BattleLogHub.Log(
string.Format(Translate("battle/log_shield_gain"),
TranslationServer.Translate(Target.Card.Name).ToString(),
result.Target.Defence),
UI.BattleLogKind.Buff);
}
}
if (progress >= 1f)
{
phase = ActionPhase.TargetReaction;
ResetTimestamp();
}
break;
}
case ActionPhase.TargetReaction:
{
float reactionProgress = EaseInOutQuad(Mathf.Min(progress * 1.5f, 1f));
blockKnockback = TargetPos + (TargetPos - SourcePos).Normalized() * 0.1f;
TargetControl.GlobalPosition = TargetPos.Lerp(blockKnockback, reactionProgress * 0.5f);
TargetControl.Scale = blockScale.Lerp(targetOriginalScale, reactionProgress);
if (progress >= 1f)
{
phase = ActionPhase.MoveForBack;
ResetTimestamp();
}
break;
}
case ActionPhase.MoveForBack:
{
float backProgress = EaseOutQuad(Mathf.Min(progress, 1f));
SourceControl.GlobalPosition = TargetPos.Lerp(SourcePos, backProgress);
SourceControl.Rotation = Mathf.Lerp(defendRotation, SourceRot, backProgress);
TargetControl.GlobalPosition = TargetControl.GlobalPosition.Lerp(TargetPos, backProgress);
TargetControl.Scale = TargetControl.Scale.Lerp(targetOriginalScale, backProgress);
Color currentColor = TargetControl.Modulate;
TargetControl.Modulate = currentColor.Lerp(targetOriginalColor, backProgress);
if (progress >= 1f)
{
phase = ActionPhase.Recover;
ResetTimestamp();
}
break;
}
case ActionPhase.Recover:
{
float recoverProgress = EaseOutQuad(Mathf.Min(progress, 1f));
SourceControl.GlobalPosition = SourceControl.GlobalPosition.Lerp(SourcePos, recoverProgress);
SourceControl.Rotation = Mathf.Lerp(SourceControl.Rotation, SourceRot, recoverProgress);
TargetControl.Rotation = Mathf.Lerp(TargetControl.Rotation, TargetRot, recoverProgress);
if (progress >= 1f)
return true;
break;
}
default:
return true;
}
return false;
}
/// <summary>Restores the target's captured visual state after the defence animation.</summary>
protected override void DoInternalEnd()
{
if (SourceControl != null)
{
SourceControl.GlobalPosition = SourcePos;
SourceControl.Rotation = SourceRot;
}
if (TargetControl != null)
{
TargetControl.GlobalPosition = TargetPos;
TargetControl.Rotation = TargetRot;
TargetControl.Scale = targetOriginalScale;
TargetControl.Modulate = targetOriginalColor;
}
}
private float EaseOutQuad(float t)
{
return 1f - (1f - t) * (1f - t);
}
private float EaseInOutQuad(float t)
{
return t < 0.5f ? 2f * t * t : 1f - Mathf.Pow(-2f * t + 2f, 2f) / 2f;
}
private static string Translate(string key)
=> TranslationServer.Translate(key).ToString();
}
}
+39 -39
View File
@@ -1,39 +1,39 @@
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Executor that adds a fixed amount of defence to the target character.
/// Defence acts as a damage-absorbing shield that is consumed before health when the
/// character takes damage (see <see cref="PhysicsDamageActionCard"/>). The defence delta
/// comes from the <c>"Defence"</c> property on the action card.
/// Registered in <see cref="CardBattleActionProvider"/> under <c>"physics/defence"</c>.
/// </summary>
public class PhysicsDefenceActionCard : ActionBase
{
/// <summary>
/// Unique identifier registered in <see cref="CardBattleActionProvider"/> as <c>"physics/defence"</c>.
/// </summary>
public override string ID => "PhysicsDefenceActionCard";
/// <summary>
/// Adds a flat defence value to <paramref name="target"/> after deducting action points
/// from <paramref name="user"/>. The returned <see cref="ChangedAttributes"/> carries the
/// defence delta for floating text display.
/// </summary>
public override ChangedAttributes Execute(ActionCharacter actionCard, Character user, Character target)
{
if (target == null || user == null || !CheckRequiredAndUsePoint(actionCard, user))
return new ChangedAttributes { Result = false };
var defenceDelta = actionCard.GetInt("Defence");
target.Attributes.Defence.Value += defenceDelta;
return new ChangedAttributes { Result = true, Target = { Defence = defenceDelta}};
}
}
}
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Executor that adds a fixed amount of defence to the target character.
/// Defence acts as a damage-absorbing shield that is consumed before health when the
/// character takes damage (see <see cref="PhysicsDamageActionCard"/>). The defence delta
/// comes from the <c>"Defence"</c> property on the action card.
/// Registered in <see cref="CardBattleActionProvider"/> under <c>"physics/defence"</c>.
/// </summary>
public class PhysicsDefenceActionCard : ActionBase
{
/// <summary>
/// Unique identifier registered in <see cref="CardBattleActionProvider"/> as <c>"physics/defence"</c>.
/// </summary>
public override string ID => "PhysicsDefenceActionCard";
/// <summary>
/// Adds a flat defence value to <paramref name="target"/> after deducting action points
/// from <paramref name="user"/>. The returned <see cref="ChangedAttributes"/> carries the
/// defence delta for floating text display.
/// </summary>
public override ChangedAttributes Execute(ActionCharacter actionCard, Character user, Character target)
{
if (target == null || user == null || !CheckRequiredAndUsePoint(actionCard, user))
return new ChangedAttributes { Result = false };
var defenceDelta = actionCard.GetInt("Defence");
target.Attributes.Defence.Value += defenceDelta;
return new ChangedAttributes { Result = true, Target = { Defence = defenceDelta}};
}
}
}
+40 -40
View File
@@ -1,40 +1,40 @@
using Cthangover.Core.Cards.StatusEffect;
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Executor that applies a stun status effect to the target character.
/// The stun duration (number of turns) is read from the <c>"Turn"</c> property on the action card.
/// During a stunned character's turn, <see cref="StatusEffectQueue.HasStun"/> returns <c>true</c>,
/// causing <see cref="CardBattleCore.RunEnemyTurn"/> to skip that character's actions entirely.
/// Registered in <see cref="CardBattleActionProvider"/> under <c>"physics/stun"</c>.
/// </summary>
public class PhysicsStunActionCard : ActionBase
{
/// <summary>
/// Unique identifier registered in <see cref="CardBattleActionProvider"/> as <c>"physics/stun"</c>.
/// </summary>
public override string ID => "PhysicsStunActionCard";
/// <summary>
/// Adds a <see cref="StunStatusEffect"/> with the configured turn count to <paramref name="target"/>'s
/// <see cref="StatusEffectQueue"/> after deducting action points from <paramref name="user"/>.
/// </summary>
public override ChangedAttributes Execute(ActionCharacter actionCard, Character user, Character target)
{
if (target == null || user == null || !CheckRequiredAndUsePoint(actionCard, user))
return new ChangedAttributes { Result = false };
if (target.HasPassiveEffect("StunImmune"))
return new ChangedAttributes { Result = true };
StatusEffectService.Of(target).Add("effect/physics/stun", actionCard.GetInt("Turn", 3));
return new ChangedAttributes { Result = true };
}
}
}
using Cthangover.Core.Cards.StatusEffect;
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.Actions
{
/// <summary>
/// Executor that applies a stun status effect to the target character.
/// The stun duration (number of turns) is read from the <c>"Turn"</c> property on the action card.
/// During a stunned character's turn, <see cref="StatusEffectQueue.HasStun"/> returns <c>true</c>,
/// causing <see cref="CardBattleCore.RunEnemyTurn"/> to skip that character's actions entirely.
/// Registered in <see cref="CardBattleActionProvider"/> under <c>"physics/stun"</c>.
/// </summary>
public class PhysicsStunActionCard : ActionBase
{
/// <summary>
/// Unique identifier registered in <see cref="CardBattleActionProvider"/> as <c>"physics/stun"</c>.
/// </summary>
public override string ID => "PhysicsStunActionCard";
/// <summary>
/// Adds a <see cref="StunStatusEffect"/> with the configured turn count to <paramref name="target"/>'s
/// <see cref="StatusEffectQueue"/> after deducting action points from <paramref name="user"/>.
/// </summary>
public override ChangedAttributes Execute(ActionCharacter actionCard, Character user, Character target)
{
if (target == null || user == null || !CheckRequiredAndUsePoint(actionCard, user))
return new ChangedAttributes { Result = false };
if (target.HasPassiveEffect("StunImmune"))
return new ChangedAttributes { Result = true };
StatusEffectService.Of(target).Add("effect/physics/stun", actionCard.GetInt("Turn", 3));
return new ChangedAttributes { Result = true };
}
}
}
+52 -52
View File
@@ -1,52 +1,52 @@
using Cthangover.CardBattle.UI;
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.Player
{
/// <summary>
/// Factory that returns the appropriate <see cref="ICardActionStrategy"/> implementation
/// for a given <see cref="ActionCharacter"/>. Dispatches based on <see cref="ActionCharacterType"/>:
/// <c>ToEnemy</c> → <see cref="AttackToEnemyStrategy"/>,
/// <c>ToAlias</c> → <see cref="SupportStrategy"/>,
/// <c>ToSelf</c> → <see cref="SelfStrategy"/>.
/// Used by <see cref="CardController"/> during drag-and-drop to determine valid targeting behavior
/// and by <see cref="CanTarget"/> for pre-validation.
/// </summary>
public static class CardActionStrategyFactory
{
/// <summary>
/// Returns the matching strategy for the action's type, or <c>null</c> if any argument is null
/// or the type is unrecognized.
/// </summary>
public static ICardActionStrategy Get(ActionCharacter action, CharacterCardNode source, CharacterCardNode target)
{
if (action == null || source == null || target == null)
return null;
switch (action.Type)
{
case ActionCharacterType.ToEnemy:
return new AttackToEnemyStrategy();
case ActionCharacterType.ToAlias:
return new SupportStrategy();
case ActionCharacterType.ToSelf:
return new SelfStrategy();
default:
return null;
}
}
/// <summary>
/// Convenience method that gets the strategy via <see cref="Get"/> and immediately calls
/// <see cref="ICardActionStrategy.Check"/> on it. Returns <c>false</c> if no strategy matches
/// or any argument is null.
/// </summary>
public static bool CanTarget(ActionCharacter action, CharacterCardNode source, CharacterCardNode target)
{
if (action == null || source == null || target == null)
return false;
return Get(action, source, target)?.Check(action, source, target) ?? false;
}
}
}
using Cthangover.CardBattle.UI;
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.Player
{
/// <summary>
/// Factory that returns the appropriate <see cref="ICardActionStrategy"/> implementation
/// for a given <see cref="ActionCharacter"/>. Dispatches based on <see cref="ActionCharacterType"/>:
/// <c>ToEnemy</c> → <see cref="AttackToEnemyStrategy"/>,
/// <c>ToAlias</c> → <see cref="SupportStrategy"/>,
/// <c>ToSelf</c> → <see cref="SelfStrategy"/>.
/// Used by <see cref="CardController"/> during drag-and-drop to determine valid targeting behavior
/// and by <see cref="CanTarget"/> for pre-validation.
/// </summary>
public static class CardActionStrategyFactory
{
/// <summary>
/// Returns the matching strategy for the action's type, or <c>null</c> if any argument is null
/// or the type is unrecognized.
/// </summary>
public static ICardActionStrategy Get(ActionCharacter action, CharacterCardNode source, CharacterCardNode target)
{
if (action == null || source == null || target == null)
return null;
switch (action.Type)
{
case ActionCharacterType.ToEnemy:
return new AttackToEnemyStrategy();
case ActionCharacterType.ToAlias:
return new SupportStrategy();
case ActionCharacterType.ToSelf:
return new SelfStrategy();
default:
return null;
}
}
/// <summary>
/// Convenience method that gets the strategy via <see cref="Get"/> and immediately calls
/// <see cref="ICardActionStrategy.Check"/> on it. Returns <c>false</c> if no strategy matches
/// or any argument is null.
/// </summary>
public static bool CanTarget(ActionCharacter action, CharacterCardNode source, CharacterCardNode target)
{
if (action == null || source == null || target == null)
return false;
return Get(action, source, target)?.Check(action, source, target) ?? false;
}
}
}
+32 -32
View File
@@ -1,32 +1,32 @@
using Cthangover.CardBattle.UI;
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.Player
{
/// <summary>
/// Strategy interface that defines validation, visual highlighting, and execution for
/// a specific type of card action during the player's drag-and-drop targeting flow.
/// The <see cref="CardActionStrategyFactory"/> dispatches to the correct strategy based on
/// the action's <see cref="ActionCharacterType"/>. <see cref="CardController"/> calls
/// <see cref="Check"/> during drag to validate targets, <see cref="HighlightTarget"/> to show
/// valid/invalid visual feedback, and <see cref="Execute"/> when the card is dropped on a valid target.
/// </summary>
public interface ICardActionStrategy
{
/// <summary>
/// Validates whether <paramref name="action"/> can be performed by <paramref name="source"/>
/// on <paramref name="target"/>. Called each frame during drag to determine visual feedback.
/// </summary>
bool Check(ActionCharacter action, CharacterCardNode source, CharacterCardNode target);
/// <summary>
/// Applies visual highlighting to both the <paramref name="actionCard"/> being dragged
/// and the <paramref name="target"/> character card (e.g. red outline for attack, green for support).
/// </summary>
void HighlightTarget(ActionCardNode actionCard, CharacterCardNode target);
/// <summary>
/// Executes the action via <see cref="ActionExecutorHub"/> and spawns floating damage/defence text.
/// Called on successful card drop by <see cref="CardController.OnEndDrag"/>.
/// </summary>
void Execute(CharacterCardNode source, CharacterCardNode target, ActionCharacter action);
}
}
using Cthangover.CardBattle.UI;
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.Player
{
/// <summary>
/// Strategy interface that defines validation, visual highlighting, and execution for
/// a specific type of card action during the player's drag-and-drop targeting flow.
/// The <see cref="CardActionStrategyFactory"/> dispatches to the correct strategy based on
/// the action's <see cref="ActionCharacterType"/>. <see cref="CardController"/> calls
/// <see cref="Check"/> during drag to validate targets, <see cref="HighlightTarget"/> to show
/// valid/invalid visual feedback, and <see cref="Execute"/> when the card is dropped on a valid target.
/// </summary>
public interface ICardActionStrategy
{
/// <summary>
/// Validates whether <paramref name="action"/> can be performed by <paramref name="source"/>
/// on <paramref name="target"/>. Called each frame during drag to determine visual feedback.
/// </summary>
bool Check(ActionCharacter action, CharacterCardNode source, CharacterCardNode target);
/// <summary>
/// Applies visual highlighting to both the <paramref name="actionCard"/> being dragged
/// and the <paramref name="target"/> character card (e.g. red outline for attack, green for support).
/// </summary>
void HighlightTarget(ActionCardNode actionCard, CharacterCardNode target);
/// <summary>
/// Executes the action via <see cref="ActionExecutorHub"/> and spawns floating damage/defence text.
/// Called on successful card drop by <see cref="CardController.OnEndDrag"/>.
/// </summary>
void Execute(CharacterCardNode source, CharacterCardNode target, ActionCharacter action);
}
}
+49 -49
View File
@@ -1,49 +1,49 @@
using Cthangover.CardBattle.UI;
using Cthangover.Core.Battle.Actions;
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.Player
{
/// <summary>
/// Strategy for stun actions targeting enemies. Extends the standard <c>ToEnemy</c> check
/// by also verifying that the action has at least 2 turns of stun duration
/// (<c>"Turn" > 1</c>). Highlights with red attack overlays and applies
/// the stun effect via <see cref="ActionExecutorHub"/> without spawning damage text
/// (stun has no numerical effect to display).
/// Not currently returned by <see cref="CardActionStrategyFactory"/> — available for
/// future use when stun cards need distinct drag-and-drop behavior from attack cards.
/// </summary>
public class StunToEnemyStrategy : ICardActionStrategy
{
/// <summary>
/// Returns <c>true</c> when the action targets an enemy and has a turn count greater than 1.
/// </summary>
public bool Check(ActionCharacter action, CharacterCardNode source, CharacterCardNode target)
{
return action.Type == ActionCharacterType.ToEnemy && target != null && !target.IsPlayer
&& action.GetInt("Turn", 0) > 1;
}
/// <summary>
/// Shows a red attack overlay on both cards, same as <see cref="AttackToEnemyStrategy"/>.
/// </summary>
public void HighlightTarget(ActionCardNode actionCard, CharacterCardNode target)
{
actionCard.Attack();
target.Attack();
}
/// <summary>
/// Applies the stun via <see cref="ActionExecutorHub"/>. Does not spawn damage/defence text.
/// </summary>
public void Execute(CharacterCardNode source, CharacterCardNode target, ActionCharacter action)
{
if (source?.Card == null || target?.Card == null || action == null)
return;
ActionExecutorHub.Instance.Execute(action, source.Card, target.Card);
source.UpdateInfo();
target.UpdateInfo();
}
}
}
using Cthangover.CardBattle.UI;
using Cthangover.Core.Battle.Actions;
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.Player
{
/// <summary>
/// Strategy for stun actions targeting enemies. Extends the standard <c>ToEnemy</c> check
/// by also verifying that the action has at least 2 turns of stun duration
/// (<c>"Turn" > 1</c>). Highlights with red attack overlays and applies
/// the stun effect via <see cref="ActionExecutorHub"/> without spawning damage text
/// (stun has no numerical effect to display).
/// Not currently returned by <see cref="CardActionStrategyFactory"/> — available for
/// future use when stun cards need distinct drag-and-drop behavior from attack cards.
/// </summary>
public class StunToEnemyStrategy : ICardActionStrategy
{
/// <summary>
/// Returns <c>true</c> when the action targets an enemy and has a turn count greater than 1.
/// </summary>
public bool Check(ActionCharacter action, CharacterCardNode source, CharacterCardNode target)
{
return action.Type == ActionCharacterType.ToEnemy && target != null && !target.IsPlayer
&& action.GetInt("Turn", 0) > 1;
}
/// <summary>
/// Shows a red attack overlay on both cards, same as <see cref="AttackToEnemyStrategy"/>.
/// </summary>
public void HighlightTarget(ActionCardNode actionCard, CharacterCardNode target)
{
actionCard.Attack();
target.Attack();
}
/// <summary>
/// Applies the stun via <see cref="ActionExecutorHub"/>. Does not spawn damage/defence text.
/// </summary>
public void Execute(CharacterCardNode source, CharacterCardNode target, ActionCharacter action)
{
if (source?.Card == null || target?.Card == null || action == null)
return;
ActionExecutorHub.Instance.Execute(action, source.Card, target.Card);
source.UpdateInfo();
target.UpdateInfo();
}
}
}
+21 -21
View File
@@ -1,21 +1,21 @@
namespace Cthangover.CardBattle.StatusEffect
{
/// <summary>
/// Interface exposed by <see cref="StatusEffectQueue"/> to <see cref="IStatusEffect"/> instances
/// so that status effects can communicate back to the queue during turn lifecycle callbacks.
/// <c>SkipTurn</c> allows an effect (e.g. stun) to mark the character's entire turn as skipped.
/// <c>RemoveStatus</c> allows an effect to self-remove from the queue.
/// </summary>
public interface IStatusActions
{
/// <summary>
/// Marks the character's current turn as skipped. Called by <see cref="StunStatusEffect.OnTurnStart"/>.
/// </summary>
void SkipTurn();
/// <summary>
/// Removes the specified status effect from the queue. Called by effects that want to
/// self-terminate before their turn count expires.
/// </summary>
void RemoveStatus(IStatusEffect status);
}
}
namespace Cthangover.CardBattle.StatusEffect
{
/// <summary>
/// Interface exposed by <see cref="StatusEffectQueue"/> to <see cref="IStatusEffect"/> instances
/// so that status effects can communicate back to the queue during turn lifecycle callbacks.
/// <c>SkipTurn</c> allows an effect (e.g. stun) to mark the character's entire turn as skipped.
/// <c>RemoveStatus</c> allows an effect to self-remove from the queue.
/// </summary>
public interface IStatusActions
{
/// <summary>
/// Marks the character's current turn as skipped. Called by <see cref="StunStatusEffect.OnTurnStart"/>.
/// </summary>
void SkipTurn();
/// <summary>
/// Removes the specified status effect from the queue. Called by effects that want to
/// self-terminate before their turn count expires.
/// </summary>
void RemoveStatus(IStatusEffect status);
}
}
+26 -26
View File
@@ -1,26 +1,26 @@
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.StatusEffect
{
/// <summary>
/// Defines a status effect that is applied to a <see cref="Character"/> during battle
/// and processed each turn by <see cref="StatusEffectQueue"/>. Effects receive callbacks
/// at turn start and turn end, and self-report expiration via <see cref="IsExpired"/>
/// when their <see cref="Turns"/> counter reaches zero. The <see cref="IStatusActions"/>
/// callback parameter allows effects to influence the character's turn (e.g. skip it).
/// </summary>
public interface IStatusEffect
{
/// <summary>Integer identifier for the effect type (1 = stun). Used for serialization.</summary>
int Type { get; }
/// <summary>Remaining turn count before expiry. Decremented by <see cref="StatusEffectQueue"/>.</summary>
int Turns { get; set; }
/// <summary>Returns <c>true</c> when <see cref="Turns"/> ≤ 0, signalling removal from the queue.</summary>
bool IsExpired { get; }
/// <summary>Called by <see cref="StatusEffectQueue.OnTurnStart"/> before the turn counter is decremented.</summary>
void OnTurnStart(Character character, IStatusActions actions);
/// <summary>Called by <see cref="StatusEffectQueue.OnTurnEnd"/> after the character's actions have resolved.</summary>
void OnTurnEnd(Character character, IStatusActions actions);
}
}
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.StatusEffect
{
/// <summary>
/// Defines a status effect that is applied to a <see cref="Character"/> during battle
/// and processed each turn by <see cref="StatusEffectQueue"/>. Effects receive callbacks
/// at turn start and turn end, and self-report expiration via <see cref="IsExpired"/>
/// when their <see cref="Turns"/> counter reaches zero. The <see cref="IStatusActions"/>
/// callback parameter allows effects to influence the character's turn (e.g. skip it).
/// </summary>
public interface IStatusEffect
{
/// <summary>Integer identifier for the effect type (1 = stun). Used for serialization.</summary>
int Type { get; }
/// <summary>Remaining turn count before expiry. Decremented by <see cref="StatusEffectQueue"/>.</summary>
int Turns { get; set; }
/// <summary>Returns <c>true</c> when <see cref="Turns"/> ≤ 0, signalling removal from the queue.</summary>
bool IsExpired { get; }
/// <summary>Called by <see cref="StatusEffectQueue.OnTurnStart"/> before the turn counter is decremented.</summary>
void OnTurnStart(Character character, IStatusActions actions);
/// <summary>Called by <see cref="StatusEffectQueue.OnTurnEnd"/> after the character's actions have resolved.</summary>
void OnTurnEnd(Character character, IStatusActions actions);
}
}
+27 -27
View File
@@ -1,27 +1,27 @@
namespace Cthangover.CardBattle.StatusEffect
{
/// <summary>
/// Data-only container for a status effect's serializable fields (<see cref="Type"/> and <see cref="Turns"/>).
/// Used in <see cref="StatusEffectQueue.Copy"/> to deep-clone status effect state when a character
/// is copied for battle. Cast to <see cref="IStatusEffect"/> when added back to a queue.
/// </summary>
public class StatusEffectItem
{
/// <summary>Effect type identifier (matches <see cref="IStatusEffect.Type"/>).</summary>
public int Type { get; set; }
/// <summary>Remaining turns before expiry.</summary>
public int Turns { get; set; }
/// <summary>
/// Creates a field-by-field copy for deep-cloning during character duplication.
/// </summary>
public StatusEffectItem Copy()
{
return new StatusEffectItem
{
Type = Type,
Turns = Turns
};
}
}
}
namespace Cthangover.CardBattle.StatusEffect
{
/// <summary>
/// Data-only container for a status effect's serializable fields (<see cref="Type"/> and <see cref="Turns"/>).
/// Used in <see cref="StatusEffectQueue.Copy"/> to deep-clone status effect state when a character
/// is copied for battle. Cast to <see cref="IStatusEffect"/> when added back to a queue.
/// </summary>
public class StatusEffectItem
{
/// <summary>Effect type identifier (matches <see cref="IStatusEffect.Type"/>).</summary>
public int Type { get; set; }
/// <summary>Remaining turns before expiry.</summary>
public int Turns { get; set; }
/// <summary>
/// Creates a field-by-field copy for deep-cloning during character duplication.
/// </summary>
public StatusEffectItem Copy()
{
return new StatusEffectItem
{
Type = Type,
Turns = Turns
};
}
}
}
+103 -103
View File
@@ -1,103 +1,103 @@
using System.Collections.Generic;
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.StatusEffect
{
/// <summary>
/// Manages the collection of <see cref="IStatusEffect"/> instances attached to a single <see cref="Character"/>.
/// Implements <see cref="IStatusActions"/> so that individual effects can call back into the queue
/// (e.g. to skip the turn). During <see cref="OnTurnStart"/>, each effect's callback is invoked and
/// its turn counter is decremented; expired effects are removed. <see cref="SkipTurn"/> is checked by
/// <see cref="CardBattleCore.RunEnemyTurn"/> to skip stunned enemies.
/// Supports deep copying for character duplication via <see cref="Copy"/>.
/// </summary>
public class StatusEffectQueue : IStatusActions
{
private readonly Character _character;
private readonly List<IStatusEffect> _effects = new();
/// <summary>
/// Set to <c>true</c> when an effect calls <see cref="IStatusActions.SkipTurn"/> during <see cref="OnTurnStart"/>.
/// Reset to <c>false</c> at the start of each turn before effects are processed.
/// </summary>
public bool SkipTurn { get; private set; }
/// <summary>
/// Creates a new queue bound to the given <paramref name="character"/>.
/// </summary>
public StatusEffectQueue(Character character)
{
_character = character;
}
/// <summary>
/// Creates a deep copy of this queue bound to <paramref name="newCharacter"/>.
/// <see cref="StatusEffectItem"/> effects are field-copied; other effect types are shared by reference.
/// </summary>
public StatusEffectQueue Copy(Character newCharacter)
{
var clone = new StatusEffectQueue(newCharacter);
foreach (var effect in _effects)
{
if (effect is StatusEffectItem item)
clone._effects.Add(item.Copy() as IStatusEffect);
else
clone._effects.Add(effect);
}
return clone;
}
/// <summary>
/// Adds a new status effect to the end of the processing list. Effects are processed
/// in reverse order during turn callbacks (last-added first).
/// </summary>
public void AddEffect(IStatusEffect effect)
{
_effects.Add(effect);
}
/// <summary>
/// Processes all effects at the start of the character's turn: resets <see cref="SkipTurn"/>,
/// calls <see cref="IStatusEffect.OnTurnStart"/> for each effect in reverse order,
/// decrements the turn counter, and removes expired effects.
/// Called by <see cref="CardBattleCore.StartPlayerTurn"/> and <see cref="CardBattleCore.RunEnemyTurn"/>.
/// </summary>
public void OnTurnStart()
{
SkipTurn = false;
for (int i = _effects.Count - 1; i >= 0; i--)
{
var effect = _effects[i];
effect.OnTurnStart(_character, this);
effect.Turns--;
if (effect.IsExpired)
_effects.RemoveAt(i);
}
}
/// <summary>
/// Processes all effects at the end of the character's turn: calls
/// <see cref="IStatusEffect.OnTurnEnd"/> in reverse order and removes expired effects.
/// </summary>
public void OnTurnEnd()
{
for (int i = _effects.Count - 1; i >= 0; i--)
{
var effect = _effects[i];
effect.OnTurnEnd(_character, this);
if (effect.IsExpired)
_effects.RemoveAt(i);
}
}
void IStatusActions.SkipTurn()
{
SkipTurn = true;
}
void IStatusActions.RemoveStatus(IStatusEffect status)
{
_effects.Remove(status);
}
}
}
using System.Collections.Generic;
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.StatusEffect
{
/// <summary>
/// Manages the collection of <see cref="IStatusEffect"/> instances attached to a single <see cref="Character"/>.
/// Implements <see cref="IStatusActions"/> so that individual effects can call back into the queue
/// (e.g. to skip the turn). During <see cref="OnTurnStart"/>, each effect's callback is invoked and
/// its turn counter is decremented; expired effects are removed. <see cref="SkipTurn"/> is checked by
/// <see cref="CardBattleCore.RunEnemyTurn"/> to skip stunned enemies.
/// Supports deep copying for character duplication via <see cref="Copy"/>.
/// </summary>
public class StatusEffectQueue : IStatusActions
{
private readonly Character _character;
private readonly List<IStatusEffect> _effects = new();
/// <summary>
/// Set to <c>true</c> when an effect calls <see cref="IStatusActions.SkipTurn"/> during <see cref="OnTurnStart"/>.
/// Reset to <c>false</c> at the start of each turn before effects are processed.
/// </summary>
public bool SkipTurn { get; private set; }
/// <summary>
/// Creates a new queue bound to the given <paramref name="character"/>.
/// </summary>
public StatusEffectQueue(Character character)
{
_character = character;
}
/// <summary>
/// Creates a deep copy of this queue bound to <paramref name="newCharacter"/>.
/// <see cref="StatusEffectItem"/> effects are field-copied; other effect types are shared by reference.
/// </summary>
public StatusEffectQueue Copy(Character newCharacter)
{
var clone = new StatusEffectQueue(newCharacter);
foreach (var effect in _effects)
{
if (effect is StatusEffectItem item)
clone._effects.Add(item.Copy() as IStatusEffect);
else
clone._effects.Add(effect);
}
return clone;
}
/// <summary>
/// Adds a new status effect to the end of the processing list. Effects are processed
/// in reverse order during turn callbacks (last-added first).
/// </summary>
public void AddEffect(IStatusEffect effect)
{
_effects.Add(effect);
}
/// <summary>
/// Processes all effects at the start of the character's turn: resets <see cref="SkipTurn"/>,
/// calls <see cref="IStatusEffect.OnTurnStart"/> for each effect in reverse order,
/// decrements the turn counter, and removes expired effects.
/// Called by <see cref="CardBattleCore.StartPlayerTurn"/> and <see cref="CardBattleCore.RunEnemyTurn"/>.
/// </summary>
public void OnTurnStart()
{
SkipTurn = false;
for (int i = _effects.Count - 1; i >= 0; i--)
{
var effect = _effects[i];
effect.OnTurnStart(_character, this);
effect.Turns--;
if (effect.IsExpired)
_effects.RemoveAt(i);
}
}
/// <summary>
/// Processes all effects at the end of the character's turn: calls
/// <see cref="IStatusEffect.OnTurnEnd"/> in reverse order and removes expired effects.
/// </summary>
public void OnTurnEnd()
{
for (int i = _effects.Count - 1; i >= 0; i--)
{
var effect = _effects[i];
effect.OnTurnEnd(_character, this);
if (effect.IsExpired)
_effects.RemoveAt(i);
}
}
void IStatusActions.SkipTurn()
{
SkipTurn = true;
}
void IStatusActions.RemoveStatus(IStatusEffect status)
{
_effects.Remove(status);
}
}
}
+52 -52
View File
@@ -1,52 +1,52 @@
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.StatusEffect
{
/// <summary>
/// Status effect that causes a character to skip their entire turn.
/// On <see cref="OnTurnStart"/>, calls <see cref="IStatusActions.SkipTurn"/> which sets
/// <see cref="StatusEffectQueue.SkipTurn"/> to <c>true</c>. <see cref="CardBattleCore.RunEnemyTurn"/>
/// checks this flag and skips the character's action selection and resolution entirely.
/// Created by <see cref="PhysicsStunActionCard"/> when a stun card is played,
/// and added to the target's <see cref="StatusEffectQueue"/> via <c>Add("effect/physics/stun", turns)</c>.
/// </summary>
public class StunStatusEffect : IStatusEffect
{
/// <summary>Type identifier for stun (1).</summary>
public int Type => 1;
/// <summary>Number of turns this stun remains active.</summary>
public int Turns { get; set; }
/// <summary>Expired when the turn counter reaches zero or below.</summary>
public bool IsExpired => Turns <= 0;
/// <summary>
/// Creates a stun effect lasting for <paramref name="turns"/> turns.
/// </summary>
public StunStatusEffect(int turns)
{
Turns = turns;
}
/// <summary>
/// Parameterless constructor for deserialization. Sets <see cref="Turns"/> to 0 (expired immediately).
/// </summary>
public StunStatusEffect()
{
}
/// <summary>
/// Calls <see cref="IStatusActions.SkipTurn"/> to mark the character's turn as skipped.
/// </summary>
public void OnTurnStart(Character character, IStatusActions actions)
{
actions.SkipTurn();
}
/// <summary>
/// Stun has no end-of-turn effect.
/// </summary>
public void OnTurnEnd(Character character, IStatusActions actions)
{
}
}
}
using Cthangover.Core.Characters;
namespace Cthangover.CardBattle.StatusEffect
{
/// <summary>
/// Status effect that causes a character to skip their entire turn.
/// On <see cref="OnTurnStart"/>, calls <see cref="IStatusActions.SkipTurn"/> which sets
/// <see cref="StatusEffectQueue.SkipTurn"/> to <c>true</c>. <see cref="CardBattleCore.RunEnemyTurn"/>
/// checks this flag and skips the character's action selection and resolution entirely.
/// Created by <see cref="PhysicsStunActionCard"/> when a stun card is played,
/// and added to the target's <see cref="StatusEffectQueue"/> via <c>Add("effect/physics/stun", turns)</c>.
/// </summary>
public class StunStatusEffect : IStatusEffect
{
/// <summary>Type identifier for stun (1).</summary>
public int Type => 1;
/// <summary>Number of turns this stun remains active.</summary>
public int Turns { get; set; }
/// <summary>Expired when the turn counter reaches zero or below.</summary>
public bool IsExpired => Turns <= 0;
/// <summary>
/// Creates a stun effect lasting for <paramref name="turns"/> turns.
/// </summary>
public StunStatusEffect(int turns)
{
Turns = turns;
}
/// <summary>
/// Parameterless constructor for deserialization. Sets <see cref="Turns"/> to 0 (expired immediately).
/// </summary>
public StunStatusEffect()
{
}
/// <summary>
/// Calls <see cref="IStatusActions.SkipTurn"/> to mark the character's turn as skipped.
/// </summary>
public void OnTurnStart(Character character, IStatusActions actions)
{
actions.SkipTurn();
}
/// <summary>
/// Stun has no end-of-turn effect.
/// </summary>
public void OnTurnEnd(Character character, IStatusActions actions)
{
}
}
}
+267 -267
View File
@@ -1,269 +1,269 @@
using System.Collections.Generic;
using System.Linq;
using Cthangover.Core.Characters;
using Cthangover.Core.Scenes;
using Cthangover.Core.UI;
using Cthangover.Core.UI.Theming;
using Godot;
using System.Collections.Generic;
using System.Linq;
using Cthangover.Core.Characters;
using Cthangover.Core.Scenes;
using Cthangover.Core.UI;
using Cthangover.Core.UI.Theming;
using Godot;
using Cthangover.Mods.Interface.Base.Lists;
using Cthangover.Mods.Interface.View;
namespace Cthangover.CardBattle.UI
{
/// <summary>
/// Container panel that manages a list of <see cref="CharacterCardNode"/> instances for one side
/// of the battle (player or enemy). Handles card creation, multi-row layout when enemy count exceeds
/// <c>MAX_CARDS_ROW</c> (10 per row), smooth animated repositioning, health-triggered death,
/// and the <see cref="CardDeathAnimation"/> dissolve sequence.
/// Two instances are created by <see cref="CardBattleCore.Init"/>: a left-aligned player panel
/// and a right-aligned enemy panel.
/// </summary>
public partial class BattleCardPanel : ModWidget
{
private const int MAX_CARDS_ROW = 10;
private const float BASE_CARD_HEIGHT = 280f;
[Export] private Control content;
/// <summary>
/// Controls card alignment within the panel. <c>"Left"</c> for the player side,
/// <c>"Right"</c> for the enemy side. Affects <see cref="CalcTargetPosition"/>.
/// </summary>
[Export] public string AlignType { get; set; } = "Left";
[Export] private float cellOffset = 10f;
/// <summary>
/// All character cards currently managed by this panel. Populated by <see cref="Init"/>
/// and modified by <see cref="Dead"/> (removal) and <see cref="ClearAll"/> (full clear).
/// </summary>
public List<CharacterCardNode> CardList { get; } = new();
private Dictionary<CharacterCardNode, Tween> activeTweens = new();
/// <summary>
/// When <c>true</c>, cards are laid out in multiple rows (up to 3) instead of a single row.
/// Set by <see cref="CardBattleCore.Start"/> when enemy count exceeds 6.
/// </summary>
public bool UseRows { get; set; }
/// <summary>
/// Fired when a card's death animation completes. <see cref="CardBattleCore"/> subscribes
/// to record the defeated character in <see cref="BattleSceneContext"/>.
/// </summary>
public event System.Action<CharacterCardNode> OnCardDead;
/// <summary>Builds the card grid container rows for one battle side.</summary>
protected override void Construct()
{
CthangoverTheme.Apply(this);
content ??= GetNodeOrNull<Control>("Content");
}
/// <summary>
/// Clears the current card list and creates new <see cref="CharacterCardNode"/> instances
/// for each <paramref name="cards"/> entry. Sets player/enemy team colors and subscribes
/// the health-change handler. Returns the created card list.
/// Called by <see cref="CardBattleCore.Start"/> with the full character roster for each side.
/// </summary>
public List<CharacterCardNode> Init(IEnumerable<Character> cards, bool isPlayer, float cardScale)
{
CardList.Clear();
activeTweens.Clear();
int idx = 0;
foreach (var card in cards)
{
CardList.Add(Create(card, isPlayer, cardScale));
idx++;
}
Redraw(animate: false);
return CardList;
}
/// <summary>
/// Instantiates a single <see cref="CharacterCardNode"/>, adds it to the content panel,
/// sets its scale, initializes it with card data, applies team colors, and hooks
/// the health-change event. Called by <see cref="Init"/> for each character.
/// </summary>
public CharacterCardNode Create(Character card, bool isPlayer, float cardScale)
{
var behaviour = (CharacterCardNode)TscnScenes.LoadAndBuild("scenes/character_card.tscn");
behaviour.EnsureConstructed();
if (content != null)
content.AddChild(behaviour);
else
AddChild(behaviour);
behaviour.Scale = new Vector2(cardScale, cardScale);
behaviour.Init(card);
behaviour.IsPlayer = isPlayer;
behaviour.SetTeamColors();
if (card != null)
card.Attributes.Health.OnChange += CheckHealthAttribute;
return behaviour;
}
private void CheckHealthAttribute(float value, float basevalue)
{
if (value > 0)
return;
foreach (var cardItem in CardList.ToList())
{
if (cardItem.Card.Attributes.Health.Value <= 0)
Dead(cardItem);
}
}
/// <summary>
/// Frees all card nodes from the scene tree, clears the card list, and triggers a redraw.
/// Called by <see cref="CardBattleCore.OnBattleCleared"/> when the battle scene is cleaned up.
/// </summary>
public void ClearAll()
{
foreach (var card in CardList.ToList())
{
if (GodotObject.IsInstanceValid(card))
card.QueueFree();
}
CardList.Clear();
activeTweens.Clear();
Redraw(animate: false);
}
/// <summary>
/// Recalculates and applies positions for all cards in the list. When <paramref name="animate"/>
/// is <c>true</c>, cards tween to their new positions with a cubic ease-out; otherwise they
/// snap instantly. Called after cards are added, removed, or when a selection/unselection
/// changes the sort order.
/// </summary>
public void Redraw(bool animate = true)
{
for (int i = 0; i < CardList.Count; i++)
{
var card = CardList[i];
if (card == null)
continue;
var (row, col) = GetRowCol(i);
SetCardPosition(card, i, row, col, animate);
}
}
private (int row, int col) GetRowCol(int index)
{
if (!UseRows || index < MAX_CARDS_ROW)
return (0, index);
if (index < MAX_CARDS_ROW * 2)
return (1, index - MAX_CARDS_ROW);
return (2, index - MAX_CARDS_ROW * 2);
}
private int CalcBaseZIndex(int row, int col)
{
return row * 20 + col;
}
private Vector2 CalcTargetPosition(CharacterCardNode card, int row, int col)
{
var panelSize = Size;
float effectiveWidth = card.Size.X * card.Scale.X;
float effectiveHeight = card.Size.Y * card.Scale.Y;
float y = row * (effectiveHeight + cellOffset);
if (AlignType == "Left")
return new Vector2((cellOffset + effectiveWidth) * col, y);
else
return new Vector2(panelSize.X - (cellOffset + effectiveWidth) * (col + 1), y);
}
/// <summary>
/// Calculates how many rows are needed to display <paramref name="enemyCount"/> enemies
/// when <see cref="UseRows"/> is enabled. Returns 1, 2, or 3.
/// Called by <see cref="CardBattleCore.CalculateCardScale"/> to determine maximum card scale.
/// </summary>
public int GetCurrentRowCount(int enemyCount)
{
if (!UseRows)
return 1;
if (enemyCount <= MAX_CARDS_ROW)
return 1;
if (enemyCount <= MAX_CARDS_ROW * 2)
return 2;
return 3;
}
/// <summary>
/// Returns the maximum cards per row constant (<c>MAX_CARDS_ROW = 10</c>).
/// Used by <see cref="CardBattleCore.CalculateCardScale"/> for scale calculations.
/// </summary>
public int GetMaxCardsPerRow()
{
return MAX_CARDS_ROW;
}
private void SetCardPosition(CharacterCardNode card, int index, int row, int col, bool animate)
{
if (card == null)
return;
var targetPos = CalcTargetPosition(card, row, col);
if (activeTweens.TryGetValue(card, out var prevTween) && prevTween != null && prevTween.IsValid())
prevTween.Kill();
if (animate)
{
var tween = CreateTween();
tween.SetEase(Tween.EaseType.Out).SetTrans(Tween.TransitionType.Cubic);
tween.TweenProperty(card, "position", targetPos, 0.3f);
activeTweens[card] = tween;
}
else
{
card.Position = targetPos;
activeTweens.Remove(card);
}
var baseZ = CalcBaseZIndex(row, col);
card.SetBaseZIndex(baseZ);
if (content != null)
content.MoveChild(card, index);
else
MoveChild(card, index);
card.UpdateInfo();
}
/// <summary>
/// Marks the card as dead and starts the <see cref="CardDeathAnimation"/> dissolve sequence.
/// Once the animation completes, the card is removed from <see cref="CardList"/>,
/// <see cref="OnCardDead"/> is fired, and the remaining cards are redrawn.
/// Triggered by the health-change handler when a character's health reaches zero.
/// </summary>
public void Dead(CharacterCardNode characterCardNode)
{
if (characterCardNode == null || characterCardNode.IsDead)
return;
characterCardNode.IsDead = true;
var animator = new Cthangover.CardBattle.UI.CardDeathAnimation();
characterCardNode.AddChild(animator);
animator.StartAnimation(characterCardNode, () =>
{
OnCardDead?.Invoke(characterCardNode);
CardList.Remove(characterCardNode);
activeTweens.Remove(characterCardNode);
Redraw();
});
}
}
}
namespace Cthangover.CardBattle.UI
{
/// <summary>
/// Container panel that manages a list of <see cref="CharacterCardNode"/> instances for one side
/// of the battle (player or enemy). Handles card creation, multi-row layout when enemy count exceeds
/// <c>MAX_CARDS_ROW</c> (10 per row), smooth animated repositioning, health-triggered death,
/// and the <see cref="CardDeathAnimation"/> dissolve sequence.
/// Two instances are created by <see cref="CardBattleCore.Init"/>: a left-aligned player panel
/// and a right-aligned enemy panel.
/// </summary>
public partial class BattleCardPanel : ModWidget
{
private const int MAX_CARDS_ROW = 10;
private const float BASE_CARD_HEIGHT = 280f;
[Export] private Control content;
/// <summary>
/// Controls card alignment within the panel. <c>"Left"</c> for the player side,
/// <c>"Right"</c> for the enemy side. Affects <see cref="CalcTargetPosition"/>.
/// </summary>
[Export] public string AlignType { get; set; } = "Left";
[Export] private float cellOffset = 10f;
/// <summary>
/// All character cards currently managed by this panel. Populated by <see cref="Init"/>
/// and modified by <see cref="Dead"/> (removal) and <see cref="ClearAll"/> (full clear).
/// </summary>
public List<CharacterCardNode> CardList { get; } = new();
private Dictionary<CharacterCardNode, Tween> activeTweens = new();
/// <summary>
/// When <c>true</c>, cards are laid out in multiple rows (up to 3) instead of a single row.
/// Set by <see cref="CardBattleCore.Start"/> when enemy count exceeds 6.
/// </summary>
public bool UseRows { get; set; }
/// <summary>
/// Fired when a card's death animation completes. <see cref="CardBattleCore"/> subscribes
/// to record the defeated character in <see cref="BattleSceneContext"/>.
/// </summary>
public event System.Action<CharacterCardNode> OnCardDead;
/// <summary>Builds the card grid container rows for one battle side.</summary>
protected override void Construct()
{
CthangoverTheme.Apply(this);
content ??= GetNodeOrNull<Control>("Content");
}
/// <summary>
/// Clears the current card list and creates new <see cref="CharacterCardNode"/> instances
/// for each <paramref name="cards"/> entry. Sets player/enemy team colors and subscribes
/// the health-change handler. Returns the created card list.
/// Called by <see cref="CardBattleCore.Start"/> with the full character roster for each side.
/// </summary>
public List<CharacterCardNode> Init(IEnumerable<Character> cards, bool isPlayer, float cardScale)
{
CardList.Clear();
activeTweens.Clear();
int idx = 0;
foreach (var card in cards)
{
CardList.Add(Create(card, isPlayer, cardScale));
idx++;
}
Redraw(animate: false);
return CardList;
}
/// <summary>
/// Instantiates a single <see cref="CharacterCardNode"/>, adds it to the content panel,
/// sets its scale, initializes it with card data, applies team colors, and hooks
/// the health-change event. Called by <see cref="Init"/> for each character.
/// </summary>
public CharacterCardNode Create(Character card, bool isPlayer, float cardScale)
{
var behaviour = (CharacterCardNode)TscnScenes.LoadAndBuild("scenes/character_card.tscn");
behaviour.EnsureConstructed();
if (content != null)
content.AddChild(behaviour);
else
AddChild(behaviour);
behaviour.Scale = new Vector2(cardScale, cardScale);
behaviour.Init(card);
behaviour.IsPlayer = isPlayer;
behaviour.SetTeamColors();
if (card != null)
card.Attributes.Health.OnChange += CheckHealthAttribute;
return behaviour;
}
private void CheckHealthAttribute(float value, float basevalue)
{
if (value > 0)
return;
foreach (var cardItem in CardList.ToList())
{
if (cardItem.Card.Attributes.Health.Value <= 0)
Dead(cardItem);
}
}
/// <summary>
/// Frees all card nodes from the scene tree, clears the card list, and triggers a redraw.
/// Called by <see cref="CardBattleCore.OnBattleCleared"/> when the battle scene is cleaned up.
/// </summary>
public void ClearAll()
{
foreach (var card in CardList.ToList())
{
if (GodotObject.IsInstanceValid(card))
card.QueueFree();
}
CardList.Clear();
activeTweens.Clear();
Redraw(animate: false);
}
/// <summary>
/// Recalculates and applies positions for all cards in the list. When <paramref name="animate"/>
/// is <c>true</c>, cards tween to their new positions with a cubic ease-out; otherwise they
/// snap instantly. Called after cards are added, removed, or when a selection/unselection
/// changes the sort order.
/// </summary>
public void Redraw(bool animate = true)
{
for (int i = 0; i < CardList.Count; i++)
{
var card = CardList[i];
if (card == null)
continue;
var (row, col) = GetRowCol(i);
SetCardPosition(card, i, row, col, animate);
}
}
private (int row, int col) GetRowCol(int index)
{
if (!UseRows || index < MAX_CARDS_ROW)
return (0, index);
if (index < MAX_CARDS_ROW * 2)
return (1, index - MAX_CARDS_ROW);
return (2, index - MAX_CARDS_ROW * 2);
}
private int CalcBaseZIndex(int row, int col)
{
return row * 20 + col;
}
private Vector2 CalcTargetPosition(CharacterCardNode card, int row, int col)
{
var panelSize = Size;
float effectiveWidth = card.Size.X * card.Scale.X;
float effectiveHeight = card.Size.Y * card.Scale.Y;
float y = row * (effectiveHeight + cellOffset);
if (AlignType == "Left")
return new Vector2((cellOffset + effectiveWidth) * col, y);
else
return new Vector2(panelSize.X - (cellOffset + effectiveWidth) * (col + 1), y);
}
/// <summary>
/// Calculates how many rows are needed to display <paramref name="enemyCount"/> enemies
/// when <see cref="UseRows"/> is enabled. Returns 1, 2, or 3.
/// Called by <see cref="CardBattleCore.CalculateCardScale"/> to determine maximum card scale.
/// </summary>
public int GetCurrentRowCount(int enemyCount)
{
if (!UseRows)
return 1;
if (enemyCount <= MAX_CARDS_ROW)
return 1;
if (enemyCount <= MAX_CARDS_ROW * 2)
return 2;
return 3;
}
/// <summary>
/// Returns the maximum cards per row constant (<c>MAX_CARDS_ROW = 10</c>).
/// Used by <see cref="CardBattleCore.CalculateCardScale"/> for scale calculations.
/// </summary>
public int GetMaxCardsPerRow()
{
return MAX_CARDS_ROW;
}
private void SetCardPosition(CharacterCardNode card, int index, int row, int col, bool animate)
{
if (card == null)
return;
var targetPos = CalcTargetPosition(card, row, col);
if (activeTweens.TryGetValue(card, out var prevTween) && prevTween != null && prevTween.IsValid())
prevTween.Kill();
if (animate)
{
var tween = CreateTween();
tween.SetEase(Tween.EaseType.Out).SetTrans(Tween.TransitionType.Cubic);
tween.TweenProperty(card, "position", targetPos, 0.3f);
activeTweens[card] = tween;
}
else
{
card.Position = targetPos;
activeTweens.Remove(card);
}
var baseZ = CalcBaseZIndex(row, col);
card.SetBaseZIndex(baseZ);
if (content != null)
content.MoveChild(card, index);
else
MoveChild(card, index);
card.UpdateInfo();
}
/// <summary>
/// Marks the card as dead and starts the <see cref="CardDeathAnimation"/> dissolve sequence.
/// Once the animation completes, the card is removed from <see cref="CardList"/>,
/// <see cref="OnCardDead"/> is fired, and the remaining cards are redrawn.
/// Triggered by the health-change handler when a character's health reaches zero.
/// </summary>
public void Dead(CharacterCardNode characterCardNode)
{
if (characterCardNode == null || characterCardNode.IsDead)
return;
characterCardNode.IsDead = true;
var animator = new Cthangover.CardBattle.UI.CardDeathAnimation();
characterCardNode.AddChild(animator);
animator.StartAnimation(characterCardNode, () =>
{
OnCardDead?.Invoke(characterCardNode);
CardList.Remove(characterCardNode);
activeTweens.Remove(characterCardNode);
Redraw();
});
}
}
}
+101 -101
View File
@@ -1,101 +1,101 @@
using System;
using Cthangover.Core.Mods;
using Cthangover.Core.Mods.Resolvers;
using Cthangover.Core.UI;
using Godot;
namespace Cthangover.CardBattle.UI
{
/// <summary>
/// Plays a death animation on a <see cref="CharacterCardNode"/> when its health reaches zero.
/// The card simultaneously fades out, shrinks toward the screen center, spins 360 degrees,
/// and applies a dissolve shader to all its <see cref="CharacterCardNode.AllImages"/> textures.
/// The dissolve shader (<c>"scene_transition"</c>) is resolved once via <see cref="ModManager"/>
/// and cached statically. Created and started by <see cref="BattleCardPanel.Dead"/>.
/// </summary>
public partial class CardDeathAnimation : Node
{
private CharacterCardNode card;
private Vector2 center;
private Action onComplete;
private static Shader dissolveShader;
/// <summary>
/// Begins the death animation sequence. Calculates a target position at the viewport center
/// (relative to the card's parent), starts fading all child <see cref="Label"/> nodes,
/// applies the dissolve shader material to all card images, and tweens position/rotation/alpha.
/// On completion, invokes <paramref name="onComplete"/> and frees this node.
/// </summary>
public void StartAnimation(CharacterCardNode card, Action onComplete)
{
this.card = card;
this.onComplete = onComplete;
var viewportCenter = card.GetViewport().GetVisibleRect().Size / 2;
center = card.GetParent<Control>() is Control parent
? viewportCenter - parent.GlobalPosition
: viewportCenter - card.GlobalPosition + card.Position;
RunAnimation();
}
private void RunAnimation()
{
float duration = 0.8f;
Vector2 endPos = center;
StartFadeOutRecursive(card, duration);
if (dissolveShader == null)
dissolveShader = Shaders.Resolve("scene_transition");
if (dissolveShader != null)
{
foreach (var img in card.AllImages)
{
var mat = new ShaderMaterial { Shader = dissolveShader };
mat.SetShaderParameter("noise_scale", 5f);
mat.SetShaderParameter("smoothness", 0.25f);
mat.SetShaderParameter("distortion", 0.092f);
mat.SetShaderParameter("glow_intensity", 0f);
mat.SetShaderParameter("hue_shift", 0f);
mat.SetShaderParameter("invert_direction", false);
mat.SetShaderParameter("progress", 0f);
img.Material = mat;
var matTween = CreateTween();
matTween.TweenMethod(
Callable.From<float>(t => mat.SetShaderParameter("progress", t)),
0f, 1f, duration);
}
}
var tween = CreateTween();
tween.SetParallel(true);
tween.TweenProperty(card, "position", endPos, duration);
tween.TweenProperty(card, "rotation", Mathf.DegToRad(360f), duration);
tween.TweenProperty(card, "modulate:a", 0f, duration);
tween.Finished += () =>
{
onComplete?.Invoke();
QueueFree();
};
}
private static void StartFadeOutRecursive(Node node, float duration)
{
foreach (Node child in node.GetChildren())
{
if (child is Label label)
{
var t = child.CreateTween();
t.TweenProperty(label, "modulate:a", 0f, duration);
}
StartFadeOutRecursive(child, duration);
}
}
}
}
using System;
using Cthangover.Core.Mods;
using Cthangover.Core.Mods.Resolvers;
using Cthangover.Core.UI;
using Godot;
namespace Cthangover.CardBattle.UI
{
/// <summary>
/// Plays a death animation on a <see cref="CharacterCardNode"/> when its health reaches zero.
/// The card simultaneously fades out, shrinks toward the screen center, spins 360 degrees,
/// and applies a dissolve shader to all its <see cref="CharacterCardNode.AllImages"/> textures.
/// The dissolve shader (<c>"scene_transition"</c>) is resolved once via <see cref="ModManager"/>
/// and cached statically. Created and started by <see cref="BattleCardPanel.Dead"/>.
/// </summary>
public partial class CardDeathAnimation : Node
{
private CharacterCardNode card;
private Vector2 center;
private Action onComplete;
private static Shader dissolveShader;
/// <summary>
/// Begins the death animation sequence. Calculates a target position at the viewport center
/// (relative to the card's parent), starts fading all child <see cref="Label"/> nodes,
/// applies the dissolve shader material to all card images, and tweens position/rotation/alpha.
/// On completion, invokes <paramref name="onComplete"/> and frees this node.
/// </summary>
public void StartAnimation(CharacterCardNode card, Action onComplete)
{
this.card = card;
this.onComplete = onComplete;
var viewportCenter = card.GetViewport().GetVisibleRect().Size / 2;
center = card.GetParent<Control>() is Control parent
? viewportCenter - parent.GlobalPosition
: viewportCenter - card.GlobalPosition + card.Position;
RunAnimation();
}
private void RunAnimation()
{
float duration = 0.8f;
Vector2 endPos = center;
StartFadeOutRecursive(card, duration);
if (dissolveShader == null)
dissolveShader = Shaders.Resolve("scene_transition");
if (dissolveShader != null)
{
foreach (var img in card.AllImages)
{
var mat = new ShaderMaterial { Shader = dissolveShader };
mat.SetShaderParameter("noise_scale", 5f);
mat.SetShaderParameter("smoothness", 0.25f);
mat.SetShaderParameter("distortion", 0.092f);
mat.SetShaderParameter("glow_intensity", 0f);
mat.SetShaderParameter("hue_shift", 0f);
mat.SetShaderParameter("invert_direction", false);
mat.SetShaderParameter("progress", 0f);
img.Material = mat;
var matTween = CreateTween();
matTween.TweenMethod(
Callable.From<float>(t => mat.SetShaderParameter("progress", t)),
0f, 1f, duration);
}
}
var tween = CreateTween();
tween.SetParallel(true);
tween.TweenProperty(card, "position", endPos, duration);
tween.TweenProperty(card, "rotation", Mathf.DegToRad(360f), duration);
tween.TweenProperty(card, "modulate:a", 0f, duration);
tween.Finished += () =>
{
onComplete?.Invoke();
QueueFree();
};
}
private static void StartFadeOutRecursive(Node node, float duration)
{
foreach (Node child in node.GetChildren())
{
if (child is Label label)
{
var t = child.CreateTween();
t.TweenProperty(label, "modulate:a", 0f, duration);
}
StartFadeOutRecursive(child, duration);
}
}
}
}
+47 -47
View File
@@ -1,48 +1,48 @@
using Cthangover.Core.UI;
using Cthangover.Core.UI.Theming;
using Godot;
using Cthangover.Core.UI;
using Cthangover.Core.UI.Theming;
using Godot;
using Cthangover.Mods.Interface.Base.Lists;
namespace Cthangover.CardBattle.UI
{
/// <summary>
/// A styled button widget that allows the player to manually end their turn.
/// Displayed during the player's turn and hidden during enemy turns and battle cleanup.
/// Fires <see cref="OnPressed"/> when clicked, which <see cref="CardBattleCore"/> binds
/// to <c>OnPlayerTurnEnd</c>. The button text is translated via the <c>"battle/end_turn"</c> key.
/// </summary>
public partial class EndTurnButton : ModWidget
{
private Button _button;
/// <summary>
/// Fired when the button is pressed. Subscribed by <see cref="CardBattleCore.OnEndTurnPressed"/>.
/// </summary>
public event System.Action OnPressed;
/// <summary>Builds the end-turn button and binds its pressed event.</summary>
protected override void Construct()
{
CthangoverTheme.Apply(this);
_button = GetNode<Button>("BtnEndTurn");
CthangoverTheme.ApplyPrimary(_button);
Size = CustomMinimumSize;
_button.Pressed += RaisePressed;
}
private void RaisePressed() => OnPressed?.Invoke();
/// <summary>
/// Shows or hides the underlying <see cref="Button"/> control.
/// Called by <see cref="CardBattleCore"/> during turn transitions and battle cleanup.
/// </summary>
public void SetVisible(bool visible)
{
if (_button != null)
_button.Visible = visible;
}
}
}
namespace Cthangover.CardBattle.UI
{
/// <summary>
/// A styled button widget that allows the player to manually end their turn.
/// Displayed during the player's turn and hidden during enemy turns and battle cleanup.
/// Fires <see cref="OnPressed"/> when clicked, which <see cref="CardBattleCore"/> binds
/// to <c>OnPlayerTurnEnd</c>. The button text is translated via the <c>"battle/end_turn"</c> key.
/// </summary>
public partial class EndTurnButton : ModWidget
{
private Button _button;
/// <summary>
/// Fired when the button is pressed. Subscribed by <see cref="CardBattleCore.OnEndTurnPressed"/>.
/// </summary>
public event System.Action OnPressed;
/// <summary>Builds the end-turn button and binds its pressed event.</summary>
protected override void Construct()
{
CthangoverTheme.Apply(this);
_button = GetNode<Button>("BtnEndTurn");
CthangoverTheme.ApplyPrimary(_button);
Size = CustomMinimumSize;
_button.Pressed += RaisePressed;
}
private void RaisePressed() => OnPressed?.Invoke();
/// <summary>
/// Shows or hides the underlying <see cref="Button"/> control.
/// Called by <see cref="CardBattleCore"/> during turn transitions and battle cleanup.
/// </summary>
public void SetVisible(bool visible)
{
if (_button != null)
_button.Visible = visible;
}
}
}