fixed attack problem

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

@ -24,8 +24,7 @@ void AMyGameState::StartGame() const {
PlayerInTurn()->StartTurn(); PlayerInTurn()->StartTurn();
} }
void AMyGameState::CycleTurns_Implementation(uint8 CurrentPlayerIndex) { void AMyGameState::CycleTurns_Implementation() {
if (CurrentPlayerTurn == CurrentPlayerIndex) {
PlayerInTurn()->EndTurn(); PlayerInTurn()->EndTurn();
for (const auto Trooper : Troopers) { for (const auto Trooper : Troopers) {
if (Trooper != nullptr) { if (Trooper != nullptr) {
@ -35,7 +34,6 @@ void AMyGameState::CycleTurns_Implementation(uint8 CurrentPlayerIndex) {
CurrentPlayerTurn = !CurrentPlayerTurn; CurrentPlayerTurn = !CurrentPlayerTurn;
PlayerInTurn()->StartTurn(); PlayerInTurn()->StartTurn();
} }
}
// void AMyGameState::CycleTurns(uint8 CurrentPlayerIndex) { // void AMyGameState::CycleTurns(uint8 CurrentPlayerIndex) {
// if (CurrentPlayerTurn == CurrentPlayerIndex) { // if (CurrentPlayerTurn == CurrentPlayerIndex) {

@ -24,7 +24,7 @@ public:
void StartGame() const; void StartGame() const;
UFUNCTION(BlueprintCallable, Server, Reliable) UFUNCTION(BlueprintCallable, Server, Reliable)
void CycleTurns(uint8 CurrentPlayerIndex); void CycleTurns();
UFUNCTION(BlueprintPure) UFUNCTION(BlueprintPure)
AMyPlayerState *PlayerInTurn() const; AMyPlayerState *PlayerInTurn() const;

@ -50,6 +50,13 @@ auto AMyPlayerController::GetMyGameMode() const {
} }
void AMyPlayerController::EndTurn() {
// if (GetMyPlayerState()->IsMyTurn()) {
// GetMyGameState()->CycleTurns();
// }
GetMyPlayerState()->CycleTurns();
}
// void AMyPlayerController::EndTurn_Implementation() { // void AMyPlayerController::EndTurn_Implementation() {
// GetMyPlayerState()->EndTurn(); // GetMyPlayerState()->EndTurn();
// } // }
@ -125,17 +132,17 @@ void AMyPlayerController::SetPlayerIndex(uint8 NewPlayerIndex) {
// GetMyPlayerState()->PlayerIndex = NewPlayerIndex; // GetMyPlayerState()->PlayerIndex = NewPlayerIndex;
} }
float AMyPlayerController::SetCurrentActionAndReturnRadius(int action) { // float AMyPlayerController::SetCurrentActionAndReturnRadius(int action) {
return GetMyPlayerState()->SetCurrentActionAndReturnRadius(action); // return GetMyPlayerState()->SetCurrentActionAndReturnRadius(action);
//
// CurrentAction = action; // // CurrentAction = 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);
// if (SelectedTrooper) { // // if (SelectedTrooper) {
// return SelectedTrooper->GetActionRadius(CurrentAction); // // return SelectedTrooper->GetActionRadius(CurrentAction);
// // }
// // return 0.0f;
// } // }
// return 0.0f;
}
void AMyPlayerController::SetEnemySelection_Implementation( void AMyPlayerController::SetEnemySelection_Implementation(

@ -19,6 +19,9 @@ public:
AMyPlayerController(); AMyPlayerController();
UFUNCTION(BlueprintCallable)
void EndTurn();
// UFUNCTION(Client, Reliable) // UFUNCTION(Client, Reliable)
// void StartTurn(); // void StartTurn();
@ -37,8 +40,8 @@ public:
UFUNCTION() UFUNCTION()
void SetPlayerIndex(uint8 NewPlayerIndex); void SetPlayerIndex(uint8 NewPlayerIndex);
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;

@ -19,6 +19,37 @@ void AMyPlayerState::SetPlayerIndex(uint8 NewPlayerIndex) {
PlayerIndex = NewPlayerIndex; PlayerIndex = NewPlayerIndex;
} }
void AMyPlayerState::MoveTrooper_Implementation(ATrooper *Trooper,
FVector Location) {
if (Trooper->CheckMoveCorrectness(Location)) {
Trooper->MoveTrooper(Location);
// GetMyGameMode()->CycleTurns();
} else {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red,
FString::Printf(
TEXT("Out of move radius!")));
}
}
void AMyPlayerState::Attack_Implementation(ATrooper *Attacker,
FVector Location,
int ActionIndex) {
if (Attacker->CheckAttackCorrectness(Location, ActionIndex)) {
Attacker->Attack(ActionIndex);
} else {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red,
FString::Printf(
TEXT(
"Out of radius or not enough Action Points!")));
}
}
void AMyPlayerState::CycleTurns() const {
if (bIsMyTurn) {
Cast<AMyGameState>(GetWorld()->GetGameState())->CycleTurns();
}
}
bool AMyPlayerState::IsMyTurn() const { bool AMyPlayerState::IsMyTurn() const {
return bIsMyTurn; return bIsMyTurn;
} }
@ -53,30 +84,6 @@ void AMyPlayerState::EndTurn_Implementation() {
} }
} }
void AMyPlayerState::MoveTrooper_Implementation(
ATrooper *Trooper,
FVector Location) {
if (Trooper->CheckMoveCorrectness(Location)) {
Trooper->MoveTrooper(Location);
// GetMyGameMode()->CycleTurns();
} else {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red,
FString::Printf(
TEXT("Out of move radius!")));
}
}
void AMyPlayerState::Attack_Implementation(ATrooper *Attacker,
FVector Location,
int ActionIndex) {
if (Attacker && CurrentAction >= 1 && CurrentAction <= 2 &&
Attacker->CheckAttackCorrectness(Location, CurrentAction)) {
Attacker->Attack(CurrentAction);
} else {
UE_LOG(LogTemp, Warning,
TEXT("Out of radius or not enough Action Points"));
}
}
void AMyPlayerState::OnPlayerAction(const FHitResult &HitResult) { void AMyPlayerState::OnPlayerAction(const FHitResult &HitResult) {
auto const NewlySelectedLocation = HitResult.Location; auto const NewlySelectedLocation = HitResult.Location;
@ -125,16 +132,13 @@ void AMyPlayerState::OnPlayerAction(const FHitResult &HitResult) {
} }
} }
float AMyPlayerState::SetCurrentActionAndReturnRadius(int Action) { void AMyPlayerState::SetCurrentAction_Implementation(int Action) {
CurrentAction = Action; CurrentAction = 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);
if (SelectedTrooper) {
return SelectedTrooper->GetActionRadius(CurrentAction);
}
return 0.0f;
} }
uint8 AMyPlayerState::GetPlayerIndex() { uint8 AMyPlayerState::GetPlayerIndex() {
return PlayerIndex; return PlayerIndex;
} }

