From 0da3851dde3d3891a08cd01af9fd18feedcab31d Mon Sep 17 00:00:00 2001 From: CatChow0 Date: Wed, 5 Nov 2025 14:19:08 +0100 Subject: [PATCH] =?UTF-8?q?Minor=20-=20G=C3=A8re=20la=20mort=20des=20segme?= =?UTF-8?q?nts=20-=20V3.4.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Modifie la logique de destruction des segments de centipède. Au lieu de détruire immédiatement le segment, désactive la collision et le rend invisible. Des champignons sont générés à l'endroit de sa mort et le score du joueur est augmenté. L'événement OnSegmentDestroyed est renommé en OnSegmentHit pour refléter la nouvelle logique. --- Config/DefaultEngine.ini | 1 + .../M4_CPP/private/M4_CentipedeController.cpp | 48 ++++++++++++++----- Source/M4_CPP/private/M4_Projectile.cpp | 38 ++++++++++----- Source/M4_CPP/public/M4_CentipedeController.h | 6 ++- 4 files changed, 68 insertions(+), 25 deletions(-) diff --git a/Config/DefaultEngine.ini b/Config/DefaultEngine.ini index 66ef5ba..c710153 100644 --- a/Config/DefaultEngine.ini +++ b/Config/DefaultEngine.ini @@ -103,6 +103,7 @@ ManualIPAddress= [CoreRedirects] +ClassRedirects=(OldName="/Script/M4_CPP.M4_CentipedeController",NewName="/Script/M4_CPP.M4_CentipedeController") ++FunctionRedirects=(OldName="/Script/M4_CPP.M4_CentipedeController.OnSegmentDestroyed",NewName="/Script/M4_CPP.M4_CentipedeController.OnSegmentHit") [/Script/Engine.CollisionProfile] -Profiles=(Name="NoCollision",CollisionEnabled=NoCollision,ObjectTypeName="WorldStatic",CustomResponses=((Channel="Visibility",Response=ECR_Ignore),(Channel="Camera",Response=ECR_Ignore)),HelpMessage="No collision",bCanModify=False) diff --git a/Source/M4_CPP/private/M4_CentipedeController.cpp b/Source/M4_CPP/private/M4_CentipedeController.cpp index 39884d4..5663b49 100644 --- a/Source/M4_CPP/private/M4_CentipedeController.cpp +++ b/Source/M4_CPP/private/M4_CentipedeController.cpp @@ -90,11 +90,11 @@ TStatId UM4_CentipedeController::GetStatId() const void UM4_CentipedeController::SpawnCentipede() { - AM4_Gamemode* GM = Cast(GetWorld()->GetAuthGameMode()); - if (!GM) return; + GM_REF_PTR = Cast(GetWorld()->GetAuthGameMode()); + if (!GM_REF_PTR) return; FVector SpawnLocation = FVector( - GM->MushroomSpawnBounds.Max.X, + GM_REF_PTR->MushroomSpawnBounds.Max.X, 0.f, 0.f ); @@ -201,13 +201,16 @@ bool UM4_CentipedeController::CheckCollision(AM4_CentipedeBody* Segment, FVector FVector CurrentPos = Segment->GetActorLocation(); FVector NextPos = CurrentPos + FVector(0.f, Direction.Y * CellSize, 0.f); - AM4_Gamemode* GM = Cast(GetWorld()->GetAuthGameMode()); - if (!GM) return false; + if (!GM_REF_PTR) + { + GM_REF_PTR = Cast(GetWorld()->GetAuthGameMode()); + if (!GM_REF_PTR) return false; + } - const float LeftBound = GM->MushroomSpawnBounds.Min.Y; - const float RightBound = GM->MushroomSpawnBounds.Max.Y; - const float MinRows = GM->MushroomSpawnBounds.Min.X; - const float MaxRows = GM->MushroomSpawnBounds.Max.X; + const float LeftBound = GM_REF_PTR->MushroomSpawnBounds.Min.Y; + const float RightBound = GM_REF_PTR->MushroomSpawnBounds.Max.Y; + const float MinRows = GM_REF_PTR->MushroomSpawnBounds.Min.X; + const float MaxRows = GM_REF_PTR->MushroomSpawnBounds.Max.X; if (NextPos.Y <= LeftBound || NextPos.Y >= RightBound) { @@ -290,7 +293,7 @@ void UM4_CentipedeController::UpdateHeadStatus() } } -void UM4_CentipedeController::OnSegmentDestroyed(AM4_CentipedeBody* DestroyedSegment) +void UM4_CentipedeController::OnSegmentHit(AM4_CentipedeBody* DestroyedSegment) { if (!DestroyedSegment) return; @@ -312,7 +315,26 @@ void UM4_CentipedeController::OnSegmentDestroyed(AM4_CentipedeBody* DestroyedSeg BodySegments[SegmentIndex - 1]->NextBody = nullptr; } - BodySegments.RemoveAt(SegmentIndex); - SegmentHistory.RemoveAt(SegmentIndex); - SegmentDirections.Remove(DestroyedSegment); + // disable collision and hide the destroyed segment + DestroyedSegment->GetStaticMeshComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision); + DestroyedSegment->SetActorHiddenInGame(true); + // Increase player score + if (!GM_REF_PTR) + { + GM_REF_PTR = Cast(GetWorld()->GetAuthGameMode()); + if (!GM_REF_PTR) return; + } + GM_REF_PTR->AddScore(100); + SpawnMushroomsAtSegmentDeathPos(DestroyedSegment->GetActorLocation()); +} + +void UM4_CentipedeController::SpawnMushroomsAtSegmentDeathPos(FVector Location) +{ + FVector Offset = FVector(0.f, FMath::RandRange(-CellSize, CellSize), 0.f); + FVector SpawnLocation = Location + Offset; + GetWorld()->SpawnActor( + AM4_Mushroom::StaticClass(), + SpawnLocation, + FRotator::ZeroRotator + ); } \ No newline at end of file diff --git a/Source/M4_CPP/private/M4_Projectile.cpp b/Source/M4_CPP/private/M4_Projectile.cpp index 586bae8..3f565dc 100644 --- a/Source/M4_CPP/private/M4_Projectile.cpp +++ b/Source/M4_CPP/private/M4_Projectile.cpp @@ -63,19 +63,35 @@ void AM4_Projectile::BeginPlay() 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) + + if (!OtherActor || OtherActor == this || !OtherComp) { - AM4_Mushroom* Mushroom = Cast(OtherActor); - if (Mushroom) - { - UE_LOG(LogTemp, Warning, TEXT("OVERLAP MUSHROOM!")); - - FVector NormalImpulse = FVector::ZeroVector; - Mushroom->OnHit(OverlappedComponent, this, OtherComp, NormalImpulse, SweepResult); - Destroy(); - } + return; } + + AM4_Mushroom* Mushroom = Cast(OtherActor); + if (Mushroom) + { + UE_LOG(LogTemp, Warning, TEXT("OVERLAP MUSHROOM!")); + + FVector NormalImpulse = FVector::ZeroVector; + Mushroom->OnHit(OverlappedComponent, this, OtherComp, NormalImpulse, SweepResult); + Destroy(); + return; + } + + AM4_CentipedeBody* CentipedeBody = Cast(OtherActor); + if (CentipedeBody) + { + UE_LOG(LogTemp, Warning, TEXT("OVERLAP CENTIPEDE BODY!")); + UM4_CentipedeController* CentipedeController = GetWorld()->GetSubsystem(); + if (CentipedeController) + { + CentipedeController->OnSegmentHit(CentipedeBody); + } + Destroy(); + } + } void AM4_Projectile::FireInDir(const FVector& ShootDir) diff --git a/Source/M4_CPP/public/M4_CentipedeController.h b/Source/M4_CPP/public/M4_CentipedeController.h index 53a98ca..31daf85 100644 --- a/Source/M4_CPP/public/M4_CentipedeController.h +++ b/Source/M4_CPP/public/M4_CentipedeController.h @@ -31,7 +31,7 @@ public: float MoveInterval = 0.1f; UFUNCTION(BlueprintCallable, Category = "Centipede") - void OnSegmentDestroyed(AM4_CentipedeBody* DestroyedSegment); + void OnSegmentHit(AM4_CentipedeBody* DestroyedSegment); private: UPROPERTY() @@ -52,9 +52,13 @@ private: void SpawnCentipede(); void UpdateHeadStatus(); + void SpawnMushroomsAtSegmentDeathPos(FVector Location); bool CheckCollision(AM4_CentipedeBody* Segment, FVector2D Direction); FVector2D GetSegmentDirection(AM4_CentipedeBody* Segment); void SetSegmentDirection(AM4_CentipedeBody* Segment, FVector2D Direction); + + UPROPERTY() + class AM4_Gamemode* GM_REF_PTR; };