diff --git a/Content/Blueprints/Character/BP_Cloud9Character.uasset b/Content/Blueprints/Character/BP_Cloud9Character.uasset index a0712de20..93ca373d8 100644 Binary files a/Content/Blueprints/Character/BP_Cloud9Character.uasset and b/Content/Blueprints/Character/BP_Cloud9Character.uasset differ diff --git a/Content/Maps/warmup.umap b/Content/Maps/warmup.umap index e385819c7..dda84eb8c 100644 Binary files a/Content/Maps/warmup.umap and b/Content/Maps/warmup.umap differ diff --git a/Source/Cloud9/Character/Cloud9Character.cpp b/Source/Cloud9/Character/Cloud9Character.cpp index 01faade1c..9720ac464 100644 --- a/Source/Cloud9/Character/Cloud9Character.cpp +++ b/Source/Cloud9/Character/Cloud9Character.cpp @@ -1,5 +1,3 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - #include "Cloud9Character.h" #include "DrawDebugHelpers.h" @@ -31,9 +29,6 @@ ACloud9Character::ACloud9Character(const FObjectInitializer& ObjectInitializer) bUseControllerRotationYaw = false; bUseControllerRotationRoll = false; - RotationSpeed = 8.0f; - TargetRotation = GetActorRotation(); - GetCapsuleComponent()->InitCapsuleSize(32.f, 72.0f); const auto Movement = GetCharacterMovement(); @@ -100,7 +95,7 @@ void ACloud9Character::UnSneak() const } } -void ACloud9Character::SetViewDirection(const FHitResult& HitResult, bool bIsHitValid) +void ACloud9Character::SetViewDirection(const FHitResult& HitResult, bool bIsHitValid) const { if (IsValid(CursorToWorld)) { @@ -150,7 +145,7 @@ void ACloud9Character::SetViewDirection(const FHitResult& HitResult, bool bIsHit const auto TargetLocation = FVector{HitResult.Location.X, HitResult.Location.Y, 0.0f}; const auto ActorLocation = GetActorLocation(); const auto LookRotation = UKismetMathLibrary::FindLookAtRotation(ActorLocation, TargetLocation); - TargetRotation.Yaw = LookRotation.Yaw; + GetCloud9CharacterMovement()->Rotate({0.0f, LookRotation.Yaw, 0.0f}); } } @@ -207,35 +202,23 @@ void ACloud9Character::OnConstruction(const FTransform& Transform) const auto Rotator = Transform.Rotator(); - TargetRotation.Yaw = Rotator.Yaw; SetCameraRotationYaw(Rotator.Yaw); - SetActorRotation(TargetRotation); + GetCloud9CharacterMovement()->Rotate({0.0f, Rotator.Yaw, 0.0f}, true); SetCursorIsHidden(true); - if (IsValid(CursorDecal)) + if (CursorDecal != nullptr) { UE_LOG(LogCloud9, Display, TEXT("Setup CursorDecal = %s"), *CursorDecal->GetName()); CursorToWorld->SetDecalMaterial(CursorDecal); CursorToWorld->DecalSize = FVector(16.0f, 32.0f, 32.0f); } - if (IsValid(GetMesh())) + if (GetMesh() != nullptr && !CameraTargetBoneName.IsNone()) { - if (!CameraTargetBoneName.IsNone()) - { - const auto HeadBoneLocation = GetMesh()->GetBoneLocation(CameraTargetBoneName, EBoneSpaces::WorldSpace); - CameraBoom->SetWorldLocation(HeadBoneLocation); - } - - const auto Box = UCloud9ToolsLibrary::GetAccurateReferencePoseBounds(GetMesh()->SkeletalMesh); - - UE_LOG(LogCloud9, Display, TEXT("Box = %s GetMesh()->Bounds = %s"), *Box.ToString(), - *GetMesh()->Bounds.ToString()); - - // float Width = 0.0f, Height = 0.0f, Depth = 0.0f; - // UCloud9ToolsLibrary::GetWidthHeightDepth(GetMesh()->Bounds.GetBox(), Width, Height, Depth); - // GetCapsuleComponent()->InitCapsuleSize(Width, Height); + const auto HeadBoneLocation = GetMesh()->GetBoneLocation(CameraTargetBoneName, EBoneSpaces::WorldSpace); + UE_LOG(LogCloud9, Display, TEXT("Setup CameraBoom = %s"), *HeadBoneLocation.ToString()); + CameraBoom->SetWorldLocation(HeadBoneLocation); } } @@ -247,37 +230,4 @@ void ACloud9Character::BeginPlay() void ACloud9Character::Tick(float DeltaSeconds) { Super::Tick(DeltaSeconds); - - const auto ActorRotation = GetActorRotation(); - const auto NewRotation = FMath::Lerp(ActorRotation, TargetRotation, RotationSpeed * DeltaSeconds); - SetActorRotation(NewRotation); - - // const auto ActorRotation = GetActorRotation(); - // ActorRotation.GetNormalized() - // const auto Remain = TargetRotation - ActorRotation; - // if (Remain != FRotator::ZeroRotator) - // { - // const auto Delta = RotationSpeed * DeltaSeconds; - // - // auto DeltaRotation = FRotator{ - // FMath::Min(Delta, Remain.Pitch), - // FMath::Min(Delta, Remain.Yaw), - // FMath::Min(Delta, Remain.Roll), - // }; - // - // if (Remain.Pitch > PI) - // DeltaRotation.Pitch = -DeltaRotation.Pitch; - // - // if (Remain.Yaw > PI) - // DeltaRotation.Yaw = -DeltaRotation.Yaw; - // - // if (Remain.Roll > PI) - // DeltaRotation.Roll = -DeltaRotation.Roll; - // - // UE_LOG(LogCloud9, Display, TEXT("%s | %s"), - // *ActorRotation.ToString(), - // *DeltaRotation.ToString()); - // - // AddActorWorldRotation(DeltaRotation); - // } } diff --git a/Source/Cloud9/Character/Cloud9Character.h b/Source/Cloud9/Character/Cloud9Character.h index 89f679820..7acd4dcca 100644 --- a/Source/Cloud9/Character/Cloud9Character.h +++ b/Source/Cloud9/Character/Cloud9Character.h @@ -44,7 +44,7 @@ class ACloud9Character : public ACharacter void UnSneak() const; - void SetViewDirection(const FHitResult& HitResult, bool bIsHitValid); + void SetViewDirection(const FHitResult& HitResult, bool bIsHitValid) const; void SetCameraRotationYaw(float Angle) const; void AddCameraRotationYaw(float Angle) const; @@ -83,10 +83,7 @@ class ACloud9Character : public ACharacter UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) UDecalComponent* CursorToWorld; + /** An inventory of the character. */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Inventory, meta = (AllowPrivateAccess = "true")) UCloud9Inventory* Inventory; - - float RotationSpeed; - - FRotator TargetRotation; }; diff --git a/Source/Cloud9/Character/Components/Cloud9CharacterMovement.cpp b/Source/Cloud9/Character/Components/Cloud9CharacterMovement.cpp index 9ddc70c77..1a270bd66 100644 --- a/Source/Cloud9/Character/Components/Cloud9CharacterMovement.cpp +++ b/Source/Cloud9/Character/Components/Cloud9CharacterMovement.cpp @@ -3,44 +3,77 @@ UCloud9CharacterMovement::UCloud9CharacterMovement() { + RotationLag = 30.0f; + TargetRotator = FRotator::ZeroRotator; + MaxWalkSpeed = 500.0f; MaxSneakSpeed = 270.0f; MaxWalkSpeedCrouched = 170.0f; + auto& NavAgentProperties = GetNavAgentPropertiesRef(); NavAgentProperties.bCanCrouch = true; NavAgentProperties.bCanSwim = false; NavAgentProperties.bCanFly = false; } -ACloud9Character* UCloud9CharacterMovement::GetMyCharacterOwner() const +ACloud9Character* UCloud9CharacterMovement::GetCloud9CharacterOwner() const { - if (CharacterOwner == nullptr) - return nullptr; - - return Cast(CharacterOwner); + return IsValid(CharacterOwner) ? Cast(CharacterOwner) : nullptr; } bool UCloud9CharacterMovement::IsSneaking() const { - return CharacterOwner && GetMyCharacterOwner()->bIsSneaking; + const auto Owner = GetCloud9CharacterOwner(); + return IsValid(Owner) && Owner->bIsSneaking; } void UCloud9CharacterMovement::Sneak() const { - if (CharacterOwner) - GetMyCharacterOwner()->bIsSneaking = true; + if (const auto Owner = GetCloud9CharacterOwner(); IsValid(Owner)) + { + Owner->bIsSneaking = true; + } } void UCloud9CharacterMovement::UnSneak() const { - if (CharacterOwner) - GetMyCharacterOwner()->bIsSneaking = false; + if (const auto Owner = GetCloud9CharacterOwner(); IsValid(Owner)) + { + Owner->bIsSneaking = false; + } +} + +void UCloud9CharacterMovement::Rotate(FRotator Rotator, bool Instant) +{ + TargetRotator = Rotator; + + if (const auto Owner = GetCloud9CharacterOwner(); Instant && IsValid(Owner)) + { + Owner->SetActorRotation(TargetRotator); + } } float UCloud9CharacterMovement::GetMaxSpeed() const { const auto MaxSpeed = Super::GetMaxSpeed(); - if (IsSneaking()) - return FMath::Min(MaxSneakSpeed, MaxSpeed); - return MaxSpeed; + return IsSneaking() ? FMath::Min(MaxSneakSpeed, MaxSpeed) : MaxSpeed; +} + +void UCloud9CharacterMovement::TickComponent( + float DeltaTime, + ELevelTick TickType, + FActorComponentTickFunction* ThisTickFunction) +{ + Super::TickComponent(DeltaTime, TickType, ThisTickFunction); + + if (const auto Owner = GetCloud9CharacterOwner(); IsValid(Owner)) + { + auto NewRotation = TargetRotator; + if (RotationLag != 0.0f) + { + const auto ActorRotation = Owner->GetActorRotation(); + NewRotation = FMath::Lerp(ActorRotation, TargetRotator, DeltaTime / RotationLag * RotationLagScale); + } + Owner->SetActorRotation(NewRotation); + } } diff --git a/Source/Cloud9/Character/Components/Cloud9CharacterMovement.h b/Source/Cloud9/Character/Components/Cloud9CharacterMovement.h index 208d4245e..be352bdb1 100644 --- a/Source/Cloud9/Character/Components/Cloud9CharacterMovement.h +++ b/Source/Cloud9/Character/Components/Cloud9CharacterMovement.h @@ -11,23 +11,38 @@ class ACloud9Character; UCLASS(Blueprintable) class CLOUD9_API UCloud9CharacterMovement : public UCharacterMovementComponent - , public ICloud9CharacterComponent + , public ICloud9CharacterComponent { GENERATED_BODY() public: UCloud9CharacterMovement(); - ACloud9Character* GetMyCharacterOwner() const; + ACloud9Character* GetCloud9CharacterOwner() const; bool IsSneaking() const; - void Sneak() const; - void UnSneak() const; + void Rotate(FRotator Rotator, bool Instant = false); + UPROPERTY(Category="Character Movement: Sneaking", EditAnywhere, BlueprintReadWrite, meta=(ClampMin="0", UIMin="0")) float MaxSneakSpeed; virtual float GetMaxSpeed() const override; + + virtual void TickComponent( + float DeltaTime, + ELevelTick TickType, + FActorComponentTickFunction* ThisTickFunction + ) override; + +private: + static constexpr float RotationLagScale = 360.0f; + + /** Character rotation lag*/ + UPROPERTY(Category="Character Movement (Rotation Settings)", EditDefaultsOnly) + float RotationLag; + + FRotator TargetRotator; }; diff --git a/Source/Cloud9/Cloud9.Build.cs b/Source/Cloud9/Cloud9.Build.cs index 96c010592..52b06c1e9 100644 --- a/Source/Cloud9/Cloud9.Build.cs +++ b/Source/Cloud9/Cloud9.Build.cs @@ -8,6 +8,18 @@ public Cloud9(ReadOnlyTargetRules Target) : base(Target) { PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; - PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "NavigationSystem", "AIModule", "Niagara" }); - } -} + PublicDependencyModuleNames.AddRange(new[] + { + "Core", + "CoreUObject", + "Engine", + "InputCore", + "HeadMountedDisplay", + "NavigationSystem", + "AIModule", + "Niagara" + }); + + CppStandard = CppStandardVersion.Cpp17; + } +} \ No newline at end of file