diff --git a/Config/DefaultEngine.ini b/Config/DefaultEngine.ini index ac3dc22..0d2ee23 100644 --- a/Config/DefaultEngine.ini +++ b/Config/DefaultEngine.ini @@ -26,3 +26,6 @@ AppliedDefaultGraphicsPerformance=Maximum +ActiveGameNameRedirects=(OldGameName="/Script/TP_Blank",NewGameName="/Script/TurnBasedTutorial") +ActiveClassRedirects=(OldClassName="TP_BlankGameModeBase",NewClassName="TurnBasedTutorialGameModeBase") + +[CoreRedirects] ++PropertyRedirects=(OldName="/Script/TurnBasedTutorial.Trooper.OnPlayersSide",NewName="/Script/TurnBasedTutorial.Trooper.bOnPlayersSide") \ No newline at end of file diff --git a/Content/BP_MyGameMode.uasset b/Content/BP_MyGameMode.uasset index f27bd1b..aec131a 100644 Binary files a/Content/BP_MyGameMode.uasset and b/Content/BP_MyGameMode.uasset differ diff --git a/Content/BP_MyPawn.uasset b/Content/BP_MyPawn.uasset new file mode 100644 index 0000000..255d436 Binary files /dev/null and b/Content/BP_MyPawn.uasset differ diff --git a/Content/BP_MyPlayerController.uasset b/Content/BP_MyPlayerController.uasset new file mode 100644 index 0000000..ca2c84f Binary files /dev/null and b/Content/BP_MyPlayerController.uasset differ diff --git a/Content/BattleField/BattleFieldMap.umap b/Content/BattleField/BattleFieldMap.umap index 9b153aa..d3fb3a4 100644 Binary files a/Content/BattleField/BattleFieldMap.umap and b/Content/BattleField/BattleFieldMap.umap differ diff --git a/Source/TurnBasedTutorial/MyGameMode.cpp b/Source/TurnBasedTutorial/MyGameMode.cpp index 9dc7afe..3f36c01 100644 --- a/Source/TurnBasedTutorial/MyGameMode.cpp +++ b/Source/TurnBasedTutorial/MyGameMode.cpp @@ -12,13 +12,48 @@ AMyGameMode::AMyGameMode() : Super() DefaultPawnClass = AMyPawn::StaticClass(); } +void AMyGameMode::BeginPlay() +{ + Super::BeginPlay(); + ATrooper::InitNumberOfTroopersForId(); + UE_LOG(LogTemp, Warning, TEXT("GameMode BeginPlay")); + if (GetWorld()->GetMapName().Contains("BattleFieldMap")) + { + UE_LOG(LogTemp, Warning, TEXT("Player Logined")); + InitializeBattleField(); + GetPlayerController()->StartTurn(); + } +} + +void AMyGameMode::InitializeBattleField() const +{ + FVector Location(2000.0f, -1000.0f, 0.0f); + FRotator Rotation(0.0f, 180.0f, 0.0f); + FActorSpawnParameters const SpawnInfo; + for (int i = 0; i < 5; ++i) + { + AActor* Spawned = GetWorld()->SpawnActor(Location, Rotation, SpawnInfo); + dynamic_cast(Spawned)->InitTrooper(Location, true); + Location += {0.f, 500.f, 0.0f}; + } + Location = {-2000.0f, -1000.0f, 0.0f}; + Rotation = {0.0f, 0.0f, 0.0f}; + for (int i = 0; i < 5; ++i) + { + AActor* Spawned = GetWorld()->SpawnActor(Location, Rotation, SpawnInfo); + dynamic_cast(Spawned)->InitTrooper(Location, false); + Location += {0.f, 500.f, 0.0f}; + } +} + + AActor* AMyGameMode::ChoosePlayerStart_Implementation(AController* Player) { UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart %d"), GetNumPlayers()); InitializeSpawnPointsIfNeeded(Player); - auto ptr = *SpawnPoints.Find(GetNumPlayers()); - UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart end %d"), ptr->GetPlayerIndex()); - return ptr; + const auto CurrentPlayerStart = *SpawnPoints.Find(GetNumPlayers()); + UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart end %d"), CurrentPlayerStart->GetPlayerIndex()); + return CurrentPlayerStart; } void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player) @@ -27,13 +62,13 @@ void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player) { return; } - auto World = GetWorld(); + const auto World = GetWorld(); for (TActorIterator PlayerStartIterator(GetWorld()); PlayerStartIterator; ++PlayerStartIterator) { - auto PlayerStart = *PlayerStartIterator; - UClass* PawnClass = GetDefaultPawnClassForController(Player); - APawn* PawnToFit = PawnClass ? PawnClass->GetDefaultObject() : nullptr; - FVector ActorLocation = PlayerStart->GetActorLocation(); + 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(); if (!World->EncroachingBlockingGeometry(PawnToFit, ActorLocation, ActorRotation)) { @@ -42,3 +77,9 @@ void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player) } } } + + +AMyPlayerController* AMyGameMode::GetPlayerController() const +{ + return dynamic_cast(UGameplayStatics::GetPlayerController(GetWorld(), 0)); +} diff --git a/Source/TurnBasedTutorial/MyGameMode.h b/Source/TurnBasedTutorial/MyGameMode.h index a0bb877..9d0eb1b 100644 --- a/Source/TurnBasedTutorial/MyGameMode.h +++ b/Source/TurnBasedTutorial/MyGameMode.h @@ -22,8 +22,14 @@ public: virtual AActor* ChoosePlayerStart_Implementation(AController* Player) override; + virtual void BeginPlay() override; + private: - void InitializeSpawnPointsIfNeeded(AController *Player); + void InitializeSpawnPointsIfNeeded(AController* Player); + + void InitializeBattleField() const; TMap SpawnPoints; + + AMyPlayerController* GetPlayerController() const; }; diff --git a/Source/TurnBasedTutorial/MyPawn.cpp b/Source/TurnBasedTutorial/MyPawn.cpp index b250cbe..640053e 100644 --- a/Source/TurnBasedTutorial/MyPawn.cpp +++ b/Source/TurnBasedTutorial/MyPawn.cpp @@ -4,32 +4,32 @@ #include "MyPawn.h" -// // 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; -// -// } -// -// // Called when the game starts or when spawned -// void AMyPawn::BeginPlay() -// { -// Super::BeginPlay(); -// -// } -// -// // Called every frame -// void AMyPawn::Tick(float DeltaTime) -// { -// Super::Tick(DeltaTime); -// -// } -// -// // Called to bind functionality to input -// void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) -// { -// Super::SetupPlayerInputComponent(PlayerInputComponent); -// -// } -// +// 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; + +} + +// Called when the game starts or when spawned +void AMyPawn::BeginPlay() +{ + Super::BeginPlay(); + +} + +// Called every frame +void AMyPawn::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); + +} + +// Called to bind functionality to input +void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) +{ + Super::SetupPlayerInputComponent(PlayerInputComponent); + +} + diff --git a/Source/TurnBasedTutorial/MyPawn.h b/Source/TurnBasedTutorial/MyPawn.h index 2049c14..40953df 100644 --- a/Source/TurnBasedTutorial/MyPawn.h +++ b/Source/TurnBasedTutorial/MyPawn.h @@ -11,21 +11,21 @@ class TURNBASEDTUTORIAL_API AMyPawn : public APawn { GENERATED_BODY() -// public: -// // Sets default values for this pawn's properties -// AMyPawn(); -// -// protected: -// // 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: + // Sets default values for this pawn's properties + AMyPawn(); + +protected: + // 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; + + }; diff --git a/Source/TurnBasedTutorial/MyPlayerController.cpp b/Source/TurnBasedTutorial/MyPlayerController.cpp index 6504d78..76ee933 100644 --- a/Source/TurnBasedTutorial/MyPlayerController.cpp +++ b/Source/TurnBasedTutorial/MyPlayerController.cpp @@ -2,62 +2,76 @@ #include "MyPlayerController.h" -// AMyPlayerController::AMyPlayerController() : Super(), IsMyTurn(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::StartTurn() { -// IsMyTurn = true; -// UE_LOG(LogTemp, Warning, TEXT("Your turn")); -// } -// -// void AMyPlayerController::EndTurn() { -// IsMyTurn = false; -// UE_LOG(LogTemp, Warning, TEXT("Not your turn")); -// } -// -// void AMyPlayerController::SetTrooperIsMoving(bool isMoving) { -// IsThereTrooperMoving = isMoving; -// } -// -// void AMyPlayerController::OnLeftMouseClick() { -// if (IsThereTrooperMoving) { -// return; -// } -// UE_LOG(LogTemp, Warning, TEXT("Mouse clicked")); -// FHitResult HitResult; -// bool IsHitResult = GetHitResultUnderCursorByChannel(TraceTypeQuery1, false, HitResult); -// if (IsHitResult) { -// AActor *actor = HitResult.Actor.Get(); -// if (actor->ActorHasTag(FName("Trooper"))) { -// ATrooper* trooper = dynamic_cast(actor); -// if (trooper != nullptr && trooper != SelectedTrooper) { -// if (trooper->IsOnPlayersSide()) { -// UE_LOG(LogTemp, Warning, TEXT("Hitted trooper id: %d, on our side"), -// trooper->GetId()); -// SelectedTrooper = trooper; -// } -// else { -// UE_LOG(LogTemp, Warning, TEXT("Hitted trooper id: %d, enemy"), -// trooper->GetId()); -// UE_LOG(LogTemp, Warning, TEXT("Trooper #%d hit enemy trooper #%d"), -// SelectedTrooper->GetId(), trooper->GetId()); -// } -// } -// } -// else if (actor->ActorHasTag(FName("Floor"))) { -// UE_LOG(LogTemp, Warning, TEXT("Hitted floor: %f, %f, %f"), HitResult.Location.X, -// HitResult.Location.Y, HitResult.Location.Z); -// if (SelectedTrooper != nullptr) { -// SelectedTrooper->MoveTrooper(HitResult.Location); -// IsThereTrooperMoving = true; -// } -// } -// } -// } +AMyPlayerController::AMyPlayerController() : Super(), IsMyTurn(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::StartTurn() +{ + IsMyTurn = true; + UE_LOG(LogTemp, Warning, TEXT("Your turn")); +} + +void AMyPlayerController::EndTurn() +{ + IsMyTurn = false; + UE_LOG(LogTemp, Warning, TEXT("Not your turn")); +} + +void AMyPlayerController::SetTrooperIsMoving(bool isMoving) +{ + IsThereTrooperMoving = isMoving; +} + +void AMyPlayerController::OnLeftMouseClick() +{ + if (IsThereTrooperMoving) + { + return; + } + UE_LOG(LogTemp, Warning, TEXT("Mouse clicked")); + FHitResult HitResult; + bool const IsHitResult = GetHitResultUnderCursorByChannel(TraceTypeQuery1, false, HitResult); + if (IsHitResult) + { + AActor* Actor = HitResult.Actor.Get(); + if (Actor->ActorHasTag(FName("Trooper"))) + { + ATrooper* Trooper = dynamic_cast(Actor); + if (Trooper != nullptr && Trooper != SelectedTrooper) + { + if (Trooper->IsOnPlayersSide()) + { + UE_LOG(LogTemp, Warning, TEXT("Hitted trooper id: %d, on our side"), + Trooper->GetId()); + SelectedTrooper = Trooper; + } + else + { + UE_LOG(LogTemp, Warning, TEXT("Hitted trooper id: %d, enemy"), + Trooper->GetId()); + UE_LOG(LogTemp, Warning, TEXT("Trooper #%d hit enemy trooper #%d"), + SelectedTrooper->GetId(), Trooper->GetId()); + } + } + } + else if (Actor->ActorHasTag(FName("Floor"))) + { + UE_LOG(LogTemp, Warning, TEXT("Hitted floor: %f, %f, %f"), HitResult.Location.X, + HitResult.Location.Y, HitResult.Location.Z); + if (SelectedTrooper != nullptr) + { + SelectedTrooper->MoveTrooper(HitResult.Location); + IsThereTrooperMoving = true; + } + } + } +} diff --git a/Source/TurnBasedTutorial/MyPlayerController.h b/Source/TurnBasedTutorial/MyPlayerController.h index 17b9cda..357e164 100644 --- a/Source/TurnBasedTutorial/MyPlayerController.h +++ b/Source/TurnBasedTutorial/MyPlayerController.h @@ -13,25 +13,24 @@ UCLASS() class TURNBASEDTUTORIAL_API AMyPlayerController : public APlayerController { GENERATED_BODY() - + public: - // AMyPlayerController(); - // - // void StartTurn(); - // - // void EndTurn(); - // - // virtual void SetupInputComponent() override; - // - // void SetTrooperIsMoving(bool isMoving); + AMyPlayerController(); + + void StartTurn(); + + void EndTurn(); + + virtual void SetupInputComponent() override; + + void SetTrooperIsMoving(bool isMoving); private: - // bool IsMyTurn; - // - // bool IsThereTrooperMoving = false; - // - // ATrooper* SelectedTrooper; - // - // void OnLeftMouseClick(); + bool IsMyTurn; + + bool IsThereTrooperMoving = false; + + ATrooper* SelectedTrooper; + void OnLeftMouseClick(); }; diff --git a/Source/TurnBasedTutorial/Trooper.cpp b/Source/TurnBasedTutorial/Trooper.cpp index 92ee67c..eac1cf6 100644 --- a/Source/TurnBasedTutorial/Trooper.cpp +++ b/Source/TurnBasedTutorial/Trooper.cpp @@ -3,7 +3,8 @@ #include "MyPlayerController.h" // Sets default values -ATrooper::ATrooper() { +ATrooper::ATrooper() +{ PrimaryActorTick.bCanEverTick = true; Tags.Add(FName("Trooper")); Id = NumberOfTroopersForId++; @@ -25,49 +26,59 @@ void ATrooper::BeginPlay() Super::BeginPlay(); } -void ATrooper::Tick(float deltaTime) { - // if (IsMoving) { - // FVector vector = (MoveToVector - Position); - // vector.Normalize(); - // vector *= (Speed * deltaTime); - // if (vector.Size() >= (MoveToVector - Position).Size()) { - // Position = MoveToVector; - // IsMoving = false; - // dynamic_cast( - // UGameplayStatics::GetPlayerController(GetWorld(), 0) - // )->SetTrooperIsMoving(false); - // } - // else { - // Position += vector; - // } - // SetActorLocation(Position); - // } +void ATrooper::Tick(float const DeltaTime) +{ + if (bIsMoving) + { + FVector vector = (MoveToVector - Position); + vector.Normalize(); + vector *= (Speed * DeltaTime); + if (vector.Size() >= (MoveToVector - Position).Size()) + { + Position = MoveToVector; + bIsMoving = false; + dynamic_cast( + UGameplayStatics::GetPlayerController(GetWorld(), 0) + )->SetTrooperIsMoving(false); + } + else + { + Position += vector; + } + SetActorLocation(Position); + } } -void ATrooper::MoveTrooper(FVector newPos) { - MoveToVector = newPos; - IsMoving = true; +void ATrooper::MoveTrooper(FVector const NewPos) +{ + MoveToVector = NewPos; + bIsMoving = true; } -int ATrooper::NumberOfTroopersForId = 0; +uint8 ATrooper::NumberOfTroopersForId = 0; -void ATrooper::InitNumberOfTroopersForId() { +void ATrooper::InitNumberOfTroopersForId() +{ NumberOfTroopersForId = 0; } -FVector ATrooper::GetPosition() { +FVector ATrooper::GetPosition() const +{ return Position; } -bool ATrooper::IsOnPlayersSide() { - return OnPlayersSide; +bool ATrooper::IsOnPlayersSide() const +{ + return bOnPlayersSide; } -int ATrooper::GetId() { +uint8 ATrooper::GetId() const +{ return Id; } -void ATrooper::InitTrooper(FVector position, bool onPlayersSide) { - Position = position; - OnPlayersSide = onPlayersSide; +void ATrooper::InitTrooper(FVector const NewPosition, bool const bIsOnPlayersSide) +{ + Position = NewPosition; + bOnPlayersSide = bIsOnPlayersSide; } diff --git a/Source/TurnBasedTutorial/Trooper.h b/Source/TurnBasedTutorial/Trooper.h index c6d2b75..fc1ee7c 100644 --- a/Source/TurnBasedTutorial/Trooper.h +++ b/Source/TurnBasedTutorial/Trooper.h @@ -1,4 +1,3 @@ - #pragma once #include "CoreMinimal.h" @@ -11,25 +10,25 @@ class TURNBASEDTUTORIAL_API ATrooper : public AActor { GENERATED_BODY() -public: +public: // Sets default values for this actor's properties ATrooper(); protected: - static int NumberOfTroopersForId; + static uint8 NumberOfTroopersForId; virtual void BeginPlay() override; - virtual void Tick(float deltaTime) override; + virtual void Tick(float const DeltaTime) override; UPROPERTY() FVector Position; UPROPERTY() - bool OnPlayersSide; + bool bOnPlayersSide; UPROPERTY() - int Id; + uint8 Id; UPROPERTY(VisibleAnywhere, BlueprintReadOnly) UStaticMeshComponent* Mesh; @@ -37,20 +36,20 @@ protected: UPROPERTY() float Speed = 300.0f; - bool IsMoving = false; + bool bIsMoving = false; FVector MoveToVector; public: - void MoveTrooper(FVector newPos); + void MoveTrooper(FVector const NewPos); static void InitNumberOfTroopersForId(); - FVector GetPosition(); + FVector GetPosition() const; - bool IsOnPlayersSide(); + bool IsOnPlayersSide() const; - int GetId(); + uint8 GetId() const; - void InitTrooper(FVector position, bool onPlayersSide); + void InitTrooper(FVector const NewPosition, bool const bIsOnPlayersSide); };