Skip to content

Commit

Permalink
#33 Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
xthebat committed Nov 6, 2023
1 parent f2d38d3 commit e81d181
Show file tree
Hide file tree
Showing 37 changed files with 1,126 additions and 223 deletions.
56 changes: 39 additions & 17 deletions Source/Cloud9/Character/Cloud9Character.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include "Cloud9Character.h"

#include "DrawDebugHelpers.h"
Expand All @@ -16,7 +39,6 @@
#include "GameFramework/SpringArmComponent.h"
#include "Materials/Material.h"
#include "Engine/World.h"
#include "GameFramework/GameUserSettings.h"
#include "Kismet/KismetMathLibrary.h"

class UCloud9SpringArmComponent;
Expand All @@ -34,7 +56,7 @@ ACloud9Character::ACloud9Character(const FObjectInitializer& ObjectInitializer)

GetCapsuleComponent()->InitCapsuleSize(32.f, 72.0f);

const auto Movement = GetCharacterMovement();
let Movement = GetCharacterMovement();
Movement->bOrientRotationToMovement = false;
Movement->bConstrainToPlane = true;
Movement->bSnapToPlaneAtStart = true;
Expand Down Expand Up @@ -62,7 +84,7 @@ ACloud9Character::ACloud9Character(const FObjectInitializer& ObjectInitializer)

UCloud9CharacterMovement* ACloud9Character::GetCloud9CharacterMovement() const
{
const auto Movement = GetCharacterMovement();
let Movement = GetCharacterMovement();
return IsValid(Movement) ? Cast<UCloud9CharacterMovement>(Movement) : nullptr;
}

Expand All @@ -73,7 +95,7 @@ ACloud9PlayerController* ACloud9Character::GetCloud9Controller() const

