Advertisement
Guest User

Untitled

a guest
Jun 16th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.10 KB | None | 0 0
  1. // Copyright Damir Halilovic 2014 - 2015
  2.  
  3. #pragma once
  4.  
  5. #include "GameFramework/Actor.h"
  6. #include "RangedWeaponBase.h"
  7. #include "ProjectileBase.generated.h"
  8.  
  9. UCLASS()
  10. class PROJECTWA_API AProjectileBase : public AActor
  11. {
  12.     GENERATED_BODY()
  13.  
  14. public:
  15.     UPROPERTY(Category = "Wa Weapons", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
  16.     UStaticMeshComponent* MeshComponent;
  17.  
  18.     UPROPERTY(Category = "Wa Weapons", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
  19.     UProjectileMovementComponent* ProjectileMovement;
  20.  
  21. public:
  22.     UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Wa Weapons")
  23.     bool bPhysicsProjectile;
  24.     UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Wa Weapons")
  25.     bool bPhysicsAfterHit;
  26.     UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Wa Weapons")
  27.     bool bDestroyAfterHit;
  28.    
  29.  
  30. public:
  31.     UFUNCTION(BlueprintCallable, Category = "Wa Weapons")
  32.     void FireProjectile(const FVector& Direction, const float SpeedMultiplier = 1.f, const bool bWorldSpace = true);
  33.     UFUNCTION(BlueprintCallable, Category = "Wa Weapons")
  34.     void StopProjectile();
  35.     UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Wa Weapons")
  36.     ARangedWeaponBase* GetParentWeapon(bool& bIsValid);
  37.     ARangedWeaponBase* GetParentWeapon();
  38.     void SetParentWeapon(ARangedWeaponBase* Weapon);
  39.  
  40. public:
  41.     // Sets default values for this actor's properties
  42.     AProjectileBase();
  43.  
  44.     virtual void Tick(float DeltaTime) override;
  45.  
  46.     // Called when the game starts or when spawned
  47.     virtual void BeginPlay() override;
  48.    
  49.     virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
  50.  
  51. private:
  52.     AActor* ThrowingActor;
  53.     ARangedWeaponBase* ParentWeapon;
  54.     bool bDoneDamage;
  55.     float StartSpeed;
  56.     UFUNCTION()
  57.     void OnOverlapStart(AActor* Other);
  58.     UFUNCTION()
  59.     void OnOverlapEnd(AActor* Other);
  60.  
  61.     FVector LastLocation;
  62.     FVector DeltaLocation;
  63.    
  64. };
  65.  
  66.  
  67.  
  68. /////////////////////////////////////////////////////CPP
  69.  
  70.  
  71. // Copyright Damir Halilovic 2014 - 2015
  72.  
  73. #include "ProjectWa.h"
  74. #include "ProjectileBase.h"
  75.  
  76.  
  77. // Sets default values
  78. AProjectileBase::AProjectileBase()
  79. {
  80.     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  81.     PrimaryActorTick.bCanEverTick = true;
  82.  
  83.     MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>("Projectile Mesh");
  84.     if (MeshComponent != nullptr)
  85.     {
  86.         RootComponent = MeshComponent;
  87.     }
  88.  
  89.     ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>("Projectile Movement");
  90.     if (ProjectileMovement != nullptr)
  91.     {
  92.         ProjectileMovement->bAutoActivate = false;
  93.         ProjectileMovement->SetUpdatedComponent(RootComponent);
  94.     }
  95. }
  96.  
  97. // Called when the game starts or when spawned
  98. void AProjectileBase::BeginPlay()
  99. {
  100.     Super::BeginPlay();
  101.     OnActorBeginOverlap.AddDynamic(this, &AProjectileBase::OnOverlapStart);
  102.     OnActorEndOverlap.AddDynamic(this, &AProjectileBase::OnOverlapEnd);
  103.     StartSpeed = ProjectileMovement->InitialSpeed; 
  104.  
  105. }
  106.  
  107. void AProjectileBase::Tick(float DeltaTime)
  108. {
  109.     Super::Tick(DeltaTime);
  110.     if (ProjectileMovement->IsActive())
  111.     {
  112.         DeltaLocation = GetActorLocation() - LastLocation;
  113.         LastLocation = GetActorLocation();
  114.  
  115.         if (DeltaLocation.Size() < 0.1f)
  116.         {
  117.             if (bDestroyAfterHit)
  118.             {
  119.                 bDoneDamage = true;
  120.                 MeshComponent->SetVisibility(false, true);
  121.             }
  122.             else if (bPhysicsAfterHit)
  123.             {
  124.                 ProjectileMovement->Deactivate();
  125.                 MeshComponent->SetSimulatePhysics(true);
  126.             }
  127.         }
  128.     }
  129. }
  130.  
  131. void AProjectileBase::EndPlay(const EEndPlayReason::Type EndPlayReason)
  132. {
  133.     Super::EndPlay(EndPlayReason);
  134.  
  135.     OnActorBeginOverlap.RemoveDynamic(this, &AProjectileBase::OnOverlapStart);
  136.     OnActorEndOverlap.RemoveDynamic(this, &AProjectileBase::OnOverlapEnd);
  137. }
  138.  
  139.  
  140.  
  141. void AProjectileBase::FireProjectile(const FVector& Direction, const float SpeedMultiplier, const bool bWorldSpace /* = true */)
  142. {
  143.     ParentWeapon->ClearDamagedActors();
  144.     FTransform OldTransform = MeshComponent->GetComponentTransform();
  145.     MeshComponent->SetAbsolute(true, true, true);
  146.     MeshComponent->SetWorldTransform(OldTransform);
  147.  
  148.     LastLocation = OldTransform.GetLocation();
  149.  
  150.     ProjectileMovement->InitialSpeed = StartSpeed * SpeedMultiplier;
  151.     FVector Velocity = Direction.GetSafeNormal() * ProjectileMovement->InitialSpeed;
  152.     if (bWorldSpace)
  153.     {
  154.         ProjectileMovement->Velocity = Velocity;
  155.     }
  156.     else
  157.     {
  158.         ProjectileMovement->SetVelocityInLocalSpace(Velocity);
  159.     }
  160.  
  161.     UE_LOG(WaGeneral, Log, TEXT("Got all the way down to the actual projectile. %f"), ProjectileMovement->InitialSpeed);
  162.  
  163.    
  164.     MeshComponent->SetSimulatePhysics(bPhysicsProjectile);
  165.     ProjectileMovement->Activate(true);
  166. }
  167.  
  168. void AProjectileBase::StopProjectile()
  169. {
  170.     MeshComponent->SetSimulatePhysics(false);
  171.     ProjectileMovement->Deactivate();
  172. }
  173.  
  174. ARangedWeaponBase* AProjectileBase::GetParentWeapon(bool& bIsValid)
  175. {
  176.     bIsValid = ParentWeapon != nullptr;
  177.     return ParentWeapon;
  178. }
  179.  
  180. ARangedWeaponBase* AProjectileBase::GetParentWeapon()
  181. {
  182.     bool bIsValid = false;
  183.     return GetParentWeapon(bIsValid);
  184. }
  185.  
  186. void AProjectileBase::OnOverlapStart(AActor* Other)
  187. {
  188.     UE_LOG(WaGeneral, Log, TEXT("Overlapping %s %f"), *Other->GetName(), DeltaLocation.Size());
  189.     if (bDoneDamage || DeltaLocation.Size() < 0.1f)
  190.     {
  191.         return;
  192.     }
  193.     else
  194.     {
  195.         bool bValidWeapon = false;
  196.         ARangedWeaponBase* Weapon = GetParentWeapon(bValidWeapon);
  197.         if (bValidWeapon)
  198.         {
  199.             bDoneDamage = Weapon->ProcessProjectileDamage(Other);
  200.             if (bDoneDamage)
  201.             {
  202.                 if (bDestroyAfterHit)
  203.                 {
  204.                     MeshComponent->SetVisibility(false, true);
  205.                 }
  206.                 else if (bPhysicsAfterHit)
  207.                 {
  208.                     ProjectileMovement->Deactivate();
  209.                     MeshComponent->SetSimulatePhysics(true);
  210.                 }
  211.             }
  212.         }
  213.     }
  214. }
  215.  
  216. void AProjectileBase::OnOverlapEnd(AActor* Other)
  217. {
  218.     if (Other == ThrowingActor)
  219.     {
  220.         UE_LOG(WaGeneral, Log, TEXT("End overlap with %s"), *ThrowingActor->GetName());
  221.         MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
  222.     }
  223.  
  224. }
  225.  
  226. void AProjectileBase::SetParentWeapon(ARangedWeaponBase* Weapon)
  227. {
  228.     ParentWeapon = Weapon;
  229.     ThrowingActor = Weapon->GetOwnerPawn();
  230.  
  231.     MeshComponent->IgnoreActorWhenMoving(ThrowingActor, true);
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement