// Fill out your copyright notice in the Description page of Project Settings. #include "Player/LMPlayer.h" #include "KismetTraceUtils.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() { // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; Camera = CreateDefaultSubobject(TEXT("Camera")); Camera->SetupAttachment(RootComponent); Camera->bUsePawnControlRotation = true; Camera->SetRelativeLocation(FVector(20, 0, 90)); ArmsMesh = CreateDefaultSubobject(TEXT("Arms Mesh")); ArmsMesh->SetupAttachment(Camera); SpreadStream = FRandomStream(FMath::Rand()); } // Called when the game starts or when spawned void ALMPlayer::BeginPlay() { Super::BeginPlay(); WeaponManager->WeaponFired.AddUniqueDynamic(this, &ALMPlayer::WeaponFired); WeaponManager->WeaponSwitched.AddUniqueDynamic(this, &ALMPlayer::WeaponSwitched); } void ALMPlayer::PickUpAmmo(ALMAmmo* Ammo) { if (GEngine) { FString AmmoAmount = FString::Printf(TEXT("Quantité de munition : %i"), Ammo->GetAmmoAmount()); GEngine->AddOnScreenDebugMessage(INDEX_NONE, 30.f, FColor::Red, AmmoAmount); } WeaponManager->AddAmmoType(Ammo->GetAmmoType(), Ammo->GetAmmoAmount()); OnAmmoPickup(); } 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(ArmsMesh); } 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(OutHit.Component)) { HitSomething = true; HitBox->OnHit(Settings); } } if (HitSomething) { OnEnnemyHit(); } } FVector ALMPlayer::GetWeaponFiringOrigin() const { if (!Camera) { GEngine->AddOnScreenDebugMessage(INDEX_NONE, 10.f, FColor::Silver, TEXT("No camera ???")); return FVector::ZeroVector; } return Camera->GetComponentTransform().GetLocation(); } FVector ALMPlayer::GetAimVector() const { return Camera->GetForwardVector(); }