158 lines
3.9 KiB
C++
158 lines
3.9 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "Player/LMPlayer.h"
|
|
|
|
#include "KismetTraceUtils.h"
|
|
#include "LMUserSettings.h"
|
|
#include "LMUtils.h"
|
|
#include "Camera/CameraComponent.h"
|
|
#include "Ammo/LMAmmo.h"
|
|
#include "Player/LMBulletInfo.h"
|
|
#include "Player/LMHitBox.h"
|
|
#include "Weapon/LMWeaponManager.h"
|
|
|
|
ALMPlayer::ALMPlayer()
|
|
{
|
|
PrimaryActorTick.bCanEverTick = true;
|
|
|
|
// Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
|
|
// Camera->SetupAttachment(RootComponent);
|
|
// Camera->bUsePawnControlRotation = true;
|
|
// Camera->SetRelativeLocation(FVector(20, 0, 90));
|
|
//
|
|
// ArmsMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Arms Mesh"));
|
|
// ArmsMesh->SetupAttachment(Camera);
|
|
|
|
SpreadStream = FRandomStream(FMath::Rand());
|
|
}
|
|
|
|
void ALMPlayer::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
WeaponManager->WeaponFired.AddUniqueDynamic(this, &ALMPlayer::WeaponFired);
|
|
WeaponManager->WeaponSwitched.AddUniqueDynamic(this, &ALMPlayer::WeaponSwitched);
|
|
|
|
GetCamera()->SetFieldOfView(ULMUserSettings::GetLegumixUserSettings()->FieldOfView);
|
|
}
|
|
|
|
bool ALMPlayer::PickUpAmmo(ALMAmmo* Ammo)
|
|
{
|
|
if (GEngine)
|
|
{
|
|
const FString AmmoAmount = FString::Printf(TEXT("Quantité de munition : %i"), Ammo->GetAmmoAmount());
|
|
GEngine->AddOnScreenDebugMessage(INDEX_NONE, 30.f, FColor::Red, AmmoAmount);
|
|
}
|
|
|
|
if (WeaponManager->AddAmmoType(Ammo->GetAmmoType(), Ammo->GetAmmoAmount()))
|
|
{
|
|
OnAmmoPickup();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
int ALMPlayer::GetAmmoCount(const EAmmoType AmmoType) const
|
|
{
|
|
return WeaponManager->GetAmmoCount(AmmoType);
|
|
}
|
|
|
|
int ALMPlayer::RemoveAmmo(const EAmmoType AmmoType, const int Count) const
|
|
{
|
|
return WeaponManager->RemoveAmmo(AmmoType, Count);
|
|
}
|
|
|
|
void ALMPlayer::SetWeaponManager(ULMWeaponManager* Manager)
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("Set weapon manager"))
|
|
if (!Manager)
|
|
{
|
|
GEngine->AddOnScreenDebugMessage(INDEX_NONE, 10.f, FColor::Red, TEXT("No Weapon Manager was given to the player !"));
|
|
return;
|
|
}
|
|
|
|
WeaponManager = Manager;
|
|
WeaponManager->Initialize(GetArms());
|
|
}
|
|
|
|
void ALMPlayer::PlayAnimation(UAnimMontage* Animation)
|
|
{
|
|
}
|
|
|
|
void ALMPlayer::FireBullets(const FLMBulletInfo Settings)
|
|
{
|
|
FVector EndLocation;
|
|
FVector ShotVector;
|
|
FHitResult OutHit = FHitResult();
|
|
bool HitSomething = false;
|
|
|
|
OnFire();
|
|
|
|
#if WITH_EDITOR
|
|
if (bDrawBulletDebug)
|
|
DrawDebugLine(GetWorld(), Settings.Origin, Settings.Origin + (Settings.Direction * Settings.MaxDistance), FColor::Blue, false, 2.f);
|
|
#endif
|
|
|
|
for (int Shots = 0; Shots < Settings.BulletCount; Shots++)
|
|
{
|
|
ShotVector = UKismetMathLibrary::RandomUnitVectorInConeInDegreesFromStream(SpreadStream, Settings.Direction, Settings.Spread);
|
|
EndLocation = Settings.Origin + (ShotVector * Settings.MaxDistance);
|
|
|
|
const bool HasHit = GetWorld()->LineTraceSingleByChannel(OutHit, Settings.Origin, EndLocation, TRACE_BULLET);
|
|
|
|
#if WITH_EDITOR
|
|
if (bDrawBulletDebug)
|
|
DrawDebugLineTraceSingle(GetWorld(), Settings.Origin, EndLocation, EDrawDebugTrace::ForDuration, HasHit, OutHit, FColor::Green, FColor::Red, 2.f);
|
|
#endif
|
|
|
|
WeaponManager->CreateTracer(Settings.AmmoType, HasHit ? OutHit.Location : EndLocation, ShotVector.Rotation());
|
|
|
|
if (!HasHit)
|
|
continue;
|
|
|
|
if (ULMHitBox* HitBox = Cast<ULMHitBox>(OutHit.Component))
|
|
{
|
|
HitSomething = true;
|
|
HitBox->OnHit(Settings);
|
|
}
|
|
}
|
|
|
|
if (HitSomething)
|
|
{
|
|
OnEnnemyHit();
|
|
}
|
|
}
|
|
|
|
FVector ALMPlayer::GetWeaponFiringOrigin()
|
|
{
|
|
if (!GetCam())
|
|
{
|
|
GEngine->AddOnScreenDebugMessage(INDEX_NONE, 10.f, FColor::Silver, TEXT("No camera ???"));
|
|
return FVector::ZeroVector;
|
|
}
|
|
return GetCam()->GetComponentTransform().GetLocation();
|
|
}
|
|
|
|
FVector ALMPlayer::GetAimVector() { return GetCam()->GetForwardVector(); }
|
|
|
|
void ALMPlayer::OnPause_Implementation(bool Paused)
|
|
{
|
|
}
|
|
|
|
UCameraComponent* ALMPlayer::GetCam()
|
|
{
|
|
if (!Camera)
|
|
Camera = GetCamera();
|
|
|
|
return Camera;
|
|
}
|
|
|
|
USkeletalMeshComponent* ALMPlayer::GetArms()
|
|
{
|
|
if (!ArmsMesh)
|
|
ArmsMesh = GetArmsBP();
|
|
|
|
return ArmsMesh;
|
|
}
|
|
|