Section 13: The Enemy Class - Lecture 254

Explosive
This commit is contained in:
charnet3d 2024-03-02 12:39:01 +01:00
parent 7d6d6ed497
commit 1de94528c8
28 changed files with 304 additions and 18 deletions

View File

@ -47,6 +47,10 @@ r.DynamicGlobalIlluminationMethod=1
r.ReflectionMethod=1
r.Shadow.Virtual.Enable=1
r.CustomDepth=3
r.Streaming.LimitPoolSizeToVRAM=1
r.Streaming.PoolSize=0
r.MSAACount=2
r.AntiAliasingMethod=2
[/Script/WorldPartitionEditor.WorldPartitionEditorSettings]
CommandletClass=Class'/Script/UnrealEd.WorldPartitionConvertCommandlet'

BIN
Content/_Game/Assets/Sounds/Ambient/Collapse01.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Collapse02.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Explosion.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Explosion01.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Explosion02.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Fire01.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Fire_Sparks01.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Light01.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Light02.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Smoke01.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Starter_Birds01.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Starter_Music01.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Starter_Wind05.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Starter_Wind06.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Ambient/Steam01.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Assets/Sounds/Impacts/FleshHit_Cue.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Enemies/EnemyBP.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Explosives/ExplosiveBP.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Maps/DefaultMap.umap (Stored with Git LFS)

Binary file not shown.

View File

@ -0,0 +1,6 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "BulletHitInterface.h"
// Add default functionality here for any IBulletHitInterface functions that are not pure virtual.

View File

@ -0,0 +1,28 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "BulletHitInterface.generated.h"
// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UBulletHitInterface : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class SHOOTER_API IBulletHitInterface
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
UFUNCTION(BlueprintNativeEvent, BlueprintCallable)
void BulletHit(FHitResult HitResult);
};

51
Source/Shooter/Enemy.cpp Normal file
View File

@ -0,0 +1,51 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Enemy.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
#include "Particles/ParticleSystemComponent.h"
// Sets default values
AEnemy::AEnemy()
{
// 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;
}
// Called when the game starts or when spawned
void AEnemy::BeginPlay()
{
Super::BeginPlay();
GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Block);
}
// Called every frame
void AEnemy::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AEnemy::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
void AEnemy::BulletHit_Implementation(FHitResult HitResult)
{
if (ImpactSound)
{
UGameplayStatics::PlaySoundAtLocation(this, ImpactSound, GetActorLocation());
}
if (ImpactParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, HitResult.Location, FRotator(0.f), true);
}
}

41
Source/Shooter/Enemy.h Normal file
View File

@ -0,0 +1,41 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BulletHitInterface.h"
#include "GameFramework/Character.h"
#include "Enemy.generated.h"
class UParticleSystem;
class USoundCue;
UCLASS()
class SHOOTER_API AEnemy : public ACharacter, public IBulletHitInterface
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AEnemy();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
/** Particles to spawn when hit by bullets */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true))
UParticleSystem* ImpactParticles;
/** Sound to play when hit by bullets */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true))
USoundCue* ImpactSound;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
virtual void BulletHit_Implementation(FHitResult HitResult) override;
};

View File

@ -0,0 +1,47 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Explosive.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
#include "Particles/ParticleSystemComponent.h"
// Sets default values
AExplosive::AExplosive()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AExplosive::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AExplosive::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AExplosive::BulletHit_Implementation(FHitResult HitResult)
{
if (ExplosionSound)
{
UGameplayStatics::PlaySoundAtLocation(this, ExplosionSound, GetActorLocation());
}
if (ExplosionParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ExplosionParticles, HitResult.Location, FRotator(0.f), true);
}
// TODO: Apply explosive damage
Destroy();
}

View File

@ -0,0 +1,41 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BulletHitInterface.h"
#include "GameFramework/Actor.h"
#include "Explosive.generated.h"
class UParticleSystem;
class USoundCue;
UCLASS()
class SHOOTER_API AExplosive : public AActor, public IBulletHitInterface
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AExplosive();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
/** Particles to spawn when hit by bullets */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true))
UParticleSystem* ExplosionParticles;
/** Sound to play when hit by bullets */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true))
USoundCue* ExplosionSound;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
virtual void BulletHit_Implementation(FHitResult HitResult) override;
};

