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;
for (int i = 0; i < 5; ++i) 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); FTransform SpawnLocationAndRotation(Rotation);
SpawnLocationAndRotation.SetLocation(Location); SpawnLocationAndRotation.SetLocation(Location);
AActor* Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(ATrooper::StaticClass(), SpawnLocationAndRotation); AActor *Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(
dynamic_cast<ATrooper*>(Spawned)->Initialize(0, Location, TrooperCount++); LoadedBpAssets[i % 2], SpawnLocationAndRotation);
dynamic_cast<ATrooper *>(Spawned)->Initialize(
0, Location, TrooperCount++);
Spawned->FinishSpawning(SpawnLocationAndRotation); Spawned->FinishSpawning(SpawnLocationAndRotation);
Spawned->SetActorLocation(Location);
Location += {0.f, 500.f, 0.0f}; Location += {0.f, 500.f, 0.0f};
} }
Location = {-2000.0f, -1000.0f, 0.0f}; Location = {-2000.0f, -1000.0f, 0.0f};
Rotation = {0.0f, 0.0f, 0.0f}; Rotation = {0.0f, 0.0f, 0.0f};
for (int i = 0; i < 5; ++i) for (int i = 0; i < 5; ++i) {
{
FTransform SpawnLocationAndRotation(Rotation); FTransform SpawnLocationAndRotation(Rotation);
SpawnLocationAndRotation.SetLocation(Location); SpawnLocationAndRotation.SetLocation(Location);
AActor* Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(ATrooper::StaticClass(), SpawnLocationAndRotation); AActor *Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(
dynamic_cast<ATrooper*>(Spawned)->Initialize(1, Location, TrooperCount++); LoadedBpAssets[i % 2], SpawnLocationAndRotation);
dynamic_cast<ATrooper *>(Spawned)->Initialize(
1, Location, TrooperCount++);
Spawned->FinishSpawning(SpawnLocationAndRotation); Spawned->FinishSpawning(SpawnLocationAndRotation);
Spawned->SetActorLocation(Location);
Location += {0.f, 500.f, 0.0f}; 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"), CurrentPlayerStart->GetPlayerIndex()); UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart end %d"),
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"),
UE_LOG(LogTemp, Warning, TEXT("InitializeSpawnPointsIfNeeded Exit %d"), SpawnPoints.Num()); 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()); PlayerStartIterator; ++PlayerStartIterator) for (TActorIterator<AMyPlayerStart> PlayerStartIterator(GetWorld());
{ PlayerStartIterator; ++PlayerStartIterator) {
const auto PlayerStart = *PlayerStartIterator; const auto PlayerStart = *PlayerStartIterator;
const UClass* PawnClass = GetDefaultPawnClassForController(Player); const UClass *PawnClass = GetDefaultPawnClassForController(Player);
const APawn* PawnToFit = PawnClass ? PawnClass->GetDefaultObject<APawn>() : nullptr; const APawn *PawnToFit = PawnClass
? PawnClass->GetDefaultObject<APawn>()
: nullptr;
const FVector ActorLocation = PlayerStart->GetActorLocation(); const FVector ActorLocation = PlayerStart->GetActorLocation();
const FRotator ActorRotation = PlayerStart->GetActorRotation(); const FRotator ActorRotation = PlayerStart->GetActorRotation();
UE_LOG(LogTemp, Warning, TEXT("PlayerStart iterator %d"), PlayerStartIterator->GetPlayerIndex()); UE_LOG(LogTemp, Warning, TEXT("PlayerStart iterator %d"),
if (!World->EncroachingBlockingGeometry(PawnToFit, ActorLocation, ActorRotation)) PlayerStartIterator->GetPlayerIndex());
{ if (!World->EncroachingBlockingGeometry(PawnToFit, ActorLocation,
SpawnPoints.Add(PlayerStartIterator->GetPlayerIndex(), *PlayerStartIterator); ActorRotation)) {
UE_LOG(LogTemp, Warning, TEXT("PlayerStart unoccupied iterator %d"), PlayerStartIterator->GetPlayerIndex()); 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); Super::PostLogin(NewPlayer);
UE_LOG(LogTemp, Warning, TEXT("PostLogin")); UE_LOG(LogTemp, Warning, TEXT("PostLogin"));
const auto World = GetWorld(); // const auto World = GetWorld();
const auto CurrentNumberOfPlayers = GetNumPlayers(); const auto CurrentNumberOfPlayers = GetNumPlayers();
// 0-indexation // 0-indexation
dynamic_cast<AMyPlayerController*>(NewPlayer)->SetPlayerIndex(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")); UE_LOG(LogTemp, Warning, TEXT("Game Start"));
// start the game // start the game
StartGame(); StartGame();
} } else {
else
{
// delay the game // delay the game
UE_LOG(LogTemp, Warning, TEXT("Game Delay")); 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; PlayerControllerIndexNotInTurn = 1;
} } else {
else
{
PlayerControllerIndexNotInTurn = 0; PlayerControllerIndexNotInTurn = 0;
} }
return GetMyPlayerController(PlayerControllerIndexNotInTurn); return GetMyPlayerController(PlayerControllerIndexNotInTurn);
} }
void AMyGameMode::CycleTurns() void AMyGameMode::CycleTurns() {
{
PlayerInTurn()->EndTurn(); PlayerInTurn()->EndTurn();
if (CurrentPlayerTurn == 0) if (CurrentPlayerTurn == 0) {
{
CurrentPlayerTurn = 1; CurrentPlayerTurn = 1;
} } else {
else
{
CurrentPlayerTurn = 0; CurrentPlayerTurn = 0;
} }
PlayerInTurn()->StartTurn(); 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,18 +9,17 @@
#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;
@ -28,23 +27,23 @@ public:
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,11 +4,11 @@
#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:
@ -24,8 +24,6 @@ public:
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;
}; };

