finished implementing attack

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

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

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

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

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

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

@ -2,7 +2,6 @@
#pragma once
#include "CoreMinimal.h"
#include "Trooper.h"
#include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h"
@ -19,7 +18,7 @@ public:
AMyPlayerController();
UFUNCTION(BlueprintCallable)
UFUNCTION(BlueprintCallable, Server, Reliable)
void EndTurn();
// UFUNCTION(Client, Reliable)
@ -40,6 +39,9 @@ public:
UFUNCTION()
void SetPlayerIndex(uint8 NewPlayerIndex);
UFUNCTION()
uint8 GetPlayerIndex() const;
// UFUNCTION(BlueprintCallable)
// float SetCurrentActionAndReturnRadius(int action);

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

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

@ -3,15 +3,19 @@
#include "MyProjectile.h"
#include "Trooper.h"
#include "Net/UnrealNetwork.h"
#include "VisualLogger/VisualLoggerCustomVersion.h"
AMyProjectile::AMyProjectile() {
// if (!RootComponent) {
// RootComponent = CreateDefaultSubobject<USceneComponent>(
// TEXT("ProjectileSceneComponent"));
// }
// if (!CollisionComponent) {
// CollisionComponent = CreateDefaultSubobject<USphereComponent>(
// TEXT("SphereComponent"));
// RootComponent = CollisionComponent;
// }
// RootComponent = CollisionComponent;
if (!ProjectileMeshComponent) {
ProjectileMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(
TEXT("ProjectileMeshComponent"));
@ -27,7 +31,9 @@ AMyProjectile::AMyProjectile() {
ProjectileMovementComponent = CreateDefaultSubobject<
UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
ProjectileMovementComponent->SetUpdatedComponent(
// CollisionComponent);
ProjectileMeshComponent);
ProjectileMovementComponent->bRotationFollowsVelocity = true;
ProjectileMovementComponent->InitialSpeed = 1000.0f;
ProjectileMovementComponent->MaxSpeed = 1000.0f;
ProjectileMovementComponent->ProjectileGravityScale = 0.0f;
@ -42,6 +48,7 @@ void AMyProjectile::Initialize(const UAbility *Ability,
ProjectileMovementComponent->MaxSpeed = Ability->Speed;
Damage = Ability->Damage;
float Scale = Ability->LinearWidth / 100;
// CollisionComponent->SetSphereRadius(Ability->LinearWidth / 2);
ProjectileMeshComponent->SetWorldScale3D({Scale, Scale, Scale});
PlayerIndex = playerIndex;
SetLifeSpan(PathLength / Ability->Speed);
@ -52,6 +59,51 @@ void AMyProjectile::Shoot(FVector From, FVector To) const {
(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() {
Super::BeginPlay();
@ -61,4 +113,8 @@ void AMyProjectile::GetLifetimeReplicatedProps(
TArray<FLifetimeProperty> &OutLifetimeProps) const {
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AMyProjectile, Damage);
DOREPLIFETIME(AMyProjectile, PlayerIndex);
// DOREPLIFETIME(AMyProjectile, CollisionComponent);
DOREPLIFETIME(AMyProjectile, ProjectileMeshComponent);
DOREPLIFETIME(AMyProjectile, ProjectileMovementComponent);
}

@ -16,25 +16,38 @@ class TURNBASEDTUTORIAL_API AMyProjectile : public AActor {
public:
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;
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)
float Damage;
UPROPERTY(Replicated)
uint8 PlayerIndex = -1;
// UPROPERTY(EditAnywhere)
int8 PlayerIndex = -1;
// UPROPERTY(EditAnywhere, Replicated)
// USphereComponent *CollisionComponent;
UPROPERTY(EditAnywhere)
UPROPERTY(EditAnywhere, Replicated)
UStaticMeshComponent *ProjectileMeshComponent;
UPROPERTY(VisibleAnywhere)
UPROPERTY(VisibleAnywhere, Replicated)
UProjectileMovementComponent *ProjectileMovementComponent;
virtual void BeginPlay() override;
};

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

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

Loading…
Cancel
Save