Added selection trooper and view of action radius

pull/6/head
m4xxx1m 2 years ago
parent d7738e00a6
commit ab6ba0a53e

Binary file not shown.

@ -143,8 +143,7 @@ void AMyGameMode::PostLogin(APlayerController *NewPlayer) {
} }
} }
void AMyGameMode::StartGame_Implementation() {
void AMyGameMode::StartGame() {
InitializeBattleField(); InitializeBattleField();
// PlayerNotInTurn()->SetEnemySelection(Troopers); // PlayerNotInTurn()->SetEnemySelection(Troopers);
// PlayerInTurn()->SetEnemySelection(Troopers); // PlayerInTurn()->SetEnemySelection(Troopers);
@ -154,6 +153,16 @@ void AMyGameMode::StartGame() {
} }
// void AMyGameMode::StartGame() {
// InitializeBattleField();
// // PlayerNotInTurn()->SetEnemySelection(Troopers);
// // PlayerInTurn()->SetEnemySelection(Troopers);
//
// // PlayerInTurn()->StartTurn();
// GetMyGameState()->StartGame();
// }
// AMyPlayerController *AMyGameMode::PlayerInTurn() const { // AMyPlayerController *AMyGameMode::PlayerInTurn() const {
// return GetMyPlayerController(CurrentPlayerTurn); // return GetMyPlayerController(CurrentPlayerTurn);
// } // }

@ -38,7 +38,7 @@ private:
// UPROPERTY() // UPROPERTY()
// mutable TArray<ATrooper *> Troopers; // mutable TArray<ATrooper *> Troopers;
UFUNCTION(BlueprintCallable) UFUNCTION(Server, Reliable)
void StartGame(); void StartGame();
// UFUNCTION(BlueprintPure) // UFUNCTION(BlueprintPure)

@ -18,12 +18,18 @@ void AMyGameState::AddTrooper(ATrooper *Trooper) {
Troopers.Add(Trooper); Troopers.Add(Trooper);
} }
void AMyGameState::StartGame() const { void AMyGameState::StartGame_Implementation() const {
// PlayerNotInTurn()->SetEnemySelection(Troopers); PlayerNotInTurn()->SetEnemySelection(Troopers);
// PlayerInTurn()->SetEnemySelection(Troopers); PlayerInTurn()->SetEnemySelection(Troopers);
PlayerInTurn()->StartTurn(); PlayerInTurn()->StartTurn();
} }
// void AMyGameState::StartGame() const {
// PlayerNotInTurn()->SetEnemySelection(Troopers);
// PlayerInTurn()->SetEnemySelection(Troopers);
// PlayerInTurn()->StartTurn();
// }
void AMyGameState::CycleTurns_Implementation() { void AMyGameState::CycleTurns_Implementation() {
PlayerInTurn()->EndTurn(); PlayerInTurn()->EndTurn();
for (const auto Trooper : Troopers) { for (const auto Trooper : Troopers) {

@ -20,7 +20,7 @@ public:
UFUNCTION() UFUNCTION()
void AddTrooper(ATrooper *Trooper); void AddTrooper(ATrooper *Trooper);
UFUNCTION() UFUNCTION(Server, Reliable)
void StartGame() const; void StartGame() const;
UFUNCTION(BlueprintCallable, Server, Reliable) UFUNCTION(BlueprintCallable, Server, Reliable)

@ -145,14 +145,14 @@ void AMyPlayerController::SetPlayerIndex(uint8 NewPlayerIndex) {
// } // }
void AMyPlayerController::SetEnemySelection_Implementation( // void AMyPlayerController::SetEnemySelection_Implementation(
const TArray<ATrooper *> &Troopers) const { // const TArray<ATrooper *> &Troopers) const {
for (const auto Trooper : Troopers) { // for (const auto Trooper : Troopers) {
if (Trooper != nullptr && Trooper->GetPlayerIndex() != PlayerIndex) { // if (Trooper != nullptr && Trooper->GetPlayerIndex() != PlayerIndex) {
Trooper->HighlightAsEnemy(); // Trooper->HighlightAsEnemy();
} // }
} // }
} // }
// void AMyPlayerController::SetEnemySelection_Implementation() { // void AMyPlayerController::SetEnemySelection_Implementation() {

@ -43,8 +43,8 @@ public:
// UFUNCTION(BlueprintCallable) // UFUNCTION(BlueprintCallable)
// float SetCurrentActionAndReturnRadius(int action); // float SetCurrentActionAndReturnRadius(int action);
UFUNCTION(Client, Reliable) // UFUNCTION(Client, Reliable)
void SetEnemySelection(const TArray<ATrooper *> &Troopers) const; // void SetEnemySelection(const TArray<ATrooper *> &Troopers) const;
private: private:
// UPROPERTY(Replicated) // UPROPERTY(Replicated)

@ -19,6 +19,15 @@ void AMyPlayerState::SetPlayerIndex(uint8 NewPlayerIndex) {
PlayerIndex = NewPlayerIndex; PlayerIndex = NewPlayerIndex;
} }
void AMyPlayerState::SetEnemySelection_Implementation(
const TArray<ATrooper *> &Troopers) const {
for (const auto Trooper : Troopers) {
if (Trooper != nullptr && Trooper->GetPlayerIndex() != PlayerIndex) {
Trooper->HighlightAsEnemy();
}
}
}
void AMyPlayerState::MoveTrooper_Implementation(ATrooper *Trooper, void AMyPlayerState::MoveTrooper_Implementation(ATrooper *Trooper,
FVector Location) { FVector Location) {
if (Trooper->CheckMoveCorrectness(Location)) { if (Trooper->CheckMoveCorrectness(Location)) {
@ -74,7 +83,7 @@ void AMyPlayerState::EndTurn_Implementation() {
UE_LOG(LogTemp, Warning, TEXT("End Turn from player %d"), PlayerIndex); UE_LOG(LogTemp, Warning, TEXT("End Turn from player %d"), PlayerIndex);
SetMyTurn(false); SetMyTurn(false);
if (SelectedTrooper) { if (SelectedTrooper) {
SelectedTrooper->SetSelection(false); SelectedTrooper->SetSelection(false, CurrentAction);
SelectedTrooper = nullptr; SelectedTrooper = nullptr;
} }
UE_LOG(LogTemp, Warning, TEXT("Not your turn, %d"), PlayerIndex); UE_LOG(LogTemp, Warning, TEXT("Not your turn, %d"), PlayerIndex);
@ -106,7 +115,7 @@ void AMyPlayerState::OnPlayerAction(const FHitResult &HitResult) {
// move this mf // move this mf
MoveTrooper(SelectedTrooper, NewlySelectedLocation); MoveTrooper(SelectedTrooper, NewlySelectedLocation);
// and reset the selection.... // and reset the selection....
SelectedTrooper->SetSelection(false); SelectedTrooper->SetSelection(false, CurrentAction);
SelectedTrooper = nullptr; SelectedTrooper = nullptr;
break; break;
default: default:
@ -114,7 +123,7 @@ void AMyPlayerState::OnPlayerAction(const FHitResult &HitResult) {
UE_LOG(LogTemp, Warning, TEXT("Do attack")); UE_LOG(LogTemp, Warning, TEXT("Do attack"));
Attack(SelectedTrooper, NewlySelectedLocation, Attack(SelectedTrooper, NewlySelectedLocation,
CurrentAction); CurrentAction);
SelectedTrooper->SetSelection(false); SelectedTrooper->SetSelection(false, CurrentAction);
SelectedTrooper = nullptr; SelectedTrooper = nullptr;
break; break;
} }
@ -125,15 +134,18 @@ void AMyPlayerState::OnPlayerAction(const FHitResult &HitResult) {
UE_LOG(LogTemp, Warning, TEXT("Do reselect")); UE_LOG(LogTemp, Warning, TEXT("Do reselect"));
// our move, selection // our move, selection
if (SelectedTrooper) { if (SelectedTrooper) {
SelectedTrooper->SetSelection(false); SelectedTrooper->SetSelection(false, CurrentAction);
} }
SelectedTrooper = NewlySelectedTrooper; SelectedTrooper = NewlySelectedTrooper;
SelectedTrooper->SetSelection(true); SelectedTrooper->SetSelection(true, CurrentAction);
} }
} }
void AMyPlayerState::SetCurrentAction_Implementation(int Action) { void AMyPlayerState::SetCurrentAction_Implementation(int Action) {
CurrentAction = Action; CurrentAction = Action;
if (SelectedTrooper) {
SelectedTrooper->UpdateSelectionRadius(Action);
}
UE_LOG(LogTemp, Warning, TEXT("SetCurrentAction: %d on Player Controller " UE_LOG(LogTemp, Warning, TEXT("SetCurrentAction: %d on Player Controller "
"with index %d"), CurrentAction, PlayerIndex); "with index %d"), CurrentAction, PlayerIndex);
} }

@ -47,6 +47,9 @@ public:
UFUNCTION() UFUNCTION()
void SetPlayerIndex(uint8 NewPlayerIndex); void SetPlayerIndex(uint8 NewPlayerIndex);
UFUNCTION(Client, Reliable)
void SetEnemySelection(const TArray<ATrooper *> &Troopers) const;
private: private:
UPROPERTY(Replicated) UPROPERTY(Replicated)
uint8 PlayerIndex; uint8 PlayerIndex;

@ -2,6 +2,7 @@
#include <Kismet/GameplayStatics.h> #include <Kismet/GameplayStatics.h>
#include "HealthBar.h" #include "HealthBar.h"
#include "MyPlayerState.h"
#include "Components/WidgetComponent.h" #include "Components/WidgetComponent.h"
#include "Net/UnrealNetwork.h" #include "Net/UnrealNetwork.h"
@ -26,7 +27,7 @@ ATrooper::ATrooper()
"StaticMesh'/Game/StarterContent/Shapes/Shape_Cylinder.Shape_Cylinder'")); "StaticMesh'/Game/StarterContent/Shapes/Shape_Cylinder.Shape_Cylinder'"));
SelectionStaticMesh->AttachToComponent(RootComponent, SelectionStaticMesh->AttachToComponent(RootComponent,
FAttachmentTransformRules::KeepRelativeTransform); FAttachmentTransformRules::KeepRelativeTransform);
SelectionStaticMesh->SetRelativeScale3D({1.8, 1.8, 0.01}); SelectionStaticMesh->SetWorldScale3D({2.0f, 2.0f, 0.01f});
SelectionStaticMesh->SetVisibility(false); SelectionStaticMesh->SetVisibility(false);
if (MeshToUse.Object) { if (MeshToUse.Object) {
@ -97,6 +98,18 @@ void ATrooper::Tick(float const DeltaTime) {
} }
} }
// void ATrooper::OnRepNotify_PlayerIndex() {
// UE_LOG(LogTemp, Warning,
// TEXT("On rep notify, index: %d, id: %d, on server: %d, player state: %d"),
// PlayerIndex, Id, GetNetMode() == NM_DedicatedServer, GetPlayerState() != nullptr);
// // if (GetNetMode() != NM_DedicatedServer && GetPlayerState()) {
// // if (Cast<AMyPlayerState>(GetPlayerState())->GetPlayerIndex() !=
// // PlayerIndex) {
// // HighlightAsEnemy();
// // }
// // }
// }
void ATrooper::MoveTrooper(FVector const NewPos) { void ATrooper::MoveTrooper(FVector const NewPos) {
TargetLocation = NewPos; TargetLocation = NewPos;
bIsMoving = true; bIsMoving = true;
@ -144,15 +157,18 @@ FVector ATrooper::GetLocation() const {
return CurrentLocation; return CurrentLocation;
} }
float ATrooper::GetActionRadius(int action) const { float ATrooper::GetActionRadius(int action) const {
switch (action) { switch (action) {
case 1: case 1:
return AttackAbility->ActionRadius; return AttackAbility->ActionCost <= ActionPoints
? AttackAbility->ActionRadius
: 0;
case 2: case 2:
return SpecialAbility->ActionRadius; return SpecialAbility->ActionCost <= ActionPoints
? SpecialAbility->ActionRadius
: 0;
default: default:
return ActionPoints; return ActionPoints / MoveCost;
} }
} }
@ -164,16 +180,29 @@ float ATrooper::GetMaxHitPoints() const {
return StartHitPoints; return StartHitPoints;
} }
void ATrooper::SetSelection(bool Selection) const { void ATrooper::SetSelection(bool Selection, uint8 ActionType) const {
if (SelectionStaticMesh) { if (SelectionStaticMesh) {
if (SelectionStaticMesh->GetMaterial(0) != GreenMaterial) { if (SelectionStaticMesh->GetMaterial(0) != GreenMaterial) {
SelectionStaticMesh->SetMaterial(0, GreenMaterial); SelectionStaticMesh->SetMaterial(0, GreenMaterial);
SelectionStaticMesh->SetRelativeLocation({0, 0, 1});
}
if (Selection) {
UpdateSelectionRadius(ActionType);
} else {
SelectionStaticMesh->SetWorldScale3D({2.f, 2.f, 0.01f});
} }
SelectionStaticMesh->SetVisibility(Selection); SelectionStaticMesh->SetVisibility(Selection);
} }
} }
void ATrooper::HighlightAsEnemy() const { void ATrooper::UpdateSelectionRadius(uint8 ActionType) const {
const float radiusScale =
GetActionRadius(ActionType) / PIXELS_IN_RADIUS;
SelectionStaticMesh->SetWorldScale3D(
{radiusScale, radiusScale, 0.01f});
}
void ATrooper::HighlightAsEnemy_Implementation() const {
SelectionStaticMesh->SetVisibility(true); SelectionStaticMesh->SetVisibility(true);
} }
@ -215,8 +244,7 @@ bool ATrooper::CheckMoveCorrectness(const FVector newPos) const {
bool ATrooper::CheckAttackCorrectness(const FVector attackLocation, bool ATrooper::CheckAttackCorrectness(const FVector attackLocation,
int abilityIndex) const { int abilityIndex) const {
return GetAbility(abilityIndex) != nullptr && ( return GetAbility(abilityIndex) != nullptr && (
attackLocation - CurrentLocation).Size() <= attackLocation - CurrentLocation).Size() < GetActionRadius(
GetAbility(abilityIndex)->ActionRadius && ActionPoints >= abilityIndex);
GetAbility(abilityIndex)->ActionCost;
// return (attackLocation - CurrentLocation).Size() <= AttackRadius; // return (attackLocation - CurrentLocation).Size() <= AttackRadius;
} }

@ -51,9 +51,12 @@ public:
float GetMaxHitPoints() const; float GetMaxHitPoints() const;
UFUNCTION() UFUNCTION()
void SetSelection(bool Selected) const; void SetSelection(bool Selected, uint8 ActionType) const;
UFUNCTION() UFUNCTION()
void UpdateSelectionRadius(uint8 ActionType) const;
UFUNCTION(Client, Reliable)
void HighlightAsEnemy() const; void HighlightAsEnemy() const;
UFUNCTION() UFUNCTION()
@ -63,6 +66,8 @@ public:
UAbility *GetAbility(int AbilityIndex) const; UAbility *GetAbility(int AbilityIndex) const;
protected: protected:
constexpr static float PIXELS_IN_RADIUS = 50;
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
UMaterialInterface *GreenMaterial = nullptr; UMaterialInterface *GreenMaterial = nullptr;
@ -118,8 +123,11 @@ protected:
// const TCHAR *MeshPath = nullptr; // const TCHAR *MeshPath = nullptr;
UPROPERTY(Replicated) // UFUNCTION()
uint8 PlayerIndex; // void OnRepNotify_PlayerIndex();
UPROPERTY(Replicated/*Using = OnRepNotify_PlayerIndex*/)
uint8 PlayerIndex = -1;
UPROPERTY(Replicated) UPROPERTY(Replicated)
uint8 Id; uint8 Id;

Loading…
Cancel
Save