Move Garlic out of PlayerCharacter to GarlicWeapon

This commit is contained in:
baz 2024-07-19 01:02:47 +01:00
parent 8e1dd981ce
commit 26edffa233
11 changed files with 146 additions and 111 deletions

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

Binary file not shown.

BIN
Content/Weapons/BP_GarlicWeapon.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Weapons/BP_WeaponComponent.uasset (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -7,7 +7,6 @@
#include "EnhancedInputComponent.h" #include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h" #include "EnhancedInputSubsystems.h"
#include "InputMappingContext.h" #include "InputMappingContext.h"
#include "Kismet/GameplayStatics.h"
APlayerCharacter::APlayerCharacter() APlayerCharacter::APlayerCharacter()
{ {
@ -34,11 +33,6 @@ APlayerCharacter::APlayerCharacter()
// Create Gold Component // Create Gold Component
GoldComponent = CreateDefaultSubobject<UGoldComponent>(TEXT("Gold Component")); GoldComponent = CreateDefaultSubobject<UGoldComponent>(TEXT("Gold Component"));
// Create Garlic Sphere Component
GarlicSphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Garlic Sphere Component"));
GarlicSphereComponent->SetupAttachment(RootComponent);
GarlicSphereComponent->SetSphereRadius(150.0f);
//Create Weapon Inventory Component //Create Weapon Inventory Component
WeaponInventoryComponent = CreateDefaultSubobject<UWeaponInventoryComponent>(TEXT("Weapon Inventory Component")); WeaponInventoryComponent = CreateDefaultSubobject<UWeaponInventoryComponent>(TEXT("Weapon Inventory Component"));
} }
@ -46,10 +40,6 @@ APlayerCharacter::APlayerCharacter()
void APlayerCharacter::BeginPlay() void APlayerCharacter::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
GarlicSphereComponent->OnComponentBeginOverlap.AddDynamic(this, &APlayerCharacter::OnGarlicBeginOverlap);
GarlicSphereComponent->OnComponentEndOverlap.AddDynamic(this, &APlayerCharacter::OnGarlicEndOverlap);
GetWorldTimerManager().SetTimer(GarlicTimerHandle, this, &APlayerCharacter::GarlicUpdate, GarlicUpdateTime, true);
} }
void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
@ -97,54 +87,3 @@ void APlayerCharacter::MovementCallback(const FInputActionInstance& Instance)
AddMovementInput({1.0f, 0.0f, 0.0f}, vec2.X); AddMovementInput({1.0f, 0.0f, 0.0f}, vec2.X);
} }
} }
void APlayerCharacter::OnGarlicBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
OverlappedEnemies.Add(Enemy);
}
}
void APlayerCharacter::OnGarlicEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
OverlappedEnemies.Remove(Enemy);
}
}
void APlayerCharacter::GarlicUpdate()
{
TArray<AEnemyCharacter*> OverlappedEnemiesCache = OverlappedEnemies;
for (int i = 0; i < OverlappedEnemiesCache.Num(); i++)
{
bool deadCheck = false;
UHealthComponent* EnemyHealthComponent = OverlappedEnemiesCache[i]->GetHealthComponent();
if (!EnemyHealthComponent->GetIsDead())
{
FVector Direction = OverlappedEnemiesCache[i]->GetActorLocation() - this->GetActorLocation();
Direction.Normalize();
float distance = GarlicSphereComponent->GetScaledSphereRadius();
Direction *= distance;
OverlappedEnemiesCache[i]->SetActorLocation(OverlappedEnemiesCache[i]->GetActorLocation() + Direction);
if (EnemyHealthComponent->GetCurrentHealth() < GarlicDamage)
{
deadCheck = true;
}
EnemyHealthComponent->TakeDamage(OverlappedEnemiesCache[i], GarlicDamage, nullptr, GetController(), this);
}
if (deadCheck)
{
i -= 1;
}
}
}

View File

