blaster/Source/Blaster/Character/BlasterCharacter.cpp

204 lines
4.9 KiB
C++
Raw Normal View History

2022-04-28 10:18:21 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "BlasterCharacter.h"
2022-04-30 11:09:28 +00:00
#include "Blaster/Components/CombatComponent.h"
2022-04-30 10:26:25 +00:00
#include "Blaster/Weapon/Weapon.h"
2022-04-28 10:41:43 +00:00
#include "Camera/CameraComponent.h"
2022-05-03 17:50:47 +00:00
#include "Components/CapsuleComponent.h"
2022-04-29 21:38:34 +00:00
#include "Components/WidgetComponent.h"
2022-04-28 13:30:18 +00:00
#include "GameFramework/CharacterMovementComponent.h"
2022-04-28 10:41:43 +00:00
#include "GameFramework/SpringArmComponent.h"
2022-04-30 10:26:25 +00:00
#include "Net/UnrealNetwork.h"
2022-04-28 10:41:43 +00:00
2022-04-28 10:18:21 +00:00
ABlasterCharacter::ABlasterCharacter()
{
PrimaryActorTick.bCanEverTick = true;
2022-04-28 10:41:43 +00:00
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(GetMesh());
CameraBoom->TargetArmLength = 600.f;
2022-04-28 12:39:36 +00:00
CameraBoom->bUsePawnControlRotation = true;
2022-04-28 10:41:43 +00:00
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
FollowCamera->bUsePawnControlRotation = false;
2022-04-28 13:30:18 +00:00
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
2022-04-29 21:38:34 +00:00
OverheadWidget = CreateDefaultSubobject<UWidgetComponent>(TEXT("OverheadWidget"));
OverheadWidget->SetupAttachment(RootComponent);
2022-04-30 11:09:28 +00:00
Combat = CreateDefaultSubobject<UCombatComponent>(TEXT("CombatComponent"));
Combat->SetIsReplicated(true);
2022-04-30 15:10:07 +00:00
GetCharacterMovement()->NavAgentProps.bCanCrouch = true;
2022-05-03 17:50:47 +00:00
GetCapsuleComponent()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
2022-04-28 10:18:21 +00:00
}
2022-04-30 10:26:25 +00:00
void ABlasterCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME_CONDITION(ABlasterCharacter, OverlappingWeapon, COND_OwnerOnly);
}
2022-04-30 11:09:28 +00:00
void ABlasterCharacter::PostInitializeComponents()
{
Super::PostInitializeComponents();
if (Combat)
{
Combat->Character = this;
}
}
2022-04-28 10:18:21 +00:00
void ABlasterCharacter::BeginPlay()
{
Super::BeginPlay();
}
void ABlasterCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
2022-04-28 12:39:36 +00:00
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
2022-04-30 11:09:28 +00:00
PlayerInputComponent->BindAction("Equip", IE_Pressed, this, &ABlasterCharacter::EquipButtonPressed);
2022-04-30 15:10:07 +00:00
PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ABlasterCharacter::CrouchButtonPressed);
2022-04-30 15:38:03 +00:00
PlayerInputComponent->BindAction("Aim", IE_Pressed, this, &ABlasterCharacter::AimButtonPressed);
PlayerInputComponent->BindAction("Aim", IE_Released, this, &ABlasterCharacter::AimButtonReleased);
2022-04-30 14:08:00 +00:00
2022-04-28 12:39:36 +00:00
PlayerInputComponent->BindAxis("MoveForward", this, &ABlasterCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &ABlasterCharacter::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &ABlasterCharacter::Turn);
PlayerInputComponent->BindAxis("LookUp", this, &ABlasterCharacter::LookUp);
2022-04-28 10:18:21 +00:00
}
2022-04-28 12:39:36 +00:00
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);
}
2022-04-30 10:26:25 +00:00
2022-04-30 11:09:28 +00:00
void ABlasterCharacter::EquipButtonPressed()
{
2022-04-30 14:08:00 +00:00
if (Combat)
2022-04-30 11:09:28 +00:00
{
2022-04-30 14:08:00 +00:00
if (HasAuthority())
{
Combat->EquipWeapon(OverlappingWeapon);
}
else
{
ServerEquipButtonPressed();
}
2022-04-30 11:09:28 +00:00
}
}
2022-04-30 15:10:07 +00:00
void ABlasterCharacter::CrouchButtonPressed()
{
if (bIsCrouched)
{
UnCrouch();
}
else
{
Crouch();
}
}
2022-04-30 15:38:03 +00:00
void ABlasterCharacter::AimButtonPressed()
{
if (Combat)
{
Combat->SetAiming(true);
}
}
void ABlasterCharacter::AimButtonReleased()
{
if (Combat)
{
Combat->SetAiming(false);
}
}
2022-04-30 14:08:00 +00:00
void ABlasterCharacter::ServerEquipButtonPressed_Implementation()
{
EquipButtonPressed();
}
2022-04-30 10:26:25 +00:00
void ABlasterCharacter::SetOverlappingWeapon(AWeapon* Weapon)
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(false);
}
2022-04-30 11:09:28 +00:00
2022-04-30 10:26:25 +00:00
OverlappingWeapon = Weapon;
if (IsLocallyControlled())
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(true);
}
}
}
2022-04-30 14:48:34 +00:00
bool ABlasterCharacter::IsWeaponEquipped()
{
return Combat && Combat->EquippedWeapon;
}
2022-04-30 15:38:03 +00:00
bool ABlasterCharacter::IsAiming()
{
return Combat && Combat->bAiming;
}
2022-04-30 10:26:25 +00:00
void ABlasterCharacter::OnRep_OverlappingWeapon(AWeapon* LastWeapon)
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(true);
}
if (LastWeapon)
{
LastWeapon->ShowPickupWidget(false);
}
}