Minor - Implémente la collision joueur-centipede - V3.5.0

Implémente la détection de collision entre le joueur et le centipede.

Le joueur perd une vie lors de la collision et devient temporairement invincible.

Ajoute la logique de fin de niveau lorsque tous les segments du centipede sont détruits.
This commit is contained in:
2025-11-06 20:47:26 +01:00
parent 0da3851dde
commit 72e9caf2c1
4 changed files with 72 additions and 3 deletions

View File

@@ -6,7 +6,7 @@
UM4_CentipedeController::UM4_CentipedeController()
{
SegmentRemainingCount = BodyCount;
}
void UM4_CentipedeController::Initialize(FSubsystemCollectionBase& Collection)
@@ -326,6 +326,13 @@ void UM4_CentipedeController::OnSegmentHit(AM4_CentipedeBody* DestroyedSegment)
}
GM_REF_PTR->AddScore(100);
SpawnMushroomsAtSegmentDeathPos(DestroyedSegment->GetActorLocation());
// Decresase the remaining segment count and check if all segments are destroyed
SegmentRemainingCount--;
if (SegmentRemainingCount <= 0)
{
// TODO: Trigger level complete or respawn centipede
UE_LOG(M4_CPP, Warning, TEXT("All centipede segments destroyed!"));
}
}
void UM4_CentipedeController::SpawnMushroomsAtSegmentDeathPos(FVector Location)

View File

@@ -1,6 +1,7 @@
#include "M4_PlayerPawn.h"
#include "EnhancedInputComponent.h"
#include "M4_Gamemode.h"
#include "M4_LOG.h"
#include "M4_Projectile.h"
#include "GameFramework/PlayerController.h"
@@ -16,8 +17,15 @@ AM4_PlayerPawn::AM4_PlayerPawn()
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
MeshComponent->SetupAttachment(RootComponent);
MeshComponent->SetMobility(EComponentMobility::Movable);
MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
MeshComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName);
MeshComponent->SetCollisionObjectType(ECC_Pawn);
MeshComponent->SetCollisionResponseToAllChannels(ECR_Block);
MeshComponent->SetGenerateOverlapEvents(true);
MeshComponent->OnComponentBeginOverlap.AddDynamic(this,&AM4_PlayerPawn::OnPlayerOverlap);
MeshComponent->SetRelativeScale3D(FVector(1.0f, MeshScale.Y, MeshScale.X));
static ConstructorHelpers::FObjectFinder<UStaticMesh> DefaultMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube"));
@@ -100,3 +108,38 @@ void AM4_PlayerPawn::Shoot(const FInputActionInstance& Inst)
}
}
};
void AM4_PlayerPawn::OnPlayerOverlap(
UPrimitiveComponent* OverlappedComponent,
AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex,
bool bFromSweep,
const FHitResult& SweepResult)
{
if (!OtherActor || OtherActor == this || bIsInvincible)
{
return;
}
AM4_CentipedeBody* CentipedeSegment = Cast<AM4_CentipedeBody>(OtherActor);
if (CentipedeSegment)
{
UE_LOG(M4_CPP, Warning, TEXT("Player collided with Centipede Segment!"));
if (AM4_Gamemode* GM = Cast<AM4_Gamemode>(GetWorld()->GetAuthGameMode()))
{
GM->LoseLife();
UE_LOG(M4_CPP, Warning, TEXT("Player Lives Remaining: %d"), GM->GetLives());
bIsInvincible = true;
GetWorldTimerManager().SetTimer(InvincibleTimerHandle,this, &AM4_PlayerPawn::EndInvincibility, InvinciblityDuration, false);
}
}
}
void AM4_PlayerPawn::EndInvincibility()
{
bIsInvincible = false;
UE_LOG(M4_CPP, Warning, TEXT("Player invincibility ended."));
}

View File

@@ -38,6 +38,7 @@ private:
TArray<AM4_CentipedeBody*> BodySegments;
TArray<TArray<FVector>> SegmentHistory; // Historique pour chaque segment
int32 MaxHistorySize = 200;
int8 SegmentRemainingCount = 0;
UPROPERTY(EditAnywhere, Category = "Centipede")
int32 SegmentSpacing = 15; // Distance en nombre de frames entre segments

View File

@@ -25,6 +25,15 @@ public:
UPROPERTY(VisibleAnywhere)
TSubclassOf<AM4_Projectile> Proj = AM4_Projectile::StaticClass();
UFUNCTION()
void OnPlayerOverlap(
UPrimitiveComponent* OverlappedComponent,
AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex,
bool bFromSweep,
const FHitResult& SweepResult);
protected:
UPROPERTY(VisibleAnywhere)
@@ -39,4 +48,13 @@ protected:
float MoveSpeed = 500.f;
FVector2D MeshScale = FVector2D(0.6f, 0.5f);
FVector2D LastMoveValue = FVector2D::ZeroVector;
UPROPERTY()
bool bIsInvincible = false;
UPROPERTY()
float InvinciblityDuration = 2.0f;
FTimerHandle InvincibleTimerHandle;
void EndInvincibility();
};