From e07abfbcfcab3cd3655157ed34737dea284adefa Mon Sep 17 00:00:00 2001 From: baz Date: Fri, 22 Nov 2024 00:29:50 +0000 Subject: [PATCH] Get Previous Movement Direction using Inputable interface --- Source/vampires/Inputable.h | 3 +++ Source/vampires/PlayerCharacter.h | 2 -- Source/vampires/VampireCharacter.cpp | 7 +++++++ Source/vampires/VampireCharacter.h | 6 +++++- Source/vampires/Weapons/KnifeWeapon.cpp | 26 +++++++++++++------------ 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/Source/vampires/Inputable.h b/Source/vampires/Inputable.h index 5cf1ff3..156c806 100644 --- a/Source/vampires/Inputable.h +++ b/Source/vampires/Inputable.h @@ -27,4 +27,7 @@ public: UFUNCTION(BlueprintCallable, BlueprintNativeEvent) UInputMappingContext* Input_GetInputMappingContext(); + + UFUNCTION(BlueprintCallable, BlueprintNativeEvent) + FVector2D Input_GetPreviousMovementDirection(); }; diff --git a/Source/vampires/PlayerCharacter.h b/Source/vampires/PlayerCharacter.h index f28f2b8..4d6db40 100644 --- a/Source/vampires/PlayerCharacter.h +++ b/Source/vampires/PlayerCharacter.h @@ -37,8 +37,6 @@ public: UPROPERTY(EditAnywhere, BlueprintReadWrite) UGoldComponent* GoldComponent; - FVector2D PreviousMovementDirection = FVector2d(1.0f, 0.0f); - UPROPERTY(EditAnywhere) UWidgetComponent* HealthBarWidgetComponent; diff --git a/Source/vampires/VampireCharacter.cpp b/Source/vampires/VampireCharacter.cpp index 84e73dd..0ddc3e1 100644 --- a/Source/vampires/VampireCharacter.cpp +++ b/Source/vampires/VampireCharacter.cpp @@ -41,6 +41,8 @@ void AVampireCharacter::Tick(float DeltaTime) void AVampireCharacter::Input_Move_Implementation(FVector2D value) { + PreviousMovementDirection = value; + if (value.Size() != 0.0f) { AddMovementInput({0.0f, 1.0f, 0.0f}, value.Y); @@ -53,6 +55,11 @@ UInputMappingContext* AVampireCharacter::Input_GetInputMappingContext_Implementa return InputMappingContext; } +FVector2D AVampireCharacter::Input_GetPreviousMovementDirection_Implementation() +{ + return PreviousMovementDirection; +} + UHealthComponent* AVampireCharacter::GetHealthComponent() { return HealthComponent; diff --git a/Source/vampires/VampireCharacter.h b/Source/vampires/VampireCharacter.h index 5564c0c..e3d676b 100644 --- a/Source/vampires/VampireCharacter.h +++ b/Source/vampires/VampireCharacter.h @@ -21,13 +21,15 @@ public: UPROPERTY(BlueprintReadWrite, EditAnywhere) UPaperFlipbookComponent* PaperFlipbookComponent; + FVector2D PreviousMovementDirection = FVector2d(1.0f, 0.0f); + protected: UPROPERTY() UHealthComponent* HealthComponent; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtr InputMappingContext; - + UPROPERTY(EditAnywhere, BlueprintReadWrite) UWeaponInventoryComponent* WeaponInventoryComponent; @@ -47,5 +49,7 @@ public: virtual UInputMappingContext* Input_GetInputMappingContext_Implementation() override; + virtual FVector2D Input_GetPreviousMovementDirection_Implementation() override; + UHealthComponent* GetHealthComponent(); }; diff --git a/Source/vampires/Weapons/KnifeWeapon.cpp b/Source/vampires/Weapons/KnifeWeapon.cpp index 884915e..6c91039 100644 --- a/Source/vampires/Weapons/KnifeWeapon.cpp +++ b/Source/vampires/Weapons/KnifeWeapon.cpp @@ -20,19 +20,21 @@ void AKnifeWeapon::FireWeaponAction_Implementation() { Super::FireWeaponAction_Implementation(); - APlayerCharacter* playerCharacter = Cast(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); - if (IsValid(ProjectileTemplate) && playerCharacter) + if (UKismetSystemLibrary::DoesImplementInterface(GetOwner(), UInputable::StaticClass())) { - FActorSpawnParameters actorSpawnParameters; - actorSpawnParameters.Owner = this; - actorSpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; - actorSpawnParameters.TransformScaleMethod = ESpawnActorScaleMethod::MultiplyWithRoot; + if (IsValid(ProjectileTemplate)) + { + FActorSpawnParameters actorSpawnParameters; + actorSpawnParameters.Owner = this; + actorSpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; + actorSpawnParameters.TransformScaleMethod = ESpawnActorScaleMethod::MultiplyWithRoot; - AProjectile* projectile = GetWorld()->SpawnActor(ProjectileTemplate, GetOwner()->GetTransform(), - actorSpawnParameters); + AProjectile* projectile = GetWorld()->SpawnActor(ProjectileTemplate, GetOwner()->GetTransform(), + actorSpawnParameters); - FVector direction = FVector(playerCharacter->PreviousMovementDirection, 0.0); - direction.Normalize(); - projectile->SetTargetDirection(direction); - } + FVector direction = FVector(IInputable::Execute_Input_GetPreviousMovementDirection(GetOwner()), 0.0); + direction.Normalize(); + projectile->SetTargetDirection(direction); + } + } }