File size: 21,465 Bytes
1e67697 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | // Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraCharacter.h"
#include "AbilitySystem/LyraAbilitySystemComponent.h"
#include "Camera/LyraCameraComponent.h"
#include "Character/LyraHealthComponent.h"
#include "Character/LyraPawnExtensionComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "LyraCharacterMovementComponent.h"
#include "LyraGameplayTags.h"
#include "LyraLogChannels.h"
#include "Net/UnrealNetwork.h"
#include "Player/LyraPlayerController.h"
#include "Player/LyraPlayerState.h"
#include "System/LyraSignificanceManager.h"
#include "TimerManager.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraCharacter)
class AActor;
class FLifetimeProperty;
class IRepChangedPropertyTracker;
class UInputComponent;
static FName NAME_LyraCharacterCollisionProfile_Capsule(TEXT("LyraPawnCapsule"));
static FName NAME_LyraCharacterCollisionProfile_Mesh(TEXT("LyraPawnMesh"));
ALyraCharacter::ALyraCharacter(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer.SetDefaultSubobjectClass<ULyraCharacterMovementComponent>(ACharacter::CharacterMovementComponentName))
{
// Avoid ticking characters if possible.
PrimaryActorTick.bCanEverTick = false;
PrimaryActorTick.bStartWithTickEnabled = false;
SetNetCullDistanceSquared(900000000.0f);
UCapsuleComponent* CapsuleComp = GetCapsuleComponent();
check(CapsuleComp);
CapsuleComp->InitCapsuleSize(40.0f, 90.0f);
CapsuleComp->SetCollisionProfileName(NAME_LyraCharacterCollisionProfile_Capsule);
USkeletalMeshComponent* MeshComp = GetMesh();
check(MeshComp);
MeshComp->SetRelativeRotation(FRotator(0.0f, -90.0f, 0.0f)); // Rotate mesh to be X forward since it is exported as Y forward.
MeshComp->SetCollisionProfileName(NAME_LyraCharacterCollisionProfile_Mesh);
ULyraCharacterMovementComponent* LyraMoveComp = CastChecked<ULyraCharacterMovementComponent>(GetCharacterMovement());
LyraMoveComp->GravityScale = 1.0f;
LyraMoveComp->MaxAcceleration = 2400.0f;
LyraMoveComp->BrakingFrictionFactor = 1.0f;
LyraMoveComp->BrakingFriction = 6.0f;
LyraMoveComp->GroundFriction = 8.0f;
LyraMoveComp->BrakingDecelerationWalking = 1400.0f;
LyraMoveComp->bUseControllerDesiredRotation = false;
LyraMoveComp->bOrientRotationToMovement = false;
LyraMoveComp->RotationRate = FRotator(0.0f, 720.0f, 0.0f);
LyraMoveComp->bAllowPhysicsRotationDuringAnimRootMotion = false;
LyraMoveComp->GetNavAgentPropertiesRef().bCanCrouch = true;
LyraMoveComp->bCanWalkOffLedgesWhenCrouching = true;
LyraMoveComp->SetCrouchedHalfHeight(65.0f);
PawnExtComponent = CreateDefaultSubobject<ULyraPawnExtensionComponent>(TEXT("PawnExtensionComponent"));
PawnExtComponent->OnAbilitySystemInitialized_RegisterAndCall(FSimpleMulticastDelegate::FDelegate::CreateUObject(this, &ThisClass::OnAbilitySystemInitialized));
PawnExtComponent->OnAbilitySystemUninitialized_Register(FSimpleMulticastDelegate::FDelegate::CreateUObject(this, &ThisClass::OnAbilitySystemUninitialized));
HealthComponent = CreateDefaultSubobject<ULyraHealthComponent>(TEXT("HealthComponent"));
HealthComponent->OnDeathStarted.AddDynamic(this, &ThisClass::OnDeathStarted);
HealthComponent->OnDeathFinished.AddDynamic(this, &ThisClass::OnDeathFinished);
CameraComponent = CreateDefaultSubobject<ULyraCameraComponent>(TEXT("CameraComponent"));
CameraComponent->SetRelativeLocation(FVector(-300.0f, 0.0f, 75.0f));
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = true;
bUseControllerRotationRoll = false;
BaseEyeHeight = 80.0f;
CrouchedEyeHeight = 50.0f;
}
void ALyraCharacter::PreInitializeComponents()
{
Super::PreInitializeComponents();
}
void ALyraCharacter::BeginPlay()
{
Super::BeginPlay();
UWorld* World = GetWorld();
const bool bRegisterWithSignificanceManager = !IsNetMode(NM_DedicatedServer);
if (bRegisterWithSignificanceManager)
{
if (ULyraSignificanceManager* SignificanceManager = USignificanceManager::Get<ULyraSignificanceManager>(World))
{
//@TODO: SignificanceManager->RegisterObject(this, (EFortSignificanceType)SignificanceType);
}
}
}
void ALyraCharacter::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);
UWorld* World = GetWorld();
const bool bRegisterWithSignificanceManager = !IsNetMode(NM_DedicatedServer);
if (bRegisterWithSignificanceManager)
{
if (ULyraSignificanceManager* SignificanceManager = USignificanceManager::Get<ULyraSignificanceManager>(World))
{
SignificanceManager->UnregisterObject(this);
}
}
}
void ALyraCharacter::Reset()
{
DisableMovementAndCollision();
K2_OnReset();
UninitAndDestroy();
}
void ALyraCharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty >& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME_CONDITION(ThisClass, ReplicatedAcceleration, COND_SimulatedOnly);
DOREPLIFETIME(ThisClass, MyTeamID)
}
void ALyraCharacter::PreReplication(IRepChangedPropertyTracker& ChangedPropertyTracker)
{
Super::PreReplication(ChangedPropertyTracker);
if (UCharacterMovementComponent* MovementComponent = GetCharacterMovement())
{
// Compress Acceleration: XY components as direction + magnitude, Z component as direct value
const double MaxAccel = MovementComponent->MaxAcceleration;
const FVector CurrentAccel = MovementComponent->GetCurrentAcceleration();
double AccelXYRadians, AccelXYMagnitude;
FMath::CartesianToPolar(CurrentAccel.X, CurrentAccel.Y, AccelXYMagnitude, AccelXYRadians);
ReplicatedAcceleration.AccelXYRadians = FMath::FloorToInt((AccelXYRadians / TWO_PI) * 255.0); // [0, 2PI] -> [0, 255]
ReplicatedAcceleration.AccelXYMagnitude = FMath::FloorToInt((AccelXYMagnitude / MaxAccel) * 255.0); // [0, MaxAccel] -> [0, 255]
ReplicatedAcceleration.AccelZ = FMath::FloorToInt((CurrentAccel.Z / MaxAccel) * 127.0); // [-MaxAccel, MaxAccel] -> [-127, 127]
}
}
void ALyraCharacter::NotifyControllerChanged()
{
const FGenericTeamId OldTeamId = GetGenericTeamId();
Super::NotifyControllerChanged();
// Update our team ID based on the controller
if (HasAuthority() && (GetController() != nullptr))
{
if (ILyraTeamAgentInterface* ControllerWithTeam = Cast<ILyraTeamAgentInterface>(GetController()))
{
MyTeamID = ControllerWithTeam->GetGenericTeamId();
ConditionalBroadcastTeamChanged(this, OldTeamId, MyTeamID);
}
}
}
ALyraPlayerController* ALyraCharacter::GetLyraPlayerController() const
{
return CastChecked<ALyraPlayerController>(GetController(), ECastCheckedType::NullAllowed);
}
ALyraPlayerState* ALyraCharacter::GetLyraPlayerState() const
{
return CastChecked<ALyraPlayerState>(GetPlayerState(), ECastCheckedType::NullAllowed);
}
ULyraAbilitySystemComponent* ALyraCharacter::GetLyraAbilitySystemComponent() const
{
return Cast<ULyraAbilitySystemComponent>(GetAbilitySystemComponent());
}
UAbilitySystemComponent* ALyraCharacter::GetAbilitySystemComponent() const
{
if (PawnExtComponent == nullptr)
{
return nullptr;
}
return PawnExtComponent->GetLyraAbilitySystemComponent();
}
void ALyraCharacter::OnAbilitySystemInitialized()
{
ULyraAbilitySystemComponent* LyraASC = GetLyraAbilitySystemComponent();
check(LyraASC);
HealthComponent->InitializeWithAbilitySystem(LyraASC);
InitializeGameplayTags();
}
void ALyraCharacter::OnAbilitySystemUninitialized()
{
HealthComponent->UninitializeFromAbilitySystem();
}
void ALyraCharacter::PossessedBy(AController* NewController)
{
const FGenericTeamId OldTeamID = MyTeamID;
Super::PossessedBy(NewController);
PawnExtComponent->HandleControllerChanged();
// Grab the current team ID and listen for future changes
if (ILyraTeamAgentInterface* ControllerAsTeamProvider = Cast<ILyraTeamAgentInterface>(NewController))
{
MyTeamID = ControllerAsTeamProvider->GetGenericTeamId();
ControllerAsTeamProvider->GetTeamChangedDelegateChecked().AddDynamic(this, &ThisClass::OnControllerChangedTeam);
}
ConditionalBroadcastTeamChanged(this, OldTeamID, MyTeamID);
}
void ALyraCharacter::UnPossessed()
{
AController* const OldController = GetController();
// Stop listening for changes from the old controller
const FGenericTeamId OldTeamID = MyTeamID;
if (ILyraTeamAgentInterface* ControllerAsTeamProvider = Cast<ILyraTeamAgentInterface>(OldController))
{
ControllerAsTeamProvider->GetTeamChangedDelegateChecked().RemoveAll(this);
}
Super::UnPossessed();
PawnExtComponent->HandleControllerChanged();
// Determine what the new team ID should be afterwards
MyTeamID = DetermineNewTeamAfterPossessionEnds(OldTeamID);
ConditionalBroadcastTeamChanged(this, OldTeamID, MyTeamID);
}
void ALyraCharacter::OnRep_Controller()
{
Super::OnRep_Controller();
PawnExtComponent->HandleControllerChanged();
}
void ALyraCharacter::OnRep_PlayerState()
{
Super::OnRep_PlayerState();
PawnExtComponent->HandlePlayerStateReplicated();
}
void ALyraCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PawnExtComponent->SetupPlayerInputComponent();
}
void ALyraCharacter::InitializeGameplayTags()
{
// Clear tags that may be lingering on the ability system from the previous pawn.
if (ULyraAbilitySystemComponent* LyraASC = GetLyraAbilitySystemComponent())
{
for (const TPair<uint8, FGameplayTag>& TagMapping : LyraGameplayTags::MovementModeTagMap)
{
if (TagMapping.Value.IsValid())
{
LyraASC->SetLooseGameplayTagCount(TagMapping.Value, 0);
}
}
for (const TPair<uint8, FGameplayTag>& TagMapping : LyraGameplayTags::CustomMovementModeTagMap)
{
if (TagMapping.Value.IsValid())
{
LyraASC->SetLooseGameplayTagCount(TagMapping.Value, 0);
}
}
ULyraCharacterMovementComponent* LyraMoveComp = CastChecked<ULyraCharacterMovementComponent>(GetCharacterMovement());
SetMovementModeTag(LyraMoveComp->MovementMode, LyraMoveComp->CustomMovementMode, true);
}
}
void ALyraCharacter::GetOwnedGameplayTags(FGameplayTagContainer& TagContainer) const
{
if (const ULyraAbilitySystemComponent* LyraASC = GetLyraAbilitySystemComponent())
{
LyraASC->GetOwnedGameplayTags(TagContainer);
}
}
bool ALyraCharacter::HasMatchingGameplayTag(FGameplayTag TagToCheck) const
{
if (const ULyraAbilitySystemComponent* LyraASC = GetLyraAbilitySystemComponent())
{
return LyraASC->HasMatchingGameplayTag(TagToCheck);
}
return false;
}
bool ALyraCharacter::HasAllMatchingGameplayTags(const FGameplayTagContainer& TagContainer) const
{
if (const ULyraAbilitySystemComponent* LyraASC = GetLyraAbilitySystemComponent())
{
return LyraASC->HasAllMatchingGameplayTags(TagContainer);
}
return false;
}
bool ALyraCharacter::HasAnyMatchingGameplayTags(const FGameplayTagContainer& TagContainer) const
{
if (const ULyraAbilitySystemComponent* LyraASC = GetLyraAbilitySystemComponent())
{
return LyraASC->HasAnyMatchingGameplayTags(TagContainer);
}
return false;
}
void ALyraCharacter::FellOutOfWorld(const class UDamageType& dmgType)
{
HealthComponent->DamageSelfDestruct(/*bFellOutOfWorld=*/ true);
}
void ALyraCharacter::OnDeathStarted(AActor*)
{
DisableMovementAndCollision();
}
void ALyraCharacter::OnDeathFinished(AActor*)
{
GetWorld()->GetTimerManager().SetTimerForNextTick(this, &ThisClass::DestroyDueToDeath);
}
void ALyraCharacter::DisableMovementAndCollision()
{
if (GetController())
{
GetController()->SetIgnoreMoveInput(true);
}
UCapsuleComponent* CapsuleComp = GetCapsuleComponent();
check(CapsuleComp);
CapsuleComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
CapsuleComp->SetCollisionResponseToAllChannels(ECR_Ignore);
ULyraCharacterMovementComponent* LyraMoveComp = CastChecked<ULyraCharacterMovementComponent>(GetCharacterMovement());
LyraMoveComp->StopMovementImmediately();
LyraMoveComp->DisableMovement();
}
void ALyraCharacter::DestroyDueToDeath()
{
K2_OnDeathFinished();
UninitAndDestroy();
}
void ALyraCharacter::UninitAndDestroy()
{
if (GetLocalRole() == ROLE_Authority)
{
DetachFromControllerPendingDestroy();
SetLifeSpan(0.1f);
}
// Uninitialize the ASC if we're still the avatar actor (otherwise another pawn already did it when they became the avatar actor)
if (ULyraAbilitySystemComponent* LyraASC = GetLyraAbilitySystemComponent())
{
if (LyraASC->GetAvatarActor() == this)
{
PawnExtComponent->UninitializeAbilitySystem();
}
}
SetActorHiddenInGame(true);
}
void ALyraCharacter::OnMovementModeChanged(EMovementMode PrevMovementMode, uint8 PreviousCustomMode)
{
Super::OnMovementModeChanged(PrevMovementMode, PreviousCustomMode);
ULyraCharacterMovementComponent* LyraMoveComp = CastChecked<ULyraCharacterMovementComponent>(GetCharacterMovement());
SetMovementModeTag(PrevMovementMode, PreviousCustomMode, false);
SetMovementModeTag(LyraMoveComp->MovementMode, LyraMoveComp->CustomMovementMode, true);
}
void ALyraCharacter::SetMovementModeTag(EMovementMode MovementMode, uint8 CustomMovementMode, bool bTagEnabled)
{
if (ULyraAbilitySystemComponent* LyraASC = GetLyraAbilitySystemComponent())
{
const FGameplayTag* MovementModeTag = nullptr;
if (MovementMode == MOVE_Custom)
{
MovementModeTag = LyraGameplayTags::CustomMovementModeTagMap.Find(CustomMovementMode);
}
else
{
MovementModeTag = LyraGameplayTags::MovementModeTagMap.Find(MovementMode);
}
if (MovementModeTag && MovementModeTag->IsValid())
{
LyraASC->SetLooseGameplayTagCount(*MovementModeTag, (bTagEnabled ? 1 : 0));
}
}
}
void ALyraCharacter::ToggleCrouch()
{
const ULyraCharacterMovementComponent* LyraMoveComp = CastChecked<ULyraCharacterMovementComponent>(GetCharacterMovement());
if (IsCrouched() || LyraMoveComp->bWantsToCrouch)
{
UnCrouch();
}
else if (LyraMoveComp->IsMovingOnGround())
{
Crouch();
}
}
void ALyraCharacter::OnStartCrouch(float HalfHeightAdjust, float ScaledHalfHeightAdjust)
{
if (ULyraAbilitySystemComponent* LyraASC = GetLyraAbilitySystemComponent())
{
LyraASC->SetLooseGameplayTagCount(LyraGameplayTags::Status_Crouching, 1);
}
Super::OnStartCrouch(HalfHeightAdjust, ScaledHalfHeightAdjust);
}
void ALyraCharacter::OnEndCrouch(float HalfHeightAdjust, float ScaledHalfHeightAdjust)
{
if (ULyraAbilitySystemComponent* LyraASC = GetLyraAbilitySystemComponent())
{
LyraASC->SetLooseGameplayTagCount(LyraGameplayTags::Status_Crouching, 0);
}
Super::OnEndCrouch(HalfHeightAdjust, ScaledHalfHeightAdjust);
}
bool ALyraCharacter::CanJumpInternal_Implementation() const
{
// same as ACharacter's implementation but without the crouch check
return JumpIsAllowedInternal();
}
void ALyraCharacter::OnRep_ReplicatedAcceleration()
{
if (ULyraCharacterMovementComponent* LyraMovementComponent = Cast<ULyraCharacterMovementComponent>(GetCharacterMovement()))
{
// Decompress Acceleration
const double MaxAccel = LyraMovementComponent->MaxAcceleration;
const double AccelXYMagnitude = double(ReplicatedAcceleration.AccelXYMagnitude) * MaxAccel / 255.0; // [0, 255] -> [0, MaxAccel]
const double AccelXYRadians = double(ReplicatedAcceleration.AccelXYRadians) * TWO_PI / 255.0; // [0, 255] -> [0, 2PI]
FVector UnpackedAcceleration(FVector::ZeroVector);
FMath::PolarToCartesian(AccelXYMagnitude, AccelXYRadians, UnpackedAcceleration.X, UnpackedAcceleration.Y);
UnpackedAcceleration.Z = double(ReplicatedAcceleration.AccelZ) * MaxAccel / 127.0; // [-127, 127] -> [-MaxAccel, MaxAccel]
LyraMovementComponent->SetReplicatedAcceleration(UnpackedAcceleration);
}
}
void ALyraCharacter::SetGenericTeamId(const FGenericTeamId& NewTeamID)
{
if (GetController() == nullptr)
{
if (HasAuthority())
{
const FGenericTeamId OldTeamID = MyTeamID;
MyTeamID = NewTeamID;
ConditionalBroadcastTeamChanged(this, OldTeamID, MyTeamID);
}
else
{
UE_LOG(LogLyraTeams, Error, TEXT("You can't set the team ID on a character (%s) except on the authority"), *GetPathNameSafe(this));
}
}
else
{
UE_LOG(LogLyraTeams, Error, TEXT("You can't set the team ID on a possessed character (%s); it's driven by the associated controller"), *GetPathNameSafe(this));
}
}
FGenericTeamId ALyraCharacter::GetGenericTeamId() const
{
return MyTeamID;
}
FOnLyraTeamIndexChangedDelegate* ALyraCharacter::GetOnTeamIndexChangedDelegate()
{
return &OnTeamChangedDelegate;
}
void ALyraCharacter::OnControllerChangedTeam(UObject* TeamAgent, int32 OldTeam, int32 NewTeam)
{
const FGenericTeamId MyOldTeamID = MyTeamID;
MyTeamID = IntegerToGenericTeamId(NewTeam);
ConditionalBroadcastTeamChanged(this, MyOldTeamID, MyTeamID);
}
void ALyraCharacter::OnRep_MyTeamID(FGenericTeamId OldTeamID)
{
ConditionalBroadcastTeamChanged(this, OldTeamID, MyTeamID);
}
bool ALyraCharacter::UpdateSharedReplication()
{
if (GetLocalRole() == ROLE_Authority)
{
FSharedRepMovement SharedMovement;
if (SharedMovement.FillForCharacter(this))
{
// Only call FastSharedReplication if data has changed since the last frame.
// Skipping this call will cause replication to reuse the same bunch that we previously
// produced, but not send it to clients that already received. (But a new client who has not received
// it, will get it this frame)
if (!SharedMovement.Equals(LastSharedReplication, this))
{
LastSharedReplication = SharedMovement;
SetReplicatedMovementMode(SharedMovement.RepMovementMode);
FastSharedReplication(SharedMovement);
}
return true;
}
}
// We cannot fastrep right now. Don't send anything.
return false;
}
void ALyraCharacter::FastSharedReplication_Implementation(const FSharedRepMovement& SharedRepMovement)
{
if (GetWorld()->IsPlayingReplay())
{
return;
}
// Timestamp is checked to reject old moves.
if (GetLocalRole() == ROLE_SimulatedProxy)
{
// Timestamp
SetReplicatedServerLastTransformUpdateTimeStamp(SharedRepMovement.RepTimeStamp);
// Movement mode
if (GetReplicatedMovementMode() != SharedRepMovement.RepMovementMode)
{
SetReplicatedMovementMode(SharedRepMovement.RepMovementMode);
GetCharacterMovement()->bNetworkMovementModeChanged = true;
GetCharacterMovement()->bNetworkUpdateReceived = true;
}
// Location, Rotation, Velocity, etc.
FRepMovement& MutableRepMovement = GetReplicatedMovement_Mutable();
MutableRepMovement = SharedRepMovement.RepMovement;
// This also sets LastRepMovement
OnRep_ReplicatedMovement();
// Jump force
SetProxyIsJumpForceApplied(SharedRepMovement.bProxyIsJumpForceApplied);
// Crouch
if (IsCrouched() != SharedRepMovement.bIsCrouched)
{
SetIsCrouched(SharedRepMovement.bIsCrouched);
OnRep_IsCrouched();
}
}
}
FSharedRepMovement::FSharedRepMovement()
{
RepMovement.LocationQuantizationLevel = EVectorQuantization::RoundTwoDecimals;
}
bool FSharedRepMovement::FillForCharacter(ACharacter* Character)
{
if (USceneComponent* PawnRootComponent = Character->GetRootComponent())
{
UCharacterMovementComponent* CharacterMovement = Character->GetCharacterMovement();
RepMovement.Location = FRepMovement::RebaseOntoZeroOrigin(PawnRootComponent->GetComponentLocation(), Character);
RepMovement.Rotation = PawnRootComponent->GetComponentRotation();
RepMovement.LinearVelocity = CharacterMovement->Velocity;
RepMovementMode = CharacterMovement->PackNetworkMovementMode();
bProxyIsJumpForceApplied = Character->GetProxyIsJumpForceApplied() || (Character->JumpForceTimeRemaining > 0.0f);
bIsCrouched = Character->IsCrouched();
// Timestamp is sent as zero if unused
if ((CharacterMovement->NetworkSmoothingMode == ENetworkSmoothingMode::Linear) || CharacterMovement->bNetworkAlwaysReplicateTransformUpdateTimestamp)
{
RepTimeStamp = CharacterMovement->GetServerLastTransformUpdateTimeStamp();
}
else
{
RepTimeStamp = 0.f;
}
return true;
}
return false;
}
bool FSharedRepMovement::Equals(const FSharedRepMovement& Other, ACharacter* Character) const
{
if (RepMovement.Location != Other.RepMovement.Location)
{
return false;
}
if (RepMovement.Rotation != Other.RepMovement.Rotation)
{
return false;
}
if (RepMovement.LinearVelocity != Other.RepMovement.LinearVelocity)
{
return false;
}
if (RepMovementMode != Other.RepMovementMode)
{
return false;
}
if (bProxyIsJumpForceApplied != Other.bProxyIsJumpForceApplied)
{
return false;
}
if (bIsCrouched != Other.bIsCrouched)
{
return false;
}
return true;
}
bool FSharedRepMovement::NetSerialize(FArchive& Ar, class UPackageMap* Map, bool& bOutSuccess)
{
bOutSuccess = true;
RepMovement.NetSerialize(Ar, Map, bOutSuccess);
Ar << RepMovementMode;
Ar << bProxyIsJumpForceApplied;
Ar << bIsCrouched;
// Timestamp, if non-zero.
uint8 bHasTimeStamp = (RepTimeStamp != 0.f);
Ar.SerializeBits(&bHasTimeStamp, 1);
if (bHasTimeStamp)
{
Ar << RepTimeStamp;
}
else
{
RepTimeStamp = 0.f;
}
return true;
} |