fix: harden gameplay state transitions

This commit is contained in:
Christophe Del Vecchio committed 2026-10-01 12:43:29 +02:00
1 parent 26a204b49c
commit 914e2d5c9b
8 files changed
+314 -68

No files matched your search

+45 -28
View File
@@ -50,7 +50,21 @@ void ACombatEnemy::DoAIComboAttack()
return;
}
// raise the attacking flag
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (!AnimInstance || !ComboAttackMontage)
{
OnAttackCompleted.ExecuteIfBound();
return;
}
const float MontageLength = AnimInstance->Montage_Play(ComboAttackMontage, 1.0f, EMontagePlayReturnType::MontageLength, 0.0f, true);
if (MontageLength <= 0.0f)
{
OnAttackCompleted.ExecuteIfBound();
return;
}
// raise the attacking flag only after the montage starts
bIsAttacking = true;
// choose how many times we're going to attack
@@ -59,18 +73,8 @@ void ACombatEnemy::DoAIComboAttack()
// reset the attack counter
CurrentComboAttack = 0;
// play the attack montage
if (UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance())
{
const float MontageLength = AnimInstance->Montage_Play(ComboAttackMontage, 1.0f, EMontagePlayReturnType::MontageLength, 0.0f, true);
// subscribe to montage completed and interrupted events
if (MontageLength > 0.0f)
{
// set the end delegate for the montage
AnimInstance->Montage_SetEndDelegate(OnAttackMontageEnded, ComboAttackMontage);
}
}
// set the end delegate for the montage
AnimInstance->Montage_SetEndDelegate(OnAttackMontageEnded, ComboAttackMontage);
}
void ACombatEnemy::DoAIChargedAttack()
@@ -81,7 +85,21 @@ void ACombatEnemy::DoAIChargedAttack()
return;
}
// raise the attacking flag
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (!AnimInstance || !ChargedAttackMontage)
{
OnAttackCompleted.ExecuteIfBound();
return;
}
const float MontageLength = AnimInstance->Montage_Play(ChargedAttackMontage, 1.0f, EMontagePlayReturnType::MontageLength, 0.0f, true);
if (MontageLength <= 0.0f)
{
OnAttackCompleted.ExecuteIfBound();
return;
}
// raise the attacking flag only after the montage starts
bIsAttacking = true;
// choose how many loops are we going to charge for
@@ -90,18 +108,8 @@ void ACombatEnemy::DoAIChargedAttack()
// reset the charge loop counter
CurrentChargeLoop = 0;
// play the attack montage
if (UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance())
{
const float MontageLength = AnimInstance->Montage_Play(ChargedAttackMontage, 1.0f, EMontagePlayReturnType::MontageLength, 0.0f, true);
// subscribe to montage completed and interrupted events
if (MontageLength > 0.0f)
{
// set the end delegate for the montage
AnimInstance->Montage_SetEndDelegate(OnAttackMontageEnded, ChargedAttackMontage);
}
}
// set the end delegate for the montage
AnimInstance->Montage_SetEndDelegate(OnAttackMontageEnded, ChargedAttackMontage);
}
void ACombatEnemy::AttackMontageEnded(UAnimMontage* Montage, bool bInterrupted)
@@ -127,6 +135,7 @@ void ACombatEnemy::DoAttackTrace(FName DamageSourceBone)
{
// sweep for objects in front of the character to be hit by the attack
TArray<FHitResult> OutHits;
TSet<AActor*> DamagedActors;
// start at the provided socket location, sweep forward
const FVector TraceStart = GetMesh()->GetSocketLocation(DamageSourceBone);
@@ -149,14 +158,22 @@ void ACombatEnemy::DoAttackTrace(FName DamageSourceBone)
// iterate over each object hit
for (const FHitResult& CurrentHit : OutHits)
{
AActor* HitActor = CurrentHit.GetActor();
if (!HitActor || DamagedActors.Contains(HitActor))
{
continue;
}
/** does the actor have the player tag? */
if (CurrentHit.GetActor()->ActorHasTag(FName("Player")))
if (HitActor->ActorHasTag(FName("Player")))
{
// check if the actor is damageable
ICombatDamageable* Damageable = Cast<ICombatDamageable>(CurrentHit.GetActor());
ICombatDamageable* Damageable = Cast<ICombatDamageable>(HitActor);
if (Damageable)
{
DamagedActors.Add(HitActor);
// knock upwards and away from the impact normal
const FVector Impulse = (CurrentHit.ImpactNormal * -MeleeKnockbackImpulse) + (FVector::UpVector * MeleeLaunchImpulse);
@@ -240,7 +240,7 @@ EStateTreeRunStatus FStateTreeSetCharacterSpeedTask::EnterState(FStateTreeExecut
// set the character's max ground speed
InstanceData.Character->GetCharacterMovement()->MaxWalkSpeed = InstanceData.Speed;
return EStateTreeRunStatus::Running;
return EStateTreeRunStatus::Succeeded;
}
#if WITH_EDITOR
+40 -28
View File
@@ -190,7 +190,19 @@ void ACombatCharacter::ResetHP()
void ACombatCharacter::ComboAttack()
{
// raise the attacking flag
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (!AnimInstance || !ComboAttackMontage)
{
return;
}
const float MontageLength = AnimInstance->Montage_Play(ComboAttackMontage, 1.0f, EMontagePlayReturnType::MontageLength, 0.0f, true);
if (MontageLength <= 0.0f)
{
return;
}
// raise the attacking flag only after the montage starts
bIsAttacking = true;
// reset the combo count
@@ -199,24 +211,25 @@ void ACombatCharacter::ComboAttack()
// notify enemies they are about to be attacked
NotifyEnemiesOfIncomingAttack();
// play the attack montage
if (UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance())
{
const float MontageLength = AnimInstance->Montage_Play(ComboAttackMontage, 1.0f, EMontagePlayReturnType::MontageLength, 0.0f, true);
// subscribe to montage completed and interrupted events
if (MontageLength > 0.0f)
{
// set the end delegate for the montage
AnimInstance->Montage_SetEndDelegate(OnAttackMontageEnded, ComboAttackMontage);
}
}
// set the end delegate for the montage
AnimInstance->Montage_SetEndDelegate(OnAttackMontageEnded, ComboAttackMontage);
}
void ACombatCharacter::ChargedAttack()
{
// raise the attacking flag
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (!AnimInstance || !ChargedAttackMontage)
{
return;
}
const float MontageLength = AnimInstance->Montage_Play(ChargedAttackMontage, 1.0f, EMontagePlayReturnType::MontageLength, 0.0f, true);
if (MontageLength <= 0.0f)
{
return;
}
// raise the attacking flag only after the montage starts
bIsAttacking = true;
// reset the charge loop flag
@@ -228,18 +241,8 @@ void ACombatCharacter::ChargedAttack()
// notify enemies they are about to be attacked
NotifyEnemiesOfIncomingAttack();
// play the charged attack montage
if (UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance())
{
const float MontageLength = AnimInstance->Montage_Play(ChargedAttackMontage, 1.0f, EMontagePlayReturnType::MontageLength, 0.0f, true);
// subscribe to montage completed and interrupted events
if (MontageLength > 0.0f)
{
// set the end delegate for the montage
AnimInstance->Montage_SetEndDelegate(OnAttackMontageEnded, ChargedAttackMontage);
}
}
// set the end delegate for the montage
AnimInstance->Montage_SetEndDelegate(OnAttackMontageEnded, ChargedAttackMontage);
}
void ACombatCharacter::AttackMontageEnded(UAnimMontage* Montage, bool bInterrupted)
@@ -268,6 +271,7 @@ void ACombatCharacter::DoAttackTrace(FName DamageSourceBone)
{
// sweep for objects in front of the character to be hit by the attack
TArray<FHitResult> OutHits;
TSet<AActor*> DamagedActors;
// start at the provided socket location, sweep forward
const FVector TraceStart = GetMesh()->GetSocketLocation(DamageSourceBone);
@@ -291,11 +295,19 @@ void ACombatCharacter::DoAttackTrace(FName DamageSourceBone)
// iterate over each object hit
for (const FHitResult& CurrentHit : OutHits)
{
AActor* HitActor = CurrentHit.GetActor();
if (!HitActor || DamagedActors.Contains(HitActor))
{
continue;
}
// check if we've hit a damageable actor
ICombatDamageable* Damageable = Cast<ICombatDamageable>(CurrentHit.GetActor());
ICombatDamageable* Damageable = Cast<ICombatDamageable>(HitActor);
if (Damageable)
{
DamagedActors.Add(HitActor);
// knock upwards and away from the impact normal
const FVector Impulse = (CurrentHit.ImpactNormal * -MeleeKnockbackImpulse) + (FVector::UpVector * MeleeLaunchImpulse);
@@ -238,6 +238,19 @@ void APlatformingCharacter::DoDash()
if (bHasDashed)
return;
// Validate the montage before committing any dash state.
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (!AnimInstance || !DashMontage)
{
return;
}
const float MontageLength = AnimInstance->Montage_Play(DashMontage, 1.0f, EMontagePlayReturnType::MontageLength, 0.0f, true);
if (MontageLength <= 0.0f)
{
return;
}
// raise the dash flags
bIsDashing = true;
bHasDashed = true;
@@ -251,17 +264,7 @@ void APlatformingCharacter::DoDash()
// enable the jump trails
SetJumpTrailState(true);
// play the dash montage
if (UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance())
{
const float MontageLength = AnimInstance->Montage_Play(DashMontage, 1.0f, EMontagePlayReturnType::MontageLength, 0.0f, true);
// has the montage played successfully?
if (MontageLength > 0.0f)
{
AnimInstance->Montage_SetEndDelegate(OnDashMontageEnded, DashMontage);
}
}
AnimInstance->Montage_SetEndDelegate(OnDashMontageEnded, DashMontage);
}
void APlatformingCharacter::DoJumpStart()
@@ -117,6 +117,8 @@ void ASideScrollingCharacter::NotifyHit(class UPrimitiveComponent* MyComp, AActo
void ASideScrollingCharacter::Landed(const FHitResult& Hit)
{
Super::Landed(Hit);
// reset the double jump
bHasDoubleJumped = false;
}