From 26edffa233d0315b5e3a27c78ab49ffd7bc022ad Mon Sep 17 00:00:00 2001 From: baz Date: Fri, 19 Jul 2024 01:02:47 +0100 Subject: [PATCH] Move Garlic out of PlayerCharacter to GarlicWeapon --- Content/Player/BP_PlayerCharacter.uasset | 4 +- Content/Weapons/BP_GarlicWeapon.uasset | 3 + Content/Weapons/BP_WeaponComponent.uasset | 3 + Source/vampires/PlayerCharacter.cpp | 61 --------------- Source/vampires/PlayerCharacter.h | 26 ------- Source/vampires/Weapon.cpp | 1 + Source/vampires/Weapon.h | 12 +-- Source/vampires/WeaponInventoryComponent.cpp | 13 ++-- Source/vampires/WeaponInventoryComponent.h | 15 ++-- Source/vampires/Weapons/GarlicWeapon.cpp | 78 ++++++++++++++++++++ Source/vampires/Weapons/GarlicWeapon.h | 41 ++++++++++ 11 files changed, 146 insertions(+), 111 deletions(-) create mode 100644 Content/Weapons/BP_GarlicWeapon.uasset create mode 100644 Content/Weapons/BP_WeaponComponent.uasset create mode 100644 Source/vampires/Weapons/GarlicWeapon.cpp create mode 100644 Source/vampires/Weapons/GarlicWeapon.h diff --git a/Content/Player/BP_PlayerCharacter.uasset b/Content/Player/BP_PlayerCharacter.uasset index 2495726..f088298 100644 --- a/Content/Player/BP_PlayerCharacter.uasset +++ b/Content/Player/BP_PlayerCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:2b56ece8cdb103336edaf87d92b54b9e994cf6265d05371acf1dea8366bde98c -size 34413 +oid sha256:57d9b6b9043902336204e2ceb1a42686c144d67e90d2fecbe21fad8d910ded26 +size 52654 diff --git a/Content/Weapons/BP_GarlicWeapon.uasset b/Content/Weapons/BP_GarlicWeapon.uasset new file mode 100644 index 0000000..254beb2 --- /dev/null +++ b/Content/Weapons/BP_GarlicWeapon.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:75c5ed8746205c69fbaf087681aba1f1a5044153afa31b33bae991b44a410979 +size 23244 diff --git a/Content/Weapons/BP_WeaponComponent.uasset b/Content/Weapons/BP_WeaponComponent.uasset new file mode 100644 index 0000000..4555517 --- /dev/null +++ b/Content/Weapons/BP_WeaponComponent.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:31dbf62e78d8086d03e0ce49e0a9320895c945de590cd4a93d8aaad88e4d0414 +size 14093 diff --git a/Source/vampires/PlayerCharacter.cpp b/Source/vampires/PlayerCharacter.cpp index d3258ad..0c3fe87 100644 --- a/Source/vampires/PlayerCharacter.cpp +++ b/Source/vampires/PlayerCharacter.cpp @@ -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(TEXT("Gold Component")); - // Create Garlic Sphere Component - GarlicSphereComponent = CreateDefaultSubobject(TEXT("Garlic Sphere Component")); - GarlicSphereComponent->SetupAttachment(RootComponent); - GarlicSphereComponent->SetSphereRadius(150.0f); - //Create Weapon Inventory Component WeaponInventoryComponent = CreateDefaultSubobject(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(OtherActor)) - { - OverlappedEnemies.Add(Enemy); - } -} - -void APlayerCharacter::OnGarlicEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, - UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) -{ - if (AEnemyCharacter* Enemy = Cast(OtherActor)) - { - OverlappedEnemies.Remove(Enemy); - } -} - -void APlayerCharacter::GarlicUpdate() -{ - TArray 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; - } - } -} diff --git a/Source/vampires/PlayerCharacter.h b/Source/vampires/PlayerCharacter.h index 27ea35c..723d018 100644 --- a/Source/vampires/PlayerCharacter.h +++ b/Source/vampires/PlayerCharacter.h @@ -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 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(); }; diff --git a/Source/vampires/Weapon.cpp b/Source/vampires/Weapon.cpp index 8871eb8..986624f 100644 --- a/Source/vampires/Weapon.cpp +++ b/Source/vampires/Weapon.cpp @@ -18,6 +18,7 @@ void AWeapon::BeginPlay() Super::BeginPlay(); GetWorldTimerManager().SetTimer(WeaponTimerHandle, this, &AWeapon::FireWeaponAction, WeaponCooldown, true); UEXPComponent* expcomponent = GetOwner()->GetComponentByClass(); + if (expcomponent) { expcomponent->OnEXPLevelUp.BindUObject(this, &AWeapon::UpgradeWeapon); diff --git a/Source/vampires/Weapon.h b/Source/vampires/Weapon.h index 4fa9ac3..c19927a 100644 --- a/Source/vampires/Weapon.h +++ b/Source/vampires/Weapon.h @@ -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(); diff --git a/Source/vampires/WeaponInventoryComponent.cpp b/Source/vampires/WeaponInventoryComponent.cpp index 99411f7..55c5b39 100644 --- a/Source/vampires/WeaponInventoryComponent.cpp +++ b/Source/vampires/WeaponInventoryComponent.cpp @@ -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 Weapon) { - AWeapon* weapon = GetWorld()->SpawnActor(Weapon); - weapon->SetActorTransform(GetOwner()->GetTransform()); - weapon->SetOwner(GetOwner()); + FActorSpawnParameters SpawnParameters; + SpawnParameters.Owner = GetOwner(); + + AWeapon* weapon = GetWorld()->SpawnActor(Weapon, GetOwner()->GetTransform(), SpawnParameters); weapon->AttachToActor(GetOwner(), FAttachmentTransformRules::KeepRelativeTransform); inventory.Add(weapon); } - diff --git a/Source/vampires/WeaponInventoryComponent.h b/Source/vampires/WeaponInventoryComponent.h index 8ce8c87..6729d4a 100644 --- a/Source/vampires/WeaponInventoryComponent.h +++ b/Source/vampires/WeaponInventoryComponent.h @@ -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> initialInventory; - + private: UPROPERTY() TArray 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 Weapon); }; diff --git a/Source/vampires/Weapons/GarlicWeapon.cpp b/Source/vampires/Weapons/GarlicWeapon.cpp new file mode 100644 index 0000000..64bf876 --- /dev/null +++ b/Source/vampires/Weapons/GarlicWeapon.cpp @@ -0,0 +1,78 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "GarlicWeapon.h" + +AGarlicWeapon::AGarlicWeapon() +{ + SphereComponent = CreateDefaultSubobject(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 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(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(OtherActor)) + { + OverlappedEnemies.Add(Enemy); + } +} + +void AGarlicWeapon::OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) +{ + if (AEnemyCharacter* Enemy = Cast(OtherActor)) + { + OverlappedEnemies.Remove(Enemy); + } +} diff --git a/Source/vampires/Weapons/GarlicWeapon.h b/Source/vampires/Weapons/GarlicWeapon.h new file mode 100644 index 0000000..9b3f2ff --- /dev/null +++ b/Source/vampires/Weapons/GarlicWeapon.h @@ -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 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); +};