View File

@ -17,6 +17,7 @@
#include "Particles/ParticleSystemComponent.h"
#include "Sound/SoundCue.h"
#include "Ammo.h"
#include "BulletHitInterface.h"
#include "PhysicalMaterials/PhysicalMaterial.h"
#include "Shooter.h"
@ -654,16 +655,29 @@ void AShooterCharacter::SendBullet()
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), EquippedWeapon->GetMuzzleFlash(), SocketTransform);
}
FVector BeamEnd;
bool bBeamEnd = GetBeamEndLocation(SocketTransform.GetLocation(), BeamEnd);
FHitResult BeamHitResult;
bool bBeamEnd = GetBeamEndLocation(SocketTransform.GetLocation(), BeamHitResult);
if (!bBeamEnd) return;
// Spawn impact particles after updating BeamEndPoint
if (ImpactParticles)
// Does hit actor implement BulletHitInterface?
IBulletHitInterface* BulletHitInterface = nullptr;
if (IsValid(BeamHitResult.GetActor()) &&
(BulletHitInterface = Cast<IBulletHitInterface>(BeamHitResult.GetActor())) != nullptr)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles,
BeamEnd);
BulletHitInterface->BulletHit_Implementation(BeamHitResult);
UE_LOG(LogTemp, Warning, TEXT("actor particle"));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("actor particle"));
}
else
{
if (ImpactParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles,
BeamHitResult.Location);
UE_LOG(LogTemp, Warning, TEXT("normal particle"));
}
}
// Spawn smoke trail particles
@ -673,10 +687,9 @@ void AShooterCharacter::SendBullet()
SocketTransform);
if (Beam)
{
Beam->SetVectorParameter("Target", BeamEnd);
Beam->SetVectorParameter("Target", BeamHitResult.Location);
}
}
}
void AShooterCharacter::PlayGunFireMontage()
@ -691,27 +704,28 @@ void AShooterCharacter::PlayGunFireMontage()
}
}
bool AShooterCharacter::GetBeamEndLocation(const FVector& MuzzleSocketLocation, FVector& OutBeamLocation)
bool AShooterCharacter::GetBeamEndLocation(const FVector& MuzzleSocketLocation, FHitResult& OutHitResult)
{
FVector OutBeamLocation;
// Check for crosshair trace hit
FHitResult CrosshairHitResult;
bool bCrosshairHit = TraceUnderCrosshairs(CrosshairHitResult, OutBeamLocation);
// Perform a second trace, this time from the gun barrel
FHitResult WeaponTraceHit;
const FVector WeaponTraceStart{ MuzzleSocketLocation };
const FVector StartToEnd{ OutBeamLocation - MuzzleSocketLocation };
const FVector WeaponTraceEnd{ MuzzleSocketLocation + StartToEnd * 1.25f };
GetWorld()->LineTraceSingleByChannel(WeaponTraceHit, WeaponTraceStart, WeaponTraceEnd,
GetWorld()->LineTraceSingleByChannel(OutHitResult, WeaponTraceStart, WeaponTraceEnd,
ECollisionChannel::ECC_Visibility);
if (WeaponTraceHit.bBlockingHit) // Object between barrel and BeamEndPoint ?
if (!OutHitResult.bBlockingHit) // Object between barrel and BeamEndPoint ?
{
OutBeamLocation = WeaponTraceHit.Location;
return true;
OutHitResult.Location = OutBeamLocation;
return false;
}
return false;
return true;
}
bool AShooterCharacter::TraceUnderCrosshairs(FHitResult& OutHitResult, FVector& OutHitLocation)

View File

@ -104,7 +104,7 @@ protected:
*/
void FireWeapon();
bool GetBeamEndLocation(const FVector& MuzzleSocketLocation, FVector& OutBeamLocation);
bool GetBeamEndLocation(const FVector& MuzzleSocketLocation, FHitResult& OutHitResult);
/* Set bAiming to true or false with button press */
void AimingButtonPressed();