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") +ActiveGameNameRedirects=(OldGameName="/Script/TP_Blank",NewGameName="/Script/TurnBasedTutorial")
+ActiveClassRedirects=(OldClassName="TP_BlankGameModeBase",NewClassName="TurnBasedTutorialGameModeBase") +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(); 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) AActor* AMyGameMode::ChoosePlayerStart_Implementation(AController* Player)
{ {
UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart %d"), GetNumPlayers()); UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart %d"), GetNumPlayers());
InitializeSpawnPointsIfNeeded(Player); InitializeSpawnPointsIfNeeded(Player);
auto ptr = *SpawnPoints.Find(GetNumPlayers()); const auto CurrentPlayerStart = *SpawnPoints.Find(GetNumPlayers());
UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart end %d"), ptr->GetPlayerIndex()); UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart end %d"), CurrentPlayerStart->GetPlayerIndex());
return ptr; return CurrentPlayerStart;
} }
void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player) void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player)
@ -27,13 +62,13 @@ void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player)
{ {
return; return;
} }
auto World = GetWorld(); const auto World = GetWorld();
for (TActorIterator<AMyPlayerStart> PlayerStartIterator(GetWorld()); PlayerStartIterator; ++PlayerStartIterator) for (TActorIterator<AMyPlayerStart> PlayerStartIterator(GetWorld()); PlayerStartIterator; ++PlayerStartIterator)
{ {
auto PlayerStart = *PlayerStartIterator; const auto PlayerStart = *PlayerStartIterator;
UClass* PawnClass = GetDefaultPawnClassForController(Player); const UClass* PawnClass = GetDefaultPawnClassForController(Player);
APawn* PawnToFit = PawnClass ? PawnClass->GetDefaultObject<APawn>() : nullptr; const APawn* PawnToFit = PawnClass ? PawnClass->GetDefaultObject<APawn>() : nullptr;
FVector ActorLocation = PlayerStart->GetActorLocation(); const FVector ActorLocation = PlayerStart->GetActorLocation();
const FRotator ActorRotation = PlayerStart->GetActorRotation(); const FRotator ActorRotation = PlayerStart->GetActorRotation();
if (!World->EncroachingBlockingGeometry(PawnToFit, ActorLocation, ActorRotation)) 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 AActor* ChoosePlayerStart_Implementation(AController* Player) override;
virtual void BeginPlay() override;
private: private:
void InitializeSpawnPointsIfNeeded(AController* Player); void InitializeSpawnPointsIfNeeded(AController* Player);
void InitializeBattleField() const;
TMap<uint8, AMyPlayerStart*> SpawnPoints; TMap<uint8, AMyPlayerStart*> SpawnPoints;
AMyPlayerController* GetPlayerController() const;
}; };

@ -4,32 +4,32 @@
#include "MyPawn.h" #include "MyPawn.h"
// // Sets default values // Sets default values
// AMyPawn::AMyPawn() AMyPawn::AMyPawn()
// { {
// // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it. // 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; PrimaryActorTick.bCanEverTick = true;
//
// } }
//
// // Called when the game starts or when spawned // Called when the game starts or when spawned
// void AMyPawn::BeginPlay() void AMyPawn::BeginPlay()
// { {
// Super::BeginPlay(); Super::BeginPlay();
//
// } }
//
// // Called every frame // Called every frame
// void AMyPawn::Tick(float DeltaTime) void AMyPawn::Tick(float DeltaTime)
// { {
// Super::Tick(DeltaTime); Super::Tick(DeltaTime);
//
// } }
//
// // Called to bind functionality to input // Called to bind functionality to input
// void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
// { {
// Super::SetupPlayerInputComponent(PlayerInputComponent); Super::SetupPlayerInputComponent(PlayerInputComponent);
//
// } }
//

@ -11,21 +11,21 @@ class TURNBASEDTUTORIAL_API AMyPawn : public APawn
{ {
GENERATED_BODY() GENERATED_BODY()
// public: public:
// // Sets default values for this pawn's properties // Sets default values for this pawn's properties
// AMyPawn(); AMyPawn();
//
// protected: protected:
// // Called when the game starts or when spawned // Called when the game starts or when spawned
// virtual void BeginPlay() override; virtual void BeginPlay() override;
//
// public: public:
// // Called every frame // Called every frame
// virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;
//
// // Called to bind functionality to input // Called to bind functionality to input
// virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
//
//
}; };

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

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

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

@ -1,4 +1,3 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
@ -16,20 +15,20 @@ public:
ATrooper(); ATrooper();
protected: protected:
static int NumberOfTroopersForId; static uint8 NumberOfTroopersForId;
virtual void BeginPlay() override; virtual void BeginPlay() override;
virtual void Tick(float deltaTime) override; virtual void Tick(float const DeltaTime) override;
UPROPERTY() UPROPERTY()
FVector Position; FVector Position;
UPROPERTY() UPROPERTY()
bool OnPlayersSide; bool bOnPlayersSide;
UPROPERTY() UPROPERTY()
int Id; uint8 Id;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly) UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UStaticMeshComponent* Mesh; UStaticMeshComponent* Mesh;
@ -37,20 +36,20 @@ protected:
UPROPERTY() UPROPERTY()
float Speed = 300.0f; float Speed = 300.0f;
bool IsMoving = false; bool bIsMoving = false;
FVector MoveToVector; FVector MoveToVector;
public: public:
void MoveTrooper(FVector newPos); void MoveTrooper(FVector const NewPos);
static void InitNumberOfTroopersForId(); 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