bool ACloud9Character::CanSneak() const
{
if (const auto Movement = GetCloud9CharacterMovement(); IsValid(Movement))
if (let Movement = GetCloud9CharacterMovement(); IsValid(Movement))
{
return !Movement->IsCrouching();
}
Expand All @@ -83,15 +105,15 @@ bool ACloud9Character::CanSneak() const

void ACloud9Character::Sneak() const
{
if (const auto Movement = GetCloud9CharacterMovement(); IsValid(Movement))
if (let Movement = GetCloud9CharacterMovement(); IsValid(Movement))
{
Movement->Sneak();
}
}

void ACloud9Character::UnSneak() const
{
if (const auto Movement = GetCloud9CharacterMovement(); IsValid(Movement))
if (let Movement = GetCloud9CharacterMovement(); IsValid(Movement))
{
Movement->UnSneak();
}
Expand All @@ -101,15 +123,15 @@ void ACloud9Character::SetViewDirection(const FHitResult& HitResult, bool bIsHit
{
if (IsValid(CursorToWorld))
{
const auto ImpactNormal = HitResult.ImpactNormal;
const auto ImpactRotation = ImpactNormal.Rotation();
let ImpactNormal = HitResult.ImpactNormal;
let ImpactRotation = ImpactNormal.Rotation();

CursorToWorld->SetWorldLocation(HitResult.Location);
CursorToWorld->SetWorldRotation(ImpactRotation);
SetCursorIsHidden(false);
}

const auto Settings = UCloud9DeveloperSettings::GetCloud9DeveloperSettings();
let Settings = UCloud9DeveloperSettings::GetCloud9DeveloperSettings();

if (Settings->bIsDrawHitCursorLine)
{
Expand Down Expand Up @@ -146,9 +168,9 @@ void ACloud9Character::SetViewDirection(const FHitResult& HitResult, bool bIsHit

if (bIsHitValid)
{
const auto TargetLocation = FVector{HitResult.Location.X, HitResult.Location.Y, 0.0f};
const auto ActorLocation = GetActorLocation();
const auto LookRotation = UKismetMathLibrary::FindLookAtRotation(ActorLocation, TargetLocation);
let TargetLocation = FVector{HitResult.Location.X, HitResult.Location.Y, 0.0f};
let ActorLocation = GetActorLocation();
let LookRotation = UKismetMathLibrary::FindLookAtRotation(ActorLocation, TargetLocation);
GetCloud9CharacterMovement()->Rotate({0.0f, LookRotation.Yaw, 0.0f});
}
}
Expand All @@ -169,13 +191,13 @@ void ACloud9Character::AddCameraRotationYaw(float Angle) const

float ACloud9Character::GetCameraRotationRoll() const
{
const auto Rotation = CameraBoom->GetRelativeRotation();
let Rotation = CameraBoom->GetRelativeRotation();
return -Rotation.Pitch;
}

void ACloud9Character::SetCameraRotationRoll(float Angle) const
{
auto Rotation = CameraBoom->GetRelativeRotation();
var Rotation = CameraBoom->GetRelativeRotation();
Rotation.Pitch = -Angle;
UE_LOG(LogCloud9, Display, TEXT("SetRelativeRotation Yaw: %s"), *Rotation.ToString());
CameraBoom->SetRelativeRotation(Rotation);
Expand Down Expand Up @@ -204,7 +226,7 @@ void ACloud9Character::OnConstruction(const FTransform& Transform)

UE_LOG(LogCloud9, Display, TEXT("Contruction transform %s"), *Transform.ToString());

const auto Rotator = Transform.Rotator();
let Rotator = Transform.Rotator();

SetCameraRotationYaw(Rotator.Yaw);
GetCloud9CharacterMovement()->Rotate({0.0f, Rotator.Yaw, 0.0f}, true);
Expand All @@ -218,9 +240,9 @@ void ACloud9Character::OnConstruction(const FTransform& Transform)
CursorToWorld->DecalSize = FVector(16.0f, 32.0f, 32.0f);
}

if (const auto MyMesh = GetMesh(); IsValid(MyMesh) && !CameraTargetBoneName.IsNone())
if (let MyMesh = GetMesh(); IsValid(MyMesh) && !CameraTargetBoneName.IsNone())
{
const auto HeadBoneLocation = MyMesh->GetBoneLocation(CameraTargetBoneName, EBoneSpaces::WorldSpace);
let HeadBoneLocation = MyMesh->GetBoneLocation(CameraTargetBoneName, EBoneSpaces::WorldSpace);
UE_LOG(LogCloud9, Display, TEXT("Setup CameraBoom = %s"), *HeadBoneLocation.ToString());
CameraBoom->SetWorldLocation(HeadBoneLocation);
}
Expand Down
23 changes: 23 additions & 0 deletions Source/Cloud9/Character/Cloud9Character.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#pragma once

#include "CoreMinimal.h"
Expand Down
34 changes: 31 additions & 3 deletions Source/Cloud9/Character/Components/Cloud9CharacterComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
#include "Cloud9CharacterComponent.h"
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include "Cloud9CharacterComponent.h"

#include "Cloud9/Cloud9.h"
#include "Cloud9/Character/Cloud9Character.h"
#include "Cloud9/Game/Cloud9PlayerController.h"


ACloud9Character* ICloud9CharacterComponent::GetPawn() const
{
if (const auto Component = Cast<UActorComponent>(this))
if (let Component = Cast<UActorComponent>(this); IsValid(Component))
{
return Component->GetOwner<ACloud9Character>();
}

return nullptr;
}

ACloud9PlayerController* ICloud9CharacterComponent::GetPlayerController() const
{
if (const auto Pawn = GetPawn())
if (let Pawn = GetPawn(); IsValid(Pawn))
{
return Cast<ACloud9PlayerController>(Pawn->GetController());
}

return nullptr;
}
25 changes: 24 additions & 1 deletion Source/Cloud9/Character/Components/Cloud9CharacterComponent.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
#pragma once
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#pragma once

#include "CoreMinimal.h"
#include "UObject/Interface.h"
Expand Down
45 changes: 35 additions & 10 deletions Source/Cloud9/Character/Components/Cloud9CharacterMovement.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
#include "Cloud9CharacterMovement.h"
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include "Cloud9CharacterMovement.h"

#include "Cloud9/Cloud9.h"
#include "Cloud9/Character/Cloud9Character.h"

UCloud9CharacterMovement::UCloud9CharacterMovement()
Expand All @@ -10,7 +35,7 @@ UCloud9CharacterMovement::UCloud9CharacterMovement()
MaxSneakSpeed = 270.0f;
MaxWalkSpeedCrouched = 170.0f;

auto& NavAgentProperties = GetNavAgentPropertiesRef();
var& NavAgentProperties = GetNavAgentPropertiesRef();
NavAgentProperties.bCanCrouch = true;
NavAgentProperties.bCanSwim = false;
NavAgentProperties.bCanFly = false;
Expand All @@ -23,21 +48,21 @@ ACloud9Character* UCloud9CharacterMovement::GetCloud9CharacterOwner() const

bool UCloud9CharacterMovement::IsSneaking() const
{
const auto Owner = GetCloud9CharacterOwner();
let Owner = GetCloud9CharacterOwner();
return IsValid(Owner) && Owner->bIsSneaking;
}

void UCloud9CharacterMovement::Sneak() const
{
if (const auto Owner = GetCloud9CharacterOwner(); IsValid(Owner))
if (let Owner = GetCloud9CharacterOwner(); IsValid(Owner))
{
Owner->bIsSneaking = true;
}
}

void UCloud9CharacterMovement::UnSneak() const
{
if (const auto Owner = GetCloud9CharacterOwner(); IsValid(Owner))
if (let Owner = GetCloud9CharacterOwner(); IsValid(Owner))
{
Owner->bIsSneaking = false;
}
Expand All @@ -47,15 +72,15 @@ void UCloud9CharacterMovement::Rotate(FRotator Rotator, bool Instant)
{
TargetRotator = Rotator;

if (const auto Owner = GetCloud9CharacterOwner(); Instant && IsValid(Owner))
if (let Owner = GetCloud9CharacterOwner(); Instant && IsValid(Owner))
{
Owner->SetActorRotation(TargetRotator);
}
}

float UCloud9CharacterMovement::GetMaxSpeed() const
{
const auto MaxSpeed = Super::GetMaxSpeed();
let MaxSpeed = Super::GetMaxSpeed();
return IsSneaking() ? FMath::Min(MaxSneakSpeed, MaxSpeed) : MaxSpeed;
}

Expand All @@ -66,12 +91,12 @@ void UCloud9CharacterMovement::TickComponent(
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

if (const auto Owner = GetCloud9CharacterOwner(); IsValid(Owner))
if (let Owner = GetCloud9CharacterOwner(); IsValid(Owner))
{
auto NewRotation = TargetRotator;
var NewRotation = TargetRotator;
if (RotationLag != 0.0f)
{
const auto ActorRotation = Owner->GetActorRotation();
let ActorRotation = Owner->GetActorRotation();
NewRotation = FMath::Lerp(ActorRotation, TargetRotator, DeltaTime / RotationLag * RotationLagScale);
}
Owner->SetActorRotation(NewRotation);
Expand Down
25 changes: 24 additions & 1 deletion Source/Cloud9/Character/Components/Cloud9CharacterMovement.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
#pragma once
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#pragma once

#include "CoreMinimal.h"
#include "Cloud9CharacterComponent.h"
Expand Down
Loading

0 comments on commit e81d181

Please sign in to comment.