Implement flanking mechanics and slightly increase enemy speed
This commit is contained in:
Binary file not shown.
Binary file not shown.
BIN
Content/Materials/Material.uasset
LFS
Normal file
BIN
Content/Materials/Material.uasset
LFS
Normal file
Binary file not shown.
BIN
Content/Materials/Material_001.uasset
LFS
Normal file
BIN
Content/Materials/Material_001.uasset
LFS
Normal file
Binary file not shown.
BIN
Content/Materials/Material_002.uasset
LFS
Normal file
BIN
Content/Materials/Material_002.uasset
LFS
Normal file
Binary file not shown.
BIN
Content/Meshes/spaceship_lowpoly.uasset
LFS
Normal file
BIN
Content/Meshes/spaceship_lowpoly.uasset
LFS
Normal file
Binary file not shown.
@@ -34,6 +34,10 @@ AEnemySpaceship::AEnemySpaceship()
|
|||||||
EnemyMesh->SetCollisionResponseToChannel(ECC_Pawn, ECR_Ignore); // Ignore other pawns
|
EnemyMesh->SetCollisionResponseToChannel(ECC_Pawn, ECR_Ignore); // Ignore other pawns
|
||||||
EnemyMesh->SetCollisionResponseToChannel(ECC_GameTraceChannel1, ECR_Block); // Block player (for damage)
|
EnemyMesh->SetCollisionResponseToChannel(ECC_GameTraceChannel1, ECR_Block); // Block player (for damage)
|
||||||
EnemyMesh->SetGenerateOverlapEvents(true);
|
EnemyMesh->SetGenerateOverlapEvents(true);
|
||||||
|
|
||||||
|
CurrentVelocity = FVector::ZeroVector;
|
||||||
|
TargetVelocity = FVector::ZeroVector;
|
||||||
|
LastPosition = FVector::ZeroVector;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AEnemySpaceship::BeginPlay()
|
void AEnemySpaceship::BeginPlay()
|
||||||
@@ -52,20 +56,24 @@ void AEnemySpaceship::BeginPlay()
|
|||||||
|
|
||||||
// Randomize initial strafe direction
|
// Randomize initial strafe direction
|
||||||
StrafeDirection = FMath::RandBool() ? 1.0f : -1.0f;
|
StrafeDirection = FMath::RandBool() ? 1.0f : -1.0f;
|
||||||
|
|
||||||
|
LastPosition = GetActorLocation();
|
||||||
|
|
||||||
|
bInitializedFlank = false;
|
||||||
|
LastFlankUpdateTime = 0.0f;
|
||||||
|
CurrentFlankTarget = GetActorLocation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Modify Tick function to include flanking behavior
|
||||||
void AEnemySpaceship::Tick(float DeltaTime)
|
void AEnemySpaceship::Tick(float DeltaTime)
|
||||||
{
|
{
|
||||||
Super::Tick(DeltaTime);
|
Super::Tick(DeltaTime);
|
||||||
|
|
||||||
if (!PlayerPawn)
|
if (!PlayerPawn)
|
||||||
{
|
{
|
||||||
// Try to find player again if not set
|
|
||||||
AActor* FoundPlayerPawn = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);
|
AActor* FoundPlayerPawn = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);
|
||||||
PlayerPawn = Cast<ASpaceshipPawn>(FoundPlayerPawn);
|
PlayerPawn = Cast<ASpaceshipPawn>(FoundPlayerPawn);
|
||||||
|
if (!PlayerPawn) return;
|
||||||
if (!PlayerPawn)
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate distance to player
|
// Calculate distance to player
|
||||||
@@ -89,6 +97,9 @@ void AEnemySpaceship::Tick(float DeltaTime)
|
|||||||
case EEnemyBehaviorState::Strafe:
|
case EEnemyBehaviorState::Strafe:
|
||||||
PerformStrafe(DeltaTime);
|
PerformStrafe(DeltaTime);
|
||||||
break;
|
break;
|
||||||
|
case EEnemyBehaviorState::Flank:
|
||||||
|
PerformFlank(DeltaTime);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug state information
|
// Debug state information
|
||||||
@@ -101,6 +112,7 @@ void AEnemySpaceship::Tick(float DeltaTime)
|
|||||||
case EEnemyBehaviorState::Attack: StateString = "Attack"; break;
|
case EEnemyBehaviorState::Attack: StateString = "Attack"; break;
|
||||||
case EEnemyBehaviorState::Retreat: StateString = "Retreat"; break;
|
case EEnemyBehaviorState::Retreat: StateString = "Retreat"; break;
|
||||||
case EEnemyBehaviorState::Strafe: StateString = "Strafe"; break;
|
case EEnemyBehaviorState::Strafe: StateString = "Strafe"; break;
|
||||||
|
case EEnemyBehaviorState::Flank: StateString = "Flank"; break;
|
||||||
}
|
}
|
||||||
|
|
||||||
GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Orange,
|
GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Orange,
|
||||||
@@ -108,30 +120,213 @@ void AEnemySpaceship::Tick(float DeltaTime)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void AEnemySpaceship::SmoothMove(const FVector& TargetLocation, float DeltaTime)
|
||||||
|
{
|
||||||
|
FVector CurrentLocation = GetActorLocation();
|
||||||
|
FVector DirectionToTarget = (TargetLocation - CurrentLocation);
|
||||||
|
float DistanceToTarget = DirectionToTarget.Size();
|
||||||
|
|
||||||
|
// Calculate target velocity
|
||||||
|
FVector NewTargetVelocity = DirectionToTarget;
|
||||||
|
if (DistanceToTarget > 1.0f)
|
||||||
|
{
|
||||||
|
NewTargetVelocity = DirectionToTarget.GetSafeNormal() * FMath::Min(MovementSpeed, DistanceToTarget / DeltaTime);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
NewTargetVelocity = FVector::ZeroVector;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Smoothly interpolate current velocity towards target velocity
|
||||||
|
CurrentVelocity = FMath::VInterpTo(CurrentVelocity, NewTargetVelocity, DeltaTime, InterpSpeed);
|
||||||
|
|
||||||
|
// Clamp acceleration
|
||||||
|
FVector Acceleration = (CurrentVelocity - (CurrentLocation - LastPosition) / DeltaTime);
|
||||||
|
if (Acceleration.SizeSquared() > MaxAcceleration * MaxAcceleration)
|
||||||
|
{
|
||||||
|
Acceleration = Acceleration.GetSafeNormal() * MaxAcceleration;
|
||||||
|
CurrentVelocity = ((CurrentLocation - LastPosition) / DeltaTime) + (Acceleration * DeltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update position
|
||||||
|
LastPosition = CurrentLocation;
|
||||||
|
FVector NewLocation = CurrentLocation + (CurrentVelocity * DeltaTime);
|
||||||
|
SetActorLocation(NewLocation);
|
||||||
|
}
|
||||||
|
|
||||||
void AEnemySpaceship::UpdateBehaviorState()
|
void AEnemySpaceship::UpdateBehaviorState()
|
||||||
{
|
{
|
||||||
// Override random behavior changes when distance requires specific behavior
|
if (!PlayerPawn) return;
|
||||||
|
|
||||||
|
FVector DesiredPosition = CalculatePositionAwayFromOtherEnemies();
|
||||||
|
FVector PlayerVelocity = PlayerPawn->GetVelocity();
|
||||||
|
float PlayerSpeed = PlayerVelocity.Size();
|
||||||
|
|
||||||
|
float AggressionRoll = FMath::FRand();
|
||||||
|
|
||||||
if (DistanceToPlayer < MinDistanceToPlayer)
|
if (DistanceToPlayer < MinDistanceToPlayer)
|
||||||
{
|
{
|
||||||
// Too close, retreat or strafe
|
if (AggressionRoll < AggressionFactor * 0.3f)
|
||||||
CurrentBehaviorState = FMath::RandBool() ? EEnemyBehaviorState::Retreat : EEnemyBehaviorState::Strafe;
|
{
|
||||||
|
CurrentBehaviorState = EEnemyBehaviorState::Attack;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CurrentBehaviorState = EEnemyBehaviorState::Retreat;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (DistanceToPlayer < AttackRange)
|
else if (DistanceToPlayer < AttackRange)
|
||||||
{
|
{
|
||||||
// Within attack range but not too close, either attack or strafe
|
// Increased chance of flanking
|
||||||
if (CurrentBehaviorState != EEnemyBehaviorState::Attack &&
|
if (AggressionRoll < AggressionFactor * FlankingFrequency)
|
||||||
CurrentBehaviorState != EEnemyBehaviorState::Strafe)
|
|
||||||
{
|
{
|
||||||
CurrentBehaviorState = FMath::RandBool() ? EEnemyBehaviorState::Attack : EEnemyBehaviorState::Strafe;
|
// Only change to flank state if we're not already flanking
|
||||||
|
// This prevents unnecessary state changes that could cause position jumps
|
||||||
|
if (CurrentBehaviorState != EEnemyBehaviorState::Flank)
|
||||||
|
{
|
||||||
|
CurrentBehaviorState = EEnemyBehaviorState::Flank;
|
||||||
|
bIsFlankingRight = FMath::RandBool();
|
||||||
|
bInitializedFlank = false; // Will trigger new flank position calculation
|
||||||
|
LastFlankUpdateTime = GetWorld()->GetTimeSeconds();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (AggressionRoll < AggressionFactor * 0.8f) // Increased from 0.7f
|
||||||
|
{
|
||||||
|
CurrentBehaviorState = EEnemyBehaviorState::Attack;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CurrentBehaviorState = EEnemyBehaviorState::Strafe;
|
||||||
|
StrafeDirection = FMath::RandBool() ? 1.0f : -1.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Too far, chase
|
|
||||||
CurrentBehaviorState = EEnemyBehaviorState::Chase;
|
CurrentBehaviorState = EEnemyBehaviorState::Chase;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Add this new function to handle enemy spacing
|
||||||
|
FVector AEnemySpaceship::CalculatePositionAwayFromOtherEnemies()
|
||||||
|
{
|
||||||
|
FVector AveragePosition = GetActorLocation();
|
||||||
|
int32 EnemyCount = 0;
|
||||||
|
|
||||||
|
// Find all enemy spaceships
|
||||||
|
TArray<AActor*> FoundEnemies;
|
||||||
|
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AEnemySpaceship::StaticClass(), FoundEnemies);
|
||||||
|
|
||||||
|
for (AActor* Enemy : FoundEnemies)
|
||||||
|
{
|
||||||
|
if (Enemy != this)
|
||||||
|
{
|
||||||
|
float Distance = FVector::Dist(GetActorLocation(), Enemy->GetActorLocation());
|
||||||
|
if (Distance < MinDistanceToOtherEnemies)
|
||||||
|
{
|
||||||
|
AveragePosition += Enemy->GetActorLocation();
|
||||||
|
EnemyCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (EnemyCount > 0)
|
||||||
|
{
|
||||||
|
AveragePosition /= (EnemyCount + 1);
|
||||||
|
// Calculate position away from the cluster
|
||||||
|
FVector AwayFromCrowd = GetActorLocation() - AveragePosition;
|
||||||
|
AwayFromCrowd.Normalize();
|
||||||
|
return GetActorLocation() + (AwayFromCrowd * MinDistanceToOtherEnemies);
|
||||||
|
}
|
||||||
|
|
||||||
|
return GetActorLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
FVector AEnemySpaceship::CalculateFlankPosition()
|
||||||
|
{
|
||||||
|
if (!PlayerPawn) return GetActorLocation();
|
||||||
|
|
||||||
|
// Get player's forward vector and velocity
|
||||||
|
FVector PlayerForward = PlayerPawn->GetActorForwardVector();
|
||||||
|
FVector PlayerVelocity = PlayerPawn->GetVelocity();
|
||||||
|
|
||||||
|
// Use player's velocity direction if they're moving, otherwise use their forward vector
|
||||||
|
FVector BaseDirection = PlayerVelocity.SizeSquared() > 100.0f ?
|
||||||
|
PlayerVelocity.GetSafeNormal() : PlayerForward;
|
||||||
|
|
||||||
|
// Calculate the flanking angle
|
||||||
|
float AngleRadians = FMath::DegreesToRadians(FlankAngle * (bIsFlankingRight ? 1.0f : -1.0f));
|
||||||
|
|
||||||
|
// Rotate the vector for flanking
|
||||||
|
FVector FlankDirection = BaseDirection.RotateAngleAxis(AngleRadians, FVector::UpVector);
|
||||||
|
|
||||||
|
// Calculate the final position with some variation in distance
|
||||||
|
float VariedDistance = FlankDistance * FMath::RandRange(0.8f, 1.2f);
|
||||||
|
return PlayerPawn->GetActorLocation() + (FlankDirection * VariedDistance);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AEnemySpaceship::PerformFlank(float DeltaTime)
|
||||||
|
{
|
||||||
|
if (!PlayerPawn) return;
|
||||||
|
|
||||||
|
float CurrentTime = GetWorld()->GetTimeSeconds();
|
||||||
|
|
||||||
|
// Initialize flank position if needed
|
||||||
|
if (!bInitializedFlank)
|
||||||
|
{
|
||||||
|
CurrentFlankTarget = CalculateFlankPosition();
|
||||||
|
bInitializedFlank = true;
|
||||||
|
LastFlankUpdateTime = CurrentTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update flank position periodically
|
||||||
|
if (CurrentTime - LastFlankUpdateTime >= FlankPositionUpdateInterval)
|
||||||
|
{
|
||||||
|
// Smoothly transition to new flank position
|
||||||
|
FVector NewFlankPosition = CalculateFlankPosition();
|
||||||
|
CurrentFlankTarget = FMath::VInterpTo(
|
||||||
|
CurrentFlankTarget,
|
||||||
|
NewFlankPosition,
|
||||||
|
DeltaTime,
|
||||||
|
2.0f // Interpolation speed
|
||||||
|
);
|
||||||
|
|
||||||
|
LastFlankUpdateTime = CurrentTime;
|
||||||
|
|
||||||
|
// Occasionally switch flanking direction
|
||||||
|
if (FMath::FRand() < 0.3f) // 30% chance to switch direction
|
||||||
|
{
|
||||||
|
bIsFlankingRight = !bIsFlankingRight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate distance to current flank target
|
||||||
|
float DistanceToFlankTarget = FVector::Dist(GetActorLocation(), CurrentFlankTarget);
|
||||||
|
|
||||||
|
// Calculate movement speed based on distance
|
||||||
|
float CurrentFlankSpeed = FMath::Min(FlankingSpeed, DistanceToFlankTarget * 2.0f);
|
||||||
|
|
||||||
|
// Move towards flank position
|
||||||
|
FVector DirectionToFlank = (CurrentFlankTarget - GetActorLocation()).GetSafeNormal();
|
||||||
|
FVector TargetPosition = GetActorLocation() + DirectionToFlank * CurrentFlankSpeed * DeltaTime;
|
||||||
|
|
||||||
|
// Use smooth movement
|
||||||
|
SmoothMove(TargetPosition, DeltaTime);
|
||||||
|
|
||||||
|
// Face the player while flanking
|
||||||
|
FVector DirectionToPlayer = (PlayerPawn->GetActorLocation() - GetActorLocation()).GetSafeNormal();
|
||||||
|
FRotator TargetRotation = DirectionToPlayer.Rotation();
|
||||||
|
FRotator NewRotation = FMath::RInterpTo(GetActorRotation(), TargetRotation, DeltaTime, InterpSpeed);
|
||||||
|
SetActorRotation(NewRotation);
|
||||||
|
|
||||||
|
// Fire more frequently while flanking
|
||||||
|
if (bCanFire && DistanceToPlayer < AttackRange)
|
||||||
|
{
|
||||||
|
Fire();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void AEnemySpaceship::ChangeBehaviorState()
|
void AEnemySpaceship::ChangeBehaviorState()
|
||||||
{
|
{
|
||||||
// Random chance to change behavior state
|
// Random chance to change behavior state
|
||||||
@@ -154,18 +349,21 @@ void AEnemySpaceship::PerformChase(float DeltaTime)
|
|||||||
if (PlayerPawn)
|
if (PlayerPawn)
|
||||||
{
|
{
|
||||||
// Calculate direction to player
|
// Calculate direction to player
|
||||||
FVector Direction = (PlayerPawn->GetActorLocation() - GetActorLocation()).GetSafeNormal();
|
FVector DirectionToPlayer = (PlayerPawn->GetActorLocation() - GetActorLocation()).GetSafeNormal();
|
||||||
|
|
||||||
// Move towards the player
|
// Calculate target position
|
||||||
FVector NewLocation = GetActorLocation() + Direction * MovementSpeed * DeltaTime;
|
FVector TargetPosition = GetActorLocation() + DirectionToPlayer * MovementSpeed * DeltaTime;
|
||||||
SetActorLocation(NewLocation);
|
|
||||||
|
|
||||||
// Face towards the player
|
// Use smooth movement
|
||||||
FRotator NewRotation = Direction.Rotation();
|
SmoothMove(TargetPosition, DeltaTime);
|
||||||
|
|
||||||
|
// Smoothly rotate to face player
|
||||||
|
FRotator TargetRotation = DirectionToPlayer.Rotation();
|
||||||
|
FRotator NewRotation = FMath::RInterpTo(GetActorRotation(), TargetRotation, DeltaTime, InterpSpeed);
|
||||||
SetActorRotation(NewRotation);
|
SetActorRotation(NewRotation);
|
||||||
|
|
||||||
// If within attack range, fire occasionally
|
// Fire if within range
|
||||||
if (DistanceToPlayer < AttackRange && bCanFire && FMath::FRand() < 0.3f)
|
if (DistanceToPlayer < AttackRange && bCanFire)
|
||||||
{
|
{
|
||||||
Fire();
|
Fire();
|
||||||
}
|
}
|
||||||
@@ -193,24 +391,32 @@ void AEnemySpaceship::PerformRetreat(float DeltaTime)
|
|||||||
{
|
{
|
||||||
if (PlayerPawn)
|
if (PlayerPawn)
|
||||||
{
|
{
|
||||||
// Move away from player
|
// Calculate ideal retreat position
|
||||||
FVector Direction = (GetActorLocation() - PlayerPawn->GetActorLocation()).GetSafeNormal();
|
FVector DirectionFromPlayer = (GetActorLocation() - PlayerPawn->GetActorLocation()).GetSafeNormal();
|
||||||
FVector NewLocation = GetActorLocation() + Direction * MovementSpeed * DeltaTime;
|
FVector DesiredPosition = PlayerPawn->GetActorLocation() + (DirectionFromPlayer * OptimalCombatDistance);
|
||||||
SetActorLocation(NewLocation);
|
|
||||||
|
|
||||||
// Keep facing the player while backing up
|
// Consider other enemies
|
||||||
FVector FaceDirection = (PlayerPawn->GetActorLocation() - GetActorLocation()).GetSafeNormal();
|
FVector AvoidCrowdingPosition = CalculatePositionAwayFromOtherEnemies();
|
||||||
FRotator NewRotation = FaceDirection.Rotation();
|
FVector FinalTargetPosition = FMath::Lerp(DesiredPosition, AvoidCrowdingPosition, 0.3f);
|
||||||
|
|
||||||
|
// Use smooth movement
|
||||||
|
SmoothMove(FinalTargetPosition, DeltaTime);
|
||||||
|
|
||||||
|
// Smoothly rotate to face player
|
||||||
|
FVector DirectionToPlayer = (PlayerPawn->GetActorLocation() - GetActorLocation()).GetSafeNormal();
|
||||||
|
FRotator TargetRotation = DirectionToPlayer.Rotation();
|
||||||
|
FRotator NewRotation = FMath::RInterpTo(GetActorRotation(), TargetRotation, DeltaTime, InterpSpeed);
|
||||||
SetActorRotation(NewRotation);
|
SetActorRotation(NewRotation);
|
||||||
|
|
||||||
// Fire while retreating
|
// Fire while retreating if in range
|
||||||
if (bCanFire)
|
if (bCanFire && DistanceToPlayer < AttackRange)
|
||||||
{
|
{
|
||||||
Fire();
|
Fire();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void AEnemySpaceship::PerformStrafe(float DeltaTime)
|
void AEnemySpaceship::PerformStrafe(float DeltaTime)
|
||||||
{
|
{
|
||||||
if (PlayerPawn)
|
if (PlayerPawn)
|
||||||
@@ -218,19 +424,44 @@ void AEnemySpaceship::PerformStrafe(float DeltaTime)
|
|||||||
// Calculate direction to player
|
// Calculate direction to player
|
||||||
FVector DirectionToPlayer = (PlayerPawn->GetActorLocation() - GetActorLocation()).GetSafeNormal();
|
FVector DirectionToPlayer = (PlayerPawn->GetActorLocation() - GetActorLocation()).GetSafeNormal();
|
||||||
|
|
||||||
// Calculate strafe direction (perpendicular to direction to player)
|
// Calculate ideal combat distance
|
||||||
|
float CurrentDistance = DistanceToPlayer;
|
||||||
|
float DistanceAdjustment = 0.0f;
|
||||||
|
|
||||||
|
if (CurrentDistance < OptimalCombatDistance)
|
||||||
|
{
|
||||||
|
DistanceAdjustment = -1.0f; // Move away
|
||||||
|
}
|
||||||
|
else if (CurrentDistance > OptimalCombatDistance + 200.0f)
|
||||||
|
{
|
||||||
|
DistanceAdjustment = 1.0f; // Move closer
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate strafe direction
|
||||||
FVector StrafeVector = FVector::CrossProduct(DirectionToPlayer, FVector::UpVector) * StrafeDirection;
|
FVector StrafeVector = FVector::CrossProduct(DirectionToPlayer, FVector::UpVector) * StrafeDirection;
|
||||||
|
|
||||||
// Move sideways while maintaining distance
|
// Calculate target position
|
||||||
FVector NewLocation = GetActorLocation() + StrafeVector * StrafeSpeed * DeltaTime;
|
FVector DistanceAdjustmentVector = DirectionToPlayer * DistanceAdjustment * MovementSpeed * 0.5f;
|
||||||
SetActorLocation(NewLocation);
|
FVector StrafeMovement = StrafeVector * StrafeSpeed;
|
||||||
|
FVector DesiredMovement = (StrafeMovement + DistanceAdjustmentVector) * DeltaTime;
|
||||||
|
|
||||||
|
// Get position avoiding other enemies
|
||||||
|
FVector CrowdAvoidancePosition = CalculatePositionAwayFromOtherEnemies();
|
||||||
|
|
||||||
|
// Blend between desired movement and crowd avoidance
|
||||||
|
FVector TargetPosition = GetActorLocation() + DesiredMovement;
|
||||||
|
FVector FinalTargetPosition = FMath::Lerp(TargetPosition, CrowdAvoidancePosition, 0.3f);
|
||||||
|
|
||||||
|
// Use smooth movement
|
||||||
|
SmoothMove(FinalTargetPosition, DeltaTime);
|
||||||
|
|
||||||
// Face towards the player
|
// Face towards the player
|
||||||
FRotator NewRotation = DirectionToPlayer.Rotation();
|
FRotator TargetRotation = DirectionToPlayer.Rotation();
|
||||||
|
FRotator NewRotation = FMath::RInterpTo(GetActorRotation(), TargetRotation, DeltaTime, InterpSpeed);
|
||||||
SetActorRotation(NewRotation);
|
SetActorRotation(NewRotation);
|
||||||
|
|
||||||
// Fire while strafing
|
// Fire while strafing if in range
|
||||||
if (bCanFire)
|
if (bCanFire && DistanceToPlayer < AttackRange)
|
||||||
{
|
{
|
||||||
Fire();
|
Fire();
|
||||||
}
|
}
|
||||||
@@ -239,12 +470,7 @@ void AEnemySpaceship::PerformStrafe(float DeltaTime)
|
|||||||
|
|
||||||
void AEnemySpaceship::Fire()
|
void AEnemySpaceship::Fire()
|
||||||
{
|
{
|
||||||
if (!ProjectileClass)
|
if (!ProjectileClass) return;
|
||||||
{
|
|
||||||
if (GEngine)
|
|
||||||
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red, TEXT("Enemy ProjectileClass not set!"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
UWorld* World = GetWorld();
|
UWorld* World = GetWorld();
|
||||||
if (World)
|
if (World)
|
||||||
@@ -252,9 +478,11 @@ void AEnemySpaceship::Fire()
|
|||||||
FVector SpawnLocation = ProjectileSpawnPoint->GetComponentLocation();
|
FVector SpawnLocation = ProjectileSpawnPoint->GetComponentLocation();
|
||||||
FRotator SpawnRotation = GetActorRotation();
|
FRotator SpawnRotation = GetActorRotation();
|
||||||
|
|
||||||
// Add slight randomness to rotation for less perfect aim
|
// Less random spread during flanking for more accurate shots
|
||||||
float RandPitch = FMath::RandRange(-5.0f, 5.0f);
|
float SpreadMultiplier = (CurrentBehaviorState == EEnemyBehaviorState::Flank) ? 0.9f : 1.0f;
|
||||||
float RandYaw = FMath::RandRange(-5.0f, 5.0f);
|
float RandPitch = FMath::RandRange(-5.0f, 5.0f) * SpreadMultiplier;
|
||||||
|
float RandYaw = FMath::RandRange(-5.0f, 5.0f) * SpreadMultiplier;
|
||||||
|
|
||||||
SpawnRotation.Pitch += RandPitch;
|
SpawnRotation.Pitch += RandPitch;
|
||||||
SpawnRotation.Yaw += RandYaw;
|
SpawnRotation.Yaw += RandYaw;
|
||||||
|
|
||||||
@@ -262,7 +490,6 @@ void AEnemySpaceship::Fire()
|
|||||||
SpawnParams.Owner = this;
|
SpawnParams.Owner = this;
|
||||||
SpawnParams.Instigator = GetInstigator();
|
SpawnParams.Instigator = GetInstigator();
|
||||||
|
|
||||||
// Spawn the projectile using EnemyProjectile class
|
|
||||||
AEnemyProjectile* Projectile = World->SpawnActor<AEnemyProjectile>(
|
AEnemyProjectile* Projectile = World->SpawnActor<AEnemyProjectile>(
|
||||||
ProjectileClass,
|
ProjectileClass,
|
||||||
SpawnLocation,
|
SpawnLocation,
|
||||||
@@ -270,12 +497,6 @@ void AEnemySpaceship::Fire()
|
|||||||
SpawnParams
|
SpawnParams
|
||||||
);
|
);
|
||||||
|
|
||||||
if (Projectile)
|
|
||||||
{
|
|
||||||
if (GEngine)
|
|
||||||
GEngine->AddOnScreenDebugMessage(-1, 0.5f, FColor::Yellow, TEXT("Enemy Fired!"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start fire rate timer
|
// Start fire rate timer
|
||||||
bCanFire = false;
|
bCanFire = false;
|
||||||
GetWorldTimerManager().SetTimer(FireTimerHandle, this, &AEnemySpaceship::ResetFire, FireRate, false);
|
GetWorldTimerManager().SetTimer(FireTimerHandle, this, &AEnemySpaceship::ResetFire, FireRate, false);
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ enum class EEnemyBehaviorState : uint8
|
|||||||
Chase, // Actively pursue the player
|
Chase, // Actively pursue the player
|
||||||
Attack, // Stop and shoot at the player
|
Attack, // Stop and shoot at the player
|
||||||
Retreat, // Move away from player when too close
|
Retreat, // Move away from player when too close
|
||||||
Strafe // Move sideways while attacking
|
Strafe, // Move sideways while attacking
|
||||||
|
Flank // Execute flanking maneuver
|
||||||
};
|
};
|
||||||
|
|
||||||
UCLASS()
|
UCLASS()
|
||||||
@@ -32,17 +33,45 @@ protected:
|
|||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
||||||
USceneComponent* ProjectileSpawnPoint;
|
USceneComponent* ProjectileSpawnPoint;
|
||||||
|
|
||||||
|
// Add interpolation speed for smooth movement
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
float MovementSpeed = 500.0f;
|
float InterpSpeed = 3.0f;
|
||||||
|
|
||||||
|
// Add max acceleration to prevent sudden movements
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
|
float MaxAcceleration = 2000.0f;
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
float AttackRange = 1500.0f;
|
float MovementSpeed = 1200.0f;
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
float MinDistanceToPlayer = 500.0f;
|
float FlankingSpeed = 1200.0f; // Even faster during flanking
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
float StrafeSpeed = 300.0f;
|
float FlankAngle = 60.0f; // Angle for flanking maneuver
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
|
float FlankDistance = 2000.0f; // Distance to maintain during flanking
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
|
float FlankPositionUpdateInterval = 1.0f; // How often to update flank position
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
|
float AttackRange = 4000.0f;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
|
float MinDistanceToPlayer = 1500.0f;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
|
float StrafeSpeed = 500.0f;
|
||||||
|
|
||||||
|
// Add new property for minimum distance to other enemies
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
|
float MinDistanceToOtherEnemies = 800.0f;
|
||||||
|
|
||||||
|
// Add new property for optimal combat distance
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
|
float OptimalCombatDistance = 2500.0f;
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
||||||
float MaxHealth = 100.0f;
|
float MaxHealth = 100.0f;
|
||||||
@@ -74,6 +103,12 @@ protected:
|
|||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
|
||||||
float StateChangeChance = 0.3f;
|
float StateChangeChance = 0.3f;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
|
||||||
|
float AggressionFactor = 0.7f; // Higher values make the enemy more likely to choose aggressive actions
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
|
||||||
|
float FlankingFrequency = 0.6f; // Increased chance of flanking (was 0.4f)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual void Tick(float DeltaTime) override;
|
virtual void Tick(float DeltaTime) override;
|
||||||
|
|
||||||
@@ -105,9 +140,28 @@ private:
|
|||||||
void ChangeBehaviorState();
|
void ChangeBehaviorState();
|
||||||
void Die();
|
void Die();
|
||||||
|
|
||||||
|
void SmoothMove(const FVector& TargetLocation, float DeltaTime);
|
||||||
|
|
||||||
// AI behavior implementation functions
|
// AI behavior implementation functions
|
||||||
void PerformChase(float DeltaTime);
|
void PerformChase(float DeltaTime);
|
||||||
void PerformAttack(float DeltaTime);
|
void PerformAttack(float DeltaTime);
|
||||||
void PerformRetreat(float DeltaTime);
|
void PerformRetreat(float DeltaTime);
|
||||||
void PerformStrafe(float DeltaTime);
|
void PerformStrafe(float DeltaTime);
|
||||||
|
void PerformFlank(float DeltaTime);
|
||||||
|
|
||||||
|
FVector CalculatePositionAwayFromOtherEnemies();
|
||||||
|
FVector CalculateFlankPosition();
|
||||||
|
|
||||||
|
FVector CurrentVelocity;
|
||||||
|
FVector TargetVelocity;
|
||||||
|
FVector LastPosition;
|
||||||
|
|
||||||
|
FVector FlankPosition;
|
||||||
|
float FlankTimer;
|
||||||
|
bool bIsFlankingRight;
|
||||||
|
FTimerHandle FlankUpdateTimer;
|
||||||
|
|
||||||
|
float LastFlankUpdateTime;
|
||||||
|
FVector CurrentFlankTarget;
|
||||||
|
bool bInitializedFlank;
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user