diff --git a/Source/M4_CPP/private/M4_CentipedeController.cpp b/Source/M4_CPP/private/M4_CentipedeController.cpp index 5663b49..18d46cb 100644 --- a/Source/M4_CPP/private/M4_CentipedeController.cpp +++ b/Source/M4_CPP/private/M4_CentipedeController.cpp @@ -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) diff --git a/Source/M4_CPP/private/M4_PlayerPawn.cpp b/Source/M4_CPP/private/M4_PlayerPawn.cpp index 22bf50a..b4228d9 100644 --- a/Source/M4_CPP/private/M4_PlayerPawn.cpp +++ b/Source/M4_CPP/private/M4_PlayerPawn.cpp @@ -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(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 DefaultMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube")); @@ -99,4 +107,39 @@ 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(OtherActor); + if (CentipedeSegment) + { + UE_LOG(M4_CPP, Warning, TEXT("Player collided with Centipede Segment!")); + + if (AM4_Gamemode* GM = Cast(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.")); +} \ No newline at end of file diff --git a/Source/M4_CPP/public/M4_CentipedeController.h b/Source/M4_CPP/public/M4_CentipedeController.h index 31daf85..f81c64f 100644 --- a/Source/M4_CPP/public/M4_CentipedeController.h +++ b/Source/M4_CPP/public/M4_CentipedeController.h @@ -38,6 +38,7 @@ private: TArray BodySegments; TArray> 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 diff --git a/Source/M4_CPP/public/M4_PlayerPawn.h b/Source/M4_CPP/public/M4_PlayerPawn.h index ebbde98..5c510e6 100644 --- a/Source/M4_CPP/public/M4_PlayerPawn.h +++ b/Source/M4_CPP/public/M4_PlayerPawn.h @@ -25,6 +25,15 @@ public: UPROPERTY(VisibleAnywhere) TSubclassOf 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(); }; \ No newline at end of file