You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
awesome_game/Source/TurnBasedTutorial/Trooper.cpp

251 lines
8.0 KiB

#include "Trooper.h"
#include <Kismet/GameplayStatics.h>
#include "HealthBar.h"
#include "MyPlayerState.h"
#include "Components/WidgetComponent.h"
#include "Net/UnrealNetwork.h"
// Sets default values
ATrooper::ATrooper()
: HitPoints(StartHitPoints), ActionPoints(StartActionPoints) {
bReplicates = true;
PrimaryActorTick.bCanEverTick = true;
Tags.Add(FName("Trooper"));
AttackAbility = CreateDefaultSubobject<UAbility>("AttackAbility");
SpecialAbility = CreateDefaultSubobject<UAbility>("SpecialAbility");
HealthWidgetComponent = CreateDefaultSubobject<UWidgetComponent>(
"HealthBar");
HealthWidgetComponent->AttachToComponent(RootComponent,
FAttachmentTransformRules::KeepRelativeTransform);
SelectionStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(
"SelectionMesh");
static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshToUse(TEXT(
"StaticMesh'/Game/StarterContent/Shapes/Shape_Cylinder.Shape_Cylinder'"));
SelectionStaticMesh->AttachToComponent(RootComponent,
FAttachmentTransformRules::KeepRelativeTransform);
SelectionStaticMesh->SetWorldScale3D({2.0f, 2.0f, 0.01f});
SelectionStaticMesh->SetVisibility(false);
if (MeshToUse.Object) {
SelectionStaticMesh->SetStaticMesh(MeshToUse.Object);
SelectionStaticMesh->SetStaticMesh(MeshToUse.Object);
}
// SelectionStaticMesh->SetRelativeTransform(FTransform({1000,1000,100}, {0, 0, 0}), false,
// nullptr, ETeleportType::TeleportPhysics);
// SelectionStaticMesh->
// MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
// RootComponent = MyStaticMesh;
// MeshPath = TEXT("StaticMesh'/Game/StarterContent/Props/SM_Chair.SM_Chair'");
// static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshToUse(MeshPath);
// if (MeshToUse.Object) {
// MyStaticMesh->SetStaticMesh(MeshToUse.Object);
// }
}
// void ATrooper::SetStaticMesh() const {
// static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshToUse(
// TEXT(
// "StaticMesh'/Game/CityofBrass_Enemies/Static/Wizard_StaticMesh.Wizard_StaticMesh'"
// )
// );
// if (MeshToUse.Object) {
// MyStaticMesh->SetStaticMesh(MeshToUse.Object);
// }
// }
// Called when the game starts or when spawned
void ATrooper::BeginPlay() {
Super::BeginPlay();
Cast<UHealthBar>(HealthWidgetComponent->GetUserWidgetObject())->
SetOwnerTrooper(this);
}
void ATrooper::Initialize(uint8 const NewPlayerIndex,
FVector const SpawnLocation,
uint8 const NewId) {
PlayerIndex = NewPlayerIndex;
bIsMoving = false;
AttackPlayedTime = 0.0f;
CurrentLocation = SpawnLocation;
Id = NewId;
}
void ATrooper::Tick(float const DeltaTime) {
if (bIsAttacking) {
AttackPlayedTime += DeltaTime;
if (AttackPlayedTime >= AttackDuration) {
AttackPlayedTime = 0.0f;
bIsAttacking = false;
}
}
if (bIsMoving) {
FVector PositionVector = (TargetLocation - CurrentLocation);
PositionVector.Normalize();
PositionVector *= (Speed * DeltaTime);
if (PositionVector.Size() >= (TargetLocation - CurrentLocation).
Size()) {
CurrentLocation = TargetLocation;
bIsMoving = false;
} else {
CurrentLocation += PositionVector;
}
SetActorLocation(CurrentLocation);
}
}
// 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) {
TargetLocation = NewPos;
bIsMoving = true;
ActionPoints -= (NewPos - CurrentLocation).Size() * MoveCost;
}
uint8 ATrooper::GetId() const {
return Id;
}
void ATrooper::GetLifetimeReplicatedProps(
TArray<FLifetimeProperty> &OutLifetimeProps) const {
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(ATrooper, PlayerIndex);
DOREPLIFETIME(ATrooper, CurrentLocation);
DOREPLIFETIME(ATrooper, TargetLocation);
DOREPLIFETIME(ATrooper, bIsMoving);
DOREPLIFETIME(ATrooper, Id);
DOREPLIFETIME(ATrooper, bIsAttacking);
DOREPLIFETIME(ATrooper, HitPoints);
DOREPLIFETIME(ATrooper, ActionPoints);
DOREPLIFETIME(ATrooper, HealthWidgetComponent);
DOREPLIFETIME(ATrooper, AttackPlayedTime);
}
uint8 ATrooper::GetPlayerIndex() const {
return PlayerIndex;
}
//
// ATrooperWizard::ATrooperWizard() {
// MeshPath = TEXT(
// // "StaticMesh'/Game/CityofBrass_Enemies/Static/Wizard_StaticMesh.Wizard_StaticMesh'");
// "StaticMesh'/Game/CityofBrass_Enemies/Static/SkeletonMelee_StaticMesh.SkeletonMelee_StaticMesh'");
// SetStaticMesh();
// }
//
// ATrooperSkeletonMelee::ATrooperSkeletonMelee() {
// MeshPath = TEXT(
// "StaticMesh'/Game/CityofBrass_Enemies/Static/SkeletonMelee_StaticMesh.SkeletonMelee_StaticMesh'");
// SetStaticMesh();
// }
FVector ATrooper::GetLocation() const {
return CurrentLocation;
}
float ATrooper::GetActionRadius(int action) const {
switch (action) {
case 1:
return AttackAbility->ActionCost <= ActionPoints
? AttackAbility->ActionRadius
: 0;
case 2:
return SpecialAbility->ActionCost <= ActionPoints
? SpecialAbility->ActionRadius
: 0;
default:
return ActionPoints / MoveCost;
}
}
float ATrooper::GetHitPoints() const {
return HitPoints;
}
float ATrooper::GetMaxHitPoints() const {
return StartHitPoints;
}
void ATrooper::SetSelection(bool Selection, uint8 ActionType) const {
if (SelectionStaticMesh) {
if (SelectionStaticMesh->GetMaterial(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);
}
}
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);
}
void ATrooper::ResetActionPoints() {
ActionPoints = StartActionPoints;
}
UAbility *ATrooper::GetAbility(int AbilityIndex) const {
switch (AbilityIndex) {
case 1:
return AttackAbility;
case 2:
return SpecialAbility;
default:
return nullptr;
}
}
float ATrooper::GetAnimationValue() {
if (bIsAttacking) {
return -100.0f;
}
if (bIsMoving) {
return 100.0f;
}
return 0.0f;
}
void ATrooper::Attack(int abilityIndex) {
bIsAttacking = true;
ActionPoints -= GetAbility(abilityIndex)->ActionCost;
}
bool ATrooper::CheckMoveCorrectness(const FVector newPos) const {
return (newPos - CurrentLocation).Size() * MoveCost <= ActionPoints;
// return (newPos - CurrentLocation).Size() <= MoveRadius;
}
bool ATrooper::CheckAttackCorrectness(const FVector attackLocation,
int abilityIndex) const {
return GetAbility(abilityIndex) != nullptr && (
attackLocation - CurrentLocation).Size() < GetActionRadius(
abilityIndex);
// return (attackLocation - CurrentLocation).Size() <= AttackRadius;
}