GASATHON/Project/Source/Gasa/Characters/GasaCharacter.cpp

233 lines
6.2 KiB
C++
Raw Normal View History

2024-04-12 11:49:22 -07:00
#include "GasaCharacter.h"
2024-04-13 08:09:22 -07:00
#include "AbilitySystemComponent.h"
2024-04-12 16:55:34 -07:00
#include "Camera/CameraComponent.h"
2024-04-12 19:05:09 -07:00
#include "Components/CapsuleComponent.h"
2024-04-12 16:55:34 -07:00
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
2024-04-12 19:05:09 -07:00
#include "Kismet/KismetSystemLibrary.h"
2024-04-12 16:55:34 -07:00
2024-04-13 08:09:22 -07:00
#include "AbilitySystem/GasaAbilitySystemComponent.h"
#include "AbilitySystem/GasaAttributeSet.h"
#include "Components/SkeletalMeshComponent.h"
#include "Engine/PostProcessVolume.h"
#include "Game/GasaGameInstance.h"
2024-04-13 08:09:22 -07:00
#include "Game/GasaLevelScriptActor.h"
#include "Game/GasaPlayerController.h"
#include "Materials/MaterialInstanceDynamic.h"
#include "Networking/GasaNetLibrary_Inlines.h"
using namespace Gasa;
2024-04-12 11:49:22 -07:00
AGasaCharacter::AGasaCharacter()
{
PrimaryActorTick.bCanEverTick = false;
2024-04-12 19:37:31 -07:00
HighlightColor = FLinearColor(0.8, 0.32, 0.05f, 1.f);
2024-04-12 16:55:34 -07:00
UCharacterMovementComponent*
Movement = GetCharacterMovement();
Movement->bOrientRotationToMovement = true;
Movement->bConstrainToPlane = true;
Movement->bSnapToPlaneAtStart = true;
Movement->RotationRate = FRotator(0.0, 400.f, 0.0);
bUseControllerRotationPitch = false;
bUseControllerRotationRoll = false;
bUseControllerRotationYaw = false;
USceneComponent* root_component = GetRootComponent();
USkeletalMeshComponent* mesh = GetMesh();
2024-04-12 11:49:22 -07:00
Weapon = CreateDefaultSubobject<USkeletalMeshComponent>("Weapon");
2024-04-12 16:55:34 -07:00
Weapon->SetupAttachment(mesh, FName("WeaponAttach"));
2024-04-12 11:49:22 -07:00
Weapon->SetCollisionEnabled(ECollisionEnabled::NoCollision);
2024-04-13 08:09:22 -07:00
if (bAutoAbilitySystem)
{
AbilitySystem = CreateDefaultSubobject<UGasaAbilitySystemComp>("Ability System");
AbilitySystem->SetIsReplicated(true);
2024-04-13 08:56:19 -07:00
AbilitySystem->SetReplicationMode(EGameplayEffectReplicationMode::Minimal);
2024-04-13 08:09:22 -07:00
Attributes = CreateDefaultSubobject<UGasaAttributeSet>("Attributes");
2024-04-13 08:09:22 -07:00
}
// Replication
bReplicates = false;
bNetLoadOnClient = true;
NetDormancy = DORM_Awake;
NetCullDistanceSquared = NetCullDist_Medium;
NetUpdateFrequency = 30.0f;
MinNetUpdateFrequency = 5.0f;
NetPriority = 2.0f;
ACharacter::SetReplicateMovement(true);
}
#pragma region GameFramework
void AGasaCharacter::Controller_OnPawnPossessed()
{
NetLog("Controller confirmed possession.");
// Do stuff here that you needed to wait for the player controller be aware of you for.
BP_Controller_OnPawnPossessed();
if (Event_OnPawnReady.IsBound())
Event_OnPawnReady.Broadcast();
}
void AGasaCharacter::ServerRPC_R_NotifyClientPawnReady_Implementation()
{
Event_OnPawnReady.Broadcast();
}
#pragma endregion GameFramework
#pragma region Highlight
void AGasaCharacter::SetHighlight(EHighlight Desired)
{
HighlightState = Desired;
2024-04-12 11:49:22 -07:00
}
#pragma endregion Highlight
2024-04-12 11:49:22 -07:00
2024-04-13 08:56:19 -07:00
#pragma region Pawn
void AGasaCharacter::OnRep_PlayerState()
{
Super::OnRep_PlayerState();
}
2024-04-13 08:56:19 -07:00
void AGasaCharacter::PossessedBy(AController* NewController)
{
NetLog("Pawn possessed.");
2024-04-13 08:56:19 -07:00
AController* OldController;
// APawn::PossessedBy
{
SetOwner(NewController);
OldController = Controller;
Controller = NewController;
ForceNetUpdate();
#if UE_WITH_IRIS
// The owning connection depends on the Controller having the new value.
UpdateOwningNetConnection();
#endif
if (Controller->PlayerState != nullptr)
SetPlayerState(Controller->PlayerState);
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
if (GetNetMode() != NM_Standalone)
{
SetReplicates(true);
SetAutonomousProxy(true);
}
}
else
CopyRemoteRoleFrom(GetDefault<APawn>());
}
if (AGasaPlayerController* PC = Cast<AGasaPlayerController>(NewController))
{
PC->Event_OnPawnPossessed.AddUniqueDynamic(this, & ThisClass::Controller_OnPawnPossessed);
}
else
{
NetLog("Controller assigned to GasaCharacter is not derived from GasaPlayerController.", ELogV::Warning);
NetLog("Controller: Name: " + NewController->GetName() + " Class: " + NewController->GetClass()->GetName(), ELogV::Warning);
}
// cont. APawn::PossessedBy
{
// Dispatch Blueprint event if necessary
if (OldController != NewController)
{
ReceivePossessed(Controller);
NotifyControllerChanged();
}
}
// ACharacter::PossessedBy
{
// If we are controlled remotely, set animation timing to be driven by client's network updates. So timing and events remain in sync.
if (GetMesh() && IsReplicatingMovement() && (GetRemoteRole() == ROLE_AutonomousProxy && GetNetConnection() != nullptr))
GetMesh()->bOnlyAllowAutonomousTickPose = true;
}
2024-04-13 08:56:19 -07:00
if (bAutoAbilitySystem)
{
// TODO(Ed): Do we need to do this for enemies?
AbilitySystem->InitAbilityActorInfo(this, this);
}
}
void AGasaCharacter::SetPlayerDefaults()
2024-04-13 08:56:19 -07:00
{
Super::SetPlayerDefaults();
}
void AGasaCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
2024-04-13 08:56:19 -07:00
}
#pragma endregion Pawn
#pragma region Actor
2024-04-12 11:49:22 -07:00
void AGasaCharacter::BeginPlay()
{
Super::BeginPlay();
if (bAutoAbilitySystem)
{
// TODO(Ed): Do we need to do this for enemies?
AbilitySystem->InitAbilityActorInfo(this, this);
}
2024-04-12 11:49:22 -07:00
}
2024-04-12 19:05:09 -07:00
void AGasaCharacter::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
USkeletalMeshComponent* mesh = GetMesh();
2024-04-12 19:05:09 -07:00
switch (HighlightState)
{
case EHighlight::Disabled:
{
mesh->SetRenderCustomDepth(false);
mesh->SetCustomDepthStencilValue(0.f);
Weapon->SetRenderCustomDepth(false);
Weapon->SetCustomDepthStencilValue(0.f);
}
2024-04-12 19:05:09 -07:00
break;
case EHighlight::Enabled:
{
mesh->SetRenderCustomDepth(true);
mesh->SetCustomDepthStencilValue(HighlightStencilDepth);
Weapon->SetRenderCustomDepth(true);
Weapon->SetCustomDepthStencilValue(HighlightStencilDepth);
APostProcessVolume* PPV = Gasa::GetLevelActor(this)->GlobalPPV;
TObjectPtr<UObject> Blendable = PPV->Settings.WeightedBlendables.Array[0].Object;
UMaterialInstanceDynamic*
MID = Cast<UMaterialInstanceDynamic>(Blendable);
MID->SetVectorParameterValue("Depth Highlight Color", HighlightColor);
#if 0
2024-04-12 19:05:09 -07:00
UCapsuleComponent* Capsule = GetCapsuleComponent();
UKismetSystemLibrary::DrawDebugCapsule(this
, Capsule->GetComponentLocation()
, Capsule->GetScaledCapsuleHalfHeight()^
2024-04-12 19:05:09 -07:00
, Capsule->GetScaledCapsuleRadius()
, Capsule->GetComponentRotation()
2024-04-12 19:37:31 -07:00
, HighlightColor
2024-04-12 19:05:09 -07:00
, 0.f
, 1.f
);
#endif
2024-04-12 19:05:09 -07:00
}
break;
}
}
2024-04-13 08:56:19 -07:00
#pragma endregion Actor