health var updates correctly on client when hit by projectile
This commit is contained in:
@@ -9,6 +9,7 @@ UDDIHealth::UDDIHealth()
|
|||||||
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
|
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
|
||||||
// off to improve performance if you don't need them.
|
// off to improve performance if you don't need them.
|
||||||
PrimaryComponentTick.bCanEverTick = true;
|
PrimaryComponentTick.bCanEverTick = true;
|
||||||
|
SetIsReplicatedByDefault(true);
|
||||||
MaxHealth = 0;
|
MaxHealth = 0;
|
||||||
CurrentHealth = 0;
|
CurrentHealth = 0;
|
||||||
ClassName = ClassNames::Scout;
|
ClassName = ClassNames::Scout;
|
||||||
@@ -41,7 +42,7 @@ void UDDIHealth::BeginPlay()
|
|||||||
MaxHealth += seg;
|
MaxHealth += seg;
|
||||||
|
|
||||||
CurrentHealth = MaxHealth;
|
CurrentHealth = MaxHealth;
|
||||||
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Cyan, FString::Printf(TEXT("CurrentHealth: %d"), CurrentHealth));
|
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Emerald, FString::Printf(TEXT("CurrentHealth: %d"), CurrentHealth));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -81,10 +82,9 @@ void UDDIHealth::Heal()
|
|||||||
// Called to cause damage
|
// Called to cause damage
|
||||||
void UDDIHealth::TakeDamage(int DamageValue)
|
void UDDIHealth::TakeDamage(int DamageValue)
|
||||||
{
|
{
|
||||||
// if (HealTimer.IsValid())
|
|
||||||
// HealTimer.Invalidate();
|
|
||||||
GetWorld()->GetTimerManager().ClearTimer(HealTimer);
|
GetWorld()->GetTimerManager().ClearTimer(HealTimer);
|
||||||
|
|
||||||
CurrentHealth -= DamageValue;
|
CurrentHealth -= DamageValue;
|
||||||
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Cyan, FString::Printf(TEXT("Client Hit\nHealth: %d"), CurrentHealth));
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -50,10 +50,55 @@ void ADDICharacter::Tick(float DeltaTime)
|
|||||||
|
|
||||||
void ADDICharacter::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
|
void ADDICharacter::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
|
||||||
{
|
{
|
||||||
|
Server_CharacterHit(HitComponent, OtherActor, OtherComp, NormalImpulse, Hit);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ADDICharacter::TakeDamage(int Damage)
|
void ADDICharacter::TakeDamage(int Damage)
|
||||||
{
|
{
|
||||||
HealthComponent->TakeDamage(Damage);
|
HealthComponent->TakeDamage(Damage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ADDICharacter::Server_SpawnProjectile_Validate()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
void ADDICharacter::Server_SpawnProjectile_Implementation()
|
||||||
|
{
|
||||||
|
FVector Location = GetActorLocation();
|
||||||
|
FRotator Rotation = GetActorRotation();
|
||||||
|
FVector Dir = GetActorForwardVector();
|
||||||
|
|
||||||
|
Location += Dir * 200.f;
|
||||||
|
if (!ProjectileClass)
|
||||||
|
{
|
||||||
|
GEngine->AddOnScreenDebugMessage(1, 10.f, FColor::Emerald, "No Projectile Set");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
GetWorld()->SpawnActor<AActor>(ProjectileClass, Location, Rotation);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void ADDICharacter::Client_CharacterHit_Implementation()
|
||||||
|
{
|
||||||
|
TakeDamage(10);
|
||||||
|
|
||||||
|
}
|
||||||
|
bool ADDICharacter::Client_CharacterHit_Validate()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
void ADDICharacter::Server_CharacterHit_Implementation(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
|
||||||
|
{
|
||||||
|
if (AProjectileBase* Projectile = Cast<AProjectileBase>(OtherActor))
|
||||||
|
{
|
||||||
|
// if (Cast<ADDICharacter>(Projectile->Owner) == this)
|
||||||
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Emerald, "Damage dealt");
|
||||||
|
Client_CharacterHit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bool ADDICharacter::Server_CharacterHit_Validate(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -31,6 +31,9 @@ public:
|
|||||||
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health")
|
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health")
|
||||||
UDDIHealth* HealthComponent;
|
UDDIHealth* HealthComponent;
|
||||||
|
|
||||||
|
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Projectile")
|
||||||
|
TSubclassOf<AActor> ProjectileClass;
|
||||||
|
|
||||||
/*Functions*/
|
/*Functions*/
|
||||||
UFUNCTION()
|
UFUNCTION()
|
||||||
void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
|
void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
|
||||||
@@ -38,6 +41,15 @@ public:
|
|||||||
UFUNCTION()
|
UFUNCTION()
|
||||||
void TakeDamage(int Damage);
|
void TakeDamage(int Damage);
|
||||||
|
|
||||||
|
UFUNCTION(Server, Reliable, WithValidation, BlueprintCallable)
|
||||||
|
void Server_SpawnProjectile();
|
||||||
|
|
||||||
|
UFUNCTION(Client, Reliable, WithValidation, BlueprintCallable)
|
||||||
|
void Client_CharacterHit();
|
||||||
|
|
||||||
|
UFUNCTION(Server, Reliable, WithValidation, BlueprintCallable)
|
||||||
|
void Server_CharacterHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
|
||||||
|
|
||||||
/*C++ only declarations*/
|
/*C++ only declarations*/
|
||||||
private:
|
private:
|
||||||
/*Properties*/
|
/*Properties*/
|
||||||
|
|||||||
@@ -46,9 +46,7 @@ void AProjectileBase::Tick(float DeltaTime)
|
|||||||
|
|
||||||
void AProjectileBase::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
|
void AProjectileBase::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
|
||||||
{
|
{
|
||||||
if (ADDICharacter* character = Cast<ADDICharacter>(OtherActor))
|
// if (ADDICharacter* character = Cast<ADDICharacter>(OtherActor))
|
||||||
{
|
|
||||||
Destroy();
|
Destroy();
|
||||||
character->TakeDamage(10);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user