Minor - Ajoute l'instanciation des champignons dans un grid - V01.10.00

Instanciation et placement aléatoire des champignons dans une zone définie.
This commit is contained in:
2025-10-16 18:47:10 +02:00
parent 134883273e
commit dc2002fbb1
4 changed files with 121 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "M4_Gamemode.h"
#include "M4_Mushroom.h"
#include "M4_PlayerController.h"
#include "M4_PlayerPawn.h"
@@ -11,10 +12,53 @@ AM4_Gamemode::AM4_Gamemode()
PlayerControllerClass = AM4_PlayerController::StaticClass();
DefaultPawnClass = AM4_PlayerPawn::StaticClass();
MushroomCount = FMath::RandRange(20, 25);
}
void AM4_Gamemode::BeginPlay()
{
Super::BeginPlay();
}
Score = 0;
Lives = 3;
// Get mushroom size from the class
AM4_Mushroom* TempMushroom = GetWorld()->SpawnActor<AM4_Mushroom>(AM4_Mushroom::StaticClass());
if (TempMushroom)
{
FVector MushroomSize = TempMushroom->GetStaticMeshComponent()->Bounds.BoxExtent * 2.0f;
CellSize = FMath::Max(MushroomSize.Y, MushroomSize.Z); // Use largest dimension
TempMushroom->Destroy();
}
// Calculate grid dimensions based on cell size
const int32 GridRows = FMath::FloorToInt(MushroomSpawnBounds.GetSize().X / CellSize);
const int32 GridCols = FMath::FloorToInt(MushroomSpawnBounds.GetSize().Y / CellSize);
// spawn mushrooms in a grid inside the spawn bounds and in random cell
if (MushroomCount > 0)
{
TSet<FIntPoint> OccupiedCells;
for (int32 i = 0; i < MushroomCount; ++i)
{
FIntPoint Cell;
do
{
Cell.X = FMath::RandRange(0, GridRows - 1);
Cell.Y = FMath::RandRange(0, GridCols - 1);
} while (OccupiedCells.Contains(Cell));
OccupiedCells.Add(Cell);
FVector SpawnLocation;
SpawnLocation.Z = MushroomSpawnBounds.Min.X + Cell.X * CellSize + CellSize / 2.0f;
SpawnLocation.Y = MushroomSpawnBounds.Min.Y + Cell.Y * CellSize + CellSize / 2.0f;
SpawnLocation.X = -400.0f;
GetWorld()->SpawnActor<AM4_Mushroom>(AM4_Mushroom::StaticClass(), SpawnLocation, FRotator::ZeroRotator);
}
}
}

View File

@@ -0,0 +1,42 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "M4_Mushroom.h"
#include "M4_CTP_Macros.h"
#include "Kismet/GameplayStatics.h"
#include "M4_Gamemode.h"
// Sets default values
AM4_Mushroom::AM4_Mushroom()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube"));
if (StaticMeshRef.Succeeded())
{
GetStaticMeshComponent()->SetStaticMesh(StaticMeshRef.Object);
}
GetStaticMeshComponent()->SetDefaultCustomPrimitiveDataVector4(0, FVector4(0.0, 0.2, 0.2, 1.0));
GetStaticMeshComponent()->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
GetStaticMeshComponent()->SetGenerateOverlapEvents(true);
// Set as static object
GetStaticMeshComponent()->SetMobility(EComponentMobility::Static);
// Set scale
const FVector2D MushroomScale = FVector2D(0.3f, 0.25f);
GetStaticMeshComponent()->SetRelativeScale3D(FVector(1.f, 0.45f, 0.20f));
// Custom preset for more advanced collision configuration
GetStaticMeshComponent()->SetCollisionProfileName(UCollisionProfile::CustomCollisionProfileName);
GetStaticMeshComponent()->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);
}
void AM4_Mushroom::BeginPlay()
{
Super::BeginPlay();
}

View File

@@ -26,6 +26,12 @@ public:
UPROPERTY(EditAnywhere)
FBox2D Bounds = FBox2D(FVector2D(-400.0f, -750.0f), FVector2D(-200.0f, 750.0f));
/**
* Bounds for spawning mushrooms
*/
UPROPERTY(EditAnywhere)
FBox2D MushroomSpawnBounds = FBox2D(FVector2D(-300.0f, -750.0f), FVector2D(550.0f, 750.0f));
/**
* Add to the player's score
* @param Amount
@@ -52,8 +58,15 @@ public:
*/
UFUNCTION(BlueprintCallable)
int GetLives() const { return Lives; }
private:
int Score = 0;
int Lives = 3;
// default mushroom count
int MushroomCount;
float CellSize = 50.f;
};

View File

@@ -0,0 +1,21 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Engine/StaticMeshActor.h"
#include "GameFramework/Pawn.h"
#include "M4_Mushroom.generated.h"
UCLASS()
class M4_CPP_API AM4_Mushroom : public AStaticMeshActor
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AM4_Mushroom();
protected:
virtual void BeginPlay() override;
};