[cpp] Bird
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- // Copyright (c) 2019-2020 The Parasight
- #include "AI/ForestEntity/Bird/RobinAIBirdMovementComponent.h"
- #include "AI/ForestEntity/RobinAIForestEntity.h"
- #include "AI/ForestEntity/RobinAIForestEntityAnimInstance.h"
- #include "DrawDebugHelpers.h"
- #include "GameLog.h"
- #include "GameProfiler.h"
- #include "Landscape.h"
- #include "LandscapeProxy.h"
- #include "Misc/RobinCharacterStatics.h"
- #include "Misc/RobinSplineActor.h"
- #include "Misc/RobinSplineComponent.h"
- #include "NavigationSystem.h"
- #include "Player/Character/RobinPlayerCharacter.h"
- #include "Player/Character/View/RobinPlayerViewControllerComponent.h"
- #include "Engine/TriggerBox.h"
- #include "Components/BoxComponent.h"
- #include "Kismet/KismetMathLibrary.h"
- #include "CommonGlobals.h"
- #include "AI/ForestEntity/Bird/RobinAIBirdAnimInstance.h"
- #include "CommonMathStatics.h"
- DECLARE_CYCLE_STAT( TEXT( "Bird:Movement:Tick" ), STAT_ForestEntity_Bird_Movement_Tick, STATGROUP_RobinAI );
- URobinAIBirdMovementComponent::URobinAIBirdMovementComponent()
- {
- TraceFrequency = 0.2f;
- #if WITH_EDITORONLY_DATA
- TraceFrequency.PerPlatform.Add( "XboxOne", 0.5f );
- TraceFrequency.PerPlatform.Add( "PS4", 0.5f );
- #endif
- }
- void URobinAIBirdMovementComponent::TickComponent( float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction )
- {
- GAME_SCOPE_CYCLE_COUNTER( STAT_ForestEntity_Bird_Movement_Tick );
- Super::TickComponent( DeltaTime, TickType, ThisTickFunction );
- if( Initialized && Status != ERobinAIBirdMovementType::Death )
- {
- if( TraceTimer >= TraceFrequency.Default &&
- ( Status == ERobinAIBirdMovementType::AirMovement || Status == ERobinAIBirdMovementType::Landing || Status == ERobinAIBirdMovementType::StartLanding ||
- Status == ERobinAIBirdMovementType::ChooseLanding ) &&
- !IsLerping )
- {
- TraceTimer = 0;
- FCollisionQueryParams const QueryParams( SCENE_QUERY_STAT( BirdLandingTraceTest ), false, GetOwner() );
- FCollisionResponseParams const ResponseParams = FCollisionResponseParams::DefaultResponseParam;
- GetWorld()->AsyncLineTraceByChannel( EAsyncTraceType::Single, UpdatedComponent->GetComponentLocation(),
- UpdatedComponent->GetComponentLocation() + UpdatedComponent->GetForwardVector() * TraceLength, ECollisionChannel::ECC_WorldStatic, QueryParams, ResponseParams,
- &DetectionTraceHandleDelegate );
- }
- else
- {
- TraceTimer += DeltaTime;
- }
- UpdateActualMaxSpeed();
- if( ObstacleRadius != 0.f && ( Status != ERobinAIBirdMovementType::LandMovement && Status != ERobinAIBirdMovementType::Death ) )
- {
- ProcessAvoidingObstacle( DeltaTime );
- }
- // else if (ObstacleRadius != 0.f && (Status != ERobinAIBirdMovementType::LandMovement && ))
- //{
- // ObstacleRadius = 0;
- //}
- if( UpdatedComponent && ObstacleRadius == 0.f )
- {
- RefreshAnimationModifierScales();
- if( Status == ERobinAIBirdMovementType::LandMovement )
- {
- if( IsFollowingPath() )
- {
- TimeSincePathFollowing += DeltaTime;
- ProcessLandMovement( DeltaTime );
- }
- else
- {
- Velocity = FVector::ZeroVector;
- DesiredVelocity = FVector::ZeroVector;
- }
- }
- else if( Status == ERobinAIBirdMovementType::AirMovement && TakingOffMontage )
- {
- ProcessAirMovement( DeltaTime );
- }
- else if( Status == ERobinAIBirdMovementType::StartLanding || Status == ERobinAIBirdMovementType::ChooseLanding )
- {
- ProcessChoosingLandingTarget();
- }
- else if( Status == ERobinAIBirdMovementType::Landing )
- {
- ProcessLanding( DeltaTime );
- }
- }
- }
- }
- void URobinAIBirdMovementComponent::BeginPlay()
- {
- Super::BeginPlay();
- if( ( !MovementSpline || AirMovementBox ) && Status == ERobinAIBirdMovementType::AirMovement )
- {
- Status = ERobinAIBirdMovementType::ChooseLanding;
- }
- DetectionTraceHandleDelegate = FTraceDelegate::CreateUObject( this, &URobinAIBirdMovementComponent::ProcessObstacleDetected );
- CachedTraceFrequency = TraceFrequency.Default;
- }
- uint32 URobinAIBirdMovementComponent::StartFollowingPath(
- FNavigationPath const& PathToFollow, FName const& FinishMessageId, float const NewApproachTargetThreshold, UObject& Object )
- {
- uint32 const RequestId = Super::StartFollowingPath( PathToFollow, FinishMessageId, NewApproachTargetThreshold, Object );
- if( IsFollowingPath() )
- {
- TimeSincePathFollowing = 0.0f;
- NextPathPoint = 1;
- }
- return RequestId;
- }
- void URobinAIBirdMovementComponent::FinishFollowingPath( bool const Success )
- {
- Super::FinishFollowingPath( Success );
- }
- void URobinAIBirdMovementComponent::UpdateCurrentPath( FNavigationPath const& PathToFollow, float const NewApproachTargetThreshold )
- {
- Super::UpdateCurrentPath( PathToFollow, NewApproachTargetThreshold );
- if( Status == ERobinAIBirdMovementType::LandMovement )
- {
- NextPathPoint = 1;
- TimeSincePathFollowing = 0.0f;
- }
- }
- void URobinAIBirdMovementComponent::StartStopping()
- {
- Status = ERobinAIBirdMovementType::Stop;
- }
- // To change
- void URobinAIBirdMovementComponent::ProcessObstacleDetected( FTraceHandle const& Handle, FTraceDatum& Datum )
- {
- if( Datum.OutHits.IsValidIndex( 0 ) && !IsLerping )
- {
- FHitResult const& Hit = Datum.OutHits.Last();
- if( Hit.bBlockingHit )
- {
- RequestGroundDetection();
- if( Hit.Actor.IsValid() && !Cast< ARobinAIForestEntity >( Hit.Actor ) )
- {
- if( Cast< ALandscapeProxy >( Hit.Actor ) || Cast< ALandscape >( Hit.Actor ) || IsMovingOnGround() )
- {
- ObstacleAvoidTarget = UpdatedComponent->GetComponentLocation() + UpdatedComponent->GetUpVector() * TraceLength;
- ObstacleRadius = TraceLength;
- ObstacleLocation = Hit.Actor->GetActorLocation();
- // FlyingPitchModifier = CachedFlyingPitchModifier + 0.1;
- }
- else
- {
- float const RandSign = FMath::RandBool() ? 1.0f : -1.0f;
- FVector BoundingBox;
- FVector Origin;
- Hit.Actor->GetActorBounds( true, Origin, BoundingBox, false );
- ObstacleRadius = BoundingBox.Size2D();
- if( Hit.Component.IsValid() && Cast< UStaticMeshComponent >( Hit.Component ) )
- {
- ObstacleRadius = BoundingBox.Size();
- ObstacleAvoidTarget = UpdatedComponent->GetComponentLocation() + UpdatedComponent->GetUpVector() * ( ObstacleRadius + TraceLength );
- ObstacleLocation = Hit.Component->GetComponentLocation();
- // FlyingPitchModifier = CachedFlyingPitchModifier + 0.1;
- // TraceFrequency = 0.1f;
- }
- else
- {
- ObstacleAvoidTarget = UpdatedComponent->GetComponentLocation() + UpdatedComponent->GetUpVector() * 300 +
- RandSign * UpdatedComponent->GetRightVector() * 2 * ( ObstacleRadius + TraceLength );
- ObstacleLocation = Hit.Actor->GetActorLocation();
- }
- }
- }
- }
- }
- else
- {
- ObstacleRadius = 0;
- FlyingPitchModifier = CachedFlyingPitchModifier;
- TraceFrequency = CachedTraceFrequency;
- }
- }
- void URobinAIBirdMovementComponent::SetStartingMovement( ERobinAIBirdStartingMovement const StartingMovement )
- {
- switch( StartingMovement )
- {
- case ERobinAIBirdStartingMovement::InAir:
- Status = ERobinAIBirdMovementType::AirMovement;
- if( MovementSpline && AirMovementBox )
- {
- IsFlyingInBox = FMath::RandBool();
- IsFlyingOnSpline = !IsFlyingInBox;
- }
- break;
- case ERobinAIBirdStartingMovement::OnLand:
- Status = ERobinAIBirdMovementType::LandMovement;
- break;
- case ERobinAIBirdStartingMovement::OnPoint:
- Status = ERobinAIBirdMovementType::LandMovement;
- break;
- }
- FlyingRotationSpeedScale = FMath::RandRange( FlyingRotationSpeed.X, FlyingRotationSpeed.Y );
- FlyingFrontOffset = FMath::RandRange( FrontFlyingOffset.X, FrontFlyingOffset.Y );
- BirdRegularFlyMoveSpeed = FMath::RandRange( RegularFlyingMoveSpeed.X, RegularFlyingMoveSpeed.Y );
- BirdAlertedFlyMoveSpeed = FMath::RandRange( AlertedFlyingMoveSpeed.X, AlertedFlyingMoveSpeed.Y );
- }
- void URobinAIBirdMovementComponent::SetMovementSpline( ARobinSplineActor* const Spline )
- {
- MovementSpline = Spline;
- IsFlyingOnSpline = true;
- // SideFlyOffset = FMath::RandRange( MinSideFlyOffset, MaxSideFlyOffset );
- }
- void URobinAIBirdMovementComponent::SetMovementStatus( ERobinAIBirdMovementType const NewStatus )
- {
- if( Status == ERobinAIBirdMovementType::LandMovement && NewStatus == ERobinAIBirdMovementType::AirMovement )
- {
- // ProcessMainMontageBlendingOut(TakingOffMontage, false);
- PlayMontage( TakingOffMontage );
- if( MovementSpline && AirMovementBox )
- {
- IsFlyingInBox = FMath::RandBool();
- IsFlyingOnSpline = !IsFlyingInBox;
- }
- }
- if( Status != ERobinAIBirdMovementType::Death && ( Status != ERobinAIBirdMovementType::LandMovement ) ) // && NewStatus == ERobinAIBirdMovementType::AirMovement))
- {
- Status = NewStatus;
- }
- }
- ERobinAIBirdMovementType URobinAIBirdMovementComponent::GetMovementStatus() const
- {
- return Status;
- }
- void URobinAIBirdMovementComponent::SetAlerted( bool const Alert )
- {
- IsAlerted = Alert;
- }
- void URobinAIBirdMovementComponent::ProcessLandMovement( float const DeltaTime )
- {
- check( UpdatedComponent );
- if( ActivePath.IsValidLocationIndex( NextPathPoint ) && !CanLandOnObjects )
- {
- FVector const InitialLocation = UpdatedComponent->GetComponentLocation();
- FVector const TargetLocation = ActivePath.GetLocation( NextPathPoint ).Location + GetFeetOffset();
- FVector const ToTargetFlat = CalculateFlatDirection( TargetLocation - InitialLocation, UpdatedComponent->GetForwardVector() );
- float const EffectiveRotationSpeed = ( RotationSpeed ? RotationSpeed->GetFloatValue( TimeSincePathFollowing ) : 1.0f );
- FRotator const NewRotation = FMath::RInterpConstantTo( UpdatedComponent->GetComponentRotation(), ToTargetFlat.ToOrientationRotator(), DeltaTime, EffectiveRotationSpeed );
- FVector const Forward = NewRotation.Vector();
- float const EffectiveMoveSpeed = LandMoveSpeed * ExtraMovementSpeedScale.GetMin( 1.0f ) * AnimationMovementModifierScale;
- FVector const LocationDelta = Forward * EffectiveMoveSpeed * DeltaTime;
- FVector const AppliedLocationDelta = ApplyLocationDelta( LocationDelta, Forward.ToOrientationQuat(), DeltaTime, true );
- Velocity = AppliedLocationDelta / FMath::Max( DeltaTime, 0.001f );
- DesiredVelocity = Velocity.GetSafeNormal() * LandMoveSpeed * ExtraMovementSpeedScale.GetMin( 1.0f );
- RequestGroundDetection();
- if( !IsMovingOnGround() )
- {
- ApplyVelocity( -FVector::UpVector * 500.0f * DeltaTime, UpdatedComponent->GetComponentQuat(), DeltaTime, true );
- }
- if( IsAtLocation( TargetLocation, TargetRadius ) )
- {
- FinishFollowingPath( true );
- }
- }
- else
- {
- FinishFollowingPath( false );
- }
- }
- void URobinAIBirdMovementComponent::ProcessChoosingLandingTarget()
- {
- check( UpdatedComponent );
- FVector const InitialLocation = UpdatedComponent->GetComponentLocation();
- if( !CanLandOnObjects )
- {
- TOptional< FVector > SelectedTarget;
- UNavigationSystemV1* const NavigationSystem = UNavigationSystemV1::GetCurrent( GetWorld() );
- if( NavigationSystem )
- {
- if( OwnerEntity.IsValid() )
- {
- INavAgentInterface* const Agent = Cast< INavAgentInterface >( OwnerEntity );
- if( Agent )
- {
- FNavAgentProperties const& AgentProperties = Agent->GetNavAgentPropertiesRef();
- ANavigationData* const NavigationData = NavigationSystem->GetNavDataForProps( AgentProperties );
- if( NavigationData )
- {
- FNavLocation ProjectedLocation;
- if( NavigationSystem->ProjectPointToNavigation(
- InitialLocation + UpdatedComponent->GetForwardVector(), ProjectedLocation, FVector( 50.f, 50.f, 5000.f ), NavigationData ) )
- {
- SelectedTarget = ProjectedLocation.Location;
- LandingStartLocation = InitialLocation + UpdatedComponent->GetForwardVector() * FVector::Distance( InitialLocation, SelectedTarget.GetValue() ) / 3;
- }
- }
- }
- }
- }
- if( SelectedTarget.IsSet() )
- {
- LandingTarget = SelectedTarget.GetValue();
- Status = ERobinAIBirdMovementType::Landing;
- IsLerping = false;
- }
- else
- {
- Status = ERobinAIBirdMovementType::AirMovement;
- }
- }
- else
- {
- if( LandingOnObjectTarget != FVector::ZeroVector )
- {
- LandingTarget = LandingOnObjectTarget;
- LandingStartLocation = InitialLocation + UpdatedComponent->GetForwardVector() * FVector::Distance( InitialLocation, LandingTarget ) / 3;
- SetMovementStatus( ERobinAIBirdMovementType::Landing );
- IsLerping = false;
- }
- else
- {
- SetMovementStatus( ERobinAIBirdMovementType::AirMovement );
- }
- }
- }
- // Work In Progress, Lot of magic numbers and random stuff, reading this may cause health issues
- void URobinAIBirdMovementComponent::ProcessAirMovement( float const DeltaTime )
- {
- check( UpdatedComponent );
- if( ( AirMovementBox || MovementSpline ) && Status != ERobinAIBirdMovementType::Death )
- {
- FVector const InitialLocation = UpdatedComponent->GetComponentLocation();
- FVector DirectionToTarget;
- if( IsFlyingInBox && !IsFlyingOnSpline && !IsFlyingToPoint )
- {
- NextPoint = GetRandomPointForFlying();
- }
- else if( IsFlyingInBox && !IsFlyingOnSpline && IsFlyingToPoint && IsAtLocation( NextPoint, TargetRadius + 50 ) )
- {
- IsFlyingToPoint = false;
- }
- else if( IsFlyingOnSpline && !IsFlyingInBox )
- {
- URobinSplineComponent const& SplineComponent = MovementSpline->GetSpline();
- FVector const ClosestPointOnSpline =
- SplineComponent.FindTangentClosestToWorldLocation( InitialLocation, ESplineCoordinateSpace::World ).GetSafeNormal() * FlyingFrontOffset;
- FVector const TargetOnSplineVector = InitialLocation + ClosestPointOnSpline;
- FVector const ClosestPositionToGet = SplineComponent.FindLocationClosestToWorldLocation( TargetOnSplineVector, ESplineCoordinateSpace::World );
- NextPoint = ClosestPositionToGet + GetFeetOffset();
- }
- DirectionToTarget = NextPoint - InitialLocation;
- float AngleToNextPoint =
- UCommonMathStatics::AngleSignedCoplanarVectorsDegrees( FVector::UpVector, UpdatedComponent->GetForwardVector(), NextPoint - UpdatedComponent->GetComponentLocation() );
- ////SCREEN_LOG("{}", ActualFlyingSpeed / ActualMaxSpeed );
- FRotator RotationToDirection = DirectionToTarget.ToOrientationRotator();
- // if (AngleToNextPoint > 0)
- //{
- // UpdatedComponent->AddRelativeRotation(FRotator(0, 0, 50));
- // //RotationToDirection.Yaw = 250;
- // //RotationToDirection.Pitch = 250;
- //}
- // else
- //{
- // UpdatedComponent->AddRelativeRotation(FRotator(0, 0, 50));
- // //RotationToDirection.Pitch = -250;
- // //RotationToDirection.Yaw = -250;
- // //RotationToDirection.Pitch = -250;
- //}
- FRotator const NewRotation = FMath::RInterpConstantTo(
- UpdatedComponent->GetComponentRotation(), RotationToDirection, DeltaTime, FlyingRotationSpeedScale / ( ActualMaxFlyingSpeed / ActualFlyingSpeed ) );
- UpdateFlyingSpeedOnSharpTurn(AngleToNextPoint, (IsFlyingInBox ? 90.f : 30), DeltaTime);
- //// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- //if( FMath::Abs( AngleToNextPoint ) >= ( IsFlyingInBox ? 90.f : 30 ) && ActualFlyingSpeed > 400 )
- //{
- // ActualFlyingSpeed -= ( AirResistance + 150 ) * DeltaTime;
- // // DrawDebugSphere(GetWorld(), NextPoint, 50, 10, FColor::Red, false, 20.f, 0, 1);
- //}
- //if( ShouldGainSpeed )
- //{
- // ActualFlyingSpeed += ( AirResistance + 60 ) * DeltaTime;
- // if( ActualFlyingSpeed >= ActualMaxFlyingSpeed )
- // {
- // ShouldGainSpeed = false;
- // }
- //}
- //else
- //{
- // ActualFlyingSpeed -= AirResistance * DeltaTime;
- // if( ActualFlyingSpeed <= 400 )
- // {
- // ShouldGainSpeed = true;
- // }
- //}
- //// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // ActualFlyingSpeed -= ( FMath::Abs( NewRotation.Yaw ) / 180 )
- FVector const Forward = FRotator( NewRotation.Pitch * FlyingPitchModifier, NewRotation.Yaw, NewRotation.Roll ).Vector();
- // FVector const Forward = NewRotation.Vector();
- float const EffectiveMoveSpeed = ActualFlyingSpeed; //* AnimationMovementModifierScale;
- FVector const LocationDelta = Forward * EffectiveMoveSpeed * DeltaTime;
- FVector const AppliedLocationDelta = ApplyLocationDelta( LocationDelta, Forward.ToOrientationQuat(), DeltaTime, false );
- Velocity = AppliedLocationDelta / FMath::Max( DeltaTime, 0.001f );
- DesiredVelocity = Velocity.GetSafeNormal() * ActualFlyingSpeed;
- CanStartLanding = false;
- CanLandOnObjects = false;
- UpdateAnimInstance( DeltaTime );
- }
- }
- // Work In Progress, Lot of magic numbers and random stuff, reading this may cause health issues
- void URobinAIBirdMovementComponent::ProcessLanding( float const DeltaTime )
- {
- check( UpdatedComponent );
- if( !IsLerping )
- {
- if( TimeSinceLanding <= MaxLandingTimeForForceMoveAttempt )
- {
- TimeSinceLanding += DeltaTime;
- }
- else
- {
- RequestGroundDetection();
- if( CanSnapToLandingTarget() )
- {
- UpdatedComponent->SetWorldLocation( LandingTarget + GetFeetOffset() );
- }
- else if( !CanSnapToLandingTarget() && IsMovingOnGround() )
- {
- CachedLandingRotationSpeed = CachedLandingRotationSpeed + 30;
- }
- }
- }
- FVector const InitialLocation = UpdatedComponent->GetComponentLocation();
- if( !CanStartLanding )
- {
- FVector const ToTarget = LandingStartLocation - InitialLocation + GetFeetOffset();
- FRotator const RotationToTarget = ToTarget.ToOrientationRotator();
- float const AngleToLandingTarget = UCommonMathStatics::AngleSignedCoplanarVectorsDegrees( FVector::UpVector, UpdatedComponent->GetForwardVector(), ToTarget );
- UpdateFlyingSpeedOnSharpTurn(AngleToLandingTarget, 120.f, DeltaTime);
- //// FMath::RInterpConstantTo( UpdatedComponent->GetComponentRotation(), ToTarget.ToOrientationRotator(), DeltaTime, FlyingRotationSpeedScale + DeltaTime );
- //// ToTarget.ToOrientationRotator();
- //// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- //if( FMath::Abs( AngleToLandingTarget ) >= 120.f && ActualFlyingSpeed > 200 )
- //{
- // ActualFlyingSpeed -= ( AirResistance + 100 ) * DeltaTime;
- // // DrawDebugSphere(GetWorld(), NextPoint, 50, 10, FColor::Red, false, 20.f, 0, 1);
- //}
- //if( ShouldGainSpeed )
- //{
- // ActualFlyingSpeed += ( AirResistance + 100 ) * DeltaTime;
- // if( ActualFlyingSpeed >= ActualMaxFlyingSpeed )
- // {
- // ShouldGainSpeed = false;
- // }
- //}
- //else
- //{
- // ActualFlyingSpeed -= AirResistance * DeltaTime;
- // if( ActualFlyingSpeed <= 200 )
- // {
- // ShouldGainSpeed = true;
- // }
- //}
- //// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- float Distance = FVector::Distance( InitialLocation, LandingStartLocation );
- FRotator const NewRotation = FMath::RInterpConstantTo(
- UpdatedComponent->GetComponentRotation(), RotationToTarget, DeltaTime, FlyingRotationSpeedScale * ( ActualMaxFlyingSpeed / ActualFlyingSpeed ) );
- if( Distance > LerpDistance )
- {
- FVector const Forward = NewRotation.Vector();
- float const EffectiveMoveSpeed = ActualFlyingSpeed * AnimationMovementModifierScale;
- FVector const LocationDelta = Forward * EffectiveMoveSpeed * DeltaTime;
- FVector const AppliedLocationDelta = ApplyLocationDelta( LocationDelta, Forward.ToOrientationQuat(), DeltaTime, false );
- Velocity = AppliedLocationDelta / FMath::Max( DeltaTime, 0.001f );
- DesiredVelocity = Velocity.GetSafeNormal() * ActualFlyingSpeed / TimeSinceLanding;
- }
- else
- {
- CanStartLanding = true;
- }
- UpdateAnimInstance( DeltaTime );
- }
- else
- {
- FVector const ToTarget = LandingTarget - InitialLocation + GetFeetOffset();
- FRotator const RotationToTarget = ToTarget.ToOrientationRotator();
- float AngleToLandingTarget = UCommonMathStatics::AngleSignedCoplanarVectorsDegrees( FVector::UpVector, UpdatedComponent->GetForwardVector(), ToTarget );
- UpdateFlyingSpeedOnSharpTurn( AngleToLandingTarget, 120.f, DeltaTime );
- // if (FMath::Abs(AngleToLandingTarget) >= 120.f && ActualFlyingSpeed > 200)
- //{
- // ActualFlyingSpeed -= (AirResistance + 150) * DeltaTime;
- // // DrawDebugSphere(GetWorld(), NextPoint, 50, 10, FColor::Red, false, 20.f, 0, 1);
- //}
- //// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // if (FMath::Abs(AngleToLandingTarget) >= 120.f && ActualFlyingSpeed > 200)
- //{
- // ActualFlyingSpeed -= (AirResistance + 100) * DeltaTime;
- // // DrawDebugSphere(GetWorld(), NextPoint, 50, 10, FColor::Red, false, 20.f, 0, 1);
- //}
- // if (ShouldGainSpeed)
- //{
- // ActualFlyingSpeed += (AirResistance + 100) * DeltaTime;
- // if (ActualFlyingSpeed >= ActualMaxFlyingSpeed)
- // {
- // ShouldGainSpeed = false;
- // }
- //}
- // else
- //{
- // ActualFlyingSpeed -= AirResistance * DeltaTime;
- // if (ActualFlyingSpeed <= 200)
- // {
- // ShouldGainSpeed = true;
- // }
- //}
- //// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- if( !IsLerping )
- {
- FRotator const NewRotation = FMath::RInterpConstantTo(
- UpdatedComponent->GetComponentRotation(), RotationToTarget, DeltaTime, CachedLandingRotationSpeed * ( ActualMaxFlyingSpeed / ActualFlyingSpeed ) );
- FVector const Forward = FRotator( NewRotation.Pitch * LandingPitchModifier, NewRotation.Yaw, NewRotation.Roll ).Vector();
- float const EffectiveMoveSpeed = ActualFlyingSpeed * AnimationMovementModifierScale;
- FVector const LocationDelta = Forward * EffectiveMoveSpeed * DeltaTime;
- FVector const AppliedLocationDelta = ApplyLocationDelta( LocationDelta, Forward.ToOrientationQuat(), DeltaTime, false );
- Velocity = AppliedLocationDelta; // / FMath::Max( DeltaTime, 0.001f );
- DesiredVelocity = Velocity.GetSafeNormal() * ActualFlyingSpeed / TimeSinceLanding;
- if( FVector::Distance( UpdatedComponent->GetComponentLocation(), LandingTarget ) < 200.0f )
- {
- IsLerping = true;
- CachedLandingRotationSpeed = LandingRotationSpeed;
- CachedRotation = UpdatedComponent->GetComponentRotation();
- CachedLocation = UpdatedComponent->GetComponentLocation();
- CachedFlatDirectionToTarget = CalculateFlatDirection( ToTarget, UpdatedComponent->GetForwardVector() );
- CachedFlatRotationToTarget = CachedFlatDirectionToTarget.ToOrientationRotator();
- TimeSinceLerping = 0.0f;
- }
- UpdateAnimInstance( DeltaTime );
- }
- else
- {
- TimeSinceLerping += DeltaTime;
- float const Alpha = FMath::Clamp( TimeSinceLerping / LerpDuration, 0.0f, 1.0f );
- Velocity = FVector::ZeroVector;
- FVector const NewLocation = FMath::Lerp( CachedLocation, LandingTarget + GetFeetOffset(), Alpha );
- FRotator NewRotation = FMath::Lerp( CachedRotation, CachedFlatRotationToTarget, Alpha );
- NewRotation.Pitch = 0;
- UpdatedComponent->SetWorldLocationAndRotation( NewLocation, NewRotation );
- if( Alpha > 0.8f )
- {
- PlayMontage( LandingMontage );
- OnFinishedLandingBP.Broadcast();
- TimeSinceLanding = 0;
- ActualFlyingSpeed = 300.f;
- UpdateAnimInstance( DeltaTime );
- }
- }
- }
- }
- void URobinAIBirdMovementComponent::ProcessAvoidingObstacle( float const DeltaTime )
- {
- if( !IsLerping )
- {
- if( UpdatedComponent && ( FVector::Distance( UpdatedComponent->GetComponentLocation(), ObstacleAvoidTarget ) < GetFeetOffset().Size() ) )
- {
- FCollisionQueryParams const QueryParams( SCENE_QUERY_STAT( BirdLandingTraceTest ), false, GetOwner() );
- FCollisionResponseParams const ResponseParams = FCollisionResponseParams::DefaultResponseParam;
- ObstacleRadius = 0;
- GetWorld()->AsyncLineTraceByChannel( EAsyncTraceType::Single, UpdatedComponent->GetComponentLocation(),
- UpdatedComponent->GetComponentLocation() + UpdatedComponent->GetForwardVector() * TraceLength, ECollisionChannel::ECC_Visibility, QueryParams, ResponseParams,
- &DetectionTraceHandleDelegate );
- }
- else
- {
- FVector const ToTarget = ObstacleAvoidTarget;
- FVector const DirectionToTarget = ToTarget - UpdatedComponent->GetComponentLocation();
- FRotator const NewRotation = FMath::RInterpConstantTo( UpdatedComponent->GetComponentRotation(), DirectionToTarget.ToOrientationRotator(), DeltaTime,
- FlyingRotationSpeedScale * ( ActualMaxFlyingSpeed / ActualFlyingSpeed ) );
- FVector const Forward = NewRotation.Vector();
- float const EffectiveMoveSpeed = ActualFlyingSpeed * AnimationMovementModifierScale;
- FVector const LocationDelta = Forward * EffectiveMoveSpeed * DeltaTime;
- FVector const AppliedLocationDelta = ApplyLocationDelta( LocationDelta, Forward.ToOrientationQuat(), DeltaTime, false );
- Velocity = AppliedLocationDelta / FMath::Max( DeltaTime, 0.001f );
- }
- }
- }
- void URobinAIBirdMovementComponent::PlayMontage( UAnimMontage* const NewMontage )
- {
- check( UpdatedComponent );
- check( OwnerEntity.IsValid() );
- if( NewMontage )
- {
- if( URobinAIForestEntityAnimInstance* const AnimInstance = OwnerEntity->GetMainAnimInstance() )
- {
- if( NewMontage != ActiveMontage )
- {
- AnimInstance->Montage_Play( NewMontage );
- ActiveMontage = NewMontage;
- }
- }
- }
- }
- // TO DELETE
- // void URobinAIBirdMovementComponent::PlayTakingOffMontage()
- //{
- // check( UpdatedComponent );
- // check( OwnerEntity.IsValid() );
- // if( URobinAIForestEntityAnimInstance* const AnimInstance = OwnerEntity->GetMainAnimInstance() )
- //{
- // AnimInstance->Montage_Play( TakingOffMontage );
- // ActiveMontage = TakingOffMontage;
- // IsLerping = false;
- //}
- //}
- void URobinAIBirdMovementComponent::ProcessMainMontageBlendingOut( UAnimMontage* Montage, bool Interrupted )
- {
- if( Montage == LandingMontage )
- {
- Status = ERobinAIBirdMovementType::LandMovement;
- }
- if( Montage == TakingOffMontage )
- {
- Status = ERobinAIBirdMovementType::AirMovement;
- }
- }
- void URobinAIBirdMovementComponent::ProccessMainMontageEnded( UAnimMontage* Montage, bool Interrupted )
- {
- if( Montage == LandingMontage || Montage == TakingOffMontage )
- {
- ActiveMontage.Reset();
- }
- }
- void URobinAIBirdMovementComponent::UpdateAnimInstance( float const DeltaTime )
- {
- if( OwnerEntity.IsValid() )
- {
- float const EffectiveAnimationSpeedScale = AnimationSpeedScale.GetProduct( 1.0f );
- URobinAIBirdAnimInstance* BirdAnimInstance = Cast< URobinAIBirdAnimInstance >( OwnerEntity->GetMainAnimInstance() );
- if( BirdAnimInstance )
- {
- if( Status != ERobinAIBirdMovementType::Landing )
- {
- BirdAnimInstance->SetSpeed( Velocity.Size2D() );
- }
- else
- {
- // During landing it will have different value
- BirdAnimInstance->SetSpeed( Velocity.Size2D() );
- }
- }
- }
- }
- void URobinAIBirdMovementComponent::SetCanLandOnObjects( bool const CanLand )
- {
- CanLandOnObjects = CanLand;
- }
- void URobinAIBirdMovementComponent::SetLandingOnObjectTarget( FVector const Location )
- {
- LandingOnObjectTarget = Location;
- }
- bool URobinAIBirdMovementComponent::CanSnapToLandingTarget() const
- {
- check( UpdatedComponent );
- ARobinPlayerCharacter* const Player = URobinCharacterStatics::GetMainPlayer( this );
- if( Player )
- {
- URobinPlayerViewControllerComponent const& ViewController = Player->GetViewController();
- if( !ViewController.IsPlayerFacingLocation( UpdatedComponent->GetComponentLocation(), 70.f, 4000.f ) )
- {
- if( !ViewController.IsPlayerFacingLocation( LandingTarget ) )
- {
- return true;
- }
- }
- return false;
- }
- return true;
- }
- bool URobinAIBirdMovementComponent::IsLandingOnObject() const
- {
- return CanLandOnObjects;
- }
- void URobinAIBirdMovementComponent::SetStartingPosition( FVector const Position )
- {
- check( GetOwner() );
- FRotator NewRotation = FRotator( 0, 0, 0 );
- NewRotation.Yaw = FMath::FRandRange( 0, 360 );
- GetOwner()->SetActorLocationAndRotation( Position + GetFeetOffset(), NewRotation );
- }
- FVector URobinAIBirdMovementComponent::GetRandomPointForFlying() const
- {
- UBoxComponent* Box = Cast< UBoxComponent >( AirMovementBox->GetCollisionComponent() );
- if( Box )
- {
- return UKismetMathLibrary::RandomPointInBoundingBox( Box->GetComponentLocation(), Box->GetScaledBoxExtent() );
- }
- return AirMovementBox->GetActorLocation();
- }
- void URobinAIBirdMovementComponent::SetSpawnerLocation( FVector const SpawnLocation )
- {
- SpawnerLocation = SpawnLocation;
- }
- void URobinAIBirdMovementComponent::SetAirMovementBox( ATriggerBox* const Box )
- {
- AirMovementBox = Box;
- IsFlyingInBox = true;
- }
- bool URobinAIBirdMovementComponent::IsPlayerLookingAtBird()
- {
- check( UpdatedComponent );
- ARobinPlayerCharacter* const Player = URobinCharacterStatics::GetMainPlayer( this );
- if( Player )
- {
- URobinPlayerViewControllerComponent const& ViewController = Player->GetViewController();
- if( !ViewController.IsPlayerFacingLocation( UpdatedComponent->GetComponentLocation() ) )
- {
- return false;
- }
- return true;
- }
- return false;
- }
- void URobinAIBirdMovementComponent::UpdateActualMaxSpeed()
- {
- if( IsAlerted )
- {
- ActualMaxFlyingSpeed = BirdAlertedFlyMoveSpeed;
- }
- else
- {
- ActualMaxFlyingSpeed = BirdRegularFlyMoveSpeed;
- }
- }
- void URobinAIBirdMovementComponent::UpdateFlyingSpeedOnSharpTurn( float const AngleToTarget, float const MaxAngleRegularTurn, float const DeltaTime )
- {
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Missing variables, moved it to separate function for testing
- if( FMath::Abs( AngleToTarget ) >= MaxAngleRegularTurn && ActualFlyingSpeed > 400 )
- {
- ActualFlyingSpeed -= ( AirResistance + 200 ) * DeltaTime;
- // DrawDebugSphere(GetWorld(), NextPoint, 50, 10, FColor::Red, false, 20.f, 0, 1);
- }
- if( ShouldGainSpeed )
- {
- ActualFlyingSpeed += ( AirResistance + 120 ) * DeltaTime;
- if( ActualFlyingSpeed >= ActualMaxFlyingSpeed )
- {
- ShouldGainSpeed = false;
- }
- }
- else
- {
- ActualFlyingSpeed -= AirResistance * DeltaTime;
- if( ActualFlyingSpeed <= 400 )
- {
- ShouldGainSpeed = true;
- }
- }
- }
Editor
You can edit this paste and save as new: