Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions Source/ALSV4_CPP/ALSV4_CPP.Build.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
// Copyright: Copyright (C) 2022 Doğa Can Yanıkoğlu
// Source Code: https://github.com/dyanikoglu/ALS-Community
// Copyright Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;

public class ALSV4_CPP : ModuleRules
{
public ALSV4_CPP(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;

PublicDependencyModuleNames.AddRange(new[]
{"Core", "CoreUObject", "Engine", "InputCore", "NavigationSystem", "AIModule", "GameplayTasks","PhysicsCore", "Niagara", "EnhancedInput"
});
PublicIncludePaths.AddRange(
new string[] {
"ALSV4_CPP/Public",
"ALSV4_CPP/Public/Character",
"ALSV4_CPP/Public/Weapon",
"ALSV4_CPP/Public/Components",
"ALSV4_CPP/Public/AI",
"ALSV4_CPP/Public/Library"
}
);

PrivateDependencyModuleNames.AddRange(new[] {"Slate", "SlateCore"});
PublicDependencyModuleNames.AddRange(
new string[] {
"Core",
"CoreUObject",
"Engine",
"InputCore",
"EnhancedInput"
}
);
}
}
}
128 changes: 128 additions & 0 deletions Source/ALSV4_CPP/Private/Character/HealthComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright: Copyright (C) 2024 Combat System
// Source Code: https://github.com/Klebold2009/ALS-Community

#include "Character/HealthComponent.h"
#include "Net/UnrealNetwork.h"
#include "GameFramework/Character.h"
#include "Particles/ParticleSystem.h"
#include "Kismet/GameplayStatics.h"

UHealthComponent::UHealthComponent()
{
PrimaryComponentTick.bCanEverTick = false;
bReplicateUsingRegisteredSubObjectList = true;
SetIsReplicatedEnabled(true);

MaxHealth = 100.0f;
CurrentHealth = 100.0f;
bIsDead = false;
BloodEffectRadius = 100.0f;
}

void UHealthComponent::BeginPlay()
{
Super::BeginPlay();

if (GetOwnerRole() == ROLE_Authority)
{
CurrentHealth = MaxHealth;
}
}

void UHealthComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
FDOReplicateVariable(UHealthComponent, CurrentHealth);
}

void UHealthComponent::TakeDamage(float DamageAmount, AActor* DamageInstigator)
{
if (GetOwnerRole() == ROLE_AutonomousProxy)
{
Server_TakeDamage(DamageAmount, DamageInstigator);
}
}

void UHealthComponent::Server_TakeDamage_Implementation(float DamageAmount, AActor* DamageInstigator)
{
if (bIsDead)
{
return;
}

CurrentHealth = FMath::Max(0.0f, CurrentHealth - DamageAmount);

Multicast_OnDamaged(DamageAmount, GetOwner()->GetActorLocation());

OnHealthChanged.Broadcast(CurrentHealth, MaxHealth, DamageInstigator);

if (CurrentHealth <= 0.0f)
{
Server_Die(DamageInstigator);
}
}

void UHealthComponent::Multicast_OnDamaged_Implementation(float DamageAmount, FVector DamageLocation)
{
SpawnBloodEffect(DamageLocation);
}

void UHealthComponent::Heal(float HealAmount)
{
if (GetOwnerRole() == ROLE_AutonomousProxy)
{
Server_Heal(HealAmount);
}
}

void UHealthComponent::Server_Heal_Implementation(float HealAmount)
{
if (bIsDead)
{
return;
}

CurrentHealth = FMath::Min(MaxHealth, CurrentHealth + HealAmount);
OnHealthChanged.Broadcast(CurrentHealth, MaxHealth, nullptr);
}

void UHealthComponent::Die(AActor* KillerActor)
{
if (GetOwnerRole() == ROLE_AutonomousProxy)
{
Server_Die(KillerActor);
}
}

void UHealthComponent::Server_Die_Implementation(AActor* KillerActor)
{
if (bIsDead)
{
return;
}

bIsDead = true;
CurrentHealth = 0.0f;

Multicast_Die(KillerActor);
}

void UHealthComponent::Multicast_Die_Implementation(AActor* KillerActor)
{
ACharacter* OwnerCharacter = Cast<ACharacter>(GetOwner());
if (OwnerCharacter)
{
OwnerCharacter->GetCharacterMovement()->DisableMovement();
OwnerCharacter->SetActorEnableCollision(false);
}

OnDeath.Broadcast(GetOwner(), KillerActor);
}

void UHealthComponent::SpawnBloodEffect(FVector Location)
{
if (BloodFX)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), BloodFX, Location);
}
}
103 changes: 103 additions & 0 deletions Source/ALSV4_CPP/Private/Character/WeaponCharacter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright: Copyright (C) 2024 Combat System
// Source Code: https://github.com/Klebold2009/ALS-Community

#include "Character/WeaponCharacter.h"
#include "Character/HealthComponent.h"
#include "Weapon/BaseWeapon.h"
#include "InputActionValue.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "Net/UnrealNetwork.h"

AWeaponCharacter::AWeaponCharacter()
{
PrimaryActorTick.TickInterval = 0.016f;
PrimaryActorTick.bCanEverTick = true;

// Create health component
HealthComponent = CreateDefaultSubobject<UHealthComponent>(TEXT("HealthComponent"));
WeaponAttachSocket = FName("weapon_r");
bIsInCombat = false;
}

void AWeaponCharacter::BeginPlay()
{
Super::BeginPlay();
}

void AWeaponCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}

void AWeaponCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}

void AWeaponCharacter::EquipWeapon(ABaseWeapon* NewWeapon)
{
if (GetOwnerRole() != ROLE_Authority)
{
return;
}

if (CurrentWeapon)
{
UnequipWeapon();
}

CurrentWeapon = NewWeapon;

if (CurrentWeapon)
{
CurrentWeapon->SetOwner(this);
CurrentWeapon->AttachToComponent(GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, WeaponAttachSocket);

FWeaponData WeaponData;
CurrentWeapon->InitializeWeapon(this, WeaponData);
}
}

void AWeaponCharacter::UnequipWeapon()
{
if (CurrentWeapon)
{
CurrentWeapon->Destroy();
CurrentWeapon = nullptr;
}
}

void AWeaponCharacter::FireWeapon()
{
if (CurrentWeapon && HealthComponent && HealthComponent->IsAlive())
{
CurrentWeapon->StartFiring();
}
}

void AWeaponCharacter::StopFireWeapon()
{
if (CurrentWeapon)
{
CurrentWeapon->StopFiring();
}
}

void AWeaponCharacter::ReloadWeapon()
{
if (CurrentWeapon)
{
CurrentWeapon->Reload();
}
}

float AWeaponCharacter::TakeDamage(float Damage, const FDamageEvent& DamageEvent, AController* EventInstigator, AActor* DamageCauser)
{
if (HealthComponent)
{
HealthComponent->Server_TakeDamage(Damage, DamageCauser);
}

return Damage;
}
Loading