146 lines
4.6 KiB
C++
146 lines
4.6 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "ShooterCharacter.h"
|
|
|
|
#include "Camera/CameraComponent.h"
|
|
#include "Engine/SkeletalMeshSocket.h"
|
|
#include "GameFramework/SpringArmComponent.h"
|
|
#include "GameFramework/CharacterMovementComponent.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
#include "Sound/SoundCue.h"
|
|
|
|
// Sets default values
|
|
AShooterCharacter::AShooterCharacter() :
|
|
BaseTurnRate(45.f),
|
|
BaseLookUpRate(45.f)
|
|
{
|
|
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
|
|
PrimaryActorTick.bCanEverTick = true;
|
|
|
|
// Create a camera boom (pulls in towards the character if there is a collision)
|
|
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
|
|
CameraBoom->SetupAttachment(RootComponent);
|
|
|
|
// The camera follows at this distance behind the character
|
|
CameraBoom->TargetArmLength = 300.f;
|
|
|
|
// Rotate the arm based on the controller
|
|
CameraBoom->bUsePawnControlRotation = true;
|
|
|
|
// Create a follow camera
|
|
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
|
|
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
|
|
|
|
// Camera does not rotate relative to arm
|
|
FollowCamera->bUsePawnControlRotation = false;
|
|
|
|
// Don't rotate when the controller does. Let the controller only affect the camera.
|
|
bUseControllerRotationPitch = false;
|
|
bUseControllerRotationYaw = false;
|
|
bUseControllerRotationRoll = false;
|
|
|
|
GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input....
|
|
GetCharacterMovement()->RotationRate = FRotator(0.f, 540.f, 0.f); // ... at this rotation rate
|
|
GetCharacterMovement()->JumpZVelocity = 600.f;
|
|
GetCharacterMovement()->AirControl = 0.2f;
|
|
}
|
|
|
|
// Called when the game starts or when spawned
|
|
void AShooterCharacter::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
|
|
}
|
|
|
|
void AShooterCharacter::MoveForward(float Value)
|
|
{
|
|
if (Controller && (Value != 0))
|
|
{
|
|
// Find out which way is forward
|
|
const FRotator Rotation{ Controller->GetControlRotation() };
|
|
const FRotator YawRotation{ 0, Rotation.Yaw, 0 };
|
|
|
|
const FVector Direction{ FRotationMatrix{YawRotation}.GetUnitAxis(EAxis::X) };
|
|
AddMovementInput(Direction, Value);
|
|
}
|
|
}
|
|
|
|
void AShooterCharacter::MoveRight(float Value)
|
|
{
|
|
if (Controller && (Value != 0))
|
|
{
|
|
// Find out which way is forward
|
|
const FRotator Rotation{ Controller->GetControlRotation() };
|
|
const FRotator YawRotation{ 0, Rotation.Yaw, 0 };
|
|
|
|
const FVector Direction{ FRotationMatrix{YawRotation}.GetUnitAxis(EAxis::Y) };
|
|
AddMovementInput(Direction, Value);
|
|
}
|
|
}
|
|
|
|
void AShooterCharacter::TurnAtRate(float Rate)
|
|
{
|
|
// Calculate delta for this frame from the rate information
|
|
AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds()); // def/sec * sec/frame
|
|
}
|
|
|
|
void AShooterCharacter::LookUpAtRate(float Rate)
|
|
{
|
|
AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds()); // def/sec * sec/frame
|
|
}
|
|
|
|
void AShooterCharacter::FireWeapon()
|
|
{
|
|
if (FireSound)
|
|
{
|
|
UGameplayStatics::PlaySound2D(this, FireSound);
|
|
}
|
|
|
|
const USkeletalMeshSocket* BarrelSocket = GetMesh()->GetSocketByName("BarrelSocket");
|
|
if (BarrelSocket)
|
|
{
|
|
const FTransform SocketTransform = BarrelSocket->GetSocketTransform(GetMesh());
|
|
|
|
if (MuzzleFlash)
|
|
{
|
|
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), MuzzleFlash, SocketTransform);
|
|
}
|
|
}
|
|
|
|
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
|
|
|
|
if (AnimInstance && HipFireMontage)
|
|
{
|
|
AnimInstance->Montage_Play(HipFireMontage);
|
|
AnimInstance->Montage_JumpToSection(FName("StartFire"));
|
|
}
|
|
}
|
|
|
|
// Called every frame
|
|
void AShooterCharacter::Tick(float DeltaTime)
|
|
{
|
|
Super::Tick(DeltaTime);
|
|
|
|
}
|
|
|
|
// Called to bind functionality to input
|
|
void AShooterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
|
|
{
|
|
Super::SetupPlayerInputComponent(PlayerInputComponent);
|
|
check(PlayerInputComponent);
|
|
|
|
PlayerInputComponent->BindAxis("MoveForward", this, &AShooterCharacter::MoveForward);
|
|
PlayerInputComponent->BindAxis("MoveRight", this, &AShooterCharacter::MoveRight);
|
|
PlayerInputComponent->BindAxis("TurnRate", this, &AShooterCharacter::TurnAtRate);
|
|
PlayerInputComponent->BindAxis("LookUpRate", this, &AShooterCharacter::LookUpAtRate);
|
|
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
|
|
PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
|
|
|
|
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
|
|
PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
|
|
|
|
PlayerInputComponent->BindAction("FireButton", IE_Pressed, this, &AShooterCharacter::FireWeapon);
|
|
}
|
|
|