From fc97901cdc39c3636491f9e1dff7e00a376f7376 Mon Sep 17 00:00:00 2001 From: CatChow0 Date: Thu, 30 Oct 2025 23:44:26 +0100 Subject: [PATCH] Minor - Change le comportement du mille-pattes - V2.2.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Améliore la logique de collision du mille-pattes avec les bords et les champignons. Le mille-pattes change de direction lorsqu'il atteint les limites de la zone de jeu ou rencontre un champignon. --- .../M4_CPP/private/M4_CentipedeController.cpp | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/Source/M4_CPP/private/M4_CentipedeController.cpp b/Source/M4_CPP/private/M4_CentipedeController.cpp index 96b237f..39884d4 100644 --- a/Source/M4_CPP/private/M4_CentipedeController.cpp +++ b/Source/M4_CPP/private/M4_CentipedeController.cpp @@ -1,6 +1,8 @@ #include "M4_CentipedeController.h" #include "M4_LOG.h" #include "M4_Gamemode.h" +#include "M4_Mushroom.h" +#include "Kismet/GameplayStatics.h" UM4_CentipedeController::UM4_CentipedeController() { @@ -143,9 +145,9 @@ void UM4_CentipedeController::Tick(float DeltaTime) FVector CurrentPos = Head->GetActorLocation(); FVector2D SegmentDirection = GetSegmentDirection(Head); - bool bShouldDescend = CheckCollision(Head, SegmentDirection); + bool bRowChanged = CheckCollision(Head, SegmentDirection); - if (bShouldDescend) + if (bRowChanged) { FVector NewPos = CurrentPos - FVector(0.f, 0.f, CellSize); Head->SetActorLocation(NewPos); @@ -204,11 +206,53 @@ bool UM4_CentipedeController::CheckCollision(AM4_CentipedeBody* Segment, FVector 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; if (NextPos.Y <= LeftBound || NextPos.Y >= RightBound) { return true; } + + if (CurrentPos.Z > MinRows && NextPos.Z <= MinRows) + { + // On inverse la direction de descente pour remonter + FVector2D CurrentDirection = GetSegmentDirection(Segment); + CurrentDirection.Y *= -1.f; + SetSegmentDirection(Segment, CurrentDirection); + return true; // Collision détectée, le centipede descendra à la prochaine itération + } + + // Vérification si on atteint la limite haute + if (CurrentPos.Z < MaxRows && NextPos.Z >= MaxRows) + { + // On inverse la direction de montée pour descendre + FVector2D CurrentDirection = GetSegmentDirection(Segment); + CurrentDirection.Y *= -1.f; + SetSegmentDirection(Segment, CurrentDirection); + return true; // Collision détectée, le centipede remontera à la prochaine itération + } + + int32 NextGridZ = FMath::RoundToInt(NextPos.Z / CellSize); + int32 NextGridY = FMath::RoundToInt(NextPos.Y / CellSize); + + TArray FoundMushrooms; + UGameplayStatics::GetAllActorsOfClass(GetWorld(), AM4_Mushroom::StaticClass(), FoundMushrooms); + + for (AActor* Actor : FoundMushrooms) + { + FVector MushroomPos = Actor->GetActorLocation(); + + // Conversion de la position du mushroom en coordonnées de grille (Z, Y) + int32 MushroomGridZ = FMath::RoundToInt(MushroomPos.Z / CellSize); + int32 MushroomGridY = FMath::RoundToInt(MushroomPos.Y / CellSize); + + // Vérification si c'est la même cellule de grille + if (NextGridZ == MushroomGridZ && NextGridY == MushroomGridY) + { + return true; + } + } return false; }