blaster/Source/Blaster/Character/BlasterCharacter.cpp

201 lines
4.7 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "BlasterCharacter.h"
#include "Blaster/Components/CombatComponent.h"
#include "Blaster/Weapon/Weapon.h"
#include "Camera/CameraComponent.h"
#include "Components/WidgetComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Net/UnrealNetwork.h"
ABlasterCharacter::ABlasterCharacter()
{
PrimaryActorTick.bCanEverTick = true;
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(GetMesh());
CameraBoom->TargetArmLength = 600.f;
CameraBoom->bUsePawnControlRotation = true;
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
FollowCamera->bUsePawnControlRotation = false;
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
OverheadWidget = CreateDefaultSubobject<UWidgetComponent>(TEXT("OverheadWidget"));
OverheadWidget->SetupAttachment(RootComponent);
Combat = CreateDefaultSubobject<UCombatComponent>(TEXT("CombatComponent"));
Combat->SetIsReplicated(true);
GetCharacterMovement()->NavAgentProps.bCanCrouch = true;
}
void ABlasterCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME_CONDITION(ABlasterCharacter, OverlappingWeapon, COND_OwnerOnly);
}
void ABlasterCharacter::PostInitializeComponents()
{
Super::PostInitializeComponents();
if (Combat)
{
Combat->Character = this;
}
}
void ABlasterCharacter::BeginPlay()
{
Super::BeginPlay();
}
void ABlasterCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
PlayerInputComponent->BindAction("Equip", IE_Pressed, this, &ABlasterCharacter::EquipButtonPressed);
PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ABlasterCharacter::CrouchButtonPressed);
PlayerInputComponent->BindAction("Aim", IE_Pressed, this, &ABlasterCharacter::AimButtonPressed);
PlayerInputComponent->BindAction("Aim", IE_Released, this, &ABlasterCharacter::AimButtonReleased);
PlayerInputComponent->BindAxis("MoveForward", this, &ABlasterCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &ABlasterCharacter::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &ABlasterCharacter::Turn);
PlayerInputComponent->BindAxis("LookUp", this, &ABlasterCharacter::LookUp);
}
void ABlasterCharacter::MoveForward(float Value)
{
if (Controller != nullptr && Value != 0.f)
{
const FRotator YawRotation(0.f, Controller->GetControlRotation().Yaw, 0.f);
const FVector Direction(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X));
AddMovementInput(Direction, Value);
}
}
void ABlasterCharacter::MoveRight(float Value)
{
if (Controller != nullptr && Value != 0.f)
{
const FRotator YawRotation(0.f, Controller->GetControlRotation().Yaw, 0.f);
const FVector Direction(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y));
AddMovementInput(Direction, Value);
}
}
void ABlasterCharacter::Turn(float Value)
{
AddControllerYawInput(Value);
}
void ABlasterCharacter::LookUp(float Value)
{
AddControllerPitchInput(Value);
}
void ABlasterCharacter::EquipButtonPressed()
{
if (Combat)
{
if (HasAuthority())
{
Combat->EquipWeapon(OverlappingWeapon);
}
else
{
ServerEquipButtonPressed();
}
}
}
void ABlasterCharacter::CrouchButtonPressed()
{
if (bIsCrouched)
{
UnCrouch();
}
else
{
Crouch();
}
}
void ABlasterCharacter::AimButtonPressed()
{
if (Combat)
{
Combat->SetAiming(true);
}
}
void ABlasterCharacter::AimButtonReleased()
{
if (Combat)
{
Combat->SetAiming(false);
}
}
void ABlasterCharacter::ServerEquipButtonPressed_Implementation()
{
EquipButtonPressed();
}
void ABlasterCharacter::SetOverlappingWeapon(AWeapon* Weapon)
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(false);
}
OverlappingWeapon = Weapon;
if (IsLocallyControlled())
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(true);
}
}
}
bool ABlasterCharacter::IsWeaponEquipped()
{
return Combat && Combat->EquippedWeapon;
}
bool ABlasterCharacter::IsAiming()
{
return Combat && Combat->bAiming;
}
void ABlasterCharacter::OnRep_OverlappingWeapon(AWeapon* LastWeapon)
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(true);
}
if (LastWeapon)
{
LastWeapon->ShowPickupWidget(false);
}
}