blaster/Source/Blaster/Components/BuffComponent.cpp

133 lines
3.3 KiB
C++
Raw Normal View History

2022-05-22 21:17:42 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "BuffComponent.h"
2022-05-22 22:16:27 +00:00
#include "Blaster/Character/BlasterCharacter.h"
2022-05-22 23:06:42 +00:00
#include "GameFramework/CharacterMovementComponent.h"
2022-05-22 22:16:27 +00:00
2022-05-22 21:17:42 +00:00
UBuffComponent::UBuffComponent()
{
PrimaryComponentTick.bCanEverTick = true;
}
2022-05-22 22:16:27 +00:00
void UBuffComponent::Heal(float HealAmount, float HealingTime)
{
bHealing = true;
HealingRate = HealAmount / HealingTime;
AmountToHeal += HealAmount;
}
2022-05-22 21:17:42 +00:00
void UBuffComponent::BeginPlay()
{
Super::BeginPlay();
}
2022-05-22 23:06:42 +00:00
void UBuffComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
HealRampUp(DeltaTime);
}
2022-05-22 22:16:27 +00:00
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;
}
}
2022-05-22 21:17:42 +00:00
2022-05-22 23:06:42 +00:00
void UBuffComponent::BuffSpeed(float BuffBaseSpeed, float BuffCrouchSpeed, float BuffTime)
2022-05-22 21:17:42 +00:00
{
2022-05-22 23:06:42 +00:00
if (Character == nullptr) return;
2022-05-22 21:17:42 +00:00
2022-05-22 23:06:42 +00:00
Character->GetWorldTimerManager().SetTimer(
SpeedBuffTimer,
this,
&UBuffComponent::ResetSpeeds,
BuffTime
);
if (Character->GetCharacterMovement())
{
Character->GetCharacterMovement()->MaxWalkSpeed = BuffBaseSpeed;
Character->GetCharacterMovement()->MaxWalkSpeedCrouched = BuffCrouchSpeed;
MulticastSpeedBuff(BuffBaseSpeed, BuffCrouchSpeed);
}
2022-05-22 21:17:42 +00:00
}
2022-05-22 23:06:42 +00:00
void UBuffComponent::MulticastSpeedBuff_Implementation(float BaseSpeed, float CrouchSpeed)
{
2022-05-22 23:53:02 +00:00
if (Character && Character->GetCharacterMovement())
2022-05-22 23:06:42 +00:00
{
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;
}
2022-05-22 23:53:02 +00:00
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);
}
}