text: // Fill out your copyright notice in the Description page of Project Settings.
  
  #include "SExplosiveBarrel.h"
  #include "SHealthComponent.h"
  #include "Kismet/GameplayStatics.h"
  #include "PhysicsEngine/RadialForceComponent.h"
  #include "Net/UnrealNetwork.h"
  
  // Sets default values
  ASExplosiveBarrel::ASExplosiveBarrel()
  {	
  	HealthComp = CreateDefaultSubobject<USHealthComponent>(TEXT("HealthComp"));
  	HealthComp->OnHealthChanged.AddDynamic(this, &ASExplosiveBarrel::OnHealthChanged);
  
  	MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));
  	MeshComp->SetSimulatePhysics(true);
  	MeshComp->SetCollisionObjectType(ECC_PhysicsBody);
  	RootComponent = MeshComp;
  
  	RadialForceComp = CreateDefaultSubobject<URadialForceComponent>(TEXT("RadialForceComp"));
  	RadialForceComp->SetupAttachment(MeshComp);
  	RadialForceComp->Radius = 250;
  	RadialForceComp->bImpulseVelChange = true;
  	RadialForceComp->bAutoActivate = false;
  	RadialForceComp->bIgnoreOwningActor = true;
  
  	ExplosionImpulse = 400;
  		
  	SetReplicates(true);
  	SetReplicateMovement(true);
  }
  
  void ASExplosiveBarrel::OnHealthChanged(USHealthComponent* OwningHealthComp, float Health, float HealthDelta, const class UDamageType* DamageType, class AController* InstigateBy, AActor* DamageCauser)
  {
  	if (bExploded) 
  	{
  		return;
  	}
  
  	if (Health <= 0.0f)
  	{
  		// Explode!
  		bExploded = true;
  		OnRep_Exploded();
  
  		// Boost the barrel upwards
  		FVector BoostIntensity = FVector::UpVector * ExplosionImpulse;
  		MeshComp->AddImpulse(BoostIntensity, NAME_None, true);
  		// Blast away nearby physics actors
  		RadialForceComp->FireImpulse();
  
  		// @TODO: Apply radial damage
  	}
  }
  
  void ASExplosiveBarrel::OnRep_Exploded()
  {
  	// Play FX and change self material to black
  	UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ExplosionEffect, GetActorLocation());
  	// Override material on mesh with blackened version
  	MeshComp->SetMaterial(0, ExplodedMaterial);
  }
  
  void ASExplosiveBarrel::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  {
  	Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  
  	DOREPLIFETIME(ASExplosiveBarrel, bExploded);
  }