Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "PWNGame.h"
- #include "PWNWeapon_Melee.h"
- APWNWeapon_Melee::APWNWeapon_Melee(const class FPostConstructInitializeProperties& PCIP)
- : Super(PCIP)
- {
- IsAttackTracing = false;
- TraceSocketNameBase = TEXT("BaseSocket");
- TraceSocketNameTip = TEXT("TipSocket");
- AttackTraceSweepRadius = 5.0f;
- DamageTypeClass = UPWNDamageType_Melee::StaticClass();
- }
- void APWNWeapon_Melee::SetAssetReferences_Specific()
- {
- // Basic
- ConstRef_ArmAnimSet_Basic_Left_BP.Empty();
- ConstRef_ArmAnimSet_Basic_Right_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Left_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Right_BP.Empty();
- // Remember to cater for base class attacking animset so it's spawned correctly
- /*******************/
- /*** Directional ***/
- /*******************/
- // Left
- ConstRef_ArmAnimSet_Attacking_Left_NoDirection_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Left_ForwardCenter_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Left_ForwardRight_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Left_CenterRight_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Left_BackRight_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Left_BackCenter_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Left_BackLeft_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Left_CenterLeft_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Left_ForwardLeft_BP.Empty();
- // Right
- ConstRef_ArmAnimSet_Attacking_Right_NoDirection_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Right_ForwardCenter_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Right_ForwardRight_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Right_CenterRight_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Right_BackRight_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Right_BackCenter_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Right_BackLeft_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Right_CenterLeft_BP.Empty();
- ConstRef_ArmAnimSet_Attacking_Right_ForwardLeft_BP.Empty();
- /*******************/
- }
- void APWNWeapon_Melee::SetAssets_Specific()
- {
- Super::SetAssets_Specific();
- // Set my directional attacks
- }
- void APWNWeapon_Melee::SpawnAssets_Specific()
- {
- // This will spawn animset attacking - must set ref before
- Super::SpawnAssets_Specific();
- /**********************************/
- /*** Spawn Directional Animsets ***/
- /**********************************/
- // Get world object for spawning
- UWorld* world = GetWorld();
- if (world)
- {
- Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_NoDirection_BP : ConstRef_ArmAnimSet_Attacking_Right_NoDirection_BP, ArmAnimSet_Attacking_NoDirection);
- Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_ForwardCenter_BP : ConstRef_ArmAnimSet_Attacking_Right_ForwardCenter_BP, ArmAnimSet_Attacking_ForwardCenter);
- Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_ForwardRight_BP : ConstRef_ArmAnimSet_Attacking_Right_ForwardRight_BP, ArmAnimSet_Attacking_ForwardRight);
- Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_CenterRight_BP : ConstRef_ArmAnimSet_Attacking_Right_CenterRight_BP, ArmAnimSet_Attacking_CenterRight);
- Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_BackRight_BP : ConstRef_ArmAnimSet_Attacking_Right_BackRight_BP, ArmAnimSet_Attacking_BackRight);
- Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_BackCenter_BP : ConstRef_ArmAnimSet_Attacking_Right_BackCenter_BP, ArmAnimSet_Attacking_BackCenter);
- Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_BackLeft_BP : ConstRef_ArmAnimSet_Attacking_Right_BackLeft_BP, ArmAnimSet_Attacking_BackLeft);
- Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_CenterLeft_BP : ConstRef_ArmAnimSet_Attacking_Right_CenterLeft_BP, ArmAnimSet_Attacking_CenterLeft);
- Spawn_AnimSet_Attacking_Instance(world, IsInLeftHand ? ConstRef_ArmAnimSet_Attacking_Left_ForwardLeft_BP : ConstRef_ArmAnimSet_Attacking_Right_ForwardLeft_BP, ArmAnimSet_Attacking_ForwardLeft);
- }
- // Set current animset to be no direction
- ArmAnimSet_Attacking = ArmAnimSet_Attacking_NoDirection;
- /**********************************/
- // Sweep params
- TraceSweepParameters.AddIgnoredActor(GetOwner());
- TraceSweepParameters.AddIgnoredActor(this);
- // Damage params
- DamageEvent.DamageTypeClass = DamageTypeClass;
- DamageEvent.Damage = 50.0f;
- }
- // Reset attack
- void APWNWeapon_Melee::AttackStart(TEnumAsByte<EMovementDirectionEnum::EMovementDirection> attackDirection)
- {
- Super::AttackStart(attackDirection);
- }
- void APWNWeapon_Melee::UpdateAttackDirectionAnimset()
- {
- switch (AttackDirection)
- {
- case EMovementDirectionEnum::MOVEMENTDIRECTION_NoDirection: ArmAnimSet_Attacking = ArmAnimSet_Attacking_NoDirection; break;
- case EMovementDirectionEnum::MOVEMENTDIRECTION_ForwardCenter: ArmAnimSet_Attacking = ArmAnimSet_Attacking_ForwardCenter; break;
- case EMovementDirectionEnum::MOVEMENTDIRECTION_ForwardRight: ArmAnimSet_Attacking = ArmAnimSet_Attacking_ForwardRight; break;
- case EMovementDirectionEnum::MOVEMENTDIRECTION_CenterRight: ArmAnimSet_Attacking = ArmAnimSet_Attacking_CenterRight; break;
- case EMovementDirectionEnum::MOVEMENTDIRECTION_BackRight: ArmAnimSet_Attacking = ArmAnimSet_Attacking_BackRight; break;
- case EMovementDirectionEnum::MOVEMENTDIRECTION_BackCenter: ArmAnimSet_Attacking = ArmAnimSet_Attacking_BackCenter; break;
- case EMovementDirectionEnum::MOVEMENTDIRECTION_BackLeft: ArmAnimSet_Attacking = ArmAnimSet_Attacking_BackLeft; break;
- case EMovementDirectionEnum::MOVEMENTDIRECTION_CenterLeft: ArmAnimSet_Attacking = ArmAnimSet_Attacking_CenterLeft; break;
- case EMovementDirectionEnum::MOVEMENTDIRECTION_ForwardLeft: ArmAnimSet_Attacking = ArmAnimSet_Attacking_ForwardLeft; break;
- default: ArmAnimSet_Attacking = ArmAnimSet_Attacking_NoDirection; break;
- }
- if (!ArmAnimSet_Attacking)
- {
- UPWNGameGlobals::PrintOnscreenString(TEXT("Directional animset is null - reverting to NoDirection."));
- ArmAnimSet_Attacking = ArmAnimSet_Attacking_NoDirection;
- }
- }
- void APWNWeapon_Melee::ArmState_PreAttacking_Start(float startPosPct)
- {
- Super::ArmState_PreAttacking_Start(startPosPct);
- }
- void APWNWeapon_Melee::ArmState_Attacking_Start(float startPosPct)
- {
- NumTracesDone = 0;
- TraceLocPrev = GetTraceLocation();
- TraceAttack();
- Super::ArmState_Attacking_Start(startPosPct);
- IsAttackTracing = true;
- }
- void APWNWeapon_Melee::ArmState_PostAttacking_Start(float startPosPct)
- {
- Super::ArmState_PostAttacking_Start(startPosPct);
- TraceAttack();
- IsAttackTracing = false;
- //UPWNGameGlobals::PrintOnscreenFloat(NumTracesDone);
- }
- void APWNWeapon_Melee::ArmState_AttackReturning_Start(float startPosPct)
- {
- Super::ArmState_AttackReturning_Start(startPosPct);
- //TraceAttack();
- //IsAttackTracing = false;
- CleanupAfterStrike();
- /*
- for (int32 i = 0; i < TraceLocs.Num(); i++)
- {
- UPWNGameGlobals::PrintOnscreenVector(TraceLocs[i].LocFrom);
- UPWNGameGlobals::PrintOnscreenVector(TraceLocs[i].LocTo);
- DrawDebugLine(GetWorld(), TraceLocs[i].LocFrom, TraceLocs[i].LocTo, FColor::Red, true);
- }
- */
- //DrawDebugLine(GetWorld(), TraceLocCurr.LocFrom, TraceLocCurr.LocTo, FColor::Red, true);
- }
- void APWNWeapon_Melee::ArmState_AttackRebounding_Start(float startPosPct)
- {
- // We were busy striking, so cleanup first
- CleanupAfterStrike();
- IsAttackTracing = false;
- Super::ArmState_AttackRebounding_Start(startPosPct);
- }
- void APWNWeapon_Melee::CleanupAfterStrike()
- {
- ActorsHitInThisStrike.Empty();
- }
- void APWNWeapon_Melee::AttackStop()
- {
- Super::AttackStop();
- }
- void APWNWeapon_Melee::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- if (IsAttackTracing)
- TraceAttack();
- }
- /**************/
- /*** Traces ***/
- /**************/
- void APWNWeapon_Melee::TraceAttack()
- {
- NumTracesDone++;
- TraceLocCurr = GetTraceLocation();
- TraceLocs.Add(TraceLocCurr);
- TraceMontageCurrDetails = GetActiveMontageDetails(Arm);
- //DrawDebugLine(GetWorld(), TraceLocCurr.LocFrom, TraceLocCurr.LocTo, FColor::Red, true);
- // Setup interp
- TArray<FTraceLocation> traceInterpLocations;
- uint32 numInterps = 30;
- // Check if we need to interpolate with animation
- if (TraceMontageCurrDetails.Name == TraceMontagePrevDetails.Name)
- {
- //UPWNGameGlobals::PrintOnscreenString("Must do interpolation");
- //DrawDebugLine(GetWorld(), TraceLocCurr.LocFrom, TraceLocCurr.LocTo, FColor::Red, true);
- float animPosDelta = TraceMontageCurrDetails.Position - TraceMontagePrevDetails.Position;
- //UPWNGameGlobals::PrintOnscreenFloat(animPosDelta);
- uint32 numIntermAnimPos = 50;
- float animPosInterm = animPosDelta / (int)numIntermAnimPos;
- float animPosCurr = TraceMontagePrevDetails.Position;
- for (uint32 i = 0; i < numIntermAnimPos; i++)
- {
- animPosCurr = TraceMontagePrevDetails.Position + ((float)i) * animPosInterm;
- //FString p = FString::SanitizeFloat(animPosCurr) + TEXT(" - ") + TraceMontageCurrDetails.Name;
- //UPWNGameGlobals::PrintOnscreenString(p);
- FTraceLocation intermAnimLoc;
- intermAnimLoc.AnimPos = animPosCurr;
- // Set pose
- SetActiveMontagePosition(Arm, intermAnimLoc.AnimPos);
- // Get locs
- intermAnimLoc = GetTraceLocation();
- // Update anim loc
- intermAnimLoc.AnimPos = animPosCurr;
- // Add to trace locs
- traceInterpLocations.Add(intermAnimLoc);
- }
- }
- else
- {
- // Get intermediate traces - could calculate numTraces
- uint32 numTraces = 20;
- traceInterpLocations = GetSubTraceLocsParallel(TraceLocPrev, TraceLocCurr, numTraces);
- }
- // Define items used in looping
- UWorld* world = GetWorld();
- TArray<FHitResult> sweepResults;
- FVector shotDir = (TraceLocCurr.LocTo - TraceLocPrev.LocTo);
- shotDir.Normalize();
- DamageEvent.ShotDirection = shotDir;
- bool hitReboundActor = false;
- // Do traces and deal damage
- for (int32 i = 0; i < traceInterpLocations.Num(); i++)
- {
- //DrawDebugLine(world, traceInterpLocations[i].LocFrom, traceInterpLocations[i].LocTo, FColor::Red, true);
- // Sweep Sphere Trace
- world->SweepMulti(sweepResults, traceInterpLocations[i].LocFrom, traceInterpLocations[i].LocTo, FQuat::Identity, FCollisionShape::MakeSphere(AttackTraceSweepRadius), TraceSweepParameters, TraceSweepQueryPararameters);
- /**************/
- // Sweep Trace - Hitresults
- for (int32 j = 0; j < sweepResults.Num(); j++)
- {
- // Current hitactor
- AActor* hitActor = sweepResults[j].GetActor();
- if (hitActor)
- {
- if (!CheckIfActorHasBeenHit(hitActor))
- {
- UPWNGameGlobals::PrintOnscreenString(hitActor->GetName());
- // Add hitinfo to damage event
- DamageEvent.HitInfo = sweepResults[j];
- // Deal damage
- hitActor->TakeDamage(50.0f, DamageEvent, Cast<APlayerController>((Cast<APawn>(GetOwner())->Controller)), this);
- //TODO: Check if we should rebound - Should move out to separate function
- if (sweepResults[j].Component.IsValid())
- {
- if (!sweepResults[j].Component->IsSimulatingPhysics())
- {
- hitReboundActor = true;
- }
- if (sweepResults[j].Component->GetMass() > 100.0f)
- hitReboundActor = true;
- }
- }
- }
- else
- {
- UPWNGameGlobals::PrintOnscreenString("Got null actor");
- hitReboundActor = true;
- }
- }
- // if hit some geom
- if (hitReboundActor)
- {
- ArmState_AttackRebounding_Start();
- return;
- }
- }
- // Roll over for next trace
- TraceLocPrev = TraceLocCurr;
- TraceMontagePrevDetails = TraceMontageCurrDetails;
- SetActiveMontagePosition(Arm, TraceMontageCurrDetails.Position);
- }
- // Includes start and end points in the return
- TArray<FTraceLocation> APWNWeapon_Melee::GetSubTraceLocsOrthogonal(FTraceLocation prevLoc, FTraceLocation currLoc, uint32 numIntermTraces)
- {
- // From and to locations
- FVector fromTot = prevLoc.LocTo - prevLoc.LocFrom;
- FVector toTot = currLoc.LocTo - currLoc.LocFrom;
- // Create object we will use in loop
- TArray<FTraceLocation> subTraceLocs;
- float currPct = 0.0f;
- FTraceLocation currTrace;
- // Add the first trace point
- currTrace.LocFrom = prevLoc.LocFrom;
- currTrace.LocTo = currLoc.LocFrom;
- subTraceLocs.Add(currTrace);
- // Add intermediate traces
- for (uint32 i = 1; i < numIntermTraces; i++)
- {
- currPct = (float)i / (float)numIntermTraces;
- currTrace.LocFrom = prevLoc.LocFrom + currPct * fromTot;
- currTrace.LocTo = currLoc.LocFrom + currPct * toTot;
- subTraceLocs.Add(currTrace);
- }
- // Add last trace point
- currTrace.LocFrom = prevLoc.LocTo;
- currTrace.LocTo = currLoc.LocTo;
- subTraceLocs.Add(currTrace);
- return subTraceLocs;
- }
- // Includes start and end points in the return
- TArray<FTraceLocation> APWNWeapon_Melee::GetSubTraceLocsParallel(FTraceLocation prevLoc, FTraceLocation currLoc, uint32 numIntermTraces)
- {
- // From and to locations
- FVector fromTot = currLoc.LocFrom - prevLoc.LocFrom;
- FVector toTot = currLoc.LocTo - prevLoc.LocTo;
- // Create objects we will use in loop
- TArray<FTraceLocation> subTraceLocs;
- FTraceLocation currTrace;
- float currPct = 0.0f;
- //Add starting loc
- subTraceLocs.Add(prevLoc);
- // Add intermediate traces
- for (uint32 i = 1; i < numIntermTraces; i++)
- {
- currPct = (float)i / (float)numIntermTraces;
- currTrace.LocFrom = prevLoc.LocFrom + currPct * fromTot;
- currTrace.LocTo = prevLoc.LocTo + currPct * toTot;
- subTraceLocs.Add(currLoc);
- }
- // Add ending loc
- subTraceLocs.Add(currLoc);
- return subTraceLocs;
- }
- void APWNWeapon_Melee::ProcessAttackTick(float prevAnimPosition, float currAnimPosition)
- {
- }
- FTraceLocation APWNWeapon_Melee::GetTraceLocation()
- {
- Arm->RefreshBoneTransforms();
- FTraceLocation loc;
- loc.AnimPos = GetActiveMontagePosition(Arm);
- loc.LocFrom = Weapon->GetSocketLocation(TraceSocketNameBase);
- loc.LocTo = Weapon->GetSocketLocation(TraceSocketNameTip);
- return loc;
- }
- bool APWNWeapon_Melee::CheckIfActorHasBeenHit(AActor* hitActor)
- {
- // TArray.find not working
- FString actorName = hitActor->GetName();
- if (ActorsHitInThisStrike.Num() < 1)
- {
- ActorsHitInThisStrike.Add(actorName);
- return false;
- }
- else
- {
- for (int i = 0; i < ActorsHitInThisStrike.Num(); i++)
- {
- if (ActorsHitInThisStrike[i] == actorName)
- {
- return true;
- }
- }
- ActorsHitInThisStrike.Add(actorName);
- return false;
- }
- }
- /**************/
- /*********************/
- /*** Serialisation ***/
- /*********************/
- UPWNSerialiser* APWNWeapon_Melee::Serialise()
- {
- // Create my serialiser - if I'm NOT a parent class in inheritance
- UPWNSerialiser* mySavedData = Super::Serialise();
- // Save my basic data
- mySavedData->AddBoolValue(TEXT("IsAttackTracing"), IsAttackTracing);
- mySavedData->AddVectorValue(TEXT("TraceLocPrev.LocFrom"), TraceLocPrev.LocFrom);
- mySavedData->AddVectorValue(TEXT("TraceLocPrev.LocTo"), TraceLocPrev.LocTo);
- mySavedData->AddNumericValue(TEXT("TraceLocPrev.AnimPos"), TraceLocPrev.AnimPos);
- mySavedData->AddArrayStringValue(TEXT("ActorsHitInThisStrike"), ActorsHitInThisStrike);
- return mySavedData;
- }
- void APWNWeapon_Melee::DeSerialise(UPWNSerialiser* mySavedData)
- {
- Super::DeSerialise(mySavedData);
- mySavedData->LoadBoolValue(TEXT("IsAttackTracing"), IsAttackTracing);
- mySavedData->LoadVectorValue(TEXT("TraceLocPrev.LocFrom"), TraceLocPrev.LocFrom);
- mySavedData->LoadVectorValue(TEXT("TraceLocPrev.LocTo"), TraceLocPrev.LocTo);
- mySavedData->LoadNumericValue(TEXT("TraceLocPrev.AnimPos"), TraceLocPrev.AnimPos);
- mySavedData->LoadArrayStringValue(TEXT("ActorsHitInThisStrike"), ActorsHitInThisStrike);
- }
- /*********************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement