Enable spawning of troopers

pull/5/head
eyakm1 2 years ago
parent d110245ba7
commit 80f821dfae

@ -26,3 +26,6 @@ AppliedDefaultGraphicsPerformance=Maximum
+ActiveGameNameRedirects=(OldGameName="/Script/TP_Blank",NewGameName="/Script/TurnBasedTutorial")
+ActiveClassRedirects=(OldClassName="TP_BlankGameModeBase",NewClassName="TurnBasedTutorialGameModeBase")
[CoreRedirects]
+PropertyRedirects=(OldName="/Script/TurnBasedTutorial.Trooper.OnPlayersSide",NewName="/Script/TurnBasedTutorial.Trooper.bOnPlayersSide")

Binary file not shown.

Binary file not shown.

@ -12,13 +12,48 @@ AMyGameMode::AMyGameMode() : Super()
DefaultPawnClass = AMyPawn::StaticClass();
}
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"));
InitializeBattleField();
GetPlayerController()->StartTurn();
}
}
void AMyGameMode::InitializeBattleField() const
{
FVector Location(2000.0f, -1000.0f, 0.0f);
FRotator Rotation(0.0f, 180.0f, 0.0f);
FActorSpawnParameters const 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};
}
}
AActor* AMyGameMode::ChoosePlayerStart_Implementation(AController* Player)
{
UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart %d"), GetNumPlayers());
InitializeSpawnPointsIfNeeded(Player);
auto ptr = *SpawnPoints.Find(GetNumPlayers());
UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart end %d"), ptr->GetPlayerIndex());
return ptr;
const auto CurrentPlayerStart = *SpawnPoints.Find(GetNumPlayers());
UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart end %d"), CurrentPlayerStart->GetPlayerIndex());
return CurrentPlayerStart;
}
void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player)
@ -27,13 +62,13 @@ void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player)
{
return;
}
auto World = GetWorld();
const auto World = GetWorld();
for (TActorIterator<AMyPlayerStart> PlayerStartIterator(GetWorld()); PlayerStartIterator; ++PlayerStartIterator)
{
auto PlayerStart = *PlayerStartIterator;
UClass* PawnClass = GetDefaultPawnClassForController(Player);
APawn* PawnToFit = PawnClass ? PawnClass->GetDefaultObject<APawn>() : nullptr;
FVector ActorLocation = PlayerStart->GetActorLocation();
const auto PlayerStart = *PlayerStartIterator;
const UClass* PawnClass = GetDefaultPawnClassForController(Player);
const APawn* PawnToFit = PawnClass ? PawnClass->GetDefaultObject<APawn>() : nullptr;
const FVector ActorLocation = PlayerStart->GetActorLocation();
const FRotator ActorRotation = PlayerStart->GetActorRotation();
if (!World->EncroachingBlockingGeometry(PawnToFit, ActorLocation, ActorRotation))
{
@ -42,3 +77,9 @@ void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player)
}
}
}
AMyPlayerController* AMyGameMode::GetPlayerController() const
{
return dynamic_cast<AMyPlayerController*>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
}

@ -22,8 +22,14 @@ public:
virtual AActor* ChoosePlayerStart_Implementation(AController* Player) override;
virtual void BeginPlay() override;
private:
void InitializeSpawnPointsIfNeeded(AController *Player);
void InitializeSpawnPointsIfNeeded(AController* Player);
void InitializeBattleField() const;
TMap<uint8, AMyPlayerStart*> SpawnPoints;
AMyPlayerController* GetPlayerController() const;
};

@ -4,32 +4,32 @@
#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);
//
// }
//
// 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);
}

@ -11,21 +11,21 @@ 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;
//
//
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;
};

