diff --git a/Content/BattleField/BattleFieldMap.umap b/Content/BattleField/BattleFieldMap.umap index 61b7e15..8f7cb0e 100644 Binary files a/Content/BattleField/BattleFieldMap.umap and b/Content/BattleField/BattleFieldMap.umap differ diff --git a/Content/MainMenu/MainMenuLevel.umap b/Content/MainMenu/MainMenuLevel.umap index 80e9e23..dbee8ab 100644 Binary files a/Content/MainMenu/MainMenuLevel.umap and b/Content/MainMenu/MainMenuLevel.umap differ diff --git a/Source/TurnBasedTutorial/MyGameMode.cpp b/Source/TurnBasedTutorial/MyGameMode.cpp index 5860a3d..d09b49d 100644 --- a/Source/TurnBasedTutorial/MyGameMode.cpp +++ b/Source/TurnBasedTutorial/MyGameMode.cpp @@ -11,6 +11,7 @@ AMyGameMode::AMyGameMode() : Super() { 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")); @@ -19,10 +20,21 @@ void AMyGameMode::BeginPlay() { } void AMyGameMode::StartGame() { - FVector Location(0.0f, 0.0f, 0.0f); - FRotator Rotation(0.0f, 0.0f, 0.0f); + FVector Location(2000.0f, -1000.0f, 0.0f); + FRotator Rotation(0.0f, 180.0f, 0.0f); FActorSpawnParameters SpawnInfo; - GetWorld()->SpawnActor(Location, Rotation, 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 }; + } GetPlayerController()->StartTurn(); } diff --git a/Source/TurnBasedTutorial/MyPlayerController.cpp b/Source/TurnBasedTutorial/MyPlayerController.cpp index 663eabd..f069d84 100644 --- a/Source/TurnBasedTutorial/MyPlayerController.cpp +++ b/Source/TurnBasedTutorial/MyPlayerController.cpp @@ -1,9 +1,8 @@ // Fill out your copyright notice in the Description page of Project Settings. - #include "MyPlayerController.h" -AMyPlayerController::AMyPlayerController() : Super(), isMyTurn(false) { +AMyPlayerController::AMyPlayerController() : Super(), IsMyTurn(false), SelectedTrooper(nullptr) { UE_LOG(LogTemp, Warning, TEXT("Player controller created")); } @@ -14,18 +13,51 @@ void AMyPlayerController::SetupInputComponent() { } void AMyPlayerController::StartTurn() { - isMyTurn = true; + IsMyTurn = true; UE_LOG(LogTemp, Warning, TEXT("Your turn")); } void AMyPlayerController::EndTurn() { - isMyTurn = false; + 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); - UE_LOG(LogTemp, Warning, TEXT("hitted: %s"), IsHitResult ? "Yes" : "No"); + 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 bdc7e25..90fb98a 100644 --- a/Source/TurnBasedTutorial/MyPlayerController.h +++ b/Source/TurnBasedTutorial/MyPlayerController.h @@ -1,8 +1,8 @@ // Fill out your copyright notice in the Description page of Project Settings. - #pragma once #include "CoreMinimal.h" +#include "Trooper.h" #include "GameFramework/PlayerController.h" #include "MyPlayerController.generated.h" @@ -23,8 +23,14 @@ public: virtual void SetupInputComponent() override; + void SetTrooperIsMoving(bool isMoving); + private: - bool isMyTurn; + bool IsMyTurn; + + bool IsThereTrooperMoving = false; + + ATrooper* SelectedTrooper; void OnLeftMouseClick(); diff --git a/Source/TurnBasedTutorial/Trooper.cpp b/Source/TurnBasedTutorial/Trooper.cpp index a762cb8..2315b23 100644 --- a/Source/TurnBasedTutorial/Trooper.cpp +++ b/Source/TurnBasedTutorial/Trooper.cpp @@ -1,7 +1,12 @@ #include "Trooper.h" +#include +#include "MyPlayerController.h" // Sets default values ATrooper::ATrooper() { + PrimaryActorTick.bCanEverTick = true; + Tags.Add(FName("Trooper")); + Id = NumberOfTroopersForId++; Position.Set(0, 0, 0); Mesh = CreateDefaultSubobject("Mesh"); RootComponent = Mesh; @@ -18,7 +23,51 @@ ATrooper::ATrooper() { 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::MoveTrooper(FVector newPos) { + MoveToVector = newPos; + IsMoving = true; +} + +int ATrooper::NumberOfTroopersForId = 0; + +void ATrooper::InitNumberOfTroopersForId() { + NumberOfTroopersForId = 0; +} +FVector ATrooper::GetPosition() { + return Position; +} + +bool ATrooper::IsOnPlayersSide() { + return OnPlayersSide; +} + +int ATrooper::GetId() { + return Id; +} + +void ATrooper::InitTrooper(FVector position, bool onPlayersSide) { + Position = position; + OnPlayersSide = onPlayersSide; +} diff --git a/Source/TurnBasedTutorial/Trooper.h b/Source/TurnBasedTutorial/Trooper.h index fbfe19d..c6d2b75 100644 --- a/Source/TurnBasedTutorial/Trooper.h +++ b/Source/TurnBasedTutorial/Trooper.h @@ -10,22 +10,47 @@ UCLASS() class TURNBASEDTUTORIAL_API ATrooper : public AActor { GENERATED_BODY() - + public: // Sets default values for this actor's properties ATrooper(); protected: - // Called when the game starts or when spawned + static int NumberOfTroopersForId; + virtual void BeginPlay() override; + virtual void Tick(float deltaTime) override; + UPROPERTY() FVector Position; + UPROPERTY() + bool OnPlayersSide; + + UPROPERTY() + int Id; + UPROPERTY(VisibleAnywhere, BlueprintReadOnly) UStaticMeshComponent* Mesh; -public: - void MoveTrooper(); + UPROPERTY() + float Speed = 300.0f; + + bool IsMoving = false; + + FVector MoveToVector; + +public: + void MoveTrooper(FVector newPos); + + static void InitNumberOfTroopersForId(); + + FVector GetPosition(); + + bool IsOnPlayersSide(); + + int GetId(); + void InitTrooper(FVector position, bool onPlayersSide); };