@ -5,98 +5,95 @@
#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, GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green,
FString::Printf( FString::Printf(
TEXT("CURRENT TURN: %d"), 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,
FVector Location) {
Trooper->MoveTrooper(Location); Trooper->MoveTrooper(Location);
GetMyGameMode()->CycleTurns(); GetMyGameMode()->CycleTurns();
} }
void AMyPlayerController::AttackTrooper_Implementation(ATrooper* Attacker, ATrooper* Victim) void AMyPlayerController::AttackTrooper_Implementation(
{ ATrooper *Attacker,
ATrooper *Victim) {
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red,
FString::Printf( FString::Printf(
TEXT("ATTACK!! %d attacked %d"), Attacker->GetId(), Victim->GetId())); TEXT("ATTACK!! %d attacked %d"),
Attacker->GetId(), Victim->GetId()));
GetMyGameMode()->CycleTurns(); 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")); UE_LOG(LogTemp, Warning, TEXT("Mouse clicked"));
FHitResult HitResult; FHitResult HitResult;
bool const IsHitResult = GetHitResultUnderCursorByChannel(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*>(HitResult.GetActor()); ATrooper *NewlySelectedTrooper = dynamic_cast<ATrooper *>(
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);
@ -105,22 +102,19 @@ void AMyPlayerController::OnLeftMouseClick()
} }
return; return;
} }
UE_LOG(LogTemp, Warning, TEXT("New Selected Player Index %d"), NewlySelectedTrooper->GetPlayerIndex()); UE_LOG(LogTemp, Warning, TEXT("New Selected Player Index %d"),
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")); UE_LOG(LogTemp, Warning, TEXT("Do reselect"));
// our move, selection // our move, selection
SelectedTrooper = NewlySelectedTrooper; SelectedTrooper = NewlySelectedTrooper;
} } else {
else
{
UE_LOG(LogTemp, Warning, TEXT("Attack or skip...")); UE_LOG(LogTemp, Warning, TEXT("Attack or skip..."));
// maybe selected trooper had gone crazy... // maybe selected trooper had gone crazy...
if (SelectedTrooper == nullptr || !SelectedTrooper->IsValidLowLevel()) if (SelectedTrooper == nullptr || !SelectedTrooper->IsValidLowLevel())

@ -6,15 +6,10 @@
#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:
@ -31,10 +26,10 @@ public:
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);
@ -45,7 +40,8 @@ private:
UPROPERTY(Replicated) UPROPERTY(Replicated)
uint8 PlayerIndex; uint8 PlayerIndex;
ATrooper* SelectedTrooper; UPROPERTY()
ATrooper *SelectedTrooper;
void OnLeftMouseClick(); void OnLeftMouseClick();

@ -1,73 +1,70 @@
#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,
uint8 const NewId) {
PlayerIndex = NewPlayerIndex; PlayerIndex = NewPlayerIndex;
bIsMoving = false; bIsMoving = false;
CurrentLocation = SpawnLocation; CurrentLocation = SpawnLocation;
Id = NewId; 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; CurrentLocation = TargetLocation;
bIsMoving = false; bIsMoving = false;
} } else {
else
{
CurrentLocation += PositionVector; CurrentLocation += PositionVector;
} }
SetActorLocation(CurrentLocation); 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);
@ -75,7 +72,20 @@ void ATrooper::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetime
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,21 +1,20 @@
#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;
@ -31,10 +30,13 @@ protected:
virtual void Tick(float const DeltaTime) override; virtual void Tick(float const DeltaTime) override;
// void SetStaticMesh() const;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly) UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UStaticMeshComponent* MyStaticMesh; UStaticMeshComponent *MyStaticMesh;
const TCHAR *MeshPath = nullptr;
private:
UPROPERTY(Replicated) UPROPERTY(Replicated)
uint8 PlayerIndex; uint8 PlayerIndex;
@ -53,3 +55,21 @@ private:
UPROPERTY(Replicated) UPROPERTY(Replicated)
bool bIsMoving = false; bool bIsMoving = false;
}; };
// UCLASS()
// class ATrooperWizard : public ATrooper {
// GENERATED_BODY()
//
// public:
// ATrooperWizard();
//
// };
//
// UCLASS()
// class ATrooperSkeletonMelee : public ATrooper {
// GENERATED_BODY()
//
// public:
// ATrooperSkeletonMelee();
//
// };

Loading…
Cancel
Save