Minor - Ajoute interaction projectile/champignon - V3.2.0

Implémente la détection de collision entre les projectiles et les champignons.

Le champignon est détruit après avoir été touché le nombre de fois défini par sa durée de vie.

Le projectile est détruit après avoir touché un champignon.
This commit is contained in:
2025-11-04 18:29:30 +01:00
parent c8f3f53a8f
commit 064d4cd576
3 changed files with 62 additions and 53 deletions

View File

@@ -6,6 +6,7 @@
#include "M4_CTP_Macros.h"
#include "Kismet/GameplayStatics.h"
#include "M4_Gamemode.h"
#include "M4_Projectile.h"
// Sets default values
@@ -38,19 +39,19 @@ AM4_Mushroom::AM4_Mushroom()
void AM4_Mushroom::BeginPlay()
{
MushLife = 3;
MushLife = 2;
DestructionHit = 0;
Super::BeginPlay();
}
void AM4_Mushroom::OnHit(UPrimitiveComponent* HitComp,AActor* OtherActor,UPrimitiveComponent* OtherComp,FVector NormalImpulse,const FHitResult& Hit)
{
if (OtherActor != this)
{
DestructionHit++;
if (DestructionHit == MushLife)
{
// check if the other actor is a projectile
AM4_Projectile* Projectile = Cast<AM4_Projectile>(OtherActor);
if (!Projectile)
return;
if (DestructionHit++ == MushLife)
Destroy();
}
}
}

View File

@@ -2,17 +2,25 @@
#include "Components/MeshComponent.h"
#include "M4_Gamemode.h"
#include "M4_Mushroom.h"
#include "Components/SphereComponent.h"
// Sets default values
AM4_Projectile::AM4_Projectile()
{
PrimaryActorTick.bCanEverTick = true;
if(!RootComponent)
{
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("ProjectileSceneComponent"));
// Crée d'abord le composant de collision comme root
CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
CollisionComp->InitSphereRadius(15.0f);
CollisionComp->SetCollisionProfileName(TEXT("Projectile"));
CollisionComp->OnComponentBeginOverlap.AddDynamic(this, &AM4_Projectile::OnOverlap);
CollisionComp->SetGenerateOverlapEvents(true);
RootComponent = CollisionComp;
// Ensuite attache le mesh au composant de collision
ProjectileMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ProjectileMeshComponent"));
ProjectileMeshComponent->SetupAttachment(RootComponent);
ProjectileMeshComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision); // Le mesh ne gère pas la collision
static ConstructorHelpers::FObjectFinder<UStaticMesh> Mesh(TEXT("/Game/CTP/04_Mesh/SM_Projectile.SM_Projectile"));
if(Mesh.Succeeded())
@@ -29,10 +37,11 @@ AM4_Projectile::AM4_Projectile()
ProjectileMeshComponent->SetMaterial(0, ProjectileMat);
ProjectileMeshComponent->SetRelativeScale3D(FVector(.8f, .3f, .35f));
ProjectileMeshComponent->SetMobility(EComponentMobility::Movable);
ProjectileMeshComponent->SetRelativeLocation(InitialLoc);
ProjectileMeshComponent->SetRelativeLocation(FVector::ZeroVector);
// Configure le ProjectileMovementComponent
ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
ProjectileMovementComponent->SetUpdatedComponent(CollisionComp);
ProjectileMovementComponent->SetUpdatedComponent(CollisionComp); // Maintenant CollisionComp existe
ProjectileMovementComponent->InitialSpeed = 380.f;
ProjectileMovementComponent->MaxSpeed = 380.f;
ProjectileMovementComponent->bRotationFollowsVelocity = false;
@@ -40,13 +49,6 @@ AM4_Projectile::AM4_Projectile()
ProjectileMovementComponent->ProjectileGravityScale = 0.0f;
InitialLifeSpan = 10.f;
}
if(!CollisionComp)
{
CollisionComp = CreateDefaultSubobject<UMeshComponent>(TEXT("MeshComponent"));
}
}
///FAIRE SUPER GAFFE AUX INDENTATIONS ET LES FOR LOOPS sont à bien faire.
///Ce qui est critique dans notre code doit être vraiment bien mis en évidence sans être obscurci par la boucle.
@@ -58,19 +60,23 @@ void AM4_Projectile::BeginPlay()
InitialLoc = GetActorLocation();
}
void AM4_Projectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
void AM4_Projectile::OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
UE_LOG(LogTemp, Warning, TEXT("Projectile OVERLAP: %s"), *OtherActor->GetName());
if (OtherActor && OtherActor != this && OtherComp != nullptr)
{
AM4_Mushroom* Mushroom = Cast<AM4_Mushroom>(OtherActor);
if (Mushroom)
{
Mushroom->OnHit(HitComp,OtherActor,OtherComp,NormalImpulse,Hit);
}
UE_LOG(LogTemp, Warning, TEXT("OVERLAP MUSHROOM!"));
FVector NormalImpulse = FVector::ZeroVector;
Mushroom->OnHit(OverlappedComponent, this, OtherComp, NormalImpulse, SweepResult);
Destroy();
}
}
}
void AM4_Projectile::FireInDir(const FVector& ShootDir)
{

View File

@@ -6,6 +6,7 @@
#include "GameFramework/Actor.h"
#include "M4_Projectile.generated.h"
class USphereComponent;
class UMeshComponent;
UCLASS()
@@ -25,7 +26,7 @@ public:
FVector NewLoc;
UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
UMeshComponent* CollisionComp;
USphereComponent* CollisionComp;
UPROPERTY(VisibleAnywhere, Category = Movement)
UProjectileMovementComponent* ProjectileMovementComponent;
@@ -37,6 +38,7 @@ public:
UMaterialInstanceDynamic* ProjectileMat;
UFUNCTION()
void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
void OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
void FireInDir(const FVector& ShootDir);
};