Get Previous Movement Direction using Inputable interface

This commit is contained in:
baz 2024-11-22 00:29:50 +00:00
parent dc5e4bb3a2
commit e07abfbcfc
5 changed files with 29 additions and 15 deletions

View File

@ -27,4 +27,7 @@ public:
UFUNCTION(BlueprintCallable, BlueprintNativeEvent) UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
UInputMappingContext* Input_GetInputMappingContext(); UInputMappingContext* Input_GetInputMappingContext();
UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
FVector2D Input_GetPreviousMovementDirection();
}; };

View File

@ -37,8 +37,6 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
UGoldComponent* GoldComponent; UGoldComponent* GoldComponent;
FVector2D PreviousMovementDirection = FVector2d(1.0f, 0.0f);
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
UWidgetComponent* HealthBarWidgetComponent; UWidgetComponent* HealthBarWidgetComponent;

View File

@ -41,6 +41,8 @@ void AVampireCharacter::Tick(float DeltaTime)
void AVampireCharacter::Input_Move_Implementation(FVector2D value) void AVampireCharacter::Input_Move_Implementation(FVector2D value)
{ {
PreviousMovementDirection = value;
if (value.Size() != 0.0f) if (value.Size() != 0.0f)
{ {
AddMovementInput({0.0f, 1.0f, 0.0f}, value.Y); AddMovementInput({0.0f, 1.0f, 0.0f}, value.Y);
@ -53,6 +55,11 @@ UInputMappingContext* AVampireCharacter::Input_GetInputMappingContext_Implementa
return InputMappingContext; return InputMappingContext;
} }
FVector2D AVampireCharacter::Input_GetPreviousMovementDirection_Implementation()
{
return PreviousMovementDirection;
}
UHealthComponent* AVampireCharacter::GetHealthComponent() UHealthComponent* AVampireCharacter::GetHealthComponent()
{ {
return HealthComponent; return HealthComponent;

View File

@ -21,6 +21,8 @@ public:
UPROPERTY(BlueprintReadWrite, EditAnywhere) UPROPERTY(BlueprintReadWrite, EditAnywhere)
UPaperFlipbookComponent* PaperFlipbookComponent; UPaperFlipbookComponent* PaperFlipbookComponent;
FVector2D PreviousMovementDirection = FVector2d(1.0f, 0.0f);
protected: protected:
UPROPERTY() UPROPERTY()
UHealthComponent* HealthComponent; UHealthComponent* HealthComponent;
@ -47,5 +49,7 @@ public:
virtual UInputMappingContext* Input_GetInputMappingContext_Implementation() override; virtual UInputMappingContext* Input_GetInputMappingContext_Implementation() override;
virtual FVector2D Input_GetPreviousMovementDirection_Implementation() override;
UHealthComponent* GetHealthComponent(); UHealthComponent* GetHealthComponent();
}; };

View File

@ -20,19 +20,21 @@ void AKnifeWeapon::FireWeaponAction_Implementation()
{ {
Super::FireWeaponAction_Implementation(); Super::FireWeaponAction_Implementation();
APlayerCharacter* playerCharacter = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); if (UKismetSystemLibrary::DoesImplementInterface(GetOwner(), UInputable::StaticClass()))
if (IsValid(ProjectileTemplate) && playerCharacter)
{ {
FActorSpawnParameters actorSpawnParameters; if (IsValid(ProjectileTemplate))
actorSpawnParameters.Owner = this; {
actorSpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; FActorSpawnParameters actorSpawnParameters;
actorSpawnParameters.TransformScaleMethod = ESpawnActorScaleMethod::MultiplyWithRoot; actorSpawnParameters.Owner = this;
actorSpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
actorSpawnParameters.TransformScaleMethod = ESpawnActorScaleMethod::MultiplyWithRoot;
AProjectile* projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(), AProjectile* projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters); actorSpawnParameters);
FVector direction = FVector(playerCharacter->PreviousMovementDirection, 0.0); FVector direction = FVector(IInputable::Execute_Input_GetPreviousMovementDirection(GetOwner()), 0.0);
direction.Normalize(); direction.Normalize();
projectile->SetTargetDirection(direction); projectile->SetTargetDirection(direction);
}
} }
} }