Add Enemy Attacks

This commit is contained in:
baz 2025-04-02 23:48:25 +01:00
parent f44915e5f0
commit 2c3a66cbd4
15 changed files with 89 additions and 14 deletions

BIN
Content/Enemy/BP_EnemyCharacter.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Enemy/BP_TestEnemy.uasset (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

BIN
Content/Pickups/Gold/BP_GoldPickup.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Player/BP_PlayerCharacter.uasset (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -6,17 +6,23 @@
#include "EnemyDataAsset.h"
#include "EXPPickup.h"
#include "HealthComponent.h"
#include "MovieSceneTracksComponentTypes.h"
#include "ObjectPoolComponent.h"
#include "ObjectPoolManager.h"
#include "PaperFlipbookComponent.h"
#include "VampireAIController.h"
#include "VampireGameMode.h"
#include "Components/CapsuleComponent.h"
#include "Components/SphereComponent.h"
#include "Kismet/GameplayStatics.h"
AEnemyCharacter::AEnemyCharacter(const FObjectInitializer& ObjectInitializer)
{
ObjectPoolComponent = CreateDefaultSubobject<UObjectPoolComponent>(TEXT("Object Pool Component"));
DamageSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Damage Sphere"));
DamageSphere->SetupAttachment(RootComponent);
DamageSphere->SetSphereRadius(50.0f);
}
void AEnemyCharacter::BeginPlay()
@ -26,6 +32,10 @@ void AEnemyCharacter::BeginPlay()
GetHealthComponent()->OnDeath.AddDynamic(this, &AEnemyCharacter::OnDeath);
ObjectPoolComponent->OnRetrieve.BindUFunction(this, "ResetHealth");
DamageSphere->OnComponentBeginOverlap.AddDynamic(this, &AEnemyCharacter::OnDamageBeginOverlap);
DamageSphere->OnComponentEndOverlap.AddDynamic(this, &AEnemyCharacter::OnDamageEndOverlap);
DamageSphere->MoveIgnoreActors.Add(this);
}
void AEnemyCharacter::Tick(float DeltaTime)
@ -104,7 +114,37 @@ UHealthComponent* AEnemyCharacter::GetEnemyHealthComponent_Implementation()
return GetHealthComponent();
}
void AEnemyCharacter::OnDamageBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (Cast<ACharacter>(OtherActor) == UGameplayStatics::GetPlayerCharacter(GetWorld(), 0) && !Player.Contains(OtherActor))
{
Player.Add(OtherActor);
GetWorldTimerManager().SetTimer(DamageTimerHandle, this, &AEnemyCharacter::DamagePlayer, AttackCooldown, true);
}
}
void AEnemyCharacter::OnDamageEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (Cast<ACharacter>(OtherActor) == UGameplayStatics::GetPlayerCharacter(GetWorld(), 0) && Player.Contains(OtherActor))
{
Player.Remove(OtherActor);
GetWorldTimerManager().ClearTimer(DamageTimerHandle);
}
}
void AEnemyCharacter::ResetHealth()
{
GetHealthComponent()->ResetHealth();
}
void AEnemyCharacter::DamagePlayer()
{
for (auto player : Player)
{
UGameplayStatics::ApplyDamage(player, Damage, GetController(), this, nullptr);
}
}

View File

@ -8,6 +8,7 @@
#include "Interfaces/Enemyable.h"
#include "EnemyCharacter.generated.h"
class USphereComponent;
class UObjectPoolComponent;
class UBehaviorTree;
class AEXPPickup;
@ -23,6 +24,15 @@ public:
UPROPERTY(EditDefaultsOnly)
TSubclassOf<AEXPPickup> EXPPickupTemplate = nullptr;
UPROPERTY(EditDefaultsOnly)
TObjectPtr<USphereComponent> DamageSphere = nullptr;
UPROPERTY(EditDefaultsOnly)
float Damage = 5.0f;;
UPROPERTY(EditDefaultsOnly)
float AttackCooldown = 1.0f;
private:
UPROPERTY(EditDefaultsOnly, Meta = (AllowPrivateAccess = "true"))
UBehaviorTree* BehaviorTree = nullptr;
@ -31,6 +41,10 @@ private:
UPickupDataAsset* PickupTemplate = nullptr;
TArray<AActor*> Player;
FTimerHandle DamageTimerHandle;
public:
AEnemyCharacter(const FObjectInitializer& ObjectInitializer);
@ -63,7 +77,18 @@ public:
UFUNCTION()
virtual UHealthComponent* GetEnemyHealthComponent_Implementation() override;
UFUNCTION()
void OnDamageBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult);
UFUNCTION()
void OnDamageEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex);
private:
UFUNCTION()
void ResetHealth();
void DamagePlayer();
};

View File

@ -21,10 +21,12 @@ APickup::APickup()
InnerSphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Inner Sphere Component"));
SetRootComponent(InnerSphereComponent);
InnerSphereComponent->SetSphereRadius(25.0f);
InnerSphereComponent->SetCollisionProfileName(TEXT("Pickup"));
OuterSphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Outer Sphere Component"));
OuterSphereComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
OuterSphereComponent->SetSphereRadius(250.0f);
OuterSphereComponent->SetCollisionProfileName(TEXT("Pickup"));
StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Static Mesh Component"));
StaticMeshComponent->SetRelativeRotation(FRotator(0.0f, 90.0f, 0.0f));

View File

@ -20,6 +20,7 @@ AProjectile::AProjectile()
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
SetRootComponent(SphereComponent);
SphereComponent->SetSphereRadius(50.0f);
SphereComponent->SetCollisionProfileName(TEXT("Weapon"));
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement"));
ProjectileMovement->ProjectileGravityScale = 0.0f;

View File

@ -11,6 +11,8 @@ AGarlicWeapon::AGarlicWeapon()
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
SphereComponent->SetupAttachment(RootComponent);
SphereComponent->SetSphereRadius(150.0f);
SphereComponent->SetCollisionProfileName(TEXT("Weapon"));
Damage = 51.0f;
Range = SphereComponent->GetScaledSphereRadius();
}

View File

@ -11,6 +11,8 @@ ALightningRingWeapon::ALightningRingWeapon()
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
SphereComponent->SetupAttachment(RootComponent);
SphereComponent->SetSphereRadius(1000.0f);
SphereComponent->SetCollisionProfileName(TEXT("Weapon"));
Damage = 51.0f;
}

View File

@ -14,6 +14,8 @@ APentagramWeapon::APentagramWeapon()
{
BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("Sphere Component"));
BoxComponent->SetupAttachment(RootComponent);
BoxComponent->SetCollisionProfileName(TEXT("Weapon"));
WeaponCooldown = 90.0f;
}

View File

@ -14,6 +14,7 @@ AProjectileWeapon::AProjectileWeapon()
{
BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("Box Component"));
BoxComponent->SetupAttachment(RootComponent);
BoxComponent->SetCollisionProfileName(TEXT("Weapon"));
}
void AProjectileWeapon::BeginPlay()