Related code:
FirstPersonCharacter.cpp
// © 2015 Dirt Productions. All rights reserved.
#include "DistantHome.h"
#include "Public/User/FirstPersonCharacter.h"
#include "Public/GameInfo.h"
#include "Public/Libraries/StaticFunctions.h"
// Sets default values
AFirstPersonCharacter::AFirstPersonCharacter()
{
// Set this character to call Tick() every frame
PrimaryActorTick.bCanEverTick = true;
bIsSprinting = false;
bWantsToSprint = false;
bIsCrouching = false;
bWantsToCrouch = false;
bIsAirborne = false;
bWantsToJump = false;
bIsFiring = false;
bWantsToFire = false;
bIsFirstFire = true;
NormalJumpVelocity = 430.0f;
SprintingJumpVelocity = (NormalJumpVelocity + 70.0f);
BulletSpread = 600.0f;
CurrentWeapon = EWeaponSlot::PrimaryWeapon;
}
// Called when the game starts or when spawned
void AFirstPersonCharacter::BeginPlay()
{
Super::BeginPlay();
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Character enabled"));
}
}
// Called every frame
void AFirstPersonCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AFirstPersonCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
InputComponent->BindAxis("MoveForward", this, &AFirstPersonCharacter::MoveForward);
InputComponent->BindAxis("MoveRight", this, &AFirstPersonCharacter::MoveRight);
InputComponent->BindAxis("Turn", this, &AFirstPersonCharacter::Turn);
InputComponent->BindAxis("LookUp", this, &AFirstPersonCharacter::LookUp);
InputComponent->BindAxis("SwitchWeapon", this , &AFirstPersonCharacter::OnSwitchWeaponAtRate);
InputComponent->BindAction("Jump", IE_Pressed, this, &AFirstPersonCharacter::OnJump);
InputComponent->BindAction("Jump", IE_Released, this, &AFirstPersonCharacter::OnStopJumping);
InputComponent->BindAction("Crouch", IE_Pressed, this, &AFirstPersonCharacter::OnCrouch);
InputComponent->BindAction("Crouch", IE_Released, this, &AFirstPersonCharacter::OnStopCrouching);
InputComponent->BindAction("Sprint", IE_Pressed, this, &AFirstPersonCharacter::OnSprint);
InputComponent->BindAction("Sprint", IE_Released, this, &AFirstPersonCharacter::OnStopSprinting);
InputComponent->BindAction("Fire", IE_Pressed, this, &AFirstPersonCharacter::OnFire);
InputComponent->BindAction("Fire", IE_Released, this, &AFirstPersonCharacter::OnStopFiring);
}
UFUNCTION()
// Handles X axis movement
void AFirstPersonCharacter::MoveForward(float Value)
{
if ((Controller != NULL) && (Value != 0.0f))
{
// Determine forward direction
FRotator Rotation = Controller->GetControlRotation();
// Limit pitch when walking/falling
if (GetCharacterMovement()->IsMovingOnGround() || GetCharacterMovement()->IsFalling())
{
Rotation.Pitch = 0.0f;
}
// Add movement to direction
const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
AddMovementInput(Direction, Value);
}
}
UFUNCTION()
// Handles Y axis movement
void AFirstPersonCharacter::MoveRight(float Value)
{
if ((Controller != NULL) && (Value != 0.0f))
{
// Determine right direction
const FRotator Rotation = Controller->GetControlRotation();
const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y);
// Add movement to direction
AddMovementInput(Direction, Value);
}
}
UFUNCTION()
// Handles mouse yaw
void AFirstPersonCharacter::Turn(float Value)
{
AddControllerYawInput(Value);
}
UFUNCTION()
// Handles mouse pitch
void AFirstPersonCharacter::LookUp(float Value)
{
AddControllerPitchInput(Value);
}
UFUNCTION()
// Handles jump input
void AFirstPersonCharacter::OnJump()
{
bWantsToJump = true;
// Ignores call is character is airborne or does not want to jump
if (bIsAirborne == false && bWantsToJump == true)
{
// Determine whether or not character is sprinting, and control flow accordingly
if (bIsSprinting == true)
{
&UCharacterMovementComponent::execJump;
}
else
{
&UCharacterMovementComponent::execJump;
}
}
}
UFUNCTION()
// Stops jumping
void AFirstPersonCharacter::OnStopJumping()
{
bWantsToJump = false;
}
UFUNCTION()
// Handles crouch input
void AFirstPersonCharacter::OnCrouch()
{
bWantsToCrouch = true;
// Ignores call if is crouching ot does not want to crouch
if (bIsCrouching == false && bWantsToCrouch == true)
{
&UCharacterMovementComponent::Crouch;
}
}
UFUNCTION()
// Stops crouching
void AFirstPersonCharacter::OnStopCrouching()
{
bWantsToCrouch = false;
// Ignores call if is not crouching or wants to crouch
if (bIsCrouching == true && bWantsToCrouch == false)
{
&UCharacterMovementComponent::UnCrouch;
}
}
UFUNCTION()
// Handles sprint input
void AFirstPersonCharacter::OnSprint()
{
bWantsToSprint = true;
// Ignores call if character is sprinting or does not want to sprint
if (bIsSprinting == false && bWantsToSprint == true)
{
}
}
UFUNCTION()
// Stops sprinting
void AFirstPersonCharacter::OnStopSprinting()
{
bWantsToSprint = false;
// Ignores call if character is not sprinting or does want to sprint
if (bIsSprinting == true && bWantsToSprint == false)
{
}
}
UFUNCTION()
// Handles fire input
void AFirstPersonCharacter::OnFire()
{
bWantsToFire = true;
// Ignores call if is firing or does not want to fire
if (bIsFiring == false && bWantsToFire == true)
{
if (bIsFirstFire == true)
{
bIsFirstFire = false;
FireWeapon(GetActorLocation(), (GetActorForwardVector()));
}
//FTimerManager::SetTimer(this, FireTrace(GetActorLocation(), (GetActorForwardVector())), 0.1f, true);
}
}
UFUNCTION()
// Stops firing
void AFirstPersonCharacter::OnStopFiring()
{
bWantsToFire = false;
// Ignores call if is not firing, or does want to fire
if (bIsFiring == true && bWantsToFire == false)
{
bIsFirstFire = true;
//FTimerManager::ClearTimer(this, FireTrace(GetActorLocation(), (GetActorForwardVector())));
}
}
UFUNCTION()
// Calculates bullet spread, fires weapon, and plays effects
void AFirstPersonCharacter::FireWeapon(FVector Start, FVector Direction)
{
Direction = &UStaticFunctions::CalculateBulletSpread(Direction, GetCurrentBulletSpread(), this);
}
UFUNCTION()
// Handles switch weapon axis input
void AFirstPersonCharacter::OnSwitchWeaponAtRate(float Value)
{
}
UFUNCTION()
// Handles switch weapon to primary input
void AFirstPersonCharacter::OnSwitchWeaponToPrimary()
{
}
UFUNCTION()
// Handles switch weapon to secondary input
void AFirstPersonCharacter::OnSwitchWeaponToSecondary()
{
}
UFUNCTION()
// Handles switch weapon to melee input
void AFirstPersonCharacter::OnSwitchWeaponToMelee()
{
}
UFUNCTION()
// Hanldes switch weapon to ability input
void AFirstPersonCharacter::OnSwitchWeaponToAbility()
{
}
UFUNCTION()
// Called when weapon changed
void AFirstPersonCharacter::OnWeaponChanged()
{
}
UFUNCTION()
// Returns bullet spread of current weapon
float AFirstPersonCharacter::GetCurrentBulletSpread()
{
return BulletSpread;
}
StaticFunctions.cpp
// © 2015 Dirt Productions. All rights reserved.
#include "DistantHome.h"
#include "Public/Libraries/StaticFunctions.h"
#include "Public/User/FirstPersonCharacter.h"
UFUNCTION()
// Generates random float between A and B
float RandomFloatInRange(float A, float B)
{
float Max, Min;
if (A == B) return A;
else if (A > B)
{
Max = A;
Min = B;
}
else if (A < B)
{
Max = B;
Min = A;
}
else return NULL;
float ReturnValue = Min + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Max - Min)));
return ReturnValue;
}
UFUNCTION()
// Calculates bullet spread
FVector CalculateBulletSpread(FVector A, float BulletSpread, AFirstPersonCharacter* Character)
{
float Modifier0;
if (Character->bIsCrouching == true) Modifier0 = BulletSpread * 0.7f;
else Modifier0 = BulletSpread;
float Modifier1;
if (Character->bIsMoving == true) Modifier1 = BulletSpread * 1.1f;
else Modifier1 = BulletSpread;
float Modifier2;
if (Character->bIsAiming == true) Modifier2 = BulletSpread * 0.95f;
else Modifier2 = BulletSpread;
float Modifier3;
if (Character->bIsAirborne == true) Modifier3 = BulletSpread * 1.25f;
else Modifier3 = BulletSpread;
BulletSpread = (Modifier0 + Modifier1 + Modifier2 + Modifier3) / 4;
float NegatedBulletSpread = BulletSpread * -1.0f;
float Random0 = RandomFloatInRange(BulletSpread, NegatedBulletSpread);
float Random1 = RandomFloatInRange(BulletSpread, NegatedBulletSpread);
float Random2 = RandomFloatInRange(BulletSpread, NegatedBulletSpread);
A.X = A.X + Random0;
A.Y = A.Y + Random1;
A.Z = A.Z + Random2;
return A;
}
.h files define functions. Help please.