Add basic Garlic weapon

This commit is contained in:
baz 2024-06-19 02:29:19 +01:00
parent 6e24728a8b
commit 619c82bd01
3 changed files with 84 additions and 3 deletions

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

Binary file not shown.

View File

@ -7,6 +7,7 @@
#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()
{ {
@ -29,11 +30,20 @@ APlayerCharacter::APlayerCharacter()
// Create EXP Component // Create EXP Component
EXPComponent = CreateDefaultSubobject<UEXPComponent>(TEXT("EXP Component")); EXPComponent = CreateDefaultSubobject<UEXPComponent>(TEXT("EXP Component"));
// Create Garlic Sphere Component
GarlicSphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Garlic Sphere Component"));
GarlicSphereComponent->SetupAttachment(RootComponent);
GarlicSphereComponent->SetSphereRadius(150.0f);
} }
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)
@ -71,3 +81,46 @@ 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()
{
for (int i = 0; i < OverlappedEnemies.Num(); i++)
{
bool deadCheck = false;
UHealthComponent* EnemyHealthComponent = OverlappedEnemies[i]->GetHealthComponent();
if (!EnemyHealthComponent->GetIsDead())
{
if (EnemyHealthComponent->GetCurrentHealth() < GarlicDamage)
{
deadCheck = true;
}
EnemyHealthComponent->TakeDamage(OverlappedEnemies[i], GarlicDamage, nullptr, GetController(), this);
}
if (deadCheck)
{
i -= 1;
}
}
}

View File

@ -3,9 +3,11 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "EnemyCharacter.h"
#include "EXPComponent.h" #include "EXPComponent.h"
#include "VampireCharacter.h" #include "VampireCharacter.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"
@ -33,11 +35,25 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UInputAction* MovementAction; UInputAction* MovementAction;
protected: UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float GarlicDamage = 51.0f;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float GarlicUpdateTime = 1.0f;
protected:
UPROPERTY() UPROPERTY()
UEXPComponent* EXPComponent; UEXPComponent* EXPComponent;
UPROPERTY(VisibleAnywhere)
USphereComponent* GarlicSphereComponent;
UPROPERTY()
TArray<AEnemyCharacter*> OverlappedEnemies;
private:
FTimerHandle GarlicTimerHandle;
public: public:
APlayerCharacter(); APlayerCharacter();
@ -50,4 +66,16 @@ 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();
}; };