Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright Damir Halilovic 2014 - 2015
- #pragma once
- #include "GameFramework/Actor.h"
- #include "RangedWeaponBase.h"
- #include "ProjectileBase.generated.h"
- UCLASS()
- class PROJECTWA_API AProjectileBase : public AActor
- {
- GENERATED_BODY()
- public:
- UPROPERTY(Category = "Wa Weapons", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
- UStaticMeshComponent* MeshComponent;
- UPROPERTY(Category = "Wa Weapons", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
- UProjectileMovementComponent* ProjectileMovement;
- public:
- UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Wa Weapons")
- bool bPhysicsProjectile;
- UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Wa Weapons")
- bool bPhysicsAfterHit;
- UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Wa Weapons")
- bool bDestroyAfterHit;
- public:
- UFUNCTION(BlueprintCallable, Category = "Wa Weapons")
- void FireProjectile(const FVector& Direction, const float SpeedMultiplier = 1.f, const bool bWorldSpace = true);
- UFUNCTION(BlueprintCallable, Category = "Wa Weapons")
- void StopProjectile();
- UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Wa Weapons")
- ARangedWeaponBase* GetParentWeapon(bool& bIsValid);
- ARangedWeaponBase* GetParentWeapon();
- void SetParentWeapon(ARangedWeaponBase* Weapon);
- public:
- // Sets default values for this actor's properties
- AProjectileBase();
- virtual void Tick(float DeltaTime) override;
- // Called when the game starts or when spawned
- virtual void BeginPlay() override;
- virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
- private:
- AActor* ThrowingActor;
- ARangedWeaponBase* ParentWeapon;
- bool bDoneDamage;
- float StartSpeed;
- UFUNCTION()
- void OnOverlapStart(AActor* Other);
- UFUNCTION()
- void OnOverlapEnd(AActor* Other);
- FVector LastLocation;
- FVector DeltaLocation;
- };
- /////////////////////////////////////////////////////CPP
- // Copyright Damir Halilovic 2014 - 2015
- #include "ProjectWa.h"
- #include "ProjectileBase.h"
- // Sets default values
- AProjectileBase::AProjectileBase()
- {
- // 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;
- MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>("Projectile Mesh");
- if (MeshComponent != nullptr)
- {
- RootComponent = MeshComponent;
- }
- ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>("Projectile Movement");
- if (ProjectileMovement != nullptr)
- {
- ProjectileMovement->bAutoActivate = false;
- ProjectileMovement->SetUpdatedComponent(RootComponent);
- }
- }
- // Called when the game starts or when spawned
- void AProjectileBase::BeginPlay()
- {
- Super::BeginPlay();
- OnActorBeginOverlap.AddDynamic(this, &AProjectileBase::OnOverlapStart);
- OnActorEndOverlap.AddDynamic(this, &AProjectileBase::OnOverlapEnd);
- StartSpeed = ProjectileMovement->InitialSpeed;
- }
- void AProjectileBase::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- if (ProjectileMovement->IsActive())
- {
- DeltaLocation = GetActorLocation() - LastLocation;
- LastLocation = GetActorLocation();
- if (DeltaLocation.Size() < 0.1f)
- {
- if (bDestroyAfterHit)
- {
- bDoneDamage = true;
- MeshComponent->SetVisibility(false, true);
- }
- else if (bPhysicsAfterHit)
- {
- ProjectileMovement->Deactivate();
- MeshComponent->SetSimulatePhysics(true);
- }
- }
- }
- }
- void AProjectileBase::EndPlay(const EEndPlayReason::Type EndPlayReason)
- {
- Super::EndPlay(EndPlayReason);
- OnActorBeginOverlap.RemoveDynamic(this, &AProjectileBase::OnOverlapStart);
- OnActorEndOverlap.RemoveDynamic(this, &AProjectileBase::OnOverlapEnd);
- }
- void AProjectileBase::FireProjectile(const FVector& Direction, const float SpeedMultiplier, const bool bWorldSpace /* = true */)
- {
- ParentWeapon->ClearDamagedActors();
- FTransform OldTransform = MeshComponent->GetComponentTransform();
- MeshComponent->SetAbsolute(true, true, true);
- MeshComponent->SetWorldTransform(OldTransform);
- LastLocation = OldTransform.GetLocation();
- ProjectileMovement->InitialSpeed = StartSpeed * SpeedMultiplier;
- FVector Velocity = Direction.GetSafeNormal() * ProjectileMovement->InitialSpeed;
- if (bWorldSpace)
- {
- ProjectileMovement->Velocity = Velocity;
- }
- else
- {
- ProjectileMovement->SetVelocityInLocalSpace(Velocity);
- }
- UE_LOG(WaGeneral, Log, TEXT("Got all the way down to the actual projectile. %f"), ProjectileMovement->InitialSpeed);
- MeshComponent->SetSimulatePhysics(bPhysicsProjectile);
- ProjectileMovement->Activate(true);
- }
- void AProjectileBase::StopProjectile()
- {
- MeshComponent->SetSimulatePhysics(false);
- ProjectileMovement->Deactivate();
- }
- ARangedWeaponBase* AProjectileBase::GetParentWeapon(bool& bIsValid)
- {
- bIsValid = ParentWeapon != nullptr;
- return ParentWeapon;
- }
- ARangedWeaponBase* AProjectileBase::GetParentWeapon()
- {
- bool bIsValid = false;
- return GetParentWeapon(bIsValid);
- }
- void AProjectileBase::OnOverlapStart(AActor* Other)
- {
- UE_LOG(WaGeneral, Log, TEXT("Overlapping %s %f"), *Other->GetName(), DeltaLocation.Size());
- if (bDoneDamage || DeltaLocation.Size() < 0.1f)
- {
- return;
- }
- else
- {
- bool bValidWeapon = false;
- ARangedWeaponBase* Weapon = GetParentWeapon(bValidWeapon);
- if (bValidWeapon)
- {
- bDoneDamage = Weapon->ProcessProjectileDamage(Other);
- if (bDoneDamage)
- {
- if (bDestroyAfterHit)
- {
- MeshComponent->SetVisibility(false, true);
- }
- else if (bPhysicsAfterHit)
- {
- ProjectileMovement->Deactivate();
- MeshComponent->SetSimulatePhysics(true);
- }
- }
- }
- }
- }
- void AProjectileBase::OnOverlapEnd(AActor* Other)
- {
- if (Other == ThrowingActor)
- {
- UE_LOG(WaGeneral, Log, TEXT("End overlap with %s"), *ThrowingActor->GetName());
- MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
- }
- }
- void AProjectileBase::SetParentWeapon(ARangedWeaponBase* Weapon)
- {
- ParentWeapon = Weapon;
- ThrowingActor = Weapon->GetOwnerPawn();
- MeshComponent->IgnoreActorWhenMoving(ThrowingActor, true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement