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

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

@ -5,31 +5,22 @@
// 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;
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();
void AMyPawn::BeginPlay() {
Super::BeginPlay();
}
// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
void AMyPawn::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
void AMyPawn::SetupPlayerInputComponent(UInputComponent *PlayerInputComponent) {
Super::SetupPlayerInputComponent(PlayerInputComponent);
}

@ -4,28 +4,26 @@
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "GameFramework/SpectatorPawn.h"
#include "MyPawn.generated.h"
UCLASS()
class TURNBASEDTUTORIAL_API AMyPawn : public APawn
{
GENERATED_BODY()
class TURNBASEDTUTORIAL_API AMyPawn : public ASpectatorPawn {
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
// Sets default values for this pawn's properties
AMyPawn();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// 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:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(
class UInputComponent *PlayerInputComponent) override;
};

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

@ -6,50 +6,46 @@
#include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h"
/**
*
*/
// DECLARE_DYNAMIC_DELEGATE_OneParam(FOnMyTurnChangedDelegate, bool, bMyTurn);
UCLASS()
class TURNBASEDTUTORIAL_API AMyPlayerController : public APlayerController
{
GENERATED_BODY()
class TURNBASEDTUTORIAL_API AMyPlayerController : public APlayerController {
GENERATED_BODY()
public:
// FOnMyTurnChangedDelegate OnMyTurnChanged;
// FOnMyTurnChangedDelegate OnMyTurnChanged;
virtual void SetupInputComponent() override;
virtual void SetupInputComponent() override;
AMyPlayerController();
AMyPlayerController();
UFUNCTION(Client, Reliable)
void StartTurn();
UFUNCTION(Client, Reliable)
void StartTurn();
UFUNCTION(Client, Reliable)
void EndTurn();
UFUNCTION(Client, Reliable)
void EndTurn();
UFUNCTION(Server, Reliable)
void MoveTrooper(ATrooper* Trooper, FVector Location);
UFUNCTION(Server, Reliable)
void MoveTrooper(ATrooper *Trooper, FVector Location);
UFUNCTION(Server, Reliable)
void AttackTrooper(ATrooper* Attacker, ATrooper* Victim);
UFUNCTION(Server, Reliable)
void AttackTrooper(ATrooper *Attacker, ATrooper *Victim);
UFUNCTION()
void SetPlayerIndex(uint8 NewPlayerIndex);
UFUNCTION()
void SetPlayerIndex(uint8 NewPlayerIndex);
private:
bool bIsMyTurn;
bool bIsMyTurn;
UPROPERTY(Replicated)
uint8 PlayerIndex;
UPROPERTY(Replicated)
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 <Kismet/GameplayStatics.h>
#include "MyPlayerController.h"
#include "Net/UnrealNetwork.h"
// Sets default values
ATrooper::ATrooper()
{
bReplicates = true;
ATrooper::ATrooper() {
bReplicates = true;
PrimaryActorTick.bCanEverTick = true;
Tags.Add(FName("Trooper"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
RootComponent = MyStaticMesh;
static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshToUse(TEXT(
"StaticMesh'/Game/StarterContent/Props/SM_Chair.SM_Chair'"
));
if (MeshToUse.Object)
{
MyStaticMesh->SetStaticMesh(MeshToUse.Object);
}
PrimaryActorTick.bCanEverTick = true;
Tags.Add(FName("Trooper"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
RootComponent = MyStaticMesh;
MeshPath = TEXT("StaticMesh'/Game/StarterContent/Props/SM_Chair.SM_Chair'");
static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshToUse(MeshPath);
if (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
void ATrooper::BeginPlay()
{
Super::BeginPlay();
void ATrooper::BeginPlay() {
Super::BeginPlay();
}
void ATrooper::Initialize(uint8 const NewPlayerIndex, FVector const SpawnLocation, uint8 const NewId)
{
PlayerIndex = NewPlayerIndex;
bIsMoving = false;
CurrentLocation = SpawnLocation;
Id = NewId;
void ATrooper::Initialize(uint8 const NewPlayerIndex,
FVector const SpawnLocation,
uint8 const NewId) {
PlayerIndex = NewPlayerIndex;
bIsMoving = false;
CurrentLocation = SpawnLocation;
Id = NewId;
}
void ATrooper::Tick(float const DeltaTime)
{
if (!bIsMoving)
return;
FVector PositionVector = (TargetLocation - CurrentLocation);
PositionVector.Normalize();
PositionVector *= (Speed * DeltaTime);
if (PositionVector.Size() >= (TargetLocation - CurrentLocation).Size())
{
CurrentLocation = TargetLocation;
bIsMoving = false;
}
else
{
CurrentLocation += PositionVector;
}
SetActorLocation(CurrentLocation);
void ATrooper::Tick(float const DeltaTime) {
if (!bIsMoving)
return;
FVector PositionVector = (TargetLocation - CurrentLocation);
PositionVector.Normalize();
PositionVector *= (Speed * DeltaTime);
if (PositionVector.Size() >= (TargetLocation - CurrentLocation).Size()) {
CurrentLocation = TargetLocation;
bIsMoving = false;
} else {
CurrentLocation += PositionVector;
}
SetActorLocation(CurrentLocation);
}
void ATrooper::MoveTrooper(FVector const NewPos)
{
TargetLocation = NewPos;
bIsMoving = true;
void ATrooper::MoveTrooper(FVector const NewPos) {
TargetLocation = NewPos;
bIsMoving = true;
}
uint8 ATrooper::GetId() const
{
return Id;
uint8 ATrooper::GetId() const {
return Id;
}
void ATrooper::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
DOREPLIFETIME(ATrooper, PlayerIndex);
DOREPLIFETIME(ATrooper, CurrentLocation);
DOREPLIFETIME(ATrooper, TargetLocation);
DOREPLIFETIME(ATrooper, bIsMoving);
DOREPLIFETIME(ATrooper, Id);
void ATrooper::GetLifetimeReplicatedProps(
TArray<FLifetimeProperty> &OutLifetimeProps) const {
DOREPLIFETIME(ATrooper, PlayerIndex);
DOREPLIFETIME(ATrooper, CurrentLocation);
DOREPLIFETIME(ATrooper, TargetLocation);
DOREPLIFETIME(ATrooper, bIsMoving);
DOREPLIFETIME(ATrooper, Id);
}
uint8 ATrooper::GetPlayerIndex() const
{
return PlayerIndex;
uint8 ATrooper::GetPlayerIndex() const {
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
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/Character.h"
#include "Trooper.generated.h"
UCLASS()
class TURNBASEDTUTORIAL_API ATrooper : public ACharacter
{
GENERATED_BODY()
class TURNBASEDTUTORIAL_API ATrooper : public ACharacter {
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATrooper();
// Sets default values for this actor's properties
ATrooper();
void Initialize(uint8 const NewPlayerIndex, FVector const SpawnLocation, uint8 const NewId);
void Initialize(uint8 const NewPlayerIndex,
FVector const SpawnLocation,
uint8 const NewId);
UFUNCTION()
uint8 GetPlayerIndex() const;
UFUNCTION()
uint8 GetPlayerIndex() const;
UFUNCTION()
void MoveTrooper(FVector const NewPos);
UFUNCTION()
void MoveTrooper(FVector const NewPos);
UFUNCTION()
uint8 GetId() const;
UFUNCTION()
uint8 GetId() const;
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)
UStaticMeshComponent* MyStaticMesh;
// void SetStaticMesh() const;
private:
UPROPERTY(Replicated)
uint8 PlayerIndex;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UStaticMeshComponent *MyStaticMesh;
UPROPERTY(Replicated)
uint8 Id;
const TCHAR *MeshPath = nullptr;
UPROPERTY()
float Speed = 300.0f;
UPROPERTY(Replicated)
uint8 PlayerIndex;
UPROPERTY(Replicated)
FVector CurrentLocation;
UPROPERTY(Replicated)
uint8 Id;
UPROPERTY(Replicated)
FVector TargetLocation;
UPROPERTY()
float Speed = 300.0f;
UPROPERTY(Replicated)
bool bIsMoving = false;
UPROPERTY(Replicated)
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