21uec++多人游戏【AI联机】-创新互联
一句话,跟掉血有关的代码都在服务器端执行
创新互联建站主营灞桥网站建设的网络公司,主营网站建设方案,app开发定制,灞桥h5小程序开发搭建,灞桥网站营销推广欢迎灞桥等地区企业咨询修改小球的游戏开始函数
在服务器端,小球自动寻找路径
void AASTrackerBot::BeginPlay()
{
Super::BeginPlay();
if (Role == ROLE_Authority)
{
NextPathPoint = GetNextPathPoint();
}
//伤害事件绑定函数
HealthComp->OnHealthChanged.AddDynamic(this, &AASTrackerBot::HandleTakeAnyDamage);
}
修改SelfDestruct函数
在服务器端小球会对自己施加伤害
void AASTrackerBot::SelfDestruct()
{
//检查是否已经爆炸了
if (bExplored)
{
return;
}
bExplored = true;
//发生爆炸
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ExplorEffect, GetActorLocation());
//设置要忽略的actor
TArray IgnoredActors;
IgnoredActors.Add(this);
// UKismetSystemLibrary::PrintString(this, FString::SanitizeFloat(222.0f));
//伤害在服务器中执行
if (Role == ROLE_Authority)
{
//对自身进行伤害
UGameplayStatics::ApplyRadialDamage(this, ExplorDamage, GetActorLocation(), ExplorRadius, nullptr, IgnoredActors, this, GetInstigatorController(), true);
SetLifeSpan(2.0f);
}
//画出伤害范围
DrawDebugSphere(GetWorld(), GetActorLocation(), ExplorRadius, 12, FColor::Green, false, 2.0f, 0, 1.0f);
//发生爆炸声,在actor的位置
UGameplayStatics::PlaySoundAtLocation(this, ExploedSound, GetActorLocation());
//自毁
//Destroy();
MeshComponent->SetVisibility(false, true);
MeshComponent->SetSimulatePhysics(false);
MeshComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
修改每帧函数
在服务器端,小球自动寻找路径
void AASTrackerBot::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// UKismetSystemLibrary::PrintString(this, FString::SanitizeFloat(HealthComp->Health));
//得到小球自身与下一个点的距离
if (Role == ROLE_Authority && !bExplored)
{
float DistanceToTarget = (NextPathPoint - GetActorLocation()).Size();
//如果大于阈值,就继续滚
if (DistanceToTarget >RequiredDistanceToTarget)
{
//小球的移动方向
FVector ForceDirection = NextPathPoint - GetActorLocation();
ForceDirection.Normalize();
//小球的推力
ForceDirection *= MovementForce;
MeshComponent->ComponentVelocity.Size();
MeshComponent->AddImpulse(ForceDirection, NAME_None, bUseVelocityChange);
DrawDebugDirectionalArrow(GetWorld(), GetActorLocation(), GetActorLocation() + ForceDirection, 32, FColor::Red, false, 0.0f, 0, 1.0f);
}
//如果到达了路径点,就生成下一个点,继续移动
else
{
NextPathPoint = GetNextPathPoint();
DrawDebugString(GetWorld(), GetActorLocation(), "Target Reached!");
}
//在下一个目标点画一个球
DrawDebugSphere(GetWorld(), NextPathPoint, 20, 12, FColor::Yellow, false, 0.0f, 0, 1.0f);
}
}
修改重叠函数
在服务器端,小球自爆倒计时
void AASTrackerBot::NotifyActorBeginOverlap(AActor * OtherActor)
{
if (!bStartSelfDamge)
{
ASCharacter * PlayerPawn = Cast(OtherActor);
if (PlayerPawn)
{
if (Role == ROLE_Authority)
{
GetWorld()->GetTimerManager().SetTimer(TimerHandle_SelfDamage, this, &AASTrackerBot::DamageSelf, 0.5f, true, 0.0f);
}
bStartSelfDamge = true;
}
//将自爆警告声音绑定到根组件
UGameplayStatics::SpawnSoundAttached(SelfDestructSound, RootComponent);
}
}
修改生命值组件
//当前生命值
UPROPERTY(ReplicatedUsing = OnRep_Health, BlueprintReadOnly, Category = "HealthComponent")
float Health;
添加网络同步函数
//生命值同步函数
UFUNCTION()
void OnRep_Health(float OldHealth);
实现一下
void USHealthComponent::OnRep_Health(float OldHealth)
{
//触发自定义事件
OnHealthChanged.Broadcast(this, Health, OldHealth - Health , nullptr, nullptr, nullptr);
}
为什么要这样同步一下呢
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
分享标题:21uec++多人游戏【AI联机】-创新互联
文章转载:http://myzitong.com/article/cophgj.html