207 lines
7.1 KiB
C++
207 lines
7.1 KiB
C++
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
#include "LuckyrobotsPawn.h"
|
|
#include "LuckyrobotsWheelFront.h"
|
|
#include "LuckyrobotsWheelRear.h"
|
|
#include "Components/SkeletalMeshComponent.h"
|
|
#include "GameFramework/SpringArmComponent.h"
|
|
#include "Camera/CameraComponent.h"
|
|
#include "EnhancedInputComponent.h"
|
|
#include "EnhancedInputSubsystems.h"
|
|
#include "InputActionValue.h"
|
|
#include "ChaosWheeledVehicleMovementComponent.h"
|
|
|
|
#define LOCTEXT_NAMESPACE "VehiclePawn"
|
|
|
|
DEFINE_LOG_CATEGORY(LogTemplateVehicle);
|
|
|
|
ALuckyrobotsPawn::ALuckyrobotsPawn()
|
|
{
|
|
// construct the front camera boom
|
|
FrontSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Front Spring Arm"));
|
|
FrontSpringArm->SetupAttachment(GetMesh());
|
|
FrontSpringArm->TargetArmLength = 0.0f;
|
|
FrontSpringArm->bDoCollisionTest = false;
|
|
FrontSpringArm->bEnableCameraRotationLag = true;
|
|
FrontSpringArm->CameraRotationLagSpeed = 15.0f;
|
|
FrontSpringArm->SetRelativeLocation(FVector(30.0f, 0.0f, 120.0f));
|
|
|
|
FrontCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("Front Camera"));
|
|
FrontCamera->SetupAttachment(FrontSpringArm);
|
|
FrontCamera->bAutoActivate = false;
|
|
|
|
// construct the back camera boom
|
|
BackSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Back Spring Arm"));
|
|
BackSpringArm->SetupAttachment(GetMesh());
|
|
BackSpringArm->TargetArmLength = 650.0f;
|
|
BackSpringArm->SocketOffset.Z = 150.0f;
|
|
BackSpringArm->bDoCollisionTest = false;
|
|
BackSpringArm->bInheritPitch = false;
|
|
BackSpringArm->bInheritRoll = false;
|
|
BackSpringArm->bEnableCameraRotationLag = true;
|
|
BackSpringArm->CameraRotationLagSpeed = 2.0f;
|
|
BackSpringArm->CameraLagMaxDistance = 50.0f;
|
|
|
|
BackCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("Back Camera"));
|
|
BackCamera->SetupAttachment(BackSpringArm);
|
|
|
|
// Configure the car mesh
|
|
GetMesh()->SetSimulatePhysics(true);
|
|
GetMesh()->SetCollisionProfileName(FName("Vehicle"));
|
|
|
|
// get the Chaos Wheeled movement component
|
|
ChaosVehicleMovement = CastChecked<UChaosWheeledVehicleMovementComponent>(GetVehicleMovement());
|
|
|
|
}
|
|
|
|
void ALuckyrobotsPawn::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
|
|
{
|
|
Super::SetupPlayerInputComponent(PlayerInputComponent);
|
|
|
|
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent))
|
|
{
|
|
// steering
|
|
EnhancedInputComponent->BindAction(SteeringAction, ETriggerEvent::Triggered, this, &ALuckyrobotsPawn::Steering);
|
|
EnhancedInputComponent->BindAction(SteeringAction, ETriggerEvent::Completed, this, &ALuckyrobotsPawn::Steering);
|
|
|
|
// throttle
|
|
EnhancedInputComponent->BindAction(ThrottleAction, ETriggerEvent::Triggered, this, &ALuckyrobotsPawn::Throttle);
|
|
EnhancedInputComponent->BindAction(ThrottleAction, ETriggerEvent::Completed, this, &ALuckyrobotsPawn::Throttle);
|
|
|
|
// break
|
|
EnhancedInputComponent->BindAction(BrakeAction, ETriggerEvent::Triggered, this, &ALuckyrobotsPawn::Brake);
|
|
EnhancedInputComponent->BindAction(BrakeAction, ETriggerEvent::Started, this, &ALuckyrobotsPawn::StartBrake);
|
|
EnhancedInputComponent->BindAction(BrakeAction, ETriggerEvent::Completed, this, &ALuckyrobotsPawn::StopBrake);
|
|
|
|
// handbrake
|
|
EnhancedInputComponent->BindAction(HandbrakeAction, ETriggerEvent::Started, this, &ALuckyrobotsPawn::StartHandbrake);
|
|
EnhancedInputComponent->BindAction(HandbrakeAction, ETriggerEvent::Completed, this, &ALuckyrobotsPawn::StopHandbrake);
|
|
|
|
// look around
|
|
EnhancedInputComponent->BindAction(LookAroundAction, ETriggerEvent::Triggered, this, &ALuckyrobotsPawn::LookAround);
|
|
|
|
// toggle camera
|
|
EnhancedInputComponent->BindAction(ToggleCameraAction, ETriggerEvent::Triggered, this, &ALuckyrobotsPawn::ToggleCamera);
|
|
|
|
// reset the vehicle
|
|
EnhancedInputComponent->BindAction(ResetVehicleAction, ETriggerEvent::Triggered, this, &ALuckyrobotsPawn::ResetVehicle);
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogTemplateVehicle, Error, TEXT("'%s' Failed to find an Enhanced Input component! This template is built to use the Enhanced Input system. If you intend to use the legacy system, then you will need to update this C++ file."), *GetNameSafe(this));
|
|
}
|
|
}
|
|
|
|
void ALuckyrobotsPawn::Tick(float Delta)
|
|
{
|
|
Super::Tick(Delta);
|
|
|
|
// add some angular damping if the vehicle is in midair
|
|
bool bMovingOnGround = ChaosVehicleMovement->IsMovingOnGround();
|
|
GetMesh()->SetAngularDamping(bMovingOnGround ? 0.0f : 3.0f);
|
|
|
|
// realign the camera yaw to face front
|
|
float CameraYaw = BackSpringArm->GetRelativeRotation().Yaw;
|
|
CameraYaw = FMath::FInterpTo(CameraYaw, 0.0f, Delta, 1.0f);
|
|
|
|
BackSpringArm->SetRelativeRotation(FRotator(0.0f, CameraYaw, 0.0f));
|
|
}
|
|
|
|
void ALuckyrobotsPawn::Steering(const FInputActionValue& Value)
|
|
{
|
|
// get the input magnitude for steering
|
|
float SteeringValue = Value.Get<float>();
|
|
|
|
// add the input
|
|
ChaosVehicleMovement->SetSteeringInput(SteeringValue);
|
|
}
|
|
|
|
void ALuckyrobotsPawn::Throttle(const FInputActionValue& Value)
|
|
{
|
|
// get the input magnitude for the throttle
|
|
float ThrottleValue = Value.Get<float>();
|
|
|
|
// add the input
|
|
ChaosVehicleMovement->SetThrottleInput(ThrottleValue);
|
|
}
|
|
|
|
void ALuckyrobotsPawn::Brake(const FInputActionValue& Value)
|
|
{
|
|
// get the input magnitude for the brakes
|
|
float BreakValue = Value.Get<float>();
|
|
|
|
// add the input
|
|
ChaosVehicleMovement->SetBrakeInput(BreakValue);
|
|
}
|
|
|
|
void ALuckyrobotsPawn::StartBrake(const FInputActionValue& Value)
|
|
{
|
|
// call the Blueprint hook for the break lights
|
|
BrakeLights(true);
|
|
}
|
|
|
|
void ALuckyrobotsPawn::StopBrake(const FInputActionValue& Value)
|
|
{
|
|
// call the Blueprint hook for the break lights
|
|
BrakeLights(false);
|
|
|
|
// reset brake input to zero
|
|
ChaosVehicleMovement->SetBrakeInput(0.0f);
|
|
}
|
|
|
|
void ALuckyrobotsPawn::StartHandbrake(const FInputActionValue& Value)
|
|
{
|
|
// add the input
|
|
ChaosVehicleMovement->SetHandbrakeInput(true);
|
|
|
|
// call the Blueprint hook for the break lights
|
|
BrakeLights(true);
|
|
}
|
|
|
|
void ALuckyrobotsPawn::StopHandbrake(const FInputActionValue& Value)
|
|
{
|
|
// add the input
|
|
ChaosVehicleMovement->SetHandbrakeInput(false);
|
|
|
|
// call the Blueprint hook for the break lights
|
|
BrakeLights(false);
|
|
}
|
|
|
|
void ALuckyrobotsPawn::LookAround(const FInputActionValue& Value)
|
|
{
|
|
// get the flat angle value for the input
|
|
float LookValue = Value.Get<float>();
|
|
|
|
// add the input
|
|
BackSpringArm->AddLocalRotation(FRotator(0.0f, LookValue, 0.0f));
|
|
}
|
|
|
|
void ALuckyrobotsPawn::ToggleCamera(const FInputActionValue& Value)
|
|
{
|
|
// toggle the active camera flag
|
|
bFrontCameraActive = !bFrontCameraActive;
|
|
|
|
FrontCamera->SetActive(bFrontCameraActive);
|
|
BackCamera->SetActive(!bFrontCameraActive);
|
|
}
|
|
|
|
void ALuckyrobotsPawn::ResetVehicle(const FInputActionValue& Value)
|
|
{
|
|
// reset to a location slightly above our current one
|
|
FVector ResetLocation = GetActorLocation() + FVector(0.0f, 0.0f, 50.0f);
|
|
|
|
// reset to our yaw. Ignore pitch and roll
|
|
FRotator ResetRotation = GetActorRotation();
|
|
ResetRotation.Pitch = 0.0f;
|
|
ResetRotation.Roll = 0.0f;
|
|
|
|
// teleport the actor to the reset spot and reset physics
|
|
SetActorTransform(FTransform(ResetRotation, ResetLocation, FVector::OneVector), false, nullptr, ETeleportType::TeleportPhysics);
|
|
|
|
GetMesh()->SetPhysicsAngularVelocityInDegrees(FVector::ZeroVector);
|
|
GetMesh()->SetPhysicsLinearVelocity(FVector::ZeroVector);
|
|
|
|
UE_LOG(LogTemplateVehicle, Error, TEXT("Reset Vehicle"));
|
|
}
|
|
|
|
#undef LOCTEXT_NAMESPACE |