From e8cacbe1c7c553a0ae9573d8c1b7df9df19de7f9 Mon Sep 17 00:00:00 2001 From: Ji Yoon Rhee Date: Thu, 22 Aug 2024 23:24:14 +0900 Subject: [PATCH] the player character can automatically pick up souls --- .../Source/hwanyoung2/HYAutomaticPickUp.cpp | 44 +++++++++++++++++++ .../Source/hwanyoung2/HYAutomaticPickUp.h | 38 ++++++++++++++++ .../Source/hwanyoung2/HYMoneyAutoPickUp.cpp | 18 ++++++++ .../Source/hwanyoung2/HYMoneyAutoPickUp.h | 26 +++++++++++ .../hwanyoung2/HYPlayerCharacController.h | 3 ++ .../Source/hwanyoung2/InventoryItem.cpp | 1 + hwanyoung2/Source/hwanyoung2/InventoryItem.h | 4 ++ .../Source/hwanyoung2/hwanyoung2Character.cpp | 22 ++++++++++ .../Source/hwanyoung2/hwanyoung2Character.h | 5 ++- 9 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 hwanyoung2/Source/hwanyoung2/HYAutomaticPickUp.cpp create mode 100644 hwanyoung2/Source/hwanyoung2/HYAutomaticPickUp.h create mode 100644 hwanyoung2/Source/hwanyoung2/HYMoneyAutoPickUp.cpp create mode 100644 hwanyoung2/Source/hwanyoung2/HYMoneyAutoPickUp.h diff --git a/hwanyoung2/Source/hwanyoung2/HYAutomaticPickUp.cpp b/hwanyoung2/Source/hwanyoung2/HYAutomaticPickUp.cpp new file mode 100644 index 00000000..9b4f1cf8 --- /dev/null +++ b/hwanyoung2/Source/hwanyoung2/HYAutomaticPickUp.cpp @@ -0,0 +1,44 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "HYAutomaticPickUp.h" +#include "HYPlayerCharacController.h" + +// Sets default values +AHYAutomaticPickUp::AHYAutomaticPickUp() +{ + // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. + PrimaryActorTick.bCanEverTick = true; + PickupMesh = CreateDefaultSubobject("PickupMesh"); + RootComponent = Cast(PickupMesh); + + ItemID = FName("No ID"); + +} + +void AHYAutomaticPickUp::Collect_Implementation(APlayerController* Controller) +{ + AHYPlayerCharacController* IController = Cast(Controller); + if (IController->AddItemToInventoryByID(ItemID)) + Destroy(); +} + +FName AHYAutomaticPickUp::GetItemID() +{ + return ItemID; +} + +// Called when the game starts or when spawned +void AHYAutomaticPickUp::BeginPlay() +{ + Super::BeginPlay(); + +} + +// Called every frame +void AHYAutomaticPickUp::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); + +} + diff --git a/hwanyoung2/Source/hwanyoung2/HYAutomaticPickUp.h b/hwanyoung2/Source/hwanyoung2/HYAutomaticPickUp.h new file mode 100644 index 00000000..674c55a9 --- /dev/null +++ b/hwanyoung2/Source/hwanyoung2/HYAutomaticPickUp.h @@ -0,0 +1,38 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/Actor.h" +#include "HYAutomaticPickUp.generated.h" + +UCLASS() +class HWANYOUNG2_API AHYAutomaticPickUp : public AActor +{ + GENERATED_BODY() + +public: + // Sets default values for this actor's properties + AHYAutomaticPickUp(); + + UFUNCTION(BlueprintNativeEvent) + void Collect(APlayerController* Controller); + virtual void Collect_Implementation(APlayerController* Controller); + + FName GetItemID(); + +protected: + // Called when the game starts or when spawned + virtual void BeginPlay() override; + + UPROPERTY(EditAnywhere) + UStaticMeshComponent* PickupMesh; + + UPROPERTY(EditAnywhere, BlueprintReadWrite) + FName ItemID; + +public: + // Called every frame + virtual void Tick(float DeltaTime) override; + +}; diff --git a/hwanyoung2/Source/hwanyoung2/HYMoneyAutoPickUp.cpp b/hwanyoung2/Source/hwanyoung2/HYMoneyAutoPickUp.cpp new file mode 100644 index 00000000..a2fc8a75 --- /dev/null +++ b/hwanyoung2/Source/hwanyoung2/HYMoneyAutoPickUp.cpp @@ -0,0 +1,18 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "HYMoneyAutoPickUp.h" +#include "HYPlayerCharacController.h" + +AHYMoneyAutoPickUp::AHYMoneyAutoPickUp() +{ + Super::ItemID = FName("Lost soul"); + Value = 1; +} + +void AHYMoneyAutoPickUp::Collect_Implementation(APlayerController* Controller) +{ + AHYPlayerCharacController* IController = Cast(Controller); + IController->Currency += Value; + Destroy(); +} diff --git a/hwanyoung2/Source/hwanyoung2/HYMoneyAutoPickUp.h b/hwanyoung2/Source/hwanyoung2/HYMoneyAutoPickUp.h new file mode 100644 index 00000000..2b233a52 --- /dev/null +++ b/hwanyoung2/Source/hwanyoung2/HYMoneyAutoPickUp.h @@ -0,0 +1,26 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "HYAutomaticPickUp.h" +#include "HYMoneyAutoPickUp.generated.h" + +/** + * + */ +UCLASS() +class HWANYOUNG2_API AHYMoneyAutoPickUp : public AHYAutomaticPickUp +{ + GENERATED_BODY() + +public: + AHYMoneyAutoPickUp(); + + void Collect_Implementation(APlayerController* Controller) override; + +protected: + UPROPERTY(EditAnywhere, BlueprintReadWrite) + int32 Value; + +}; diff --git a/hwanyoung2/Source/hwanyoung2/HYPlayerCharacController.h b/hwanyoung2/Source/hwanyoung2/HYPlayerCharacController.h index 4aa47006..035fa8c5 100644 --- a/hwanyoung2/Source/hwanyoung2/HYPlayerCharacController.h +++ b/hwanyoung2/Source/hwanyoung2/HYPlayerCharacController.h @@ -43,6 +43,9 @@ public: UPROPERTY(EditAnywhere, BlueprintReadWrite) int32 InventoryWeightLimit; + UPROPERTY(EditAnywhere, BlueprintReadWrite) + int32 Currency; + //interacts with the Interactable objects UFUNCTION(BlueprintCallable, Category = "Utils") void Interact(); diff --git a/hwanyoung2/Source/hwanyoung2/InventoryItem.cpp b/hwanyoung2/Source/hwanyoung2/InventoryItem.cpp index 0548a054..fd8f098c 100644 --- a/hwanyoung2/Source/hwanyoung2/InventoryItem.cpp +++ b/hwanyoung2/Source/hwanyoung2/InventoryItem.cpp @@ -7,5 +7,6 @@ FInventoryItem::FInventoryItem() { this->ItemName = FText::FromString("No Name"); this->ItemWeight = 1; + this->ItemValue = 1; this->ItemDescription = FText::FromString("No Description"); } diff --git a/hwanyoung2/Source/hwanyoung2/InventoryItem.h b/hwanyoung2/Source/hwanyoung2/InventoryItem.h index f81e44f2..c9f14748 100644 --- a/hwanyoung2/Source/hwanyoung2/InventoryItem.h +++ b/hwanyoung2/Source/hwanyoung2/InventoryItem.h @@ -37,6 +37,10 @@ public: UPROPERTY(EditAnywhere, BlueprintReadWrite) int32 ItemWeight; + //value of the item + UPROPERTY(EditAnywhere, BlueprintReadWrite) + int32 ItemValue; + //is the item only available through drops? UPROPERTY(EditAnywhere, BlueprintReadWrite) bool OnlyDropped; diff --git a/hwanyoung2/Source/hwanyoung2/hwanyoung2Character.cpp b/hwanyoung2/Source/hwanyoung2/hwanyoung2Character.cpp index 77720d47..701cd7d4 100644 --- a/hwanyoung2/Source/hwanyoung2/hwanyoung2Character.cpp +++ b/hwanyoung2/Source/hwanyoung2/hwanyoung2Character.cpp @@ -12,6 +12,7 @@ #include "EnhancedInputSubsystems.h" #include "HYInteractableActor.h" #include "InventoryItem.h" +#include "HYAutomaticPickUp.h" @@ -97,12 +98,29 @@ void Ahwanyoung2Character::CheckForInteractables() } +void Ahwanyoung2Character::CollectAutoPickups() +{ + // Stores all the overlapping actors in an array + TArray CollectedActors; + CollectionSphere->GetOverlappingActors(CollectedActors); + + AHYPlayerCharacController* IController = Cast(GetController()); + + for (int32 indCollect = 0; indCollect < CollectedActors.Num(); ++indCollect) { + AHYAutomaticPickUp* const Pickup = Cast(CollectedActors[indCollect]); + if (Pickup && !Pickup->IsPendingKill()) { + Pickup->Collect(IController); + } + } +} + void Ahwanyoung2Character::Tick(float DeltaTime) { Super::Tick(DeltaTime); CheckForInteractables(); + CollectAutoPickups(); } ////////////////////////////////////////////////////////////////////////// @@ -201,6 +219,10 @@ void Ahwanyoung2Character::Initialize() // Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character) // are set in the derived blueprint asset named ThirdPersonCharacter (to avoid direct content references in C++) + + CollectionSphere = CreateDefaultSubobject(TEXT("CollectionSphere")); + CollectionSphere->SetupAttachment(RootComponent); + CollectionSphere->SetSphereRadius(200.f); } diff --git a/hwanyoung2/Source/hwanyoung2/hwanyoung2Character.h b/hwanyoung2/Source/hwanyoung2/hwanyoung2Character.h index c231b135..f6f4bc22 100644 --- a/hwanyoung2/Source/hwanyoung2/hwanyoung2Character.h +++ b/hwanyoung2/Source/hwanyoung2/hwanyoung2Character.h @@ -6,6 +6,7 @@ #include "GameFramework/Character.h" #include "InputAction.h" #include "hwanyoung2.h" +#include "Components/SphereComponent.h" #include "hwanyoung2Character.generated.h" @@ -102,7 +103,9 @@ protected: /** Called for checking for the closest Interactable in sight and in range*/ void CheckForInteractables(); - + + /** Called for collecting automatic pick-upable interactables*/ + void CollectAutoPickups(); protected: // APawn interface