@ -2,62 +2,76 @@
#include "MyPlayerController.h"
// AMyPlayerController::AMyPlayerController() : Super(), IsMyTurn(false), SelectedTrooper(nullptr) {
// UE_LOG(LogTemp, Warning, TEXT("Player controller created"));
// }
//
// void AMyPlayerController::SetupInputComponent() {
// Super::SetupInputComponent();
// InputComponent->BindAction("MyAction", IE_Pressed, this,
// &AMyPlayerController::OnLeftMouseClick);
// }
//
// void AMyPlayerController::StartTurn() {
// IsMyTurn = true;
// UE_LOG(LogTemp, Warning, TEXT("Your turn"));
// }
//
// void AMyPlayerController::EndTurn() {
// IsMyTurn = false;
// UE_LOG(LogTemp, Warning, TEXT("Not your turn"));
// }
//
// void AMyPlayerController::SetTrooperIsMoving(bool isMoving) {
// IsThereTrooperMoving = isMoving;
// }
//
// void AMyPlayerController::OnLeftMouseClick() {
// if (IsThereTrooperMoving) {
// return;
// }
// UE_LOG(LogTemp, Warning, TEXT("Mouse clicked"));
// FHitResult HitResult;
// bool IsHitResult = GetHitResultUnderCursorByChannel(TraceTypeQuery1, false, HitResult);
// if (IsHitResult) {
// AActor *actor = HitResult.Actor.Get();
// if (actor->ActorHasTag(FName("Trooper"))) {
// ATrooper* trooper = dynamic_cast<ATrooper*>(actor);
// if (trooper != nullptr && trooper != SelectedTrooper) {
// if (trooper->IsOnPlayersSide()) {
// UE_LOG(LogTemp, Warning, TEXT("Hitted trooper id: %d, on our side"),
// trooper->GetId());
// SelectedTrooper = trooper;
// }
// else {
// UE_LOG(LogTemp, Warning, TEXT("Hitted trooper id: %d, enemy"),
// trooper->GetId());
// UE_LOG(LogTemp, Warning, TEXT("Trooper #%d hit enemy trooper #%d"),
// SelectedTrooper->GetId(), trooper->GetId());
// }
// }
// }
// else if (actor->ActorHasTag(FName("Floor"))) {
// UE_LOG(LogTemp, Warning, TEXT("Hitted floor: %f, %f, %f"), HitResult.Location.X,
// HitResult.Location.Y, HitResult.Location.Z);
// if (SelectedTrooper != nullptr) {
// SelectedTrooper->MoveTrooper(HitResult.Location);
// IsThereTrooperMoving = true;
// }
// }
// }
// }
AMyPlayerController::AMyPlayerController() : Super(), IsMyTurn(false), SelectedTrooper(nullptr)
{
UE_LOG(LogTemp, Warning, TEXT("Player controller created"));
}
void AMyPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
InputComponent->BindAction("MyAction", IE_Pressed, this,
&AMyPlayerController::OnLeftMouseClick);
}
void AMyPlayerController::StartTurn()
{
IsMyTurn = true;
UE_LOG(LogTemp, Warning, TEXT("Your turn"));
}
void AMyPlayerController::EndTurn()
{
IsMyTurn = false;
UE_LOG(LogTemp, Warning, TEXT("Not your turn"));
}
void AMyPlayerController::SetTrooperIsMoving(bool isMoving)
{
IsThereTrooperMoving = isMoving;
}
void AMyPlayerController::OnLeftMouseClick()
{
if (IsThereTrooperMoving)
{
return;
}
UE_LOG(LogTemp, Warning, TEXT("Mouse clicked"));
FHitResult HitResult;
bool const IsHitResult = GetHitResultUnderCursorByChannel(TraceTypeQuery1, false, HitResult);
if (IsHitResult)
{
AActor* Actor = HitResult.Actor.Get();
if (Actor->ActorHasTag(FName("Trooper")))
{
ATrooper* Trooper = dynamic_cast<ATrooper*>(Actor);
if (Trooper != nullptr && Trooper != SelectedTrooper)
{
if (Trooper->IsOnPlayersSide())
{
UE_LOG(LogTemp, Warning, TEXT("Hitted trooper id: %d, on our side"),
Trooper->GetId());
SelectedTrooper = Trooper;
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Hitted trooper id: %d, enemy"),
Trooper->GetId());
UE_LOG(LogTemp, Warning, TEXT("Trooper #%d hit enemy trooper #%d"),
SelectedTrooper->GetId(), Trooper->GetId());
}
}
}
else if (Actor->ActorHasTag(FName("Floor")))
{
UE_LOG(LogTemp, Warning, TEXT("Hitted floor: %f, %f, %f"), HitResult.Location.X,
HitResult.Location.Y, HitResult.Location.Z);
if (SelectedTrooper != nullptr)
{
SelectedTrooper->MoveTrooper(HitResult.Location);
IsThereTrooperMoving = true;
}
}
}
}

@ -13,25 +13,24 @@ UCLASS()
class TURNBASEDTUTORIAL_API AMyPlayerController : public APlayerController
{
GENERATED_BODY()
public:
// AMyPlayerController();
//
// void StartTurn();
//
// void EndTurn();
//
// virtual void SetupInputComponent() override;
//
// void SetTrooperIsMoving(bool isMoving);
AMyPlayerController();
void StartTurn();
void EndTurn();
virtual void SetupInputComponent() override;
void SetTrooperIsMoving(bool isMoving);
private:
// bool IsMyTurn;
//
// bool IsThereTrooperMoving = false;
//
// ATrooper* SelectedTrooper;
//
// void OnLeftMouseClick();
bool IsMyTurn;
bool IsThereTrooperMoving = false;
ATrooper* SelectedTrooper;
void OnLeftMouseClick();
};

