继承自初始写的BaseItem类,对炸弹类的模型、是否自动旋转、是否显示特效、碰到后是否显示粒子特效进行处理,这里不再进行统一的描述,附上代码:
static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset(TEXT("StaticMesh'/Game/Res/Items/Bomb/SM_TapLob_Projectile_Bomb.SM_TapLob_Projectile_Bomb'"));
if (MeshAsset.Succeeded())
{
GetMeshComponent()->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
GetMeshComponent()->SetRelativeScale3D(FVector(1.0f));
GetMeshComponent()->SetStaticMesh(MeshAsset.Object);
}
GetParticleSystemComponent()->bAutoActivate = false;
bIsMeshRotate = false;
//查找粒子系统资源
static ConstructorHelpers::FObjectFinder<UParticleSystem> Effect(TEXT("ParticleSystem'/Game/StarterContent/Particles/P_Explosion.P_Explosion'"));
if (Effect.Succeeded())
{
EffectAsset = Effect.Object;
}
//查找音效资源
static ConstructorHelpers::FObjectFinder<USoundCue> Sound(TEXT("SoundCue'/Game/StarterContent/Audio/Explosion_Cue.Explosion_Cue'"));
if (Sound.Succeeded())
{
SoundCue = Sound.Object;
}
上面的代码只是查找资源赋值,部分变量、指针声明放到父类里面了。
申请伤害接口:
伤害的接口有ApplyDamage、ApplyPointDamage、ApplyRadialDamage几种,这里我使用的是ApplyDamage,参数:对谁造成伤害,伤害的数值,导致伤害的控制器(可为nullptr),伤害制造者,伤害类型。
造成伤害接口(TakeDamage):
重写自APawn或者AActor中;
virtual float TakeDamage(float Damage, struct FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser) override;
?对角色造成伤害,角色扣血,对于角色的扣血处理,我写到了玩家控制器里面。
float AFPSShootingGameCharacter::TakeDamage(float Damage, struct FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Take Damege!"));
if (PC->GetCurrentPlayerHP() <= 0.0f)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Player Died!"));
return 0.0f;
}
PC->SetPlayerHP(Damage);
return 0.0f;
}
玩家控制器中,扣血处理,更新视图,保存数据:
void AFGPlayerController::SetPlayerHP(float DelatHP)
{
if (CurrentPlayerHP <= 0.0f)
{
return;
}
CurrentPlayerHP -= DelatHP;
MainUIWidget->UpdateHPProgressBar(CurrentPlayerHP/TotalPlayerHP);
SaveGame();
}
效果:
?
|