added different troopers, made camera spectate, changed floor material, made some other small changes + clang-format

pull/5/head
m4xxx1m 2 years ago
parent 985612b4de
commit 114f9b28e1

@ -5,152 +5,159 @@
#include "MyPawn.h" #include "MyPawn.h"
#include "MyGameState.h" #include "MyGameState.h"
AMyGameMode::AMyGameMode() : Super() AMyGameMode::AMyGameMode()
{ : Super() {
UE_LOG(LogTemp, Warning, TEXT("GameMode Constructor")); UE_LOG(LogTemp, Warning, TEXT("GameMode Constructor"));
GameStateClass = AMyGameState::StaticClass(); GameStateClass = AMyGameState::StaticClass();
PlayerControllerClass = AMyPlayerController::StaticClass(); PlayerControllerClass = AMyPlayerController::StaticClass();
DefaultPawnClass = AMyPawn::StaticClass(); DefaultPawnClass = AMyPawn::StaticClass();
} }
void AMyGameMode::BeginPlay() void AMyGameMode::BeginPlay() {
{ Super::BeginPlay();
Super::BeginPlay();
} }
void AMyGameMode::InitializeBattleField() const void AMyGameMode::InitializeBattleField() const {
{ UE_LOG(LogTemp, Warning, TEXT("InitializeBattleField"));
UE_LOG(LogTemp, Warning, TEXT("InitializeBattleField")); FVector Location(2000.0f, -1000.0f, 0.0f);
FVector Location(2000.0f, -1000.0f, 0.0f); FRotator Rotation(0.0f, 180.0f, 0.0f);
FRotator Rotation(0.0f, 180.0f, 0.0f);
uint8 TrooperCount = 0;
uint8 TrooperCount = 0;
TArray<const TCHAR *> bpPaths{
for (int i = 0; i < 5; ++i) TEXT(
{ "Blueprint'/Game/Troopers/TrooperSkeletonMelee.TrooperSkeletonMelee_C'"
FTransform SpawnLocationAndRotation(Rotation); ),
SpawnLocationAndRotation.SetLocation(Location); TEXT("Blueprint'/Game/Troopers/TrooperWizard.TrooperWizard_C'")
AActor* Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(ATrooper::StaticClass(), SpawnLocationAndRotation); };
dynamic_cast<ATrooper*>(Spawned)->Initialize(0, Location, TrooperCount++); TArray<UClass *> LoadedBpAssets;
Spawned->FinishSpawning(SpawnLocationAndRotation); for (int i = 0; i < bpPaths.Num(); ++i) {
Location += {0.f, 500.f, 0.0f}; TSoftClassPtr<ATrooper> ActorBpClass = TSoftClassPtr<ATrooper>(
} FSoftObjectPath(bpPaths[i])
Location = {-2000.0f, -1000.0f, 0.0f}; );
Rotation = {0.0f, 0.0f, 0.0f}; LoadedBpAssets.Push(ActorBpClass.LoadSynchronous());
for (int i = 0; i < 5; ++i) }
{
FTransform SpawnLocationAndRotation(Rotation); for (int i = 0; i < 5; ++i) {
SpawnLocationAndRotation.SetLocation(Location); FTransform SpawnLocationAndRotation(Rotation);
AActor* Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(ATrooper::StaticClass(), SpawnLocationAndRotation); SpawnLocationAndRotation.SetLocation(Location);
dynamic_cast<ATrooper*>(Spawned)->Initialize(1, Location, TrooperCount++); AActor *Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(
Spawned->FinishSpawning(SpawnLocationAndRotation); LoadedBpAssets[i % 2], SpawnLocationAndRotation);
Location += {0.f, 500.f, 0.0f}; dynamic_cast<ATrooper *>(Spawned)->Initialize(
} 0, Location, TrooperCount++);
Spawned->FinishSpawning(SpawnLocationAndRotation);
Spawned->SetActorLocation(Location);
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) {
FTransform SpawnLocationAndRotation(Rotation);
SpawnLocationAndRotation.SetLocation(Location);
AActor *Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(
LoadedBpAssets[i % 2], SpawnLocationAndRotation);
dynamic_cast<ATrooper *>(Spawned)->Initialize(
1, Location, TrooperCount++);
Spawned->FinishSpawning(SpawnLocationAndRotation);
Spawned->SetActorLocation(Location);
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); const auto CurrentPlayerStart = *SpawnPoints.Find(GetNumPlayers());
const auto CurrentPlayerStart = *SpawnPoints.Find(GetNumPlayers()); UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart end %d"),
UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart end %d"), CurrentPlayerStart->GetPlayerIndex()); CurrentPlayerStart->GetPlayerIndex());
return CurrentPlayerStart; return CurrentPlayerStart;
} }
void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player) void AMyGameMode::InitializeSpawnPointsIfNeeded(AController *Player) {
{ UE_LOG(LogTemp, Warning, TEXT("InitializeSpawnPointsIfNeeded"));
UE_LOG(LogTemp, Warning, TEXT("InitializeSpawnPointsIfNeeded")); if (SpawnPoints.Num() != 0) {
if (SpawnPoints.Num() != 0) UE_LOG(LogTemp, Warning, TEXT("InitializeSpawnPointsIfNeeded Exit %d"),
{ SpawnPoints.Num());
UE_LOG(LogTemp, Warning, TEXT("InitializeSpawnPointsIfNeeded Exit %d"), SpawnPoints.Num()); return;
return; }
} UE_LOG(LogTemp, Warning, TEXT("Rebuilding spawnpoints"));
UE_LOG(LogTemp, Warning, TEXT("Rebuilding spawnpoints")); const auto World = GetWorld();
const auto World = GetWorld(); for (TActorIterator<AMyPlayerStart> PlayerStartIterator(GetWorld());
for (TActorIterator<AMyPlayerStart> PlayerStartIterator(GetWorld()); PlayerStartIterator; ++PlayerStartIterator) PlayerStartIterator; ++PlayerStartIterator) {
{ const auto PlayerStart = *PlayerStartIterator;
const auto PlayerStart = *PlayerStartIterator; const UClass *PawnClass = GetDefaultPawnClassForController(Player);
const UClass* PawnClass = GetDefaultPawnClassForController(Player); const APawn *PawnToFit = PawnClass
const APawn* PawnToFit = PawnClass ? PawnClass->GetDefaultObject<APawn>() : nullptr; ? PawnClass->GetDefaultObject<APawn>()
const FVector ActorLocation = PlayerStart->GetActorLocation(); : nullptr;
const FRotator ActorRotation = PlayerStart->GetActorRotation(); const FVector ActorLocation = PlayerStart->GetActorLocation();
UE_LOG(LogTemp, Warning, TEXT("PlayerStart iterator %d"), PlayerStartIterator->GetPlayerIndex()); const FRotator ActorRotation = PlayerStart->GetActorRotation();
if (!World->EncroachingBlockingGeometry(PawnToFit, ActorLocation, ActorRotation)) UE_LOG(LogTemp, Warning, TEXT("PlayerStart iterator %d"),
{ PlayerStartIterator->GetPlayerIndex());
SpawnPoints.Add(PlayerStartIterator->GetPlayerIndex(), *PlayerStartIterator); if (!World->EncroachingBlockingGeometry(PawnToFit, ActorLocation,
UE_LOG(LogTemp, Warning, TEXT("PlayerStart unoccupied iterator %d"), PlayerStartIterator->GetPlayerIndex()); ActorRotation)) {
} SpawnPoints.Add(PlayerStartIterator->GetPlayerIndex(),
} *PlayerStartIterator);
UE_LOG(LogTemp, Warning, TEXT("PlayerStart unoccupied iterator %d"),
PlayerStartIterator->GetPlayerIndex());
}
}
} }
void AMyGameMode::PostLogin(APlayerController *NewPlayer) {
void AMyGameMode::PostLogin(APlayerController* NewPlayer) Super::PostLogin(NewPlayer);
{ UE_LOG(LogTemp, Warning, TEXT("PostLogin"));
Super::PostLogin(NewPlayer); // const auto World = GetWorld();
UE_LOG(LogTemp, Warning, TEXT("PostLogin")); const auto CurrentNumberOfPlayers = GetNumPlayers();
const auto World = GetWorld();
const auto CurrentNumberOfPlayers = GetNumPlayers(); // 0-indexation
dynamic_cast<AMyPlayerController *>(NewPlayer)->SetPlayerIndex(
// 0-indexation CurrentNumberOfPlayers - 1);
dynamic_cast<AMyPlayerController*>(NewPlayer)->SetPlayerIndex(CurrentNumberOfPlayers - 1); UE_LOG(LogTemp, Warning, TEXT("%d"), CurrentNumberOfPlayers);
UE_LOG(LogTemp, Warning, TEXT("%d"), CurrentNumberOfPlayers); if (CurrentNumberOfPlayers == 2) {
if (CurrentNumberOfPlayers == 2) UE_LOG(LogTemp, Warning, TEXT("Game Start"));
{ // start the game
UE_LOG(LogTemp, Warning, TEXT("Game Start")); StartGame();
// start the game } else {
StartGame(); // delay the game
} UE_LOG(LogTemp, Warning, TEXT("Game Delay"));
else }
{
// delay the game
UE_LOG(LogTemp, Warning, TEXT("Game Delay"));
}
} }
void AMyGameMode::StartGame() void AMyGameMode::StartGame() {
{ InitializeBattleField();
InitializeBattleField(); PlayerInTurn()->StartTurn();
PlayerInTurn()->StartTurn();
} }
AMyPlayerController* AMyGameMode::PlayerInTurn() const AMyPlayerController *AMyGameMode::PlayerInTurn() const {
{ return GetMyPlayerController(CurrentPlayerTurn);
return GetMyPlayerController(CurrentPlayerTurn);
} }
AMyPlayerController* AMyGameMode::PlayerNotInTurn() const AMyPlayerController *AMyGameMode::PlayerNotInTurn() const {
{ uint8 PlayerControllerIndexNotInTurn;
uint8 PlayerControllerIndexNotInTurn = 0; if (CurrentPlayerTurn == 0) {
if (CurrentPlayerTurn == 0) PlayerControllerIndexNotInTurn = 1;
{ } else {
PlayerControllerIndexNotInTurn = 1; PlayerControllerIndexNotInTurn = 0;
} }
else return GetMyPlayerController(PlayerControllerIndexNotInTurn);
{
PlayerControllerIndexNotInTurn = 0;
}
return GetMyPlayerController(PlayerControllerIndexNotInTurn);
} }
void AMyGameMode::CycleTurns() void AMyGameMode::CycleTurns() {
{ PlayerInTurn()->EndTurn();
PlayerInTurn()->EndTurn(); if (CurrentPlayerTurn == 0) {
if (CurrentPlayerTurn == 0) CurrentPlayerTurn = 1;
{ } else {
CurrentPlayerTurn = 1; CurrentPlayerTurn = 0;
} }
else PlayerInTurn()->StartTurn();
{
CurrentPlayerTurn = 0;
}
PlayerInTurn()->StartTurn();
} }
AMyPlayerController* AMyGameMode::GetMyPlayerController(uint8 const PlayerIndex) const AMyPlayerController *AMyGameMode::GetMyPlayerController(
{ uint8 const PlayerIndex) const {
return dynamic_cast<AMyPlayerController*>(UGameplayStatics::GetPlayerController(GetWorld(), PlayerIndex)); return dynamic_cast<AMyPlayerController *>(
UGameplayStatics::GetPlayerController(GetWorld(), PlayerIndex));
} }

@ -9,43 +9,42 @@
#include "GameFramework/GameMode.h" #include "GameFramework/GameMode.h"
#include "MyGameMode.generated.h" #include "MyGameMode.generated.h"
UCLASS() UCLASS()
class TURNBASEDTUTORIAL_API AMyGameMode : public AGameMode class TURNBASEDTUTORIAL_API AMyGameMode : public AGameMode {
{ GENERATED_BODY()
GENERATED_BODY()
public: public:
AMyGameMode(); AMyGameMode();
virtual AActor* ChoosePlayerStart_Implementation(AController* Player) override; virtual AActor *
ChoosePlayerStart_Implementation(AController *Player) override;
virtual void PostLogin(APlayerController* NewPlayer) override; virtual void PostLogin(APlayerController *NewPlayer) override;
virtual void BeginPlay() override; virtual void BeginPlay() override;
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
void CycleTurns(); void CycleTurns();
private: private:
void InitializeSpawnPointsIfNeeded(AController* Player); void InitializeSpawnPointsIfNeeded(AController *Player);
void InitializeBattleField() const; void InitializeBattleField() const;
UPROPERTY() UPROPERTY()
TMap<uint8, AMyPlayerStart*> SpawnPoints{}; TMap<uint8, AMyPlayerStart *> SpawnPoints{};
AMyPlayerController* GetMyPlayerController(uint8 const PlayerIndex) const; AMyPlayerController *GetMyPlayerController(uint8 const PlayerIndex) const;
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
void StartGame(); void StartGame();
UFUNCTION(BlueprintPure) UFUNCTION(BlueprintPure)
AMyPlayerController* PlayerInTurn() const; AMyPlayerController *PlayerInTurn() const;
UFUNCTION(BlueprintPure) UFUNCTION(BlueprintPure)
AMyPlayerController* PlayerNotInTurn() const; AMyPlayerController *PlayerNotInTurn() const;
UPROPERTY() UPROPERTY()
uint8 CurrentPlayerTurn{0}; uint8 CurrentPlayerTurn{0};
}; };

@ -5,31 +5,22 @@
// 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);
} }

@ -4,28 +4,26 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "GameFramework/Pawn.h" #include "GameFramework/Pawn.h"
#include "GameFramework/SpectatorPawn.h"
#include "MyPawn.generated.h" #include "MyPawn.generated.h"
UCLASS() UCLASS()
class TURNBASEDTUTORIAL_API AMyPawn : public APawn class TURNBASEDTUTORIAL_API AMyPawn : public ASpectatorPawn {
{ 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
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(
class UInputComponent *PlayerInputComponent) override;
}; };

@ -5,129 +5,123 @@
#include "MyGameMode.h" #include "MyGameMode.h"
#include "Net/UnrealNetwork.h" #include "Net/UnrealNetwork.h"
AMyPlayerController::AMyPlayerController() : Super(), bIsMyTurn(false), SelectedTrooper(nullptr) AMyPlayerController::AMyPlayerController()
{ : Super(), bIsMyTurn(false), SelectedTrooper(nullptr) {
UE_LOG(LogTemp, Warning, TEXT("Player controller created")); UE_LOG(LogTemp, Warning, TEXT("Player controller created"));
} }
void AMyPlayerController::SetupInputComponent() void AMyPlayerController::SetupInputComponent() {
{ Super::SetupInputComponent();
Super::SetupInputComponent(); InputComponent->BindAction("MyAction", IE_Pressed, this,
InputComponent->BindAction("MyAction", IE_Pressed, this, &AMyPlayerController::OnLeftMouseClick);
&AMyPlayerController::OnLeftMouseClick);
} }
void AMyPlayerController::SetMyTurn(bool bMyTurn) void AMyPlayerController::SetMyTurn(bool bMyTurn) {
{ bIsMyTurn = bMyTurn;
bIsMyTurn = bMyTurn; if (bIsMyTurn) {
if (bIsMyTurn) GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green,
{ FString::Printf(
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, TEXT("CURRENT TURN: %d"),
FString::Printf( PlayerIndex));
TEXT("CURRENT TURN: %d"), PlayerIndex)); }
} // OnMyTurnChanged.ExecuteIfBound(bIsMyTurn);
// OnMyTurnChanged.ExecuteIfBound(bIsMyTurn);
} }
void AMyPlayerController::StartTurn_Implementation() void AMyPlayerController::StartTurn_Implementation() {
{ SetMyTurn(true);
SetMyTurn(true); UE_LOG(LogTemp, Warning, TEXT("Your turn, %d"), PlayerIndex);
UE_LOG(LogTemp, Warning, TEXT("Your turn, %d"), PlayerIndex);
} }
void AMyPlayerController::EndTurn_Implementation() void AMyPlayerController::EndTurn_Implementation() {
{ SetMyTurn(false);
SetMyTurn(false); UE_LOG(LogTemp, Warning, TEXT("Not your turn, %d"), PlayerIndex);
UE_LOG(LogTemp, Warning, TEXT("Not your turn, %d"), PlayerIndex);
} }
auto AMyPlayerController::GetMyGameMode() const auto AMyPlayerController::GetMyGameMode() const {
{ return dynamic_cast<AMyGameMode *>(
return dynamic_cast<AMyGameMode*>(UGameplayStatics::GetGameMode(GetWorld())); UGameplayStatics::GetGameMode(GetWorld()));
} }
void AMyPlayerController::MoveTrooper_Implementation(ATrooper* Trooper, FVector Location) void AMyPlayerController::MoveTrooper_Implementation(
{ ATrooper *Trooper,
Trooper->MoveTrooper(Location); FVector Location) {
GetMyGameMode()->CycleTurns(); Trooper->MoveTrooper(Location);
GetMyGameMode()->CycleTurns();
} }
void AMyPlayerController::AttackTrooper_Implementation(ATrooper* Attacker, ATrooper* Victim) void AMyPlayerController::AttackTrooper_Implementation(
{ ATrooper *Attacker,
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, ATrooper *Victim) {
FString::Printf( GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red,
TEXT("ATTACK!! %d attacked %d"), Attacker->GetId(), Victim->GetId())); FString::Printf(
GetMyGameMode()->CycleTurns(); TEXT("ATTACK!! %d attacked %d"),
Attacker->GetId(), Victim->GetId()));
GetMyGameMode()->CycleTurns();
} }
void AMyPlayerController::SetPlayerIndex(uint8 NewPlayerIndex) void AMyPlayerController::SetPlayerIndex(uint8 NewPlayerIndex) {
{ PlayerIndex = NewPlayerIndex;
PlayerIndex = NewPlayerIndex;
} }
void AMyPlayerController::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const void AMyPlayerController::GetLifetimeReplicatedProps(
{ TArray<FLifetimeProperty> &OutLifetimeProps) const {
DOREPLIFETIME(AMyPlayerController, PlayerIndex); DOREPLIFETIME(AMyPlayerController, PlayerIndex);
} }
void AMyPlayerController::OnLeftMouseClick() void AMyPlayerController::OnLeftMouseClick() {
{ if (!bIsMyTurn) {
if (!bIsMyTurn) return;
{ }
return; UE_LOG(LogTemp, Warning, TEXT("Mouse clicked"));
} FHitResult HitResult;
UE_LOG(LogTemp, Warning, TEXT("Mouse clicked")); bool const IsHitResult = GetHitResultUnderCursorByChannel(
FHitResult HitResult; TraceTypeQuery1, false, HitResult);
bool const IsHitResult = GetHitResultUnderCursorByChannel(TraceTypeQuery1, false, HitResult); if (!IsHitResult)
if (!IsHitResult) return;
return; UE_LOG(LogTemp, Warning, TEXT("Got hit result"));
UE_LOG(LogTemp, Warning, TEXT("Got hit result")); auto const NewlySelectedLocation = HitResult.Location;
auto const NewlySelectedLocation = HitResult.Location; ATrooper *NewlySelectedTrooper = dynamic_cast<ATrooper *>(
ATrooper* NewlySelectedTrooper = dynamic_cast<ATrooper*>(HitResult.GetActor()); HitResult.GetActor());
if (NewlySelectedTrooper == nullptr || !NewlySelectedTrooper->IsValidLowLevel()) if (NewlySelectedTrooper == nullptr || !NewlySelectedTrooper->
{ IsValidLowLevel()) {
// we selected something that is not a trooper (or trooper in shitty state...) // we selected something that is not a trooper (or trooper in shitty state...)
// probably we should move to it if we can... // probably we should move to it if we can...
UE_LOG(LogTemp, Warning, TEXT("Not a trooper")); UE_LOG(LogTemp, Warning, TEXT("Not a trooper"));
// if initial trooper is valid... // if initial trooper is valid...
if (SelectedTrooper != nullptr && SelectedTrooper->IsValidLowLevel()) if (SelectedTrooper != nullptr && SelectedTrooper->IsValidLowLevel()) {
{ UE_LOG(LogTemp, Warning, TEXT("Do move"));
UE_LOG(LogTemp, Warning, TEXT("Do move")); // move this mf
// move this mf MoveTrooper(SelectedTrooper, NewlySelectedLocation);
MoveTrooper(SelectedTrooper, NewlySelectedLocation); // and reset the selection....
// and reset the selection.... SelectedTrooper = nullptr;
SelectedTrooper = nullptr; }
} return;
return; }
} UE_LOG(LogTemp, Warning, TEXT("New Selected Player Index %d"),
UE_LOG(LogTemp, Warning, TEXT("New Selected Player Index %d"), NewlySelectedTrooper->GetPlayerIndex()); NewlySelectedTrooper->GetPlayerIndex());
// skip re-selection // skip re-selection
if (SelectedTrooper == NewlySelectedTrooper) if (SelectedTrooper == NewlySelectedTrooper) {
{ UE_LOG(LogTemp, Warning, TEXT("Skip reselection"));
UE_LOG(LogTemp, Warning, TEXT("Skip reselection")); return;
return; }
} // we selected valid trooper...
// we selected valid trooper... if (NewlySelectedTrooper->GetPlayerIndex() == PlayerIndex) {
if (NewlySelectedTrooper->GetPlayerIndex() == PlayerIndex) UE_LOG(LogTemp, Warning, TEXT("Do reselect"));
{ // our move, selection
UE_LOG(LogTemp, Warning, TEXT("Do reselect")); SelectedTrooper = NewlySelectedTrooper;
// our move, selection } else {
SelectedTrooper = NewlySelectedTrooper; UE_LOG(LogTemp, Warning, TEXT("Attack or skip..."));
} // maybe selected trooper had gone crazy...
else if (SelectedTrooper == nullptr || !SelectedTrooper->IsValidLowLevel())
{ return;
UE_LOG(LogTemp, Warning, TEXT("Attack or skip...")); UE_LOG(LogTemp, Warning, TEXT("Do attack"));
// maybe selected trooper had gone crazy... // ATTACK!!! ATTACK!!!!!!
if (SelectedTrooper == nullptr || !SelectedTrooper->IsValidLowLevel()) AttackTrooper(SelectedTrooper, NewlySelectedTrooper);
return; SelectedTrooper = nullptr;
UE_LOG(LogTemp, Warning, TEXT("Do attack")); }
// ATTACK!!! ATTACK!!!!!!
AttackTrooper(SelectedTrooper, NewlySelectedTrooper);
SelectedTrooper = nullptr;
}
} }