@ -3,13 +3,11 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "EnemyCharacter.h"
#include "EXPComponent.h" #include "EXPComponent.h"
#include "GoldComponent.h" #include "GoldComponent.h"
#include "VampireCharacter.h" #include "VampireCharacter.h"
#include "WeaponInventoryComponent.h" #include "WeaponInventoryComponent.h"
#include "Camera/CameraComponent.h" #include "Camera/CameraComponent.h"
#include "Components/SphereComponent.h"
#include "GameFramework/SpringArmComponent.h" #include "GameFramework/SpringArmComponent.h"
#include "PlayerCharacter.generated.h" #include "PlayerCharacter.generated.h"
@ -37,18 +35,6 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UInputAction* MovementAction; UInputAction* MovementAction;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float GarlicDamage = 51.0f;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float GarlicUpdateTime = 1.0f;
UPROPERTY(VisibleAnywhere)
USphereComponent* GarlicSphereComponent;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TArray<AEnemyCharacter*> OverlappedEnemies;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
UEXPComponent* EXPComponent; UEXPComponent* EXPComponent;
@ -77,16 +63,4 @@ public:
private: private:
UFUNCTION() UFUNCTION()
void MovementCallback(const FInputActionInstance& Instance); void MovementCallback(const FInputActionInstance& Instance);
UFUNCTION()
void OnGarlicBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult);
UFUNCTION()
void OnGarlicEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex);
UFUNCTION()
void GarlicUpdate();
}; };

View File

