Advertisement
Guest User

PWNWeapon_Melee.cpp

a guest
Mar 11th, 2015
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.52 KB | None | 0 0
  1.  
  2.  
  3. #include "PWNGame.h"
  4. #include "PWNWeapon_Melee.h"
  5.  
  6.  
  7. APWNWeapon_Melee::APWNWeapon_Melee(const class FPostConstructInitializeProperties& PCIP)
  8.     : Super(PCIP)
  9. {
  10.     IsAttackTracing = false;
  11.     TraceSocketNameBase = TEXT("BaseSocket");
  12.     TraceSocketNameTip = TEXT("TipSocket");
  13.     AttackTraceSweepRadius = 5.0f;
  14.     DamageTypeClass = UPWNDamageType_Melee::StaticClass();
  15. }
  16.  
  17. void APWNWeapon_Melee::SetAssetReferences_Specific()
  18. {
  19.     // Basic
  20.     ConstRef_ArmAnimSet_Basic_Left_BP.Empty();
  21.     ConstRef_ArmAnimSet_Basic_Right_BP.Empty();
  22.     ConstRef_ArmAnimSet_Attacking_Left_BP.Empty();
  23.     ConstRef_ArmAnimSet_Attacking_Right_BP.Empty();
  24.  
  25.     // Remember to cater for base class attacking animset so it's spawned correctly
  26.  
  27.     /*******************/
  28.     /*** Directional ***/
  29.     /*******************/
  30.     // Left
  31.     ConstRef_ArmAnimSet_Attacking_Left_NoDirection_BP.Empty();
  32.     ConstRef_ArmAnimSet_Attacking_Left_ForwardCenter_BP.Empty();
  33.     ConstRef_ArmAnimSet_Attacking_Left_ForwardRight_BP.Empty();
  34.     ConstRef_ArmAnimSet_Attacking_Left_CenterRight_BP.Empty();
  35.     ConstRef_ArmAnimSet_Attacking_Left_BackRight_BP.Empty();
  36.     ConstRef_ArmAnimSet_Attacking_Left_BackCenter_BP.Empty();
  37.     ConstRef_ArmAnimSet_Attacking_Left_BackLeft_BP.Empty();
  38.     ConstRef_ArmAnimSet_Attacking_Left_CenterLeft_BP.Empty();
  39.     ConstRef_ArmAnimSet_Attacking_Left_ForwardLeft_BP.Empty();
  40.    
  41.     // Right
  42.     ConstRef_ArmAnimSet_Attacking_Right_NoDirection_BP.Empty();
  43.     ConstRef_ArmAnimSet_Attacking_Right_ForwardCenter_BP.Empty();
  44.     ConstRef_ArmAnimSet_Attacking_Right_ForwardRight_BP.Empty();
  45.     ConstRef_ArmAnimSet_Attacking_Right_CenterRight_BP.Empty();
  46.     ConstRef_ArmAnimSet_Attacking_Right_BackRight_BP.Empty();
  47.     ConstRef_ArmAnimSet_Attacking_Right_BackCenter_BP.Empty();
  48.     ConstRef_ArmAnimSet_Attacking_Right_BackLeft_BP.Empty();
  49.     ConstRef_ArmAnimSet_Attacking_Right_CenterLeft_BP.Empty();
  50.     ConstRef_ArmAnimSet_Attacking_Right_ForwardLeft_BP.Empty();
  51.     /*******************/
  52. }
  53.  
  54.  
  55. void APWNWeapon_Melee::SetAssets_Specific()
  56. {
  57.     Super::SetAssets_Specific();
  58.  
  59.     // Set my directional attacks
  60.  
  61. }
  62.  
  63. void APWNWeapon_Melee::SpawnAssets_Specific()
  64. {
  65.     // This will spawn animset attacking - must set ref before
  66.     Super::SpawnAssets_Specific();
  67.  
  68.     /**********************************/
  69.     /*** Spawn Directional Animsets ***/
  70.     /**********************************/
  71.     // Get world object for spawning
  72.     UWorld* world = GetWorld();
  73.  
  74.     if (world)
  75.     {  
  76.         Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_NoDirection_BP : ConstRef_ArmAnimSet_Attacking_Right_NoDirection_BP, ArmAnimSet_Attacking_NoDirection);
  77.         Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_ForwardCenter_BP : ConstRef_ArmAnimSet_Attacking_Right_ForwardCenter_BP, ArmAnimSet_Attacking_ForwardCenter);
  78.         Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_ForwardRight_BP : ConstRef_ArmAnimSet_Attacking_Right_ForwardRight_BP, ArmAnimSet_Attacking_ForwardRight);
  79.         Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_CenterRight_BP : ConstRef_ArmAnimSet_Attacking_Right_CenterRight_BP, ArmAnimSet_Attacking_CenterRight);
  80.         Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_BackRight_BP : ConstRef_ArmAnimSet_Attacking_Right_BackRight_BP, ArmAnimSet_Attacking_BackRight);
  81.         Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_BackCenter_BP : ConstRef_ArmAnimSet_Attacking_Right_BackCenter_BP, ArmAnimSet_Attacking_BackCenter);
  82.         Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_BackLeft_BP : ConstRef_ArmAnimSet_Attacking_Right_BackLeft_BP, ArmAnimSet_Attacking_BackLeft);
  83.         Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_CenterLeft_BP : ConstRef_ArmAnimSet_Attacking_Right_CenterLeft_BP, ArmAnimSet_Attacking_CenterLeft);
  84.         Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_ForwardLeft_BP : ConstRef_ArmAnimSet_Attacking_Right_ForwardLeft_BP, ArmAnimSet_Attacking_ForwardLeft);
  85.     }
  86.  
  87.     // Set current animset to be no direction
  88.     ArmAnimSet_Attacking = ArmAnimSet_Attacking_NoDirection;
  89.     /**********************************/
  90.  
  91.     // Sweep params
  92.     TraceSweepParameters.AddIgnoredActor(GetOwner());
  93.     TraceSweepParameters.AddIgnoredActor(this);
  94.  
  95.     // Damage params
  96.     DamageEvent.DamageTypeClass = DamageTypeClass;
  97.     DamageEvent.Damage = 50.0f;
  98. }
  99.  
  100. // Reset attack
  101. void APWNWeapon_Melee::AttackStart(TEnumAsByte<EMovementDirectionEnum::EMovementDirection> attackDirection)
  102. {
  103.     Super::AttackStart(attackDirection);
  104. }
  105.  
  106. void APWNWeapon_Melee::UpdateAttackDirectionAnimset()
  107. {
  108.     switch (AttackDirection)
  109.     {
  110.         case EMovementDirectionEnum::MOVEMENTDIRECTION_NoDirection: ArmAnimSet_Attacking = ArmAnimSet_Attacking_NoDirection; break;
  111.         case EMovementDirectionEnum::MOVEMENTDIRECTION_ForwardCenter: ArmAnimSet_Attacking = ArmAnimSet_Attacking_ForwardCenter; break;
  112.         case EMovementDirectionEnum::MOVEMENTDIRECTION_ForwardRight: ArmAnimSet_Attacking = ArmAnimSet_Attacking_ForwardRight; break;
  113.         case EMovementDirectionEnum::MOVEMENTDIRECTION_CenterRight: ArmAnimSet_Attacking = ArmAnimSet_Attacking_CenterRight; break;
  114.         case EMovementDirectionEnum::MOVEMENTDIRECTION_BackRight: ArmAnimSet_Attacking = ArmAnimSet_Attacking_BackRight; break;
  115.         case EMovementDirectionEnum::MOVEMENTDIRECTION_BackCenter: ArmAnimSet_Attacking = ArmAnimSet_Attacking_BackCenter; break;
  116.         case EMovementDirectionEnum::MOVEMENTDIRECTION_BackLeft: ArmAnimSet_Attacking = ArmAnimSet_Attacking_BackLeft; break;
  117.         case EMovementDirectionEnum::MOVEMENTDIRECTION_CenterLeft: ArmAnimSet_Attacking = ArmAnimSet_Attacking_CenterLeft; break;
  118.         case EMovementDirectionEnum::MOVEMENTDIRECTION_ForwardLeft: ArmAnimSet_Attacking = ArmAnimSet_Attacking_ForwardLeft; break;
  119.         default: ArmAnimSet_Attacking = ArmAnimSet_Attacking_NoDirection; break;
  120.     }
  121.  
  122.     if (!ArmAnimSet_Attacking)
  123.     {
  124.         UPWNGameGlobals::PrintOnscreenString(TEXT("Directional animset is null - reverting to NoDirection."));
  125.         ArmAnimSet_Attacking = ArmAnimSet_Attacking_NoDirection;
  126.     }
  127. }
  128.  
  129. void APWNWeapon_Melee::ArmState_PreAttacking_Start(float startPosPct)
  130. {  
  131.     Super::ArmState_PreAttacking_Start(startPosPct);           
  132. }
  133.  
  134. void APWNWeapon_Melee::ArmState_Attacking_Start(float startPosPct)
  135. {  
  136.     NumTracesDone = 0;
  137.     TraceLocPrev = GetTraceLocation();
  138.     TraceAttack(); 
  139.     Super::ArmState_Attacking_Start(startPosPct);  
  140.     IsAttackTracing = true;
  141. }
  142.  
  143. void APWNWeapon_Melee::ArmState_PostAttacking_Start(float startPosPct)
  144. {  
  145.     Super::ArmState_PostAttacking_Start(startPosPct);
  146.     TraceAttack(); 
  147.     IsAttackTracing = false;
  148.     //UPWNGameGlobals::PrintOnscreenFloat(NumTracesDone);
  149. }
  150.  
  151. void APWNWeapon_Melee::ArmState_AttackReturning_Start(float startPosPct)
  152. {      
  153.     Super::ArmState_AttackReturning_Start(startPosPct);
  154.     //TraceAttack();
  155.     //IsAttackTracing = false;
  156.     CleanupAfterStrike();
  157.  
  158.     /*
  159.     for (int32 i = 0; i < TraceLocs.Num(); i++)
  160.     {
  161.         UPWNGameGlobals::PrintOnscreenVector(TraceLocs[i].LocFrom);
  162.         UPWNGameGlobals::PrintOnscreenVector(TraceLocs[i].LocTo);
  163.         DrawDebugLine(GetWorld(), TraceLocs[i].LocFrom, TraceLocs[i].LocTo, FColor::Red, true);
  164.     }
  165.     */
  166.    
  167.     //DrawDebugLine(GetWorld(), TraceLocCurr.LocFrom, TraceLocCurr.LocTo, FColor::Red, true);
  168. }
  169.  
  170. void APWNWeapon_Melee::ArmState_AttackRebounding_Start(float startPosPct)
  171. {
  172.     // We were busy striking, so cleanup first
  173.     CleanupAfterStrike();
  174.     IsAttackTracing = false;
  175.     Super::ArmState_AttackRebounding_Start(startPosPct);
  176. }
  177.  
  178. void APWNWeapon_Melee::CleanupAfterStrike()
  179. {      
  180.     ActorsHitInThisStrike.Empty(); 
  181. }
  182.  
  183.  
  184. void APWNWeapon_Melee::AttackStop()
  185. {
  186.     Super::AttackStop();
  187. }
  188.  
  189. void APWNWeapon_Melee::Tick(float DeltaTime)
  190. {
  191.     Super::Tick(DeltaTime);
  192.  
  193.     if (IsAttackTracing)
  194.         TraceAttack();
  195. }
  196.  
  197. /**************/
  198. /*** Traces ***/
  199. /**************/
  200. void APWNWeapon_Melee::TraceAttack()
  201. {
  202.     NumTracesDone++;
  203.    
  204.     TraceLocCurr = GetTraceLocation();
  205.     TraceLocs.Add(TraceLocCurr);
  206.    
  207.     TraceMontageCurrDetails = GetActiveMontageDetails(Arm);
  208.     //DrawDebugLine(GetWorld(), TraceLocCurr.LocFrom, TraceLocCurr.LocTo, FColor::Red, true);
  209.    
  210.     // Setup interp
  211.     TArray<FTraceLocation> traceInterpLocations;
  212.     uint32 numInterps = 30;
  213.    
  214.     // Check if we need to interpolate with animation
  215.     if (TraceMontageCurrDetails.Name == TraceMontagePrevDetails.Name)
  216.     {
  217.         //UPWNGameGlobals::PrintOnscreenString("Must do interpolation");
  218.         //DrawDebugLine(GetWorld(), TraceLocCurr.LocFrom, TraceLocCurr.LocTo, FColor::Red, true);
  219.  
  220.         float animPosDelta = TraceMontageCurrDetails.Position - TraceMontagePrevDetails.Position;
  221.         //UPWNGameGlobals::PrintOnscreenFloat(animPosDelta);
  222.         uint32 numIntermAnimPos = 50;
  223.         float animPosInterm = animPosDelta / (int)numIntermAnimPos;
  224.         float animPosCurr = TraceMontagePrevDetails.Position;
  225.  
  226.         for (uint32 i = 0; i < numIntermAnimPos; i++)
  227.         {
  228.             animPosCurr = TraceMontagePrevDetails.Position + ((float)i) * animPosInterm;
  229.             //FString p = FString::SanitizeFloat(animPosCurr) + TEXT(" - ") + TraceMontageCurrDetails.Name;        
  230.             //UPWNGameGlobals::PrintOnscreenString(p);
  231.  
  232.             FTraceLocation intermAnimLoc;
  233.             intermAnimLoc.AnimPos = animPosCurr;
  234.            
  235.             // Set pose
  236.             SetActiveMontagePosition(Arm, intermAnimLoc.AnimPos);
  237.             // Get locs
  238.             intermAnimLoc = GetTraceLocation();
  239.             // Update anim loc
  240.             intermAnimLoc.AnimPos = animPosCurr;
  241.  
  242.             // Add to trace locs
  243.             traceInterpLocations.Add(intermAnimLoc);
  244.         }
  245.     }
  246.     else
  247.     {
  248.         // Get intermediate traces - could calculate numTraces
  249.         uint32 numTraces = 20;
  250.         traceInterpLocations = GetSubTraceLocsParallel(TraceLocPrev, TraceLocCurr, numTraces);
  251.     }
  252.  
  253.     // Define items used in looping
  254.     UWorld* world = GetWorld();
  255.     TArray<FHitResult> sweepResults;
  256.     FVector shotDir = (TraceLocCurr.LocTo - TraceLocPrev.LocTo);
  257.     shotDir.Normalize();
  258.     DamageEvent.ShotDirection = shotDir;
  259.     bool hitReboundActor = false;
  260.  
  261.     // Do traces and deal damage
  262.     for (int32 i = 0; i < traceInterpLocations.Num(); i++)
  263.     {
  264.         //DrawDebugLine(world, traceInterpLocations[i].LocFrom, traceInterpLocations[i].LocTo, FColor::Red, true);
  265.  
  266.         // Sweep Sphere Trace
  267.         world->SweepMulti(sweepResults, traceInterpLocations[i].LocFrom, traceInterpLocations[i].LocTo, FQuat::Identity, FCollisionShape::MakeSphere(AttackTraceSweepRadius), TraceSweepParameters, TraceSweepQueryPararameters);
  268.         /**************/
  269.  
  270.         // Sweep Trace - Hitresults
  271.         for (int32 j = 0; j < sweepResults.Num(); j++)
  272.         {          
  273.             // Current hitactor
  274.             AActor* hitActor = sweepResults[j].GetActor();
  275.  
  276.             if (hitActor)
  277.             {
  278.                 if (!CheckIfActorHasBeenHit(hitActor))
  279.                 {
  280.                     UPWNGameGlobals::PrintOnscreenString(hitActor->GetName());
  281.  
  282.                     // Add hitinfo to damage event
  283.                     DamageEvent.HitInfo = sweepResults[j];
  284.  
  285.                     // Deal damage                 
  286.                     hitActor->TakeDamage(50.0f, DamageEvent, Cast<APlayerController>((Cast<APawn>(GetOwner())->Controller)), this);
  287.  
  288.                     //TODO: Check if we should rebound - Should move out to separate function  
  289.                     if (sweepResults[j].Component.IsValid())
  290.                     {
  291.                         if (!sweepResults[j].Component->IsSimulatingPhysics())
  292.                         {
  293.                             hitReboundActor = true;
  294.                         }
  295.  
  296.                         if (sweepResults[j].Component->GetMass() > 100.0f)
  297.                             hitReboundActor = true;
  298.                     }                  
  299.                 }
  300.             }
  301.             else
  302.             {
  303.                 UPWNGameGlobals::PrintOnscreenString("Got null actor");
  304.                 hitReboundActor = true;
  305.             }
  306.         }
  307.  
  308.         // if hit some geom        
  309.         if (hitReboundActor)
  310.         {  
  311.             ArmState_AttackRebounding_Start();
  312.             return;
  313.         }
  314.     }
  315.    
  316.     // Roll over for next trace
  317.     TraceLocPrev = TraceLocCurr;   
  318.     TraceMontagePrevDetails = TraceMontageCurrDetails;
  319.     SetActiveMontagePosition(Arm, TraceMontageCurrDetails.Position);
  320. }
  321.  
  322. // Includes start and end points in the return
  323. TArray<FTraceLocation> APWNWeapon_Melee::GetSubTraceLocsOrthogonal(FTraceLocation prevLoc, FTraceLocation currLoc, uint32 numIntermTraces)
  324. {
  325.     // From and to locations
  326.     FVector fromTot = prevLoc.LocTo - prevLoc.LocFrom;
  327.     FVector toTot = currLoc.LocTo - currLoc.LocFrom;
  328.  
  329.     // Create object we will use in loop
  330.     TArray<FTraceLocation> subTraceLocs;
  331.     float currPct = 0.0f;
  332.     FTraceLocation currTrace;
  333.  
  334.     // Add the first trace point
  335.     currTrace.LocFrom = prevLoc.LocFrom;
  336.     currTrace.LocTo = currLoc.LocFrom;
  337.     subTraceLocs.Add(currTrace);
  338.  
  339.     // Add intermediate traces
  340.     for (uint32 i = 1; i < numIntermTraces; i++)
  341.     {
  342.         currPct = (float)i / (float)numIntermTraces;
  343.         currTrace.LocFrom = prevLoc.LocFrom + currPct * fromTot;
  344.         currTrace.LocTo = currLoc.LocFrom + currPct * toTot;
  345.         subTraceLocs.Add(currTrace);
  346.     }
  347.  
  348.     // Add last trace point
  349.     currTrace.LocFrom = prevLoc.LocTo;
  350.     currTrace.LocTo = currLoc.LocTo;
  351.     subTraceLocs.Add(currTrace);
  352.  
  353.     return subTraceLocs;
  354. }
  355.  
  356. // Includes start and end points in the return
  357. TArray<FTraceLocation> APWNWeapon_Melee::GetSubTraceLocsParallel(FTraceLocation prevLoc, FTraceLocation currLoc, uint32 numIntermTraces)
  358. {
  359.     // From and to locations
  360.     FVector fromTot = currLoc.LocFrom - prevLoc.LocFrom;
  361.     FVector toTot = currLoc.LocTo - prevLoc.LocTo;
  362.  
  363.     // Create objects we will use in loop
  364.     TArray<FTraceLocation> subTraceLocs;
  365.     FTraceLocation currTrace;
  366.     float currPct = 0.0f;
  367.  
  368.     //Add starting loc 
  369.     subTraceLocs.Add(prevLoc);
  370.  
  371.     // Add intermediate traces
  372.     for (uint32 i = 1; i < numIntermTraces; i++)
  373.     {
  374.         currPct = (float)i / (float)numIntermTraces;
  375.         currTrace.LocFrom = prevLoc.LocFrom + currPct * fromTot;
  376.         currTrace.LocTo = prevLoc.LocTo + currPct * toTot;
  377.         subTraceLocs.Add(currLoc);
  378.     }
  379.  
  380.     // Add ending loc
  381.     subTraceLocs.Add(currLoc);
  382.  
  383.     return subTraceLocs;
  384. }
  385.  
  386. void APWNWeapon_Melee::ProcessAttackTick(float prevAnimPosition, float currAnimPosition)
  387. {
  388. }
  389.  
  390. FTraceLocation APWNWeapon_Melee::GetTraceLocation()
  391. {
  392.     Arm->RefreshBoneTransforms();
  393.     FTraceLocation loc;
  394.     loc.AnimPos = GetActiveMontagePosition(Arm);   
  395.     loc.LocFrom = Weapon->GetSocketLocation(TraceSocketNameBase);
  396.     loc.LocTo = Weapon->GetSocketLocation(TraceSocketNameTip);
  397.     return loc;
  398. }
  399.  
  400. bool APWNWeapon_Melee::CheckIfActorHasBeenHit(AActor* hitActor)
  401. {
  402.     // TArray.find not working
  403.     FString actorName = hitActor->GetName();
  404.  
  405.     if (ActorsHitInThisStrike.Num() < 1)
  406.     {
  407.         ActorsHitInThisStrike.Add(actorName);
  408.         return false;
  409.     }
  410.     else
  411.     {
  412.         for (int i = 0; i < ActorsHitInThisStrike.Num(); i++)
  413.         {
  414.             if (ActorsHitInThisStrike[i] == actorName)
  415.             {
  416.                 return true;
  417.             }
  418.         }
  419.  
  420.         ActorsHitInThisStrike.Add(actorName);
  421.         return false;
  422.     }
  423. }
  424.  
  425.  
  426. /**************/
  427.  
  428. /*********************/
  429. /*** Serialisation ***/
  430. /*********************/
  431. UPWNSerialiser* APWNWeapon_Melee::Serialise()
  432. {
  433.     // Create my serialiser - if I'm NOT a parent class in inheritance
  434.     UPWNSerialiser* mySavedData = Super::Serialise();
  435.  
  436.     // Save my basic data
  437.     mySavedData->AddBoolValue(TEXT("IsAttackTracing"), IsAttackTracing);
  438.     mySavedData->AddVectorValue(TEXT("TraceLocPrev.LocFrom"), TraceLocPrev.LocFrom);
  439.     mySavedData->AddVectorValue(TEXT("TraceLocPrev.LocTo"), TraceLocPrev.LocTo);
  440.     mySavedData->AddNumericValue(TEXT("TraceLocPrev.AnimPos"), TraceLocPrev.AnimPos);
  441.     mySavedData->AddArrayStringValue(TEXT("ActorsHitInThisStrike"), ActorsHitInThisStrike);
  442.  
  443.     return mySavedData;
  444. }
  445.  
  446. void APWNWeapon_Melee::DeSerialise(UPWNSerialiser* mySavedData)
  447. {
  448.     Super::DeSerialise(mySavedData);
  449.     mySavedData->LoadBoolValue(TEXT("IsAttackTracing"), IsAttackTracing);
  450.     mySavedData->LoadVectorValue(TEXT("TraceLocPrev.LocFrom"), TraceLocPrev.LocFrom);
  451.     mySavedData->LoadVectorValue(TEXT("TraceLocPrev.LocTo"), TraceLocPrev.LocTo);
  452.     mySavedData->LoadNumericValue(TEXT("TraceLocPrev.AnimPos"), TraceLocPrev.AnimPos);
  453.     mySavedData->LoadArrayStringValue(TEXT("ActorsHitInThisStrike"), ActorsHitInThisStrike);
  454. }
  455. /*********************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement