finished implementing attack

pull/6/head
m4xxx1m 2 years ago
parent b2d6fadf2b
commit a51acf9d02

@ -6,6 +6,7 @@
#include "MyGameState.h" #include "MyGameState.h"
#include "MyPlayerController.h" #include "MyPlayerController.h"
#include "MyPlayerState.h" #include "MyPlayerState.h"
#include "Trooper.h"
AMyGameMode::AMyGameMode() AMyGameMode::AMyGameMode()
: Super() { : Super() {
@ -25,7 +26,7 @@ auto AMyGameMode::GetMyGameState() const {
} }
void AMyGameMode::InitializeBattleField() const { void AMyGameMode::InitializeBattleField_Implementation() const {
UE_LOG(LogTemp, Warning, TEXT("InitializeBattleField")); UE_LOG(LogTemp, Warning, TEXT("InitializeBattleField"));
FVector Location(2000.0f, -1000.0f, 0.0f); FVector Location(2000.0f, -1000.0f, 0.0f);
FRotator Rotation(0.0f, 180.0f, 0.0f); FRotator Rotation(0.0f, 180.0f, 0.0f);
@ -121,6 +122,7 @@ void AMyGameMode::InitializeSpawnPointsIfNeeded(AController *Player) {
void AMyGameMode::PostLogin(APlayerController *NewPlayer) { void AMyGameMode::PostLogin(APlayerController *NewPlayer) {
Super::PostLogin(NewPlayer); Super::PostLogin(NewPlayer);
NewPlayer->SetShowMouseCursor(true);
UE_LOG(LogTemp, Warning, TEXT("PostLogin")); UE_LOG(LogTemp, Warning, TEXT("PostLogin"));
// PlayerControllers.Add(dynamic_cast<AMyPlayerController *>(NewPlayer)); // PlayerControllers.Add(dynamic_cast<AMyPlayerController *>(NewPlayer));
// const auto World = GetWorld(); // const auto World = GetWorld();
@ -137,6 +139,8 @@ void AMyGameMode::PostLogin(APlayerController *NewPlayer) {
// GetWorld()->GetGameState())->StartGame(); // GetWorld()->GetGameState())->StartGame();
// InitializeBattleField(); // InitializeBattleField();
StartGame(); StartGame();
// GetMyGameState()->PlayerInTurn()->SetEnemySelection();
// GetMyGameState()->PlayerNotInTurn()->SetEnemySelection();
} else { } else {
// delay the game // delay the game
UE_LOG(LogTemp, Warning, TEXT("Game Delay")); UE_LOG(LogTemp, Warning, TEXT("Game Delay"));

@ -30,6 +30,7 @@ public:
private: private:
void InitializeSpawnPointsIfNeeded(AController *Player); void InitializeSpawnPointsIfNeeded(AController *Player);
UFUNCTION(Server, Reliable)
void InitializeBattleField() const; void InitializeBattleField() const;
UPROPERTY() UPROPERTY()
@ -41,6 +42,7 @@ private:
UFUNCTION(Server, Reliable) UFUNCTION(Server, Reliable)
void StartGame(); void StartGame();
// UFUNCTION(BlueprintPure) // UFUNCTION(BlueprintPure)
// AMyPlayerController *PlayerInTurn() const; // AMyPlayerController *PlayerInTurn() const;
// //

@ -14,7 +14,7 @@ void AMyGameState::BeginPlay() {
Super::BeginPlay(); Super::BeginPlay();
} }
void AMyGameState::AddTrooper(ATrooper *Trooper) { void AMyGameState::AddTrooper_Implementation(ATrooper *Trooper) {
Troopers.Add(Trooper); Troopers.Add(Trooper);
} }
@ -66,6 +66,10 @@ TArray<ATrooper *> AMyGameState::GetTroopers() const {
return Troopers; return Troopers;
} }
bool AMyGameState::IsInTurn(uint8 PlayerIndex) const {
return PlayerIndex == CurrentPlayerTurn;
}
void AMyGameState::GetLifetimeReplicatedProps( void AMyGameState::GetLifetimeReplicatedProps(
TArray<FLifetimeProperty> &OutLifetimeProps) const { TArray<FLifetimeProperty> &OutLifetimeProps) const {
Super::GetLifetimeReplicatedProps(OutLifetimeProps); Super::GetLifetimeReplicatedProps(OutLifetimeProps);

@ -17,7 +17,7 @@ class TURNBASEDTUTORIAL_API AMyGameState : public AGameState {
public: public:
virtual void BeginPlay() override; virtual void BeginPlay() override;
UFUNCTION() UFUNCTION(Server, Reliable)
void AddTrooper(ATrooper *Trooper); void AddTrooper(ATrooper *Trooper);
UFUNCTION(Server, Reliable) UFUNCTION(Server, Reliable)
@ -35,6 +35,9 @@ public:
UFUNCTION() UFUNCTION()
TArray<ATrooper *> GetTroopers() const; TArray<ATrooper *> GetTroopers() const;
UFUNCTION()
bool IsInTurn(uint8 PlayerIndex) const;
private: private:
UPROPERTY(Replicated) UPROPERTY(Replicated)
TArray<ATrooper *> Troopers; TArray<ATrooper *> Troopers;

@ -50,11 +50,13 @@ auto AMyPlayerController::GetMyGameMode() const {
} }
void AMyPlayerController::EndTurn() { void AMyPlayerController::EndTurn_Implementation() {
// if (GetMyPlayerState()->IsMyTurn()) { // if (GetMyPlayerState()->IsMyTurn()) {
// GetMyGameState()->CycleTurns(); // GetMyGameState()->CycleTurns();
// } // }
GetMyPlayerState()->CycleTurns(); // GetMyPlayerState()->CycleTurns();
if (GetMyGameState()->IsInTurn(PlayerIndex))
GetMyGameState()->CycleTurns();
} }
// void AMyPlayerController::EndTurn_Implementation() { // void AMyPlayerController::EndTurn_Implementation() {
@ -132,6 +134,10 @@ void AMyPlayerController::SetPlayerIndex(uint8 NewPlayerIndex) {
// GetMyPlayerState()->PlayerIndex = NewPlayerIndex; // GetMyPlayerState()->PlayerIndex = NewPlayerIndex;
} }
uint8 AMyPlayerController::GetPlayerIndex() const {
return PlayerIndex;
}
// float AMyPlayerController::SetCurrentActionAndReturnRadius(int action) { // float AMyPlayerController::SetCurrentActionAndReturnRadius(int action) {
// return GetMyPlayerState()->SetCurrentActionAndReturnRadius(action); // return GetMyPlayerState()->SetCurrentActionAndReturnRadius(action);
// //

@ -2,7 +2,6 @@
#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"
@ -19,7 +18,7 @@ public:
AMyPlayerController(); AMyPlayerController();
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable, Server, Reliable)
void EndTurn(); void EndTurn();
// UFUNCTION(Client, Reliable) // UFUNCTION(Client, Reliable)
@ -40,6 +39,9 @@ public:
UFUNCTION() UFUNCTION()
void SetPlayerIndex(uint8 NewPlayerIndex); void SetPlayerIndex(uint8 NewPlayerIndex);
UFUNCTION()
uint8 GetPlayerIndex() const;
// UFUNCTION(BlueprintCallable) // UFUNCTION(BlueprintCallable)
// float SetCurrentActionAndReturnRadius(int action); // float SetCurrentActionAndReturnRadius(int action);

@ -4,7 +4,6 @@
#include "MyPlayerState.h" #include "MyPlayerState.h"
#include "MyGameState.h" #include "MyGameState.h"
#include "Trooper.h"
#include "Net/UnrealNetwork.h" #include "Net/UnrealNetwork.h"
AMyPlayerState::AMyPlayerState() AMyPlayerState::AMyPlayerState()
@ -34,6 +33,7 @@ void AMyPlayerState::SetEnemySelection_Implementation(
void AMyPlayerState::MoveTrooper_Implementation(ATrooper *Trooper, void AMyPlayerState::MoveTrooper_Implementation(ATrooper *Trooper,
FVector Location) { FVector Location) {
Location.Z = 0.0f;
if (Trooper->CheckMoveCorrectness(Location)) { if (Trooper->CheckMoveCorrectness(Location)) {
Trooper->MoveTrooper(Location); Trooper->MoveTrooper(Location);
// GetMyGameMode()->CycleTurns(); // GetMyGameMode()->CycleTurns();
@ -76,11 +76,11 @@ void AMyPlayerState::Attack_Implementation(ATrooper *Attacker,
} }
} }
void AMyPlayerState::CycleTurns() const { // void AMyPlayerState::CycleTurns_Implementation() const {
if (bIsMyTurn) { // if (bIsMyTurn) {
GetMyGameState()->CycleTurns(); // GetMyGameState()->CycleTurns();
} // }
} // }
bool AMyPlayerState::IsMyTurn() const { bool AMyPlayerState::IsMyTurn() const {
return bIsMyTurn; return bIsMyTurn;
@ -175,7 +175,7 @@ void AMyPlayerState::SetCurrentAction_Implementation(int Action) {
} }
uint8 AMyPlayerState::GetPlayerIndex() { uint8 AMyPlayerState::GetPlayerIndex() const {
return PlayerIndex; return PlayerIndex;
} }

@ -2,6 +2,7 @@
#pragma once #pragma once
#include "Trooper.h"
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "GameFramework/PlayerState.h" #include "GameFramework/PlayerState.h"
#include "MyPlayerState.generated.h" #include "MyPlayerState.generated.h"
@ -33,8 +34,8 @@ public:
int ActionIndex, int ActionIndex,
const TArray<ATrooper *> &Troopers); const TArray<ATrooper *> &Troopers);
UFUNCTION() // UFUNCTION(Client, Reliable)
void CycleTurns() const; // void CycleTurns() const;
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
bool IsMyTurn() const; bool IsMyTurn() const;
@ -46,7 +47,7 @@ public:
void SetCurrentAction(int Action); void SetCurrentAction(int Action);
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
uint8 GetPlayerIndex(); uint8 GetPlayerIndex() const;
UFUNCTION() UFUNCTION()
void SetPlayerIndex(uint8 NewPlayerIndex); void SetPlayerIndex(uint8 NewPlayerIndex);
@ -54,7 +55,9 @@ public:
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)
uint8 PlayerIndex; uint8 PlayerIndex;

@ -3,15 +3,19 @@
#include "MyProjectile.h" #include "MyProjectile.h"
#include "Trooper.h"
#include "Net/UnrealNetwork.h" #include "Net/UnrealNetwork.h"
#include "VisualLogger/VisualLoggerCustomVersion.h"
AMyProjectile::AMyProjectile() { AMyProjectile::AMyProjectile() {
// if (!RootComponent) {
// RootComponent = CreateDefaultSubobject<USceneComponent>(
// TEXT("ProjectileSceneComponent"));
// }
// if (!CollisionComponent) { // if (!CollisionComponent) {
// CollisionComponent = CreateDefaultSubobject<USphereComponent>( // CollisionComponent = CreateDefaultSubobject<USphereComponent>(
// TEXT("SphereComponent")); // TEXT("SphereComponent"));
// RootComponent = CollisionComponent;
// } // }
// RootComponent = CollisionComponent;
if (!ProjectileMeshComponent) { if (!ProjectileMeshComponent) {
ProjectileMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>( ProjectileMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(
TEXT("ProjectileMeshComponent")); TEXT("ProjectileMeshComponent"));
@ -27,7 +31,9 @@ AMyProjectile::AMyProjectile() {
ProjectileMovementComponent = CreateDefaultSubobject< ProjectileMovementComponent = CreateDefaultSubobject<
UProjectileMovementComponent>(TEXT("ProjectileMovementComponent")); UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
ProjectileMovementComponent->SetUpdatedComponent( ProjectileMovementComponent->SetUpdatedComponent(
// CollisionComponent);
ProjectileMeshComponent); ProjectileMeshComponent);
ProjectileMovementComponent->bRotationFollowsVelocity = true;
ProjectileMovementComponent->InitialSpeed = 1000.0f; ProjectileMovementComponent->InitialSpeed = 1000.0f;
ProjectileMovementComponent->MaxSpeed = 1000.0f; ProjectileMovementComponent->MaxSpeed = 1000.0f;
ProjectileMovementComponent->ProjectileGravityScale = 0.0f; ProjectileMovementComponent->ProjectileGravityScale = 0.0f;
@ -42,6 +48,7 @@ void AMyProjectile::Initialize(const UAbility *Ability,
ProjectileMovementComponent->MaxSpeed = Ability->Speed; ProjectileMovementComponent->MaxSpeed = Ability->Speed;
Damage = Ability->Damage; Damage = Ability->Damage;
float Scale = Ability->LinearWidth / 100; float Scale = Ability->LinearWidth / 100;
// CollisionComponent->SetSphereRadius(Ability->LinearWidth / 2);
ProjectileMeshComponent->SetWorldScale3D({Scale, Scale, Scale}); ProjectileMeshComponent->SetWorldScale3D({Scale, Scale, Scale});
PlayerIndex = playerIndex; PlayerIndex = playerIndex;
SetLifeSpan(PathLength / Ability->Speed); SetLifeSpan(PathLength / Ability->Speed);
@ -52,6 +59,51 @@ void AMyProjectile::Shoot(FVector From, FVector To) const {
(To - From).GetSafeNormal() * ProjectileMovementComponent->InitialSpeed; (To - From).GetSafeNormal() * ProjectileMovementComponent->InitialSpeed;
} }
void AMyProjectile::NotifyActorBeginOverlap(AActor *OtherActor) {
Super::NotifyActorBeginOverlap(OtherActor);
ATrooper *OtherTrooper = Cast<ATrooper>(OtherActor);
if (OtherTrooper) {
UE_LOG(LogTemp, Warning,
TEXT("Begin overlap: id: %d, index: %d, damage: %f, my index: %d"
),
OtherTrooper->GetId(), OtherTrooper->GetPlayerIndex(), Damage,
PlayerIndex);
if (PlayerIndex != -1 && PlayerIndex != OtherTrooper->
GetPlayerIndex()) {
OtherTrooper->TakeDamage(Damage);
}
} else {
UE_LOG(LogTemp, Warning, TEXT("Overlapped not a trooper"));
}
}
void AMyProjectile::NotifyHit(UPrimitiveComponent *MyComp,
AActor *Other,
UPrimitiveComponent *OtherComp,
bool bSelfMoved,
FVector HitLocation,
FVector HitNormal,
FVector NormalImpulse,
const FHitResult &Hit) {
Super::NotifyHit(MyComp, Other, OtherComp, bSelfMoved, HitLocation,
HitNormal,
NormalImpulse, Hit);
ATrooper *OtherTrooper = Cast<ATrooper>(Other);
if (OtherTrooper) {
UE_LOG(LogTemp, Warning,
TEXT("On Hit: id: %d, index: %d, damage: %f, my index: %d"
),
OtherTrooper->GetId(), OtherTrooper->GetPlayerIndex(), Damage,
PlayerIndex);
if (PlayerIndex != -1 && PlayerIndex != OtherTrooper->
GetPlayerIndex()) {
OtherTrooper->TakeDamage(Damage);
}
} else {
UE_LOG(LogTemp, Warning, TEXT("Overlapped not a trooper"));
}
}
void AMyProjectile::BeginPlay() { void AMyProjectile::BeginPlay() {
Super::BeginPlay(); Super::BeginPlay();
@ -61,4 +113,8 @@ void AMyProjectile::GetLifetimeReplicatedProps(
TArray<FLifetimeProperty> &OutLifetimeProps) const { TArray<FLifetimeProperty> &OutLifetimeProps) const {
Super::GetLifetimeReplicatedProps(OutLifetimeProps); Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AMyProjectile, Damage); DOREPLIFETIME(AMyProjectile, Damage);
DOREPLIFETIME(AMyProjectile, PlayerIndex);
// DOREPLIFETIME(AMyProjectile, CollisionComponent);
DOREPLIFETIME(AMyProjectile, ProjectileMeshComponent);
DOREPLIFETIME(AMyProjectile, ProjectileMovementComponent);
} }

@ -16,24 +16,37 @@ class TURNBASEDTUTORIAL_API AMyProjectile : public AActor {
public: public:
AMyProjectile(); AMyProjectile();
void Initialize(const UAbility *Ability, uint8 playerIndex, float PathLength); void Initialize(const UAbility *Ability,
uint8 playerIndex,
float PathLength);
void Shoot(FVector From, FVector To) const; void Shoot(FVector From, FVector To) const;
protected: protected:
virtual void NotifyActorBeginOverlap(AActor *OtherActor) override;
virtual void NotifyHit(UPrimitiveComponent *MyComp,
AActor *Other,
UPrimitiveComponent *OtherComp,
bool bSelfMoved,
FVector HitLocation,
FVector HitNormal,
FVector NormalImpulse,
const FHitResult &Hit) override;
UPROPERTY(Replicated) UPROPERTY(Replicated)
float Damage; float Damage;
UPROPERTY(Replicated) UPROPERTY(Replicated)
uint8 PlayerIndex = -1; int8 PlayerIndex = -1;
// UPROPERTY(EditAnywhere) // UPROPERTY(EditAnywhere, Replicated)
// USphereComponent *CollisionComponent; // USphereComponent *CollisionComponent;
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere, Replicated)
UStaticMeshComponent *ProjectileMeshComponent; UStaticMeshComponent *ProjectileMeshComponent;
UPROPERTY(VisibleAnywhere) UPROPERTY(VisibleAnywhere, Replicated)
UProjectileMovementComponent *ProjectileMovementComponent; UProjectileMovementComponent *ProjectileMovementComponent;
virtual void BeginPlay() override; virtual void BeginPlay() override;

@ -2,6 +2,8 @@
#include <Kismet/GameplayStatics.h> #include <Kismet/GameplayStatics.h>
#include "HealthBar.h" #include "HealthBar.h"
#include "MyPlayerController.h"
#include "MyPlayerState.h"
#include "MyProjectile.h" #include "MyProjectile.h"
#include "Components/WidgetComponent.h" #include "Components/WidgetComponent.h"
#include "Net/UnrealNetwork.h" #include "Net/UnrealNetwork.h"
@ -111,17 +113,20 @@ void ATrooper::Tick(float const DeltaTime) {
} }
} }
// void ATrooper::OnRepNotify_PlayerIndex() { void ATrooper::OnRepNotify_PlayerIndex() const {
// UE_LOG(LogTemp, Warning, const AMyPlayerState *player = Cast<AMyPlayerState>(
// TEXT("On rep notify, index: %d, id: %d, on server: %d, player state: %d"), GetPlayerState());
// PlayerIndex, Id, GetNetMode() == NM_DedicatedServer, GetPlayerState() != nullptr); if (!player)
// // if (GetNetMode() != NM_DedicatedServer && GetPlayerState()) { return;
// // if (Cast<AMyPlayerState>(GetPlayerState())->GetPlayerIndex() != const uint8 ClientIndex = player->GetPlayerIndex();
// // PlayerIndex) { UE_LOG(LogTemp, Warning,
// // HighlightAsEnemy(); TEXT("On rep notify, index: %d, client index: %d, id: %d"),
// // } PlayerIndex,
// // } ClientIndex, Id);
// } if (ClientIndex == PlayerIndex) {
HighlightAsEnemy();
}
}
void ATrooper::MoveTrooper(FVector const NewPos) { void ATrooper::MoveTrooper(FVector const NewPos) {
TargetLocation = NewPos; TargetLocation = NewPos;
@ -240,6 +245,9 @@ UAbility *ATrooper::GetAbility(int AbilityIndex) const {
} }
bool ATrooper::TakeDamage(float Damage) { bool ATrooper::TakeDamage(float Damage) {
if (bIsTakingDamage) {
return false;
}
HitPoints = FMath::Max<float>(0, HitPoints - Damage); HitPoints = FMath::Max<float>(0, HitPoints - Damage);
if (HitPoints == 0) { if (HitPoints == 0) {
bIsDead = true; bIsDead = true;
@ -279,7 +287,8 @@ void ATrooper::FireProjectile_Implementation() {
AMyProjectile *Projectile = GetWorld()->SpawnActor<AMyProjectile>( AMyProjectile *Projectile = GetWorld()->SpawnActor<AMyProjectile>(
GetProjectileClass(CurrentAbilityIndex), SpawnTransform, GetProjectileClass(CurrentAbilityIndex), SpawnTransform,
SpawnParameters); SpawnParameters);
Projectile->Initialize(GetAbility(CurrentAbilityIndex), CurrentAbilityIndex, Projectile->SetActorLocation(SpawnLocation);
Projectile->Initialize(GetAbility(CurrentAbilityIndex), PlayerIndex,
(CurrentAbilityDestination - SpawnLocation). (CurrentAbilityDestination - SpawnLocation).
Size()); Size());
Projectile->Shoot(SpawnLocation, CurrentAbilityDestination); Projectile->Shoot(SpawnLocation, CurrentAbilityDestination);

@ -160,10 +160,10 @@ protected:
// const TCHAR *MeshPath = nullptr; // const TCHAR *MeshPath = nullptr;
// UFUNCTION() UFUNCTION()
// void OnRepNotify_PlayerIndex(); void OnRepNotify_PlayerIndex() const;
UPROPERTY(Replicated/*Using = OnRepNotify_PlayerIndex*/) UPROPERTY(ReplicatedUsing = OnRepNotify_PlayerIndex)
uint8 PlayerIndex = -1; uint8 PlayerIndex = -1;
UPROPERTY(Replicated) UPROPERTY(Replicated)

Loading…
Cancel
Save