@ -18,6 +18,7 @@ void AWeapon::BeginPlay()
Super::BeginPlay(); Super::BeginPlay();
GetWorldTimerManager().SetTimer(WeaponTimerHandle, this, &AWeapon::FireWeaponAction, WeaponCooldown, true); GetWorldTimerManager().SetTimer(WeaponTimerHandle, this, &AWeapon::FireWeaponAction, WeaponCooldown, true);
UEXPComponent* expcomponent = GetOwner()->GetComponentByClass<UEXPComponent>(); UEXPComponent* expcomponent = GetOwner()->GetComponentByClass<UEXPComponent>();
if (expcomponent) if (expcomponent)
{ {
expcomponent->OnEXPLevelUp.BindUObject(this, &AWeapon::UpgradeWeapon); expcomponent->OnEXPLevelUp.BindUObject(this, &AWeapon::UpgradeWeapon);

View File

@ -12,22 +12,24 @@ class VAMPIRES_API AWeapon : public AActor
GENERATED_BODY() GENERATED_BODY()
public: public:
UPROPERTY() UPROPERTY()
float WeaponCooldown = 1.0f; float WeaponCooldown = 1.0f;
UPROPERTY()
float Damage;
private: private:
FTimerHandle WeaponTimerHandle; FTimerHandle WeaponTimerHandle;
public: public:
// Sets default values for this actor's properties // Sets default values for this actor's properties
AWeapon(); AWeapon();
protected: protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned
virtual void BeginPlay() override; virtual void BeginPlay() override;
public: public:
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
virtual void FireWeaponAction(); virtual void FireWeaponAction();

View File

@ -3,8 +3,6 @@
#include "WeaponInventoryComponent.h" #include "WeaponInventoryComponent.h"
#include "Kismet/GameplayStatics.h"
// Sets default values for this component's properties // Sets default values for this component's properties
UWeaponInventoryComponent::UWeaponInventoryComponent() UWeaponInventoryComponent::UWeaponInventoryComponent()
{ {
@ -21,8 +19,7 @@ void UWeaponInventoryComponent::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
// ... InitializeInventory();
} }
void UWeaponInventoryComponent::InitializeInventory() void UWeaponInventoryComponent::InitializeInventory()
@ -37,10 +34,10 @@ void UWeaponInventoryComponent::InitializeInventory()
void UWeaponInventoryComponent::AddWeaponToInventory(TSubclassOf<AWeapon> Weapon) void UWeaponInventoryComponent::AddWeaponToInventory(TSubclassOf<AWeapon> Weapon)
{ {
AWeapon* weapon = GetWorld()->SpawnActor<AWeapon>(Weapon); FActorSpawnParameters SpawnParameters;
weapon->SetActorTransform(GetOwner()->GetTransform()); SpawnParameters.Owner = GetOwner();
weapon->SetOwner(GetOwner());
AWeapon* weapon = GetWorld()->SpawnActor<AWeapon>(Weapon, GetOwner()->GetTransform(), SpawnParameters);
weapon->AttachToActor(GetOwner(), FAttachmentTransformRules::KeepRelativeTransform); weapon->AttachToActor(GetOwner(), FAttachmentTransformRules::KeepRelativeTransform);
inventory.Add(weapon); inventory.Add(weapon);
} }

View File

@ -8,21 +8,20 @@
#include "WeaponInventoryComponent.generated.h" #include "WeaponInventoryComponent.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class VAMPIRES_API UWeaponInventoryComponent : public UActorComponent class VAMPIRES_API UWeaponInventoryComponent : public UActorComponent
{ {
GENERATED_BODY() GENERATED_BODY()
public: public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UPROPERTY()
TArray<TSubclassOf<AWeapon>> initialInventory; TArray<TSubclassOf<AWeapon>> initialInventory;
private: private:
UPROPERTY() UPROPERTY()
TArray<AWeapon*> inventory; TArray<AWeapon*> inventory;
public: public:
// Sets default values for this component's properties // Sets default values for this component's properties
UWeaponInventoryComponent(); UWeaponInventoryComponent();
@ -30,12 +29,10 @@ protected:
// Called when the game starts // Called when the game starts
virtual void BeginPlay() override; virtual void BeginPlay() override;
public: public:
UFUNCTION() UFUNCTION()
void InitializeInventory(); void InitializeInventory();
UFUNCTION() UFUNCTION()
void AddWeaponToInventory(TSubclassOf<AWeapon> Weapon); void AddWeaponToInventory(TSubclassOf<AWeapon> Weapon);
}; };

View File

@ -0,0 +1,78 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "GarlicWeapon.h"
AGarlicWeapon::AGarlicWeapon()
{
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
SphereComponent->SetupAttachment(RootComponent);
SphereComponent->SetSphereRadius(150.0f);
Damage = 51.0f;
}
void AGarlicWeapon::BeginPlay()
{
Super::BeginPlay();
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AGarlicWeapon::OnBeginOverlap);
SphereComponent->OnComponentEndOverlap.AddDynamic(this, &AGarlicWeapon::OnEndOverlap);
}
void AGarlicWeapon::FireWeaponAction()
{
TArray<AEnemyCharacter*> OverlappedEnemiesCache = OverlappedEnemies;
for (int i = 0; i < OverlappedEnemiesCache.Num(); i++)
{
bool deadCheck = false;
UHealthComponent* EnemyHealthComponent = OverlappedEnemiesCache[i]->GetHealthComponent();
if (!EnemyHealthComponent->GetIsDead())
{
FVector Direction = OverlappedEnemiesCache[i]->GetActorLocation() - this->GetActorLocation();
Direction.Normalize();
float distance = SphereComponent->GetScaledSphereRadius();
Direction *= distance;
OverlappedEnemiesCache[i]->SetActorLocation(OverlappedEnemiesCache[i]->GetActorLocation() + Direction);
if (EnemyHealthComponent->GetCurrentHealth() < Damage)
{
deadCheck = true;
}
AController* ownerController = nullptr;
if (AVampireCharacter* character = Cast<AVampireCharacter>(GetOwner()))
{
ownerController = character->GetController();
}
EnemyHealthComponent->TakeDamage(OverlappedEnemiesCache[i], Damage, nullptr, ownerController, this);
}
if (deadCheck)
{
i -= 1;
}
}
Super::FireWeaponAction();
}
void AGarlicWeapon::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
OverlappedEnemies.Add(Enemy);
}
}
void AGarlicWeapon::OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
OverlappedEnemies.Remove(Enemy);
}
}

View File

@ -0,0 +1,41 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "../Weapon.h"
#include "Components/SphereComponent.h"
#include "vampires/EnemyCharacter.h"
#include "GarlicWeapon.generated.h"
/**
*
*/
UCLASS()
class VAMPIRES_API AGarlicWeapon : public AWeapon
{
GENERATED_BODY()
USphereComponent* SphereComponent;
TArray<AEnemyCharacter*> OverlappedEnemies;
public:
AGarlicWeapon();
protected:
virtual void BeginPlay() override;
public:
virtual void FireWeaponAction() override;
protected:
UFUNCTION()
void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult);
UFUNCTION()
void OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex);
};