Advertisement
Guest User

Projectile Class

a guest
Jul 27th, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.02 KB | None | 0 0
  1. Header File:
  2.  
  3. // Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
  4. #pragma once
  5. #include "GameFramework/Actor.h"
  6. #include "CodeBounceBallProjectile.generated.h"
  7.  
  8. UCLASS(config=Game)
  9. class ACodeBounceBallProjectile : public AActor
  10. {
  11.     GENERATED_BODY()
  12.  
  13.     /** Sphere collision component */
  14.     UPROPERTY(VisibleDefaultsOnly, Category=Projectile)
  15.         class USphereComponent* CollisionComp;
  16.  
  17.     //Mesh component
  18.     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Projectile, meta = (AllowPrivateAccess = "true"))
  19.         class UStaticMeshComponent* ProjectileMesh;
  20.    
  21.  
  22.     /** Projectile movement component */
  23.     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = "true"))
  24.         class UProjectileMovementComponent* ProjectileMovement;
  25.  
  26. public:
  27.     ACodeBounceBallProjectile();
  28.  
  29.     /** called when projectile hits something */
  30.     UFUNCTION()
  31.     void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
  32.  
  33.     float TakeDamage(float DamageAmount, struct FDamageEvent const & DamageEvent, class AController * EventInstigator, AActor * DamageCauser) override;
  34.  
  35.     //Changes the material instance dynamic's color
  36.     void ChangeMaterialColor(FColor Color);
  37.  
  38.     void SetToDestroy();
  39.    
  40.     UFUNCTION()
  41.         void OnRep_WallsHit();
  42.    
  43.     /** Returns CollisionComp subobject **/
  44.     FORCEINLINE class USphereComponent* GetCollisionComp() const { return CollisionComp; }
  45.     /** Returns ProjectileMovement subobject **/
  46.     FORCEINLINE class UProjectileMovementComponent* GetProjectileMovement() const { return ProjectileMovement; }
  47.  
  48. private:
  49.  
  50.  
  51.     UPROPERTY(ReplicatedUsing = OnRep_WallsHit)
  52.         uint8 WallsHit;
  53.  
  54.     UPROPERTY(EditDefaultsOnly)
  55.         float BaseDamage;
  56.     //Lifespan after the projectile became useless
  57.     UPROPERTY(EditDefaultsOnly)
  58.         float ExtraLifespan;
  59.    
  60.     //Notify whether or not the projectile has become useless
  61.     uint8 bIsUseless : 1;
  62.  
  63.     UMaterialInstanceDynamic* ProjectileMID;
  64.  
  65.     //Neutral projectile color
  66.     UPROPERTY(EditAnywhere)
  67.         FColor NeutralColor;
  68.     //Color of the projectile for friendlies after it hit a wall once
  69.     UPROPERTY(EditAnywhere)
  70.         FColor AllyColor_1;
  71.     //Color of the projectile for friendlies after it hit a wall twice
  72.     UPROPERTY(EditAnywhere)
  73.         FColor AllyColor_2;
  74.     //Color of the projectile for enemies after it hit a wall once
  75.     UPROPERTY(EditAnywhere)
  76.         FColor FoeColor_1;
  77.     //Color of the projectile for enemies after it hit a wall twice
  78.     UPROPERTY(EditAnywhere)
  79.         FColor FoeColor_2;
  80.    
  81. protected:
  82.  
  83. };
  84.  
  85.  
  86.  
  87. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88.  
  89.  
  90. C++ File:
  91.  
  92. // Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
  93.  
  94. #include "CodeBounceBall.h"
  95. #include "CodeBounceBallProjectile.h"
  96. #include "GameFramework/ProjectileMovementComponent.h"
  97. #include "GameWall.h"
  98. #include "CodeBounceBallCharacter.h"
  99.  
  100. ACodeBounceBallProjectile::ACodeBounceBallProjectile()
  101. {
  102.     // Use a sphere as a simple collision representation
  103.     CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComp"));
  104.     CollisionComp->InitSphereRadius(5.0f);
  105.     CollisionComp->BodyInstance.SetCollisionProfileName("Projectile");
  106.     CollisionComp->OnComponentHit.AddDynamic(this, &ACodeBounceBallProjectile::OnHit);      // set up a notification for when this component hits something blocking
  107.     RootComponent = CollisionComp;
  108.  
  109.     // Players can't walk on it
  110.     CollisionComp->SetWalkableSlopeOverride(FWalkableSlopeOverride(WalkableSlope_Unwalkable, 0.f));
  111.     CollisionComp->CanCharacterStepUpOn = ECB_No;
  112.    
  113.     ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ProjectileMesh"));
  114.     ProjectileMesh->SetupAttachment(RootComponent);
  115.    
  116.     // Use a ProjectileMovementComponent to govern this projectile's movement
  117.     ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileComp"));
  118.     ProjectileMovement->UpdatedComponent = RootComponent;
  119.     ProjectileMovement->InitialSpeed = 3000.f;
  120.     ProjectileMovement->MaxSpeed = 3000.f;
  121.     ProjectileMovement->bRotationFollowsVelocity = true;
  122.     ProjectileMovement->bShouldBounce = true;
  123.  
  124.     // Die after 3 seconds by default
  125.     InitialLifeSpan = 3.0f;
  126.  
  127.     ///////////////////////////////////////////////////////////////////////////////////
  128.  
  129.     static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshAsset(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'"));
  130.     static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialAsset(TEXT("Material'/Game/FirstPerson/Meshes/BaseMaterial.BaseMaterial'"));
  131.     if (MaterialAsset.Object && StaticMeshAsset.Object)
  132.     {
  133.         ProjectileMesh->SetStaticMesh(StaticMeshAsset.Object);
  134.         ProjectileMID = ProjectileMesh->CreateDynamicMaterialInstance(0, MaterialAsset.Object);
  135.         ProjectileMesh->SetMaterial(0, ProjectileMID);
  136.     }
  137.  
  138.     bReplicates = true;
  139.     bReplicateMovement = true;
  140.     bAlwaysRelevant = true;
  141.     bNetLoadOnClient = true;
  142.  
  143.     WallsHit = 0;
  144.     BaseDamage = 20.f;
  145.     ExtraLifespan = .5f;
  146.     bIsUseless = false;
  147.  
  148.     NeutralColor = FColor::Yellow;
  149.     AllyColor_1 = FColor::Green;
  150.     AllyColor_2 = FColor::Blue;
  151.     FoeColor_1 = FColor::Red;
  152.     FoeColor_2 = FColor::Black;
  153.  
  154.     ChangeMaterialColor(NeutralColor);
  155.  
  156. }
  157.  
  158. void ACodeBounceBallProjectile::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
  159. {
  160.     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  161.  
  162.     DOREPLIFETIME(ACodeBounceBallProjectile, WallsHit);
  163. }
  164.  
  165. void ACodeBounceBallProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
  166. {
  167.     if (HasAuthority()) {
  168.  
  169.         //If the projectile hits a wall
  170.         if (AGameWall* Wall = Cast<AGameWall>(OtherActor)) {
  171.             if (0 <= WallsHit && WallsHit < 2 && !bIsUseless) {
  172.                 WallsHit++;
  173.             }
  174.         }
  175.  
  176.         //If the projectile hits a player
  177.         else if (ACodeBounceBallCharacter* Player = Cast<ACodeBounceBallCharacter>(OtherActor)) {
  178.             //If the projectile is supposed to do damage
  179.             if (GetInstigator() != Player && WallsHit != 0 && !bIsUseless) {
  180.                 if (!Player->GetShield()) {
  181.                     if (HasAuthority()) {
  182.                         Player->TakeDamage(BaseDamage * WallsHit, FDamageEvent(UDamageType::StaticClass()), GetInstigatorController(), this);
  183.                         SetToDestroy();
  184.                     }
  185.                 }
  186.                 else {
  187.                     Player->ToogleShield(false);
  188.                     if (GetInstigator() != Player && !bIsUseless) {
  189.                         //PlayBreakSound();
  190.                     }
  191.                     SetToDestroy();
  192.                 }
  193.             }
  194.             else {
  195.                 if (GetInstigator() != Player && !bIsUseless) {
  196.                     //PlaySqueakSound();
  197.                 }
  198.                 SetToDestroy();
  199.             }
  200.         }
  201.         else if (ACodeBounceBallProjectile* Projectile = Cast<ACodeBounceBallProjectile>(OtherActor)) {
  202.             Projectile->SetToDestroy();
  203.             SetToDestroy();
  204.         }
  205.     }
  206. }
  207.  
  208. float ACodeBounceBallProjectile::TakeDamage(float DamageAmount, FDamageEvent const & DamageEvent, AController * EventInstigator, AActor * DamageCauser)
  209. {
  210.     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Taken Damage"));
  211.     if (HasAuthority()) {
  212.  
  213.     }
  214.     return 0.0f;
  215. }
  216.  
  217. void ACodeBounceBallProjectile::ChangeMaterialColor(FColor Color)
  218. {
  219.     ProjectileMID->SetVectorParameterValue("DiffuseColor", Color);
  220. }
  221.  
  222. void ACodeBounceBallProjectile::SetToDestroy()
  223. {
  224.     bIsUseless = true;
  225.     WallsHit = 0;
  226.     SetLifeSpan(ExtraLifespan);
  227. }
  228.  
  229. void ACodeBounceBallProjectile::OnRep_WallsHit()
  230. {
  231.     APawn* Player = Cast<APawn>(UGameplayStatics::GetPlayerCharacter(this, 0));
  232.  
  233.     if (Player) {
  234.         switch (WallsHit){
  235.  
  236.             case 0:
  237.                 ChangeMaterialColor(NeutralColor);
  238.                 break;
  239.  
  240.             //If the projectile hit the wall once
  241.             case 1:
  242.                 if (GetInstigator() == Player) {
  243.                     ChangeMaterialColor(AllyColor_1);
  244.                 }
  245.                 else {
  246.                     ChangeMaterialColor(FoeColor_1);
  247.                 }
  248.                 break;
  249.            
  250.             //If the projectile hit the wall twice
  251.             case 2:
  252.                 //GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("%s"), GetInstigator()->GetName()));
  253.                 if (GetInstigator() == Player) {
  254.                     ChangeMaterialColor(AllyColor_2);
  255.                 }
  256.                 else {
  257.                     ChangeMaterialColor(FoeColor_2);
  258.                 }
  259.                 break;
  260.         }
  261.     }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement