From 4fa8210707d61e36e108a523064988a4740b2c8c Mon Sep 17 00:00:00 2001 From: baz Date: Wed, 13 Nov 2024 23:39:08 +0000 Subject: [PATCH] Update Projectile to use a ProjectileMovementComponent --- Content/Player/BP_PlayerCharacter.uasset | 4 ++-- .../MagicWand/BP_MagicWandProjectile.uasset | 4 ++-- Source/vampires/Projectile.cpp | 23 +++++++++++-------- Source/vampires/Projectile.h | 13 +++++++---- Source/vampires/Weapons/FireWandWeapon.cpp | 2 +- Source/vampires/Weapons/GarlicWeapon.cpp | 2 +- Source/vampires/Weapons/GarlicWeapon.h | 4 ++-- Source/vampires/Weapons/GunWeapon.cpp | 8 +++---- Source/vampires/Weapons/KnifeWeapon.cpp | 2 +- Source/vampires/Weapons/MagicWandWeapon.cpp | 2 +- 10 files changed, 37 insertions(+), 27 deletions(-) diff --git a/Content/Player/BP_PlayerCharacter.uasset b/Content/Player/BP_PlayerCharacter.uasset index 168ce0d..4d93cdf 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:d6b3052c9ed4804cdaeae311229341b50b1512193b324a34d324ac0e7b960cc6 -size 53705 +oid sha256:160b5fb612b6e3066f11fcd0e06a80791e506f509d590557040440b5e9ac93cf +size 53734 diff --git a/Content/Weapons/MagicWand/BP_MagicWandProjectile.uasset b/Content/Weapons/MagicWand/BP_MagicWandProjectile.uasset index 928146e..a3dae4f 100644 --- a/Content/Weapons/MagicWand/BP_MagicWandProjectile.uasset +++ b/Content/Weapons/MagicWand/BP_MagicWandProjectile.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7c2d58cdb689ca105f0ccf3be23f6c14baaae9bee870f4480eaabc533a9a837c -size 30683 +oid sha256:eaa8647597a45cde00d8ad66e0a4a119835ac76a9112f808ddf6bb62dc59a5d2 +size 30952 diff --git a/Source/vampires/Projectile.cpp b/Source/vampires/Projectile.cpp index 42c7546..9e12c96 100644 --- a/Source/vampires/Projectile.cpp +++ b/Source/vampires/Projectile.cpp @@ -3,17 +3,20 @@ #include "Projectile.h" +#include "Components/SphereComponent.h" +#include "GameFramework/ProjectileMovementComponent.h" #include "Weapons/ProjectileWeapon.h" // Sets default values AProjectile::AProjectile() -{ - // 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; - - SphereComponent = CreateDefaultSubobject(TEXT("Sphere Component")); +{ SphereComponent = CreateDefaultSubobject(TEXT("Sphere Component")); SetRootComponent(SphereComponent); SphereComponent->SetSphereRadius(50.0f); + + ProjectileMovement = CreateDefaultSubobject(TEXT("Projectile Movement")); + ProjectileMovement->ProjectileGravityScale = 0.0f; + ProjectileMovement->Friction = 0.0f;; + ProjectileMovement->bIsSliding = true; } // Called when the game starts or when spawned @@ -22,12 +25,14 @@ void AProjectile::BeginPlay() Super::BeginPlay(); AProjectileWeapon* OwnerWeapon = Cast(GetOwner()); SphereComponent->OnComponentBeginOverlap.AddDynamic(OwnerWeapon, &AProjectileWeapon::OnProjectileBeginOverlap); + + ProjectileMovement->InitialSpeed = ProjectileSpeed; + ProjectileMovement->MaxSpeed = ProjectileSpeed; } -// Called every frame -void AProjectile::Tick(float DeltaTime) +void AProjectile::SetTargetDirection(FVector direction) { - Super::Tick(DeltaTime); - SetActorLocation(GetActorLocation() + (TargetDirection * ProjectileSpeed)); + TargetDirection = direction; + ProjectileMovement->SetVelocityInLocalSpace(TargetDirection * ProjectileSpeed); } diff --git a/Source/vampires/Projectile.h b/Source/vampires/Projectile.h index 9427d1f..5165f10 100644 --- a/Source/vampires/Projectile.h +++ b/Source/vampires/Projectile.h @@ -3,10 +3,12 @@ #pragma once #include "CoreMinimal.h" -#include "Components/SphereComponent.h" #include "GameFramework/Actor.h" #include "Projectile.generated.h" +class UProjectileMovementComponent; +class USphereComponent; + UCLASS() class VAMPIRES_API AProjectile : public AActor { @@ -16,10 +18,13 @@ public: UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) USphereComponent* SphereComponent = nullptr; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + UProjectileMovementComponent* ProjectileMovement = nullptr; + FVector TargetDirection = FVector::ZeroVector; UPROPERTY(EditAnywhere, BlueprintReadWrite) - float ProjectileSpeed = 1.0f; + float ProjectileSpeed = 500.0f; public: // Sets default values for this actor's properties @@ -30,6 +35,6 @@ protected: virtual void BeginPlay() override; public: - // Called every frame - virtual void Tick(float DeltaTime) override; + + void SetTargetDirection(FVector direction); }; diff --git a/Source/vampires/Weapons/FireWandWeapon.cpp b/Source/vampires/Weapons/FireWandWeapon.cpp index 3bd6897..66914cc 100644 --- a/Source/vampires/Weapons/FireWandWeapon.cpp +++ b/Source/vampires/Weapons/FireWandWeapon.cpp @@ -34,6 +34,6 @@ void AFireWandWeapon::FireWeaponAction_Implementation() direction.Z = 0.0; direction.Normalize(); - projectile->TargetDirection = direction; + projectile->SetTargetDirection(direction); } } diff --git a/Source/vampires/Weapons/GarlicWeapon.cpp b/Source/vampires/Weapons/GarlicWeapon.cpp index 5e36a59..0cee41e 100644 --- a/Source/vampires/Weapons/GarlicWeapon.cpp +++ b/Source/vampires/Weapons/GarlicWeapon.cpp @@ -80,7 +80,7 @@ void AGarlicWeapon::GarlicFireWeaponAction(FOverlappedEnemy EnemyCharacter) } EnemyHealthComponent->TakeDamage(EnemyCharacter.OverlappedEnemyCharacter, Damage, nullptr, - ownerController, this); + ownerController, this); } } } diff --git a/Source/vampires/Weapons/GarlicWeapon.h b/Source/vampires/Weapons/GarlicWeapon.h index b23b8b0..cc2ac3f 100644 --- a/Source/vampires/Weapons/GarlicWeapon.h +++ b/Source/vampires/Weapons/GarlicWeapon.h @@ -12,11 +12,11 @@ USTRUCT() struct FOverlappedEnemy { GENERATED_BODY() - + AEnemyCharacter* OverlappedEnemyCharacter; FTimerHandle OverlappedTimerHandle; -}; +}; /** * diff --git a/Source/vampires/Weapons/GunWeapon.cpp b/Source/vampires/Weapons/GunWeapon.cpp index 7f168bb..dd23fd2 100644 --- a/Source/vampires/Weapons/GunWeapon.cpp +++ b/Source/vampires/Weapons/GunWeapon.cpp @@ -53,18 +53,18 @@ void AGunWeapon::FireWeaponAction_Implementation() AProjectile* projectile = GetWorld()->SpawnActor(ProjectileTemplate, GetOwner()->GetTransform(), actorSpawnParameters); - projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopLeft); + projectile->SetTargetDirection(UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopLeft)); projectile = GetWorld()->SpawnActor(ProjectileTemplate, GetOwner()->GetTransform(), actorSpawnParameters); - projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopRight); + projectile->SetTargetDirection(UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopRight)); projectile = GetWorld()->SpawnActor(ProjectileTemplate, GetOwner()->GetTransform(), actorSpawnParameters); - projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, BottomLeft); + projectile->SetTargetDirection(UKismetMathLibrary::GetDirectionUnitVector(actorLocation, BottomLeft)); projectile = GetWorld()->SpawnActor(ProjectileTemplate, GetOwner()->GetTransform(), actorSpawnParameters); - projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, BottomRight); + projectile->SetTargetDirection(UKismetMathLibrary::GetDirectionUnitVector(actorLocation, BottomRight)); } } \ No newline at end of file diff --git a/Source/vampires/Weapons/KnifeWeapon.cpp b/Source/vampires/Weapons/KnifeWeapon.cpp index dcf4947..7080aba 100644 --- a/Source/vampires/Weapons/KnifeWeapon.cpp +++ b/Source/vampires/Weapons/KnifeWeapon.cpp @@ -32,6 +32,6 @@ void AKnifeWeapon::FireWeaponAction_Implementation() FVector direction = FVector(playerCharacter->PreviousMovementDirection, 0.0); direction.Normalize(); - projectile->TargetDirection = direction; + projectile->SetTargetDirection(direction); } } diff --git a/Source/vampires/Weapons/MagicWandWeapon.cpp b/Source/vampires/Weapons/MagicWandWeapon.cpp index d5ff066..0533cdb 100644 --- a/Source/vampires/Weapons/MagicWandWeapon.cpp +++ b/Source/vampires/Weapons/MagicWandWeapon.cpp @@ -37,7 +37,7 @@ void AMagicWandWeapon::FireWeaponAction_Implementation() GetActorLocation(), nearestActor->GetActorLocation()); direction.Z = 0.0; direction.Normalize(); - projectile->TargetDirection = direction; + projectile->SetTargetDirection(direction); } } }