78 lines
2.2 KiB
C++
78 lines
2.2 KiB
C++
#include "EnemySpaceship.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
|
|
AEnemySpaceship::AEnemySpaceship()
|
|
{
|
|
PrimaryActorTick.bCanEverTick = true;
|
|
|
|
// Create and setup the enemy mesh
|
|
EnemyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("EnemyMesh"));
|
|
RootComponent = EnemyMesh;
|
|
|
|
// Disable gravity and physics simulation
|
|
EnemyMesh->SetSimulatePhysics(false);
|
|
EnemyMesh->SetEnableGravity(false);
|
|
|
|
// Set up collision for the enemy mesh
|
|
EnemyMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
|
|
EnemyMesh->SetCollisionObjectType(ECC_Pawn);
|
|
EnemyMesh->SetCollisionResponseToAllChannels(ECR_Block);
|
|
EnemyMesh->SetCollisionResponseToChannel(ECC_Pawn, ECR_Ignore); // Ignore other pawns
|
|
EnemyMesh->SetCollisionResponseToChannel(ECC_GameTraceChannel1, ECR_Ignore); // Ignore player
|
|
EnemyMesh->SetGenerateOverlapEvents(true);
|
|
}
|
|
|
|
void AEnemySpaceship::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
|
|
// Find the player pawn
|
|
PlayerPawn = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);
|
|
}
|
|
|
|
void AEnemySpaceship::Tick(float DeltaTime)
|
|
{
|
|
Super::Tick(DeltaTime);
|
|
|
|
if (PlayerPawn)
|
|
{
|
|
// Move towards the player
|
|
FVector Direction = (PlayerPawn->GetActorLocation() - GetActorLocation()).GetSafeNormal();
|
|
FVector NewLocation = GetActorLocation() + Direction * MovementSpeed * DeltaTime;
|
|
SetActorLocation(NewLocation);
|
|
|
|
// Face towards the player
|
|
FRotator NewRotation = Direction.Rotation();
|
|
SetActorRotation(NewRotation);
|
|
}
|
|
}
|
|
|
|
float AEnemySpaceship::TakeDamage(float DamageAmount, FDamageEvent const& DamageEvent,
|
|
AController* EventInstigator, AActor* DamageCauser)
|
|
{
|
|
float DamageToApply = Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
|
|
|
|
CurrentHealth -= DamageToApply;
|
|
|
|
// Debug message
|
|
if (GEngine)
|
|
{
|
|
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red,
|
|
FString::Printf(TEXT("Enemy Health: %f"), CurrentHealth));
|
|
}
|
|
|
|
if (CurrentHealth <= 0)
|
|
{
|
|
Die();
|
|
}
|
|
|
|
return DamageToApply;
|
|
}
|
|
|
|
void AEnemySpaceship::Die()
|
|
{
|
|
// Add any death effects here
|
|
|
|
// Destroy the enemy
|
|
Destroy();
|
|
} |