185 lines
5.8 KiB
C++
185 lines
5.8 KiB
C++
#pragma once
|
|
|
|
#include "CoreMinimal.h"
|
|
#include "GameFramework/Actor.h"
|
|
#include "EnemySpaceship.generated.h"
|
|
|
|
// Define AI behavior states
|
|
UENUM(BlueprintType)
|
|
enum class EEnemyBehaviorState : uint8
|
|
{
|
|
Chase, // Actively pursue the player
|
|
Attack, // Stop and shoot at the player
|
|
Retreat, // Move away from player when too close
|
|
Strafe, // Move sideways while attacking
|
|
Flank // Execute flanking maneuver
|
|
};
|
|
|
|
UCLASS()
|
|
class MYPROJECT3_API AEnemySpaceship : public AActor
|
|
{
|
|
GENERATED_BODY()
|
|
|
|
public:
|
|
AEnemySpaceship();
|
|
|
|
protected:
|
|
virtual void BeginPlay() override;
|
|
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components")
|
|
UStaticMeshComponent* EnemyMesh;
|
|
|
|
// Projectile spawn points
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
|
USceneComponent* ProjectileSpawnPoint;
|
|
|
|
// Add interpolation speed for smooth movement
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
|
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")
|
|
float MovementSpeed = 1200.0f;
|
|
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
|
float FlankingSpeed = 1200.0f; // Even faster during flanking
|
|
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
|
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")
|
|
float MaxHealth = 100.0f;
|
|
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
|
float CurrentHealth = 100.0f;
|
|
|
|
// Projectile class to spawn
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
|
TSubclassOf<class AEnemyProjectile> ProjectileClass;
|
|
|
|
// Firing properties
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
|
float FireRate = 1.0f;
|
|
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
|
float ProjectileSpeed = 2000.0f;
|
|
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
|
float ProjectileDamage = 10.0f;
|
|
|
|
// AI behavior properties
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
|
|
EEnemyBehaviorState CurrentBehaviorState = EEnemyBehaviorState::Chase;
|
|
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
|
|
float BehaviorChangeTime = 3.0f;
|
|
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
|
|
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)
|
|
|
|
UPROPERTY()
|
|
UMaterialInterface* OriginalMaterial;
|
|
|
|
UPROPERTY()
|
|
UMaterialInstanceDynamic* DynamicMaterialInstance;
|
|
|
|
UPROPERTY(EditAnywhere, Category = "Effects")
|
|
FLinearColor DamageFlashColor = FLinearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
UPROPERTY(EditAnywhere, Category = "Effects")
|
|
float DamageFlashDuration = 0.1f;
|
|
|
|
FTimerHandle DamageFlashTimerHandle;
|
|
|
|
public:
|
|
virtual void Tick(float DeltaTime) override;
|
|
|
|
// Override TakeDamage to handle damage taken
|
|
virtual float TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent,
|
|
class AController* EventInstigator, AActor* DamageCauser) override;
|
|
|
|
private:
|
|
// Reference to player
|
|
class ASpaceshipPawn* PlayerPawn;
|
|
|
|
// Distance to player
|
|
float DistanceToPlayer;
|
|
|
|
// Fire control
|
|
FTimerHandle FireTimerHandle;
|
|
bool bCanFire = true;
|
|
|
|
// Strafe direction (1 = right, -1 = left)
|
|
float StrafeDirection = 1.0f;
|
|
|
|
// Timer for changing behavior
|
|
FTimerHandle BehaviorTimerHandle;
|
|
|
|
// Helper functions
|
|
void Fire();
|
|
void ResetFire();
|
|
void UpdateBehaviorState();
|
|
void ChangeBehaviorState();
|
|
void Die();
|
|
|
|
void SmoothMove(const FVector& TargetLocation, float DeltaTime);
|
|
|
|
// AI behavior implementation functions
|
|
void PerformChase(float DeltaTime);
|
|
void PerformAttack(float DeltaTime);
|
|
void PerformRetreat(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;
|
|
|
|
void InitializeDynamicMaterial();
|
|
void ApplyDamageFlash();
|
|
void ResetDamageFlash();
|
|
}; |