@ -3,7 +3,8 @@
#include "MyPlayerController.h"
// Sets default values
ATrooper::ATrooper() {
ATrooper::ATrooper()
{
PrimaryActorTick.bCanEverTick = true;
Tags.Add(FName("Trooper"));
Id = NumberOfTroopersForId++;
@ -25,49 +26,59 @@ void ATrooper::BeginPlay()
Super::BeginPlay();
}
void ATrooper::Tick(float deltaTime) {
// if (IsMoving) {
// FVector vector = (MoveToVector - Position);
// vector.Normalize();
// vector *= (Speed * deltaTime);
// if (vector.Size() >= (MoveToVector - Position).Size()) {
// Position = MoveToVector;
// IsMoving = false;
// dynamic_cast<AMyPlayerController*>(
// UGameplayStatics::GetPlayerController(GetWorld(), 0)
// )->SetTrooperIsMoving(false);
// }
// else {
// Position += vector;
// }
// SetActorLocation(Position);
// }
void ATrooper::Tick(float const DeltaTime)
{
if (bIsMoving)
{
FVector vector = (MoveToVector - Position);
vector.Normalize();
vector *= (Speed * DeltaTime);
if (vector.Size() >= (MoveToVector - Position).Size())
{
Position = MoveToVector;
bIsMoving = false;
dynamic_cast<AMyPlayerController*>(
UGameplayStatics::GetPlayerController(GetWorld(), 0)
)->SetTrooperIsMoving(false);
}
else
{
Position += vector;
}
SetActorLocation(Position);
}
}
void ATrooper::MoveTrooper(FVector newPos) {
MoveToVector = newPos;
IsMoving = true;
void ATrooper::MoveTrooper(FVector const NewPos)
{
MoveToVector = NewPos;
bIsMoving = true;
}
int ATrooper::NumberOfTroopersForId = 0;
uint8 ATrooper::NumberOfTroopersForId = 0;
void ATrooper::InitNumberOfTroopersForId() {
void ATrooper::InitNumberOfTroopersForId()
{
NumberOfTroopersForId = 0;
}
FVector ATrooper::GetPosition() {
FVector ATrooper::GetPosition() const
{
return Position;
}
bool ATrooper::IsOnPlayersSide() {
return OnPlayersSide;
bool ATrooper::IsOnPlayersSide() const
{
return bOnPlayersSide;
}
int ATrooper::GetId() {
uint8 ATrooper::GetId() const
{
return Id;
}
void ATrooper::InitTrooper(FVector position, bool onPlayersSide) {
Position = position;
OnPlayersSide = onPlayersSide;
void ATrooper::InitTrooper(FVector const NewPosition, bool const bIsOnPlayersSide)
{
Position = NewPosition;
bOnPlayersSide = bIsOnPlayersSide;
}

@ -1,4 +1,3 @@
#pragma once
#include "CoreMinimal.h"
@ -11,25 +10,25 @@ class TURNBASEDTUTORIAL_API ATrooper : public AActor
{
GENERATED_BODY()
public:
public:
// Sets default values for this actor's properties
ATrooper();
protected:
static int NumberOfTroopersForId;
static uint8 NumberOfTroopersForId;
virtual void BeginPlay() override;
virtual void Tick(float deltaTime) override;
virtual void Tick(float const DeltaTime) override;
UPROPERTY()
FVector Position;
UPROPERTY()
bool OnPlayersSide;
bool bOnPlayersSide;
UPROPERTY()
int Id;
uint8 Id;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UStaticMeshComponent* Mesh;
@ -37,20 +36,20 @@ protected:
UPROPERTY()
float Speed = 300.0f;
bool IsMoving = false;
bool bIsMoving = false;
FVector MoveToVector;
public:
void MoveTrooper(FVector newPos);
void MoveTrooper(FVector const NewPos);
static void InitNumberOfTroopersForId();
FVector GetPosition();
FVector GetPosition() const;
bool IsOnPlayersSide();
bool IsOnPlayersSide() const;
int GetId();
uint8 GetId() const;
void InitTrooper(FVector position, bool onPlayersSide);
void InitTrooper(FVector const NewPosition, bool const bIsOnPlayersSide);
};

Loading…
Cancel
Save