presidents-brigade/Source/PresidentsBrigade/PlayerPawn.cpp
Sara Montecino 0bf9105fcd Add boost
2022-11-11 17:33:00 -08:00

96 lines
2.8 KiB
C++
Executable File

// Fill out your copyright notice in the Description page of Project Settings.
#include "PlayerPawn.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/FloatingPawnMovement.h"
#include "Math/UnrealMathUtility.h"
#include "Util.h"
#define LogError(Msg) Util::log_error(TEXT("APlayerPawn"), Msg)
APlayerPawn::APlayerPawn(const FObjectInitializer &object_initializer):
ABasePawn(object_initializer)
{
main_camera = object_initializer.CreateDefaultSubobject<UCameraComponent>(this, FName("MainCamera"));
main_camera->SetupAttachment(root_component);
boost_multiplier = 3.0f;
reset_threshold_s = .5f;
cooldown_threshold_s = 3.0f;
// You can boost right away.
cooldown_wait_s = cooldown_threshold_s + 1.0f;
reset_wait_s = reset_threshold_s + 1.0f;
}
// Called to bind functionality to input
void APlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &ABasePawn::handle_move_forward);
PlayerInputComponent->BindAxis("MoveRight", this, &ABasePawn::handle_move_right);
PlayerInputComponent->BindAction("Shoot", EInputEvent::IE_Pressed, this, &ABasePawn::shoot);
PlayerInputComponent->BindAction("Boost", EInputEvent::IE_Pressed, this, &APlayerPawn::boost);
PlayerInputComponent->BindAction("Boost", EInputEvent::IE_Released, this, &APlayerPawn::reset_boost);
}
void APlayerPawn::Tick(float delta_time)
{
Super::Tick(delta_time);
if (cooldown_wait_s <= cooldown_threshold_s)
{
cooldown_wait_s += delta_time;
}
if (reset_wait_s <= reset_threshold_s)
{
reset_wait_s += delta_time;
}
if (reset_wait_s > reset_threshold_s)
{
reset_boost();
}
}
void APlayerPawn::boost()
{
const bool boost_allowed = cooldown_wait_s > cooldown_threshold_s;
if (!boost_allowed)
{
LogError("Cannot boost! You're on cooldown");
return;
}
const float current_speed = movement_component->GetMaxSpeed();
const float current_accel = movement_component->Acceleration;
if (!default_max_speed || FMath::IsNearlyEqual(current_speed, default_max_speed, 0.001f))
{
default_max_speed = current_speed;
default_max_accel = current_accel;
movement_component->MaxSpeed = default_max_speed * boost_multiplier;
movement_component->Acceleration = default_max_accel * boost_multiplier;
reset_wait_s = 0;
}
}
void APlayerPawn::reset_boost()
{
if (!default_max_speed)
{
// We haven't boosted yet. Ignore.
return;
}
const float current_speed = movement_component->GetMaxSpeed();
const float current_accel = movement_component->Acceleration;
if (!FMath::IsNearlyEqual(current_speed, default_max_speed, 0.001f))
{
movement_component->MaxSpeed = default_max_speed;
movement_component->Acceleration = default_max_accel;
cooldown_wait_s = 0;
}
}