@ -30,13 +30,16 @@ public:
void Attack(ATrooper *Attacker, FVector Location, int ActionIndex); void Attack(ATrooper *Attacker, FVector Location, int ActionIndex);
UFUNCTION() UFUNCTION()
void CycleTurns() const;
UFUNCTION(BlueprintCallable)
bool IsMyTurn() const; bool IsMyTurn() const;
UFUNCTION() UFUNCTION()
void OnPlayerAction(const FHitResult &HitResult); void OnPlayerAction(const FHitResult &HitResult);
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable, Client, Reliable)
float SetCurrentActionAndReturnRadius(int Action); void SetCurrentAction(int Action);
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
uint8 GetPlayerIndex(); uint8 GetPlayerIndex();
@ -57,7 +60,6 @@ private:
UPROPERTY(Replicated) UPROPERTY(Replicated)
int CurrentAction = 0; int CurrentAction = 0;
UPROPERTY(Replicated) UPROPERTY(Replicated)
ATrooper *SelectedTrooper; ATrooper *SelectedTrooper;
}; };

@ -214,7 +214,8 @@ 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 (attackLocation - CurrentLocation).Size() <= return GetAbility(abilityIndex) != nullptr && (
attackLocation - CurrentLocation).Size() <=
GetAbility(abilityIndex)->ActionRadius && ActionPoints >= GetAbility(abilityIndex)->ActionRadius && ActionPoints >=
GetAbility(abilityIndex)->ActionCost; GetAbility(abilityIndex)->ActionCost;
// return (attackLocation - CurrentLocation).Size() <= AttackRadius; // return (attackLocation - CurrentLocation).Size() <= AttackRadius;

Loading…
Cancel
Save