コード例 #1
0
float ABaseCharacter::TakeDamage(float Damage, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, class AActor* DamageCauser)
{
	if (Health > 0.f)
	{
		/* Modify based based on gametype rules */
		//ASGameMode* MyGameMode = Cast<ASGameMode>(GetWorld()->GetAuthGameMode());
		//Damage = MyGameMode ? MyGameMode->ModifyDamage(Damage, this, DamageEvent, EventInstigator, DamageCauser) : Damage;


		const float ActualDamage = Super::TakeDamage(Damage, DamageEvent, EventInstigator, DamageCauser);
		if (ActualDamage > 0.f)
		{
			Health -= ActualDamage;
			if (Health <= 0)
			{
				bool bCanDie = true;

				/* Check the damagetype, always allow dying if the cast fails, otherwise check the property if player can die from damagetype */
				if (DamageEvent.DamageTypeClass)
				{
					UShooterDamageType* DmgType = Cast<UShooterDamageType>(DamageEvent.DamageTypeClass->GetDefaultObject());
					bCanDie = (DmgType == nullptr || (DmgType && DmgType->GetCanDieFrom()));
				}

				if (bCanDie)
				{
					Die(ActualDamage, DamageEvent, EventInstigator, DamageCauser);
				}
				else
				{
					/* Player cannot die from this damage type, set hitpoints to 1.0 */
					Health = 1.0f;
				}
			}
			else
			{
				/* Shorthand for - if x != null pick1 else pick2 */
				//APawn* Pawn = EventInstigator ? EventInstigator->GetPawn() : nullptr;
				PlayHit(false, ActualDamage, DamageEvent, EventInstigator->GetPawn(), DamageCauser);
			}
		}
		GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Red, FString::SanitizeFloat(Health));
		return ActualDamage;
	}
	else
	{
		ApplyPhysicsToTheRagdolledBody(DamageEvent);

	}
	return 0;
}
コード例 #2
0
float AShooterCharacter::TakeDamage(float Damage, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, class AActor* DamageCauser)
{
	if (Health <= 0.f)
	{
		return 0.f;
	}

	const float ActualDamage = Super::TakeDamage(Damage, DamageEvent, EventInstigator, DamageCauser);
	if (ActualDamage > 0.f)
	{
		Health -= ActualDamage;
		if (Health <= 0)
		{
			bool bCanDie = true;

			/* Check the damagetype, always allow dying if the cast fails, otherwise check the property if player can die from damagetype */
			if (DamageEvent.DamageTypeClass)
			{
				UShooterDamageType* DmgType = Cast<UShooterDamageType>(DamageEvent.DamageTypeClass->GetDefaultObject());
				bCanDie = (DmgType == nullptr || (DmgType && DmgType->GetCanDieFrom()));
			}

			if (bCanDie)
			{
				Die(ActualDamage, DamageEvent, EventInstigator, DamageCauser);
			}
			else
			{
				/* Player cannot die from this damage type, set hitpoints to 1.0 */
				Health = 1.0f;
			}
		}
		else
		{
			/* Shorthand for - if x != null pick1 else pick2 */
			APawn* Pawn = EventInstigator ? EventInstigator->GetPawn() : nullptr;
			PlayHit(ActualDamage, DamageEvent, Pawn, DamageCauser, false);
		}
	}

	return ActualDamage;
}