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 "EnhancedInputSubsystems.h"
#include "InputMappingContext.h"
#include "Kismet/GameplayStatics.h"
APlayerCharacter::APlayerCharacter()
{
@ -34,11 +33,6 @@ APlayerCharacter::APlayerCharacter()
// Create 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
WeaponInventoryComponent = CreateDefaultSubobject<UWeaponInventoryComponent>(TEXT("Weapon Inventory Component"));
}
@ -46,10 +40,6 @@ APlayerCharacter::APlayerCharacter()
void APlayerCharacter::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)
@ -97,54 +87,3 @@ void APlayerCharacter::MovementCallback(const FInputActionInstance& Instance)
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
#include "CoreMinimal.h"
#include "EnemyCharacter.h"
#include "EXPComponent.h"
#include "GoldComponent.h"
#include "VampireCharacter.h"
#include "WeaponInventoryComponent.h"
#include "Camera/CameraComponent.h"
#include "Components/SphereComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "PlayerCharacter.generated.h"
@ -37,18 +35,6 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
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)
UEXPComponent* EXPComponent;
@ -77,16 +63,4 @@ public:
private:
UFUNCTION()
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();
GetWorldTimerManager().SetTimer(WeaponTimerHandle, this, &AWeapon::FireWeaponAction, WeaponCooldown, true);
UEXPComponent* expcomponent = GetOwner()->GetComponentByClass<UEXPComponent>();
if (expcomponent)
{
expcomponent->OnEXPLevelUp.BindUObject(this, &AWeapon::UpgradeWeapon);

View File

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

View File

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

View File

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