added movement, definition of side of trooper, 5 troopers on each side, moved camera etc

pull/4/head
Maxim 2 years ago
parent 4c09614bb6
commit a6f936e73e

@ -11,6 +11,7 @@ AMyGameMode::AMyGameMode() : Super() {
void AMyGameMode::BeginPlay() { void AMyGameMode::BeginPlay() {
Super::BeginPlay(); Super::BeginPlay();
ATrooper::InitNumberOfTroopersForId();
UE_LOG(LogTemp, Warning, TEXT("GameMode BeginPlay")); UE_LOG(LogTemp, Warning, TEXT("GameMode BeginPlay"));
if (GetWorld()->GetMapName().Contains("BattleFieldMap")) { if (GetWorld()->GetMapName().Contains("BattleFieldMap")) {
UE_LOG(LogTemp, Warning, TEXT("Player Logined")); UE_LOG(LogTemp, Warning, TEXT("Player Logined"));
@ -19,10 +20,21 @@ void AMyGameMode::BeginPlay() {
} }
void AMyGameMode::StartGame() { void AMyGameMode::StartGame() {
FVector Location(0.0f, 0.0f, 0.0f); FVector Location(2000.0f, -1000.0f, 0.0f);
FRotator Rotation(0.0f, 0.0f, 0.0f); FRotator Rotation(0.0f, 180.0f, 0.0f);
FActorSpawnParameters SpawnInfo; FActorSpawnParameters SpawnInfo;
GetWorld()->SpawnActor<ATrooper>(Location, Rotation, SpawnInfo); for (int i = 0; i < 5; ++i) {
AActor *spawned = GetWorld()->SpawnActor<ATrooper>(Location, Rotation, SpawnInfo);
dynamic_cast<ATrooper*>(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<ATrooper>(Location, Rotation, SpawnInfo);
dynamic_cast<ATrooper*>(spawned)->InitTrooper(Location, false);
Location += { 0.f, 500.f, 0.0f };
}
GetPlayerController()->StartTurn(); GetPlayerController()->StartTurn();
} }

@ -1,9 +1,8 @@
// Fill out your copyright notice in the Description page of Project Settings. // Fill out your copyright notice in the Description page of Project Settings.
#include "MyPlayerController.h" #include "MyPlayerController.h"
AMyPlayerController::AMyPlayerController() : Super(), isMyTurn(false) { AMyPlayerController::AMyPlayerController() : Super(), IsMyTurn(false), SelectedTrooper(nullptr) {
UE_LOG(LogTemp, Warning, TEXT("Player controller created")); UE_LOG(LogTemp, Warning, TEXT("Player controller created"));
} }
@ -14,18 +13,51 @@ void AMyPlayerController::SetupInputComponent() {
} }
void AMyPlayerController::StartTurn() { void AMyPlayerController::StartTurn() {
isMyTurn = true; IsMyTurn = true;
UE_LOG(LogTemp, Warning, TEXT("Your turn")); UE_LOG(LogTemp, Warning, TEXT("Your turn"));
} }
void AMyPlayerController::EndTurn() { void AMyPlayerController::EndTurn() {
isMyTurn = false; IsMyTurn = false;
UE_LOG(LogTemp, Warning, TEXT("Not your turn")); UE_LOG(LogTemp, Warning, TEXT("Not your turn"));
} }
void AMyPlayerController::SetTrooperIsMoving(bool isMoving) {
IsThereTrooperMoving = isMoving;
}
void AMyPlayerController::OnLeftMouseClick() { void AMyPlayerController::OnLeftMouseClick() {
if (IsThereTrooperMoving) {
return;
}
UE_LOG(LogTemp, Warning, TEXT("Mouse clicked")); UE_LOG(LogTemp, Warning, TEXT("Mouse clicked"));
FHitResult HitResult; FHitResult HitResult;
bool IsHitResult = GetHitResultUnderCursorByChannel(TraceTypeQuery1, false, 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<ATrooper*>(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;
}
}
}
} }

@ -1,8 +1,8 @@
// Fill out your copyright notice in the Description page of Project Settings. // Fill out your copyright notice in the Description page of Project Settings.
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "Trooper.h"
#include "GameFramework/PlayerController.h" #include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h" #include "MyPlayerController.generated.h"
@ -23,8 +23,14 @@ public:
virtual void SetupInputComponent() override; virtual void SetupInputComponent() override;
void SetTrooperIsMoving(bool isMoving);
private: private:
bool isMyTurn; bool IsMyTurn;
bool IsThereTrooperMoving = false;
ATrooper* SelectedTrooper;
void OnLeftMouseClick(); void OnLeftMouseClick();

@ -1,7 +1,12 @@
#include "Trooper.h" #include "Trooper.h"
#include <Kismet/GameplayStatics.h>
#include "MyPlayerController.h"
// Sets default values // Sets default values
ATrooper::ATrooper() { ATrooper::ATrooper() {
PrimaryActorTick.bCanEverTick = true;
Tags.Add(FName("Trooper"));
Id = NumberOfTroopersForId++;
Position.Set(0, 0, 0); Position.Set(0, 0, 0);
Mesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh"); Mesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
RootComponent = Mesh; RootComponent = Mesh;
@ -18,7 +23,51 @@ ATrooper::ATrooper() {
void ATrooper::BeginPlay() void ATrooper::BeginPlay()
{ {
Super::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<AMyPlayerController*>(
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;
}

@ -10,22 +10,47 @@ UCLASS()
class TURNBASEDTUTORIAL_API ATrooper : public AActor class TURNBASEDTUTORIAL_API ATrooper : public AActor
{ {
GENERATED_BODY() GENERATED_BODY()
public: public:
// Sets default values for this actor's properties // Sets default values for this actor's properties
ATrooper(); ATrooper();
protected: protected:
// Called when the game starts or when spawned static int NumberOfTroopersForId;
virtual void BeginPlay() override; virtual void BeginPlay() override;
virtual void Tick(float deltaTime) override;
UPROPERTY() UPROPERTY()
FVector Position; FVector Position;
UPROPERTY()
bool OnPlayersSide;
UPROPERTY()
int Id;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly) UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UStaticMeshComponent* Mesh; UStaticMeshComponent* Mesh;
public: UPROPERTY()
void MoveTrooper(); 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);
}; };

Loading…
Cancel
Save