Attempt to make two spawn points and assign connecting players to them

pull/5/head
eyakm1 2 years ago
parent f2ed196c3e
commit c86a21fa67

@ -2,44 +2,39 @@
#include "MyGameMode.h"
#include "Kismet/GameplayStatics.h"
#include "Trooper.h"
AMyGameMode::AMyGameMode() : Super() {
UE_LOG(LogTemp, Warning, TEXT("GameMode Constructor"));
PlayerControllerClass = AMyPlayerController::StaticClass();
UE_LOG(LogTemp, Warning, TEXT("GameMode Constructor"));
GameStateClass = AMyGameState::StaticClass();
PlayerControllerClass = AMyPlayerController::StaticClass();
DefaultPawnClass = AMyPawn::StaticClass();
}
AActor *AMyGameMode::ChoosePlayerStart(AController *Player) {
InitializeSpawnPointsIfNeeded();
return *SpawnPoints.Find(GetNumPlayers());
}
void AMyGameMode::InitializeSpawnPointsIfNeeded() {
if (SpawnPoints.Num() != 0) {
return;
}
for (TActorIterator <AMyPlayerStart> PlayerStartIterator(GetWorld()); PlayerStartIterator; ++PlayerStartIterator) {
SpawnPoints[PlayerStartIterator->GetPlayerIndex()] = *PlayerStartIterator;
}
}
void AMyGameMode::BeginPlay() {
Super::BeginPlay();
ATrooper::InitNumberOfTroopersForId();
UE_LOG(LogTemp, Warning, TEXT("GameMode BeginPlay"));
if (GetWorld()->GetMapName().Contains("BattleFieldMap")) {
UE_LOG(LogTemp, Warning, TEXT("Player Logined"));
StartGame();
}
Super::BeginPlay();
}
void AMyGameMode::StartGame() {
FVector Location(2000.0f, -1000.0f, 0.0f);
FRotator Rotation(0.0f, 180.0f, 0.0f);
FActorSpawnParameters SpawnInfo;
for (int i = 0; i < 5; ++i) {
AActor *spawned = GetWorld()->SpawnActor<ATrooper>(Location, Rotation, SpawnInfo);
dynamic_cast<ATrooper*>(spawned)->InitTrooper(Location, true);
Location += { 0.f, 500.f, 0.0f };
}
Location = { -2000.0f, -1000.0f, 0.0f };
Rotation = { 0.0f, 0.0f, 0.0f };
for (int i = 0; i < 5; ++i) {
AActor* spawned = GetWorld()->SpawnActor<ATrooper>(Location, Rotation, SpawnInfo);
dynamic_cast<ATrooper*>(spawned)->InitTrooper(Location, false);
Location += { 0.f, 500.f, 0.0f };
}
GetPlayerController()->StartTurn();
GetPlayerController()->StartTurn();
}
AMyPlayerController *AMyGameMode::GetPlayerController() {
return dynamic_cast<AMyPlayerController *>(
UGameplayStatics::GetPlayerController(GetWorld(), 0)
);
return dynamic_cast<AMyPlayerController *>(
UGameplayStatics::GetPlayerController(GetWorld(), 0)
);
}

@ -3,7 +3,11 @@
#pragma once
#include "CoreMinimal.h"
#include "EngineUtils.h"
#include "MyPlayerController.h"
#include "MyGameState.h"
#include "MyPawn.h"
#include "MyPlayerStart.h"
#include "GameFramework/GameMode.h"
#include "MyGameMode.generated.h"
@ -13,14 +17,19 @@ UCLASS()
class TURNBASEDTUTORIAL_API AMyGameMode : public AGameMode
{
GENERATED_BODY()
public:
AMyGameMode();
void BeginPlay() override;
AActor *ChoosePlayerStart(AController * Player);
private:
void StartGame();
void InitializeSpawnPointsIfNeeded();
AMyPlayerController *GetPlayerController();
TMap<uint8, AMyPlayerStart*> SpawnPoints;
};

@ -0,0 +1,8 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyGameState.h"

@ -0,0 +1,20 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameState.h"
#include "MyGameState.generated.h"
/**
*
*/
UCLASS()
class TURNBASEDTUTORIAL_API AMyGameState : public AGameState
{
GENERATED_BODY()
};

@ -0,0 +1,35 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
// Sets default values
AMyPawn::AMyPawn()
{
// 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;
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}

@ -0,0 +1,31 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"
UCLASS()
class TURNBASEDTUTORIAL_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};

@ -0,0 +1,10 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPlayerStart.h"
uint8 AMyPlayerStart::GetPlayerIndex() const {
return PlayerIndex;
}

@ -0,0 +1,24 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerStart.h"
#include "MyPlayerStart.generated.h"
/**
*
*/
UCLASS()
class TURNBASEDTUTORIAL_API AMyPlayerStart : public APlayerStart
{
GENERATED_BODY()
public:
uint8 GetPlayerIndex() const;
protected:
private:
UPROPERTY(EditAnywhere, Category="Spawn Info")
uint8 PlayerIndex=0;
};
Loading…
Cancel
Save