-
Notifications
You must be signed in to change notification settings - Fork 921
Open
Labels
Description
// AimTrainerGameMode.cpp
// All-in-One Unreal C++ Aim Trainer GameMode
#include "Engine/StaticMeshActor.h"
#include "GameFramework/GameModeBase.h"
#include "GameFramework/Character.h"
#include "GameFramework/PlayerController.h"
#include "Kismet/GameplayStatics.h"
#include "UObject/ConstructorHelpers.h"
#include "Components/StaticMeshComponent.h"
#include "Engine/World.h"
#include "Engine/Engine.h"
#include "UObject/ObjectMacros.h"
#include "UObject/UObjectGlobals.h"
#include "UObject/ScriptMacros.h"
#include "Templates/SubclassOf.h"
class AAimTrainerGameMode : public AGameModeBase
{
public:
GENERATED_BODY()
AAimTrainerGameMode()
{
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereMesh(TEXT("/Engine/BasicShapes/Sphere"));
if (SphereMesh.Succeeded())
{
TargetMesh = SphereMesh.Object;
}
}
protected:
virtual void BeginPlay() override
{
Super::BeginPlay();
// Make player invisible and lock camera
ACharacter* Char = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
if (Char)
{
Char->GetMesh()->SetVisibility(false, true);
Char->GetMesh()->SetHiddenInGame(true);
APlayerController* PC = Cast<APlayerController>(Char->GetController());
if (PC)
{
PC->bShowMouseCursor = true;
PC->SetInputMode(FInputModeGameOnly());
}
}
// Spawn 20 targets
for (int i = 0; i < 20; ++i)
{
SpawnTarget();
}
}
private:
UStaticMesh* TargetMesh = nullptr;
int32 Score = 0;
void SpawnTarget()
{
if (!TargetMesh) return;
FVector ArenaMin(-200.f, -200.f, 100.f);
FVector ArenaMax(200.f, 200.f, 300.f);
FVector Location = FMath::RandPointInBox(FBox(ArenaMin, ArenaMax));
AStaticMeshActor* Target = GetWorld()->SpawnActor<AStaticMeshActor>(AStaticMeshActor::StaticClass(), Location, FRotator::ZeroRotator);
if (Target)
{
UStaticMeshComponent* Mesh = Target->GetStaticMeshComponent();
if (Mesh)
{
Mesh->SetStaticMesh(TargetMesh);
Mesh->SetWorldScale3D(FVector(0.3f));
Mesh->SetMobility(EComponentMobility::Movable);
Mesh->SetSimulatePhysics(false);
Mesh->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
Mesh->SetCollisionResponseToAllChannels(ECR_Ignore);
Mesh->SetCollisionResponseToChannel(ECC_Visibility, ECR_Block);
Mesh->OnClicked.AddDynamic(this, &AAimTrainerGameMode::HandleTargetClick);
}
}
}
UFUNCTION()
void HandleTargetClick(UPrimitiveComponent* TouchedComp, FKey ButtonPressed)
{
if (!TouchedComp) return;
AActor* HitTarget = TouchedComp->GetOwner();
if (HitTarget)
{
HitTarget->Destroy();
}
++Score;
FString Msg = FString::Printf(TEXT("Score: %d"), Score);
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 1.5f, FColor::Green, Msg);
}
}
};
XXOTIC-1XXOTIC-1