GKLCharacter.cpp
Click a line number to link it. Shift-click to select a range.
| 1 | // Copyright (c) 2026 Reignance Studios. MIT License, see LICENSE. |
| 2 | |
| 3 | #include "GKLCharacter.h" |
| 4 | |
| 5 | #include "AbilitySystemComponent.h" |
| 6 | #include "GameFramework/CharacterMovementComponent.h" |
| 7 | #include "GKLPlayerState.h" |
| 8 | #include "Net/UnrealNetwork.h" |
| 9 | |
| 10 | AGKLCharacter::AGKLCharacter(const FObjectInitializer& ObjectInitializer) |
| 11 | : Super(ObjectInitializer) |
| 12 | { |
| 13 | bReplicates = true; |
| 14 | SetReplicateMovement(true); |
| 15 | } |
| 16 | |
| 17 | void AGKLCharacter::PossessedBy(AController* NewController) |
| 18 | { |
| 19 | Super::PossessedBy(NewController); |
| 20 | InitAbilitySystem(); // server |
| 21 | } |
| 22 | |
| 23 | void AGKLCharacter::OnRep_PlayerState() |
| 24 | { |
| 25 | Super::OnRep_PlayerState(); |
| 26 | InitAbilitySystem(); // owning client (and simulated proxies, which is harmless) |
| 27 | } |
| 28 | |
| 29 | void AGKLCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const |
| 30 | { |
| 31 | Super::GetLifetimeReplicatedProps(OutLifetimeProps); |
| 32 | DOREPLIFETIME(AGKLCharacter, bIsDead); |
| 33 | } |
| 34 | |
| 35 | UAbilitySystemComponent* AGKLCharacter::GetAbilitySystemComponent() const |
| 36 | { |
| 37 | const AGKLPlayerState* PS = GetPlayerState<AGKLPlayerState>(); |
| 38 | return PS ? PS->GetAbilitySystemComponent() : nullptr; |
| 39 | } |
| 40 | |
| 41 | void AGKLCharacter::InitAbilitySystem() |
| 42 | { |
| 43 | AGKLPlayerState* PS = GetPlayerState<AGKLPlayerState>(); |
| 44 | if (UAbilitySystemComponent* ASC = PS ? PS->GetAbilitySystemComponent() : nullptr) |
| 45 | { |
| 46 | // Owner = PlayerState (survives respawn), avatar = this pawn (what abilities act on). |
| 47 | ASC->InitAbilityActorInfo(PS, this); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | bool AGKLCharacter::ActivateAbilityByTag(FGameplayTag AbilityTag) |
| 52 | { |
| 53 | UAbilitySystemComponent* ASC = GetAbilitySystemComponent(); |
| 54 | return ASC && AbilityTag.IsValid() && ASC->TryActivateAbilitiesByTag(FGameplayTagContainer(AbilityTag)); |
| 55 | } |
| 56 | |
| 57 | void AGKLCharacter::Die() |
| 58 | { |
| 59 | if (!HasAuthority() || bIsDead) |
| 60 | { |
| 61 | return; |
| 62 | } |
| 63 | bIsDead = true; |
| 64 | OnDeath(); |
| 65 | } |
| 66 | |
| 67 | void AGKLCharacter::OnRep_IsDead() |
| 68 | { |
| 69 | if (bIsDead) |
| 70 | { |
| 71 | OnDeath(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void AGKLCharacter::OnDeath_Implementation() |
| 76 | { |
| 77 | GetCharacterMovement()->DisableMovement(); |
| 78 | } |