From 8bf01234a0854907da9830295d54824259fa859b Mon Sep 17 00:00:00 2001 From: baz Date: Wed, 12 Jun 2024 19:06:51 +0100 Subject: [PATCH] Add EXPComponent --- Source/vampires/EXPComponent.cpp | 52 +++++++++++++++++++++++++++++ Source/vampires/EXPComponent.h | 48 ++++++++++++++++++++++++++ Source/vampires/PlayerCharacter.cpp | 14 +++++--- Source/vampires/PlayerCharacter.h | 10 +++--- 4 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 Source/vampires/EXPComponent.cpp create mode 100644 Source/vampires/EXPComponent.h diff --git a/Source/vampires/EXPComponent.cpp b/Source/vampires/EXPComponent.cpp new file mode 100644 index 0000000..670f703 --- /dev/null +++ b/Source/vampires/EXPComponent.cpp @@ -0,0 +1,52 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "EXPComponent.h" + +// Sets default values for this component's properties +UEXPComponent::UEXPComponent() +{ + // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features + // off to improve performance if you don't need them. + PrimaryComponentTick.bCanEverTick = false; + + // ... +} + +void UEXPComponent::IncrementEXP(int value) +{ + // TODO: I should be updating the level here + CurrentEXP += value; + OnEXPGained.ExecuteIfBound(); +} + +void UEXPComponent::SetCurrentEXP(int value) +{ + // TODO: I should be updating the level here + CurrentEXP = value; + OnEXPGained.ExecuteIfBound(); +} + +int UEXPComponent::GetCurrentEXP() +{ + return CurrentEXP; +} + +int UEXPComponent::GetCurrentLevel() +{ + return CurrentLevel; +} + +void UEXPComponent::Reset() +{ + CurrentEXP = 0; + CurrentLevel = 0; +} + +// Called when the game starts +void UEXPComponent::BeginPlay() +{ + Super::BeginPlay(); + + Reset(); +} diff --git a/Source/vampires/EXPComponent.h b/Source/vampires/EXPComponent.h new file mode 100644 index 0000000..021c89e --- /dev/null +++ b/Source/vampires/EXPComponent.h @@ -0,0 +1,48 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Components/ActorComponent.h" +#include "EXPComponent.generated.h" + +DECLARE_DELEGATE(FOnEXPGainedDelegate) +DECLARE_DELEGATE(FOnEXPLevelUpDelegate) + +UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent)) +class VAMPIRES_API UEXPComponent : public UActorComponent +{ + GENERATED_BODY() + +public: + FOnEXPGainedDelegate OnEXPGained; + FOnEXPLevelUpDelegate OnEXPLevelUp; + +protected: + int CurrentEXP = 0; + + int CurrentLevel = 0; + +public: + // Sets default values for this component's properties + UEXPComponent(); + + UFUNCTION() + void IncrementEXP(int value); + + UFUNCTION() + void SetCurrentEXP(int value); + + UFUNCTION() + int GetCurrentEXP(); + + UFUNCTION() + int GetCurrentLevel(); + + UFUNCTION() + void Reset(); + +protected: + // Called when the game starts + virtual void BeginPlay() override; +}; diff --git a/Source/vampires/PlayerCharacter.cpp b/Source/vampires/PlayerCharacter.cpp index 62f3e91..7f78c2a 100644 --- a/Source/vampires/PlayerCharacter.cpp +++ b/Source/vampires/PlayerCharacter.cpp @@ -17,7 +17,7 @@ APlayerCharacter::APlayerCharacter() CameraSpringArmComponent->bUsePawnControlRotation = false; CameraSpringArmComponent->TargetArmLength = 1000; CameraSpringArmComponent->bEnableCameraLag = false; - CameraSpringArmComponent->SocketOffset = { 0.0f, 0.0f, 0.0f }; + CameraSpringArmComponent->SocketOffset = {0.0f, 0.0f, 0.0f}; CameraSpringArmComponent->SetRelativeRotation({-90.0, 0.0f, 0.0f}); // Create Camera @@ -29,6 +29,9 @@ APlayerCharacter::APlayerCharacter() // Create Health Component HealthComponent = CreateDefaultSubobject(TEXT("Health Component")); + + // Create EXP Component + EXPComponent = CreateDefaultSubobject(TEXT("EXP Component")); } void APlayerCharacter::BeginPlay() @@ -42,12 +45,13 @@ void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputCom if (AVampirePlayerController* TankPlayerController = Cast(GetController())) { - if (UEnhancedInputLocalPlayerSubsystem* InputSystem = ULocalPlayer::GetSubsystem(TankPlayerController->GetLocalPlayer())) + if (UEnhancedInputLocalPlayerSubsystem* InputSystem = ULocalPlayer::GetSubsystem< + UEnhancedInputLocalPlayerSubsystem>(TankPlayerController->GetLocalPlayer())) { if (!InputMappingContext.IsNull()) { InputSystem->AddMappingContext(InputMappingContext.LoadSynchronous(), 0); - } + } } } @@ -66,7 +70,7 @@ void APlayerCharacter::MovementCallback(const FInputActionInstance& Instance) if (vec2.Size() != 0.0f) { - AddMovementInput({0.0f,1.0f,0.0f}, vec2.Y); - AddMovementInput({1.0f,0.0f,0.0f}, vec2.X); + AddMovementInput({0.0f, 1.0f, 0.0f}, vec2.Y); + AddMovementInput({1.0f, 0.0f, 0.0f}, vec2.X); } } diff --git a/Source/vampires/PlayerCharacter.h b/Source/vampires/PlayerCharacter.h index 7c40b8e..294bbd1 100644 --- a/Source/vampires/PlayerCharacter.h +++ b/Source/vampires/PlayerCharacter.h @@ -3,6 +3,7 @@ #pragma once #include "CoreMinimal.h" +#include "EXPComponent.h" #include "HealthComponent.h" #include "VampireCharacter.h" #include "Camera/CameraComponent.h" @@ -21,7 +22,6 @@ class VAMPIRES_API APlayerCharacter : public AVampireCharacter GENERATED_BODY() public: - UPROPERTY(EditAnywhere, BlueprintReadWrite) USpringArmComponent* CameraSpringArmComponent = nullptr; @@ -34,11 +34,14 @@ public: UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UInputAction* MovementAction; +protected: UPROPERTY() UHealthComponent* HealthComponent; + UPROPERTY() + UEXPComponent* EXPComponent; + public: - APlayerCharacter(); protected: @@ -48,9 +51,6 @@ public: virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override; private: - UFUNCTION() void MovementCallback(const FInputActionInstance& Instance); }; - -