diff --git a/Content/BattleField/BattleFieldMap.umap b/Content/BattleField/BattleFieldMap.umap index 49216a9..c64ed5a 100644 Binary files a/Content/BattleField/BattleFieldMap.umap and b/Content/BattleField/BattleFieldMap.umap differ diff --git a/Content/CityofBrass_Enemies/Static/SkeletonMelee_StaticMesh.uasset b/Content/CityofBrass_Enemies/Static/SkeletonMelee_StaticMesh.uasset new file mode 100644 index 0000000..4377c2b Binary files /dev/null and b/Content/CityofBrass_Enemies/Static/SkeletonMelee_StaticMesh.uasset differ diff --git a/Content/CityofBrass_Enemies/Static/Wizard_StaticMesh.uasset b/Content/CityofBrass_Enemies/Static/Wizard_StaticMesh.uasset new file mode 100644 index 0000000..2cb8a10 Binary files /dev/null and b/Content/CityofBrass_Enemies/Static/Wizard_StaticMesh.uasset differ diff --git a/Content/Troopers/TrooperSkeletonMelee.uasset b/Content/Troopers/TrooperSkeletonMelee.uasset new file mode 100644 index 0000000..84204ca Binary files /dev/null and b/Content/Troopers/TrooperSkeletonMelee.uasset differ diff --git a/Content/Troopers/TrooperWizard.uasset b/Content/Troopers/TrooperWizard.uasset new file mode 100644 index 0000000..cd7a0a3 Binary files /dev/null and b/Content/Troopers/TrooperWizard.uasset differ diff --git a/Source/TurnBasedTutorial/MyGameMode.cpp b/Source/TurnBasedTutorial/MyGameMode.cpp index 5a52fa9..4017722 100644 --- a/Source/TurnBasedTutorial/MyGameMode.cpp +++ b/Source/TurnBasedTutorial/MyGameMode.cpp @@ -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::StaticClass(), SpawnLocationAndRotation); - dynamic_cast(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::StaticClass(), SpawnLocationAndRotation); - dynamic_cast(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 bpPaths{ + TEXT( + "Blueprint'/Game/Troopers/TrooperSkeletonMelee.TrooperSkeletonMelee_C'" + ), + TEXT("Blueprint'/Game/Troopers/TrooperWizard.TrooperWizard_C'") + }; + TArray LoadedBpAssets; + for (int i = 0; i < bpPaths.Num(); ++i) { + TSoftClassPtr ActorBpClass = TSoftClassPtr( + FSoftObjectPath(bpPaths[i]) + ); + LoadedBpAssets.Push(ActorBpClass.LoadSynchronous()); + } + + for (int i = 0; i < 5; ++i) { + FTransform SpawnLocationAndRotation(Rotation); + SpawnLocationAndRotation.SetLocation(Location); + AActor *Spawned = GetWorld()->SpawnActorDeferred( + LoadedBpAssets[i % 2], SpawnLocationAndRotation); + dynamic_cast(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( + LoadedBpAssets[i % 2], SpawnLocationAndRotation); + dynamic_cast(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 PlayerStartIterator(GetWorld()); PlayerStartIterator; ++PlayerStartIterator) - { - const auto PlayerStart = *PlayerStartIterator; - const UClass* PawnClass = GetDefaultPawnClassForController(Player); - const APawn* PawnToFit = PawnClass ? PawnClass->GetDefaultObject() : 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 PlayerStartIterator(GetWorld()); + PlayerStartIterator; ++PlayerStartIterator) { + const auto PlayerStart = *PlayerStartIterator; + const UClass *PawnClass = GetDefaultPawnClassForController(Player); + const APawn *PawnToFit = PawnClass + ? PawnClass->GetDefaultObject() + : 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(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(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(UGameplayStatics::GetPlayerController(GetWorld(), PlayerIndex)); +AMyPlayerController *AMyGameMode::GetMyPlayerController( + uint8 const PlayerIndex) const { + return dynamic_cast( + UGameplayStatics::GetPlayerController(GetWorld(), PlayerIndex)); } diff --git a/Source/TurnBasedTutorial/MyGameMode.h b/Source/TurnBasedTutorial/MyGameMode.h index 265066c..72ca264 100644 --- a/Source/TurnBasedTutorial/MyGameMode.h +++ b/Source/TurnBasedTutorial/MyGameMode.h @@ -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 SpawnPoints{}; + UPROPERTY() + TMap 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}; }; diff --git a/Source/TurnBasedTutorial/MyPawn.cpp b/Source/TurnBasedTutorial/MyPawn.cpp index 640053e..0895d91 100644 --- a/Source/TurnBasedTutorial/MyPawn.cpp +++ b/Source/TurnBasedTutorial/MyPawn.cpp @@ -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); } - diff --git a/Source/TurnBasedTutorial/MyPawn.h b/Source/TurnBasedTutorial/MyPawn.h index 40953df..04a4157 100644 --- a/Source/TurnBasedTutorial/MyPawn.h +++ b/Source/TurnBasedTutorial/MyPawn.h @@ -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; }; diff --git a/Source/TurnBasedTutorial/MyPlayerController.cpp b/Source/TurnBasedTutorial/MyPlayerController.cpp index 991d020..2eb244c 100644 --- a/Source/TurnBasedTutorial/MyPlayerController.cpp +++ b/Source/TurnBasedTutorial/MyPlayerController.cpp @@ -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(UGameplayStatics::GetGameMode(GetWorld())); +auto AMyPlayerController::GetMyGameMode() const { + return dynamic_cast( + 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& OutLifetimeProps) const -{ - DOREPLIFETIME(AMyPlayerController, PlayerIndex); +void AMyPlayerController::GetLifetimeReplicatedProps( + TArray &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(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( + 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; + } } diff --git a/Source/TurnBasedTutorial/MyPlayerController.h b/Source/TurnBasedTutorial/MyPlayerController.h index 195aee2..8b171b8 100644 --- a/Source/TurnBasedTutorial/MyPlayerController.h +++ b/Source/TurnBasedTutorial/MyPlayerController.h @@ -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; }; diff --git a/Source/TurnBasedTutorial/Trooper.cpp b/Source/TurnBasedTutorial/Trooper.cpp index 3a594fc..631dc70 100644 --- a/Source/TurnBasedTutorial/Trooper.cpp +++ b/Source/TurnBasedTutorial/Trooper.cpp @@ -1,81 +1,91 @@ #include "Trooper.h" #include -#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("Mesh"); - RootComponent = MyStaticMesh; - static ConstructorHelpers::FObjectFinder 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("Mesh"); + RootComponent = MyStaticMesh; + MeshPath = TEXT("StaticMesh'/Game/StarterContent/Props/SM_Chair.SM_Chair'"); + static ConstructorHelpers::FObjectFinder MeshToUse(MeshPath); + if (MeshToUse.Object) { + MyStaticMesh->SetStaticMesh(MeshToUse.Object); + } } +// void ATrooper::SetStaticMesh() const { +// static ConstructorHelpers::FObjectFinder 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& OutLifetimeProps) const -{ - DOREPLIFETIME(ATrooper, PlayerIndex); - DOREPLIFETIME(ATrooper, CurrentLocation); - DOREPLIFETIME(ATrooper, TargetLocation); - DOREPLIFETIME(ATrooper, bIsMoving); - DOREPLIFETIME(ATrooper, Id); +void ATrooper::GetLifetimeReplicatedProps( + TArray &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(); +// } diff --git a/Source/TurnBasedTutorial/Trooper.h b/Source/TurnBasedTutorial/Trooper.h index 46cf1ae..70bda09 100644 --- a/Source/TurnBasedTutorial/Trooper.h +++ b/Source/TurnBasedTutorial/Trooper.h @@ -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(); +// +// };