diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..aa76900 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +UnrealToolbox.exe filter=lfs diff=lfs merge=lfs -text \ No newline at end of file diff --git a/Content/CTP/01_Level/L_Default.umap b/Content/CTP/01_Level/L_Default.umap index 183c427..c957fa7 100644 Binary files a/Content/CTP/01_Level/L_Default.umap and b/Content/CTP/01_Level/L_Default.umap differ diff --git a/Source/M4_CPP/private/M4_Gamemode.cpp b/Source/M4_CPP/private/M4_Gamemode.cpp index ebfccda..8a98972 100644 --- a/Source/M4_CPP/private/M4_Gamemode.cpp +++ b/Source/M4_CPP/private/M4_Gamemode.cpp @@ -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(); -} \ No newline at end of file + + Score = 0; + Lives = 3; + + // Get mushroom size from the class + AM4_Mushroom* TempMushroom = GetWorld()->SpawnActor(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 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::StaticClass(), SpawnLocation, FRotator::ZeroRotator); + } + } +} diff --git a/Source/M4_CPP/private/M4_Mushroom.cpp b/Source/M4_CPP/private/M4_Mushroom.cpp new file mode 100644 index 0000000..234932a --- /dev/null +++ b/Source/M4_CPP/private/M4_Mushroom.cpp @@ -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 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(); +} \ No newline at end of file diff --git a/Source/M4_CPP/public/M4_Gamemode.h b/Source/M4_CPP/public/M4_Gamemode.h index a81d4fb..ef577a8 100644 --- a/Source/M4_CPP/public/M4_Gamemode.h +++ b/Source/M4_CPP/public/M4_Gamemode.h @@ -20,6 +20,53 @@ public: virtual void BeginPlay() override; + /** + * The movement bounds for the player pawn + */ 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 + */ + UFUNCTION(BlueprintCallable) + void AddScore(int Amount) { Score += Amount; } + + /** + * Get the player's current score + * @return Current score + */ + UFUNCTION(BlueprintCallable) + int GetScore() const { return Score; } + + /** + * Decrease the player's lives by 1, but not below 0 + */ + UFUNCTION(BlueprintCallable) + void LoseLife() { Lives = FMath::Max(0, Lives - 1); } + + /** + * Get the player's current lives + * @return Current lives + */ + UFUNCTION(BlueprintCallable) + int GetLives() const { return Lives; } + + +private: + int Score = 0; + int Lives = 3; + + // default mushroom count + int MushroomCount; + float CellSize = 50.f; + + }; diff --git a/Source/M4_CPP/public/M4_Mushroom.h b/Source/M4_CPP/public/M4_Mushroom.h new file mode 100644 index 0000000..7dbe9ce --- /dev/null +++ b/Source/M4_CPP/public/M4_Mushroom.h @@ -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; +};