Skip to content

Commit

Permalink
#30 Added template for SpringArm
Browse files Browse the repository at this point in the history
  • Loading branch information
xthebat committed Oct 30, 2023
1 parent 2c1c218 commit cc4a6e3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Source/Cloud9/Character/Cloud9Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
#include "Camera/CameraComponent.h"
#include "Cloud9/Cloud9.h"
#include "Cloud9/Game/Cloud9PlayerController.h"
#include "Cloud9/Tools/Cloud9ToolsLibrary.h"
#include "Components/DecalComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/Cloud9CharacterMovement.h"
#include "Components/Cloud9Inventory.h"
#include "Components/Cloud9SpringArmComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/PlayerController.h"
#include "GameFramework/SpringArmComponent.h"
#include "Materials/Material.h"
#include "Engine/World.h"
#include "Kismet/KismetMathLibrary.h"

class UCloud9SpringArmComponent;
const FName ACloud9Character::SpringArmComponentName = TEXT("CameraBoom");
const FName ACloud9Character::CameraComponentName = TEXT("TopDownCamera");
const FName ACloud9Character::DecalComponentName = TEXT("CursorToWorld");
Expand All @@ -37,7 +38,7 @@ ACloud9Character::ACloud9Character(const FObjectInitializer& ObjectInitializer)
Movement->bSnapToPlaneAtStart = true;
Movement->JumpZVelocity = 320.0f;

CameraBoom = CreateDefaultSubobject<USpringArmComponent>(SpringArmComponentName);
CameraBoom = CreateDefaultSubobject<UCloud9SpringArmComponent>(SpringArmComponentName);
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->SetUsingAbsoluteRotation(true); // Don't want arm to rotate when character does
CameraBoom->bDoCollisionTest = true; // Don't want to pull camera in when it collides with level
Expand Down
35 changes: 35 additions & 0 deletions Source/Cloud9/Character/Components/Cloud9SpringArmComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "Cloud9SpringArmComponent.h"


UCloud9SpringArmComponent::UCloud9SpringArmComponent()
{
RotationVerticalLagSpeed = 0.0f;
}

void UCloud9SpringArmComponent::OnRegister()
{
Super::OnRegister();
TargetRelativeRotation = RelativeSocketRotation.Rotator();
}

void UCloud9SpringArmComponent::TickComponent(
float DeltaTime,
ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

auto NewRotation = TargetRelativeRotation;
if (RotationVerticalLagSpeed != 0.0f)
{
const auto OldRotation = RelativeSocketRotation.Rotator();
NewRotation = FMath::Lerp(
OldRotation,
TargetRelativeRotation,
DeltaTime / RotationVerticalLagSpeed * RotationScale);
}

RelativeSocketRotation = NewRotation.Quaternion();

UpdateChildTransforms();
}
36 changes: 36 additions & 0 deletions Source/Cloud9/Character/Components/Cloud9SpringArmComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include "CoreMinimal.h"
#include "Cloud9CharacterComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Cloud9SpringArmComponent.generated.h"


UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class CLOUD9_API UCloud9SpringArmComponent
: public USpringArmComponent
, public ICloud9CharacterComponent
{
GENERATED_BODY()

public:
UCloud9SpringArmComponent();

virtual void TickComponent(
float DeltaTime,
ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction
) override;

virtual void OnRegister() override;

private:
static constexpr float RotationScale = 360.0f;

/** Vertical rotation speed lag for camera*/
UPROPERTY(EditDefaultsOnly, Category=Lag,
meta=(ClampMin="0.0", ClampMax="10000.0", UIMin = "0.0", UIMax = "10000.0"))
float RotationVerticalLagSpeed;

FRotator TargetRelativeRotation;
};

0 comments on commit cc4a6e3

Please sign in to comment.