@ -6,50 +6,46 @@
#include "GameFramework/PlayerController.h" #include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h" #include "MyPlayerController.generated.h"
/**
*
*/
// DECLARE_DYNAMIC_DELEGATE_OneParam(FOnMyTurnChangedDelegate, bool, bMyTurn); // DECLARE_DYNAMIC_DELEGATE_OneParam(FOnMyTurnChangedDelegate, bool, bMyTurn);
UCLASS() UCLASS()
class TURNBASEDTUTORIAL_API AMyPlayerController : public APlayerController class TURNBASEDTUTORIAL_API AMyPlayerController : public APlayerController {
{ GENERATED_BODY()
GENERATED_BODY()
public: public:
// FOnMyTurnChangedDelegate OnMyTurnChanged; // FOnMyTurnChangedDelegate OnMyTurnChanged;
virtual void SetupInputComponent() override; virtual void SetupInputComponent() override;
AMyPlayerController(); AMyPlayerController();
UFUNCTION(Client, Reliable) UFUNCTION(Client, Reliable)
void StartTurn(); void StartTurn();
UFUNCTION(Client, Reliable) UFUNCTION(Client, Reliable)
void EndTurn(); void EndTurn();
UFUNCTION(Server, Reliable) UFUNCTION(Server, Reliable)
void MoveTrooper(ATrooper* Trooper, FVector Location); void MoveTrooper(ATrooper *Trooper, FVector Location);
UFUNCTION(Server, Reliable) UFUNCTION(Server, Reliable)
void AttackTrooper(ATrooper* Attacker, ATrooper* Victim); void AttackTrooper(ATrooper *Attacker, ATrooper *Victim);
UFUNCTION() UFUNCTION()
void SetPlayerIndex(uint8 NewPlayerIndex); void SetPlayerIndex(uint8 NewPlayerIndex);
private: private:
bool bIsMyTurn; bool bIsMyTurn;
UPROPERTY(Replicated) UPROPERTY(Replicated)
uint8 PlayerIndex; uint8 PlayerIndex;
ATrooper* SelectedTrooper; UPROPERTY()
ATrooper *SelectedTrooper;
void OnLeftMouseClick(); void OnLeftMouseClick();
void SetMyTurn(bool bMyTurn); void SetMyTurn(bool bMyTurn);
auto GetMyGameMode() const; auto GetMyGameMode() const;
}; };

@ -1,81 +1,91 @@
#include "Trooper.h" #include "Trooper.h"
#include <Kismet/GameplayStatics.h> #include <Kismet/GameplayStatics.h>
#include "MyPlayerController.h"
#include "Net/UnrealNetwork.h" #include "Net/UnrealNetwork.h"
// Sets default values // Sets default values
ATrooper::ATrooper() ATrooper::ATrooper() {
{ bReplicates = true;
bReplicates = true;
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
Tags.Add(FName("Trooper")); Tags.Add(FName("Trooper"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh"); MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
RootComponent = MyStaticMesh; RootComponent = MyStaticMesh;
static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshToUse(TEXT( MeshPath = TEXT("StaticMesh'/Game/StarterContent/Props/SM_Chair.SM_Chair'");
"StaticMesh'/Game/StarterContent/Props/SM_Chair.SM_Chair'" static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshToUse(MeshPath);
)); if (MeshToUse.Object) {
if (MeshToUse.Object) MyStaticMesh->SetStaticMesh(MeshToUse.Object);
{ }
MyStaticMesh->SetStaticMesh(MeshToUse.Object);
}
} }
// void ATrooper::SetStaticMesh() const {
// static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshToUse(MeshPath);
// if (MeshToUse.Object) {
// MyStaticMesh->SetStaticMesh(MeshToUse.Object);
// }
// }
// Called when the game starts or when spawned // Called when the game starts or when spawned
void ATrooper::BeginPlay() void ATrooper::BeginPlay() {
{ Super::BeginPlay();
Super::BeginPlay();
} }
void ATrooper::Initialize(uint8 const NewPlayerIndex, FVector const SpawnLocation, uint8 const NewId) void ATrooper::Initialize(uint8 const NewPlayerIndex,
{ FVector const SpawnLocation,
PlayerIndex = NewPlayerIndex; uint8 const NewId) {
bIsMoving = false; PlayerIndex = NewPlayerIndex;
CurrentLocation = SpawnLocation; bIsMoving = false;
Id = NewId; CurrentLocation = SpawnLocation;
Id = NewId;
} }
void ATrooper::Tick(float const DeltaTime) void ATrooper::Tick(float const DeltaTime) {
{ if (!bIsMoving)
if (!bIsMoving) return;
return; FVector PositionVector = (TargetLocation - CurrentLocation);
FVector PositionVector = (TargetLocation - CurrentLocation); PositionVector.Normalize();
PositionVector.Normalize(); PositionVector *= (Speed * DeltaTime);
PositionVector *= (Speed * DeltaTime); if (PositionVector.Size() >= (TargetLocation - CurrentLocation).Size()) {
if (PositionVector.Size() >= (TargetLocation - CurrentLocation).Size()) CurrentLocation = TargetLocation;
{ bIsMoving = false;
CurrentLocation = TargetLocation; } else {
bIsMoving = false; CurrentLocation += PositionVector;
} }
else SetActorLocation(CurrentLocation);
{
CurrentLocation += PositionVector;
}
SetActorLocation(CurrentLocation);
} }
void ATrooper::MoveTrooper(FVector const NewPos) void ATrooper::MoveTrooper(FVector const NewPos) {
{ TargetLocation = NewPos;
TargetLocation = NewPos; bIsMoving = true;
bIsMoving = true;
} }
uint8 ATrooper::GetId() const uint8 ATrooper::GetId() const {
{ return Id;
return Id;
} }
void ATrooper::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const void ATrooper::GetLifetimeReplicatedProps(
{ TArray<FLifetimeProperty> &OutLifetimeProps) const {
DOREPLIFETIME(ATrooper, PlayerIndex); DOREPLIFETIME(ATrooper, PlayerIndex);
DOREPLIFETIME(ATrooper, CurrentLocation); DOREPLIFETIME(ATrooper, CurrentLocation);
DOREPLIFETIME(ATrooper, TargetLocation); DOREPLIFETIME(ATrooper, TargetLocation);
DOREPLIFETIME(ATrooper, bIsMoving); DOREPLIFETIME(ATrooper, bIsMoving);
DOREPLIFETIME(ATrooper, Id); DOREPLIFETIME(ATrooper, Id);
} }
uint8 ATrooper::GetPlayerIndex() const uint8 ATrooper::GetPlayerIndex() const {
{ return PlayerIndex;
return PlayerIndex;
} }
//
// ATrooperWizard::ATrooperWizard() {
// MeshPath = TEXT(
// // "StaticMesh'/Game/CityofBrass_Enemies/Static/Wizard_StaticMesh.Wizard_StaticMesh'");
// "StaticMesh'/Game/CityofBrass_Enemies/Static/SkeletonMelee_StaticMesh.SkeletonMelee_StaticMesh'");
// SetStaticMesh();
// }
//
// ATrooperSkeletonMelee::ATrooperSkeletonMelee() {
// MeshPath = TEXT(
// "StaticMesh'/Game/CityofBrass_Enemies/Static/SkeletonMelee_StaticMesh.SkeletonMelee_StaticMesh'");
// SetStaticMesh();
// }

@ -1,55 +1,75 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/Character.h" #include "GameFramework/Character.h"
#include "Trooper.generated.h" #include "Trooper.generated.h"
UCLASS() UCLASS()
class TURNBASEDTUTORIAL_API ATrooper : public ACharacter class TURNBASEDTUTORIAL_API ATrooper : public ACharacter {
{ GENERATED_BODY()
GENERATED_BODY()
public: public:
// Sets default values for this actor's properties // Sets default values for this actor's properties
ATrooper(); ATrooper();
void Initialize(uint8 const NewPlayerIndex, FVector const SpawnLocation, uint8 const NewId); void Initialize(uint8 const NewPlayerIndex,
FVector const SpawnLocation,
uint8 const NewId);
UFUNCTION() UFUNCTION()
uint8 GetPlayerIndex() const; uint8 GetPlayerIndex() const;
UFUNCTION() UFUNCTION()
void MoveTrooper(FVector const NewPos); void MoveTrooper(FVector const NewPos);
UFUNCTION() UFUNCTION()
uint8 GetId() const; uint8 GetId() const;
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
virtual void Tick(float const DeltaTime) override; virtual void Tick(float const DeltaTime) override;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly) // void SetStaticMesh() const;
UStaticMeshComponent* MyStaticMesh;
private: UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UPROPERTY(Replicated) UStaticMeshComponent *MyStaticMesh;
uint8 PlayerIndex;
UPROPERTY(Replicated) const TCHAR *MeshPath = nullptr;
uint8 Id;
UPROPERTY() UPROPERTY(Replicated)
float Speed = 300.0f; uint8 PlayerIndex;
UPROPERTY(Replicated) UPROPERTY(Replicated)
FVector CurrentLocation; uint8 Id;
UPROPERTY(Replicated) UPROPERTY()
FVector TargetLocation; float Speed = 300.0f;
UPROPERTY(Replicated) UPROPERTY(Replicated)
bool bIsMoving = false; FVector CurrentLocation;
UPROPERTY(Replicated)
FVector TargetLocation;
UPROPERTY(Replicated)
bool bIsMoving = false;
}; };
// UCLASS()
// class ATrooperWizard : public ATrooper {
// GENERATED_BODY()
//
// public:
// ATrooperWizard();
//
// };
//
// UCLASS()
// class ATrooperSkeletonMelee : public ATrooper {
// GENERATED_BODY()
//
// public:
// ATrooperSkeletonMelee();
//
// };

Loading…
Cancel
Save