blaster/Source/Blaster/Components/BuffComponent.cpp

157 lines
4.0 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "BuffComponent.h"
#include "Blaster/Character/BlasterCharacter.h"
#include "GameFramework/CharacterMovementComponent.h"
UBuffComponent::UBuffComponent()
{
PrimaryComponentTick.bCanEverTick = true;
}
void UBuffComponent::Heal(float HealAmount, float HealingTime)
{
bHealing = true;
HealingRate = HealAmount / HealingTime;
AmountToHeal += HealAmount;
}
void UBuffComponent::ReplenishShield(float ShieldAmount, float ReplenishTime)
{
bReplenishingShield = true;
ShieldReplenishRate = ShieldAmount / ReplenishTime;
ShieldReplenishAmount += ShieldAmount;
}
void UBuffComponent::BeginPlay()
{
Super::BeginPlay();
}
void UBuffComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
HealRampUp(DeltaTime);
ShieldRampUp(DeltaTime);
}
void UBuffComponent::HealRampUp(float DeltaTime)
{
if (!bHealing || Character == nullptr || Character->IsEliminated()) return;
const float HealThisFrame = HealingRate * DeltaTime;
Character->SetHealth(FMath::Clamp(Character->GetHealth() + HealThisFrame, 0, Character->GetMaxHealth()));
Character->UpdateHUDHealth();
AmountToHeal -= HealThisFrame;
if (AmountToHeal <= 0 || Character->GetHealth() >= Character->GetMaxHealth())
{
bHealing = false;
AmountToHeal = 0.f;
}
}
void UBuffComponent::ShieldRampUp(float DeltaTime)
{
if (!bReplenishingShield || Character == nullptr || Character->IsEliminated()) return;
const float ReplenishThisFrame = ShieldReplenishRate * DeltaTime;
Character->SetShield(FMath::Clamp(Character->GetShield() + ReplenishThisFrame, 0, Character->GetMaxShield()));
Character->UpdateHUDShield();
ShieldReplenishAmount -= ReplenishThisFrame;
if (ShieldReplenishAmount <= 0 || Character->GetShield() >= Character->GetMaxShield())
{
bReplenishingShield = false;
ShieldReplenishAmount = 0.f;
}
}
void UBuffComponent::BuffSpeed(float BuffBaseSpeed, float BuffCrouchSpeed, float BuffTime)
{
if (Character == nullptr) return;
Character->GetWorldTimerManager().SetTimer(
SpeedBuffTimer,
this,
&UBuffComponent::ResetSpeeds,
BuffTime
);
if (Character->GetCharacterMovement())
{
Character->GetCharacterMovement()->MaxWalkSpeed = BuffBaseSpeed;
Character->GetCharacterMovement()->MaxWalkSpeedCrouched = BuffCrouchSpeed;
MulticastSpeedBuff(BuffBaseSpeed, BuffCrouchSpeed);
}
}
void UBuffComponent::MulticastSpeedBuff_Implementation(float BaseSpeed, float CrouchSpeed)
{
if (Character && Character->GetCharacterMovement())
{
Character->GetCharacterMovement()->MaxWalkSpeed = BaseSpeed;
Character->GetCharacterMovement()->MaxWalkSpeedCrouched = CrouchSpeed;
}
}
void UBuffComponent::ResetSpeeds()
{
if (Character && Character->GetCharacterMovement())
{
Character->GetCharacterMovement()->MaxWalkSpeed = InitialBaseSpeed;
Character->GetCharacterMovement()->MaxWalkSpeedCrouched = InitialCrouchSpeed;
MulticastSpeedBuff(InitialBaseSpeed, InitialCrouchSpeed);
}
}
void UBuffComponent::SetInitialSpeeds(float BaseSpeed, float CrouchSpeed)
{
InitialBaseSpeed = BaseSpeed;
InitialCrouchSpeed = CrouchSpeed;
}
void UBuffComponent::SetInitialJumpVelocity(float Velocity)
{
InitialJumpZVelocity = Velocity;
}
void UBuffComponent::BuffJump(float BuffJumpVelocity, float BuffTime)
{
if (Character == nullptr) return;
Character->GetWorldTimerManager().SetTimer(
JumpBuffTimer,
this,
&UBuffComponent::ResetJump,
BuffTime
);
if (Character->GetCharacterMovement())
{
Character->GetCharacterMovement()->JumpZVelocity = BuffJumpVelocity;
MulticastJumpBuff(BuffJumpVelocity);
}
}
void UBuffComponent::MulticastJumpBuff_Implementation(float JumpVelocity)
{
if (Character && Character->GetCharacterMovement())
{
Character->GetCharacterMovement()->JumpZVelocity = JumpVelocity;
}
}
void UBuffComponent::ResetJump()
{
if (Character && Character->GetCharacterMovement())
{
Character->GetCharacterMovement()->JumpZVelocity = InitialJumpZVelocity;
MulticastJumpBuff(InitialJumpZVelocity);
}
}