linux下网站开发,深圳教育平台网站建设,用word可以做网站吗,太原汽车网站建设之前一直没搞懂按下鼠标左键开火之后#xff0c;代码的逻辑是怎么走的#xff0c;今天看懂了之前没看懂的部分#xff0c;进了一步 ShooterCharacter.cpp void AShooterCharacter::OnStartFire()
{AShooterPlayerController* MyPC CastAShooterPlayerController(Co…之前一直没搞懂按下鼠标左键开火之后代码的逻辑是怎么走的今天看懂了之前没看懂的部分进了一步 ShooterCharacter.cpp void AShooterCharacter::OnStartFire()
{AShooterPlayerController* MyPC CastAShooterPlayerController(Controller);if (MyPC MyPC-IsGameInputAllowed()){if (IsRunning()){SetRunning(false, false);}StartWeaponFire();}
} void AShooterCharacter::StartWeaponFire()
{if (!bWantsToFire){bWantsToFire true;if (CurrentWeapon){CurrentWeapon-StartFire();}}
} ShooterWeapon.cpp其中RoleROLE_Authority表示该程序运行在服务器。如果是客户端则将调用ServerStartFire来调用服务端的StartFire方法。这是多人游戏中的机制 void AShooterWeapon::StartFire()
{if (Role ROLE_Authority){ServerStartFire();}if (!bWantsToFire){bWantsToFire true;DetermineWeaponState();}
} void AShooterWeapon::DetermineWeaponState()
{EWeaponState::Type NewState EWeaponState::Idle;if (bIsEquipped){if( bPendingReload ){if( CanReload() false ){NewState CurrentState;}else{NewState EWeaponState::Reloading;}} else if ( (bPendingReload false ) ( bWantsToFire true ) ( CanFire() true )){NewState EWeaponState::Firing;}}else if (bPendingEquip){NewState EWeaponState::Equipping;}SetWeaponState(NewState);
} void AShooterWeapon::SetWeaponState(EWeaponState::Type NewState)
{const EWeaponState::Type PrevState CurrentState;if (PrevState EWeaponState::Firing NewState ! EWeaponState::Firing){OnBurstFinished();}CurrentState NewState;if (PrevState ! EWeaponState::Firing NewState EWeaponState::Firing){OnBurstStarted();}
} void AShooterWeapon::OnBurstStarted()
{// start firing, can be delayed to satisfy TimeBetweenShotsconst float GameTime GetWorld()-GetTimeSeconds();if (LastFireTime 0 WeaponConfig.TimeBetweenShots 0.0f LastFireTime WeaponConfig.TimeBetweenShots GameTime){GetWorldTimerManager().SetTimer(TimerHandle_HandleFiring, this, AShooterWeapon::HandleFiring, LastFireTime WeaponConfig.TimeBetweenShots - GameTime, false);}else{HandleFiring();}
} 下面标红的FireWeapon这个函数实际上在AShooterWeapon中是一个虚函数并且没有实现实现是放在了子类中。它有两个子类分别是AShooterWeapon_Instant弹道类和AShooterWeapon_Projectile投掷类 我觉得肯定真正使用到的类就是这两个子类所以包括之前的这个流程实际上都是子类中的只不过是从父类中继承而已当然我此时此刻没有去深究那些方法的可见性修饰符是否会被继承这些细节问题我先暂时这么认为吧 void AShooterWeapon::HandleFiring()
{if ((CurrentAmmoInClip 0 || HasInfiniteClip() || HasInfiniteAmmo()) CanFire()){if (GetNetMode() ! NM_DedicatedServer){SimulateWeaponFire();}if (MyPawn MyPawn-IsLocallyControlled()){FireWeapon();UseAmmo();// update firing FX on remote clients if function was called on serverBurstCounter;}}else if (CanReload()){StartReload();}else if (MyPawn MyPawn-IsLocallyControlled()){if (GetCurrentAmmo() 0 !bRefiring){PlayWeaponSound(OutOfAmmoSound);AShooterPlayerController* MyPC CastAShooterPlayerController(MyPawn-Controller);AShooterHUD* MyHUD MyPC ? CastAShooterHUD(MyPC-GetHUD()) : NULL;if (MyHUD){MyHUD-NotifyOutOfAmmo();}}// stop weapon fire FX, but stay in Firing stateif (BurstCounter 0){OnBurstFinished();}}if (MyPawn MyPawn-IsLocallyControlled()){// local client will notify serverif (Role ROLE_Authority){ServerHandleFiring();}// reload after firing last roundif (CurrentAmmoInClip 0 CanReload()){StartReload();}// setup refire timerbRefiring (CurrentState EWeaponState::Firing WeaponConfig.TimeBetweenShots 0.0f);if (bRefiring){GetWorldTimerManager().SetTimer(TimerHandle_HandleFiring, this, AShooterWeapon::HandleFiring, WeaponConfig.TimeBetweenShots, false);}}LastFireTime GetWorld()-GetTimeSeconds();
} void AShooterWeapon_Instant::FireWeapon()
{const int32 RandomSeed FMath::Rand();FRandomStream WeaponRandomStream(RandomSeed);const float CurrentSpread GetCurrentSpread();const float ConeHalfAngle FMath::DegreesToRadians(CurrentSpread * 0.5f);//获取AShooterPlayerController的rotation的单位向量const FVector AimDir GetAdjustedAim();const FVector StartTrace GetCameraDamageStartLocation(AimDir);const FVector ShootDir WeaponRandomStream.VRandCone(AimDir, ConeHalfAngle, ConeHalfAngle);const FVector EndTrace StartTrace ShootDir * InstantConfig.WeaponRange;const FHitResult Impact WeaponTrace(StartTrace, EndTrace);ProcessInstantHit(Impact, StartTrace, ShootDir, RandomSeed, CurrentSpread);CurrentFiringSpread FMath::Min(InstantConfig.FiringSpreadMax, CurrentFiringSpread InstantConfig.FiringSpreadIncrement);
} 转载于:https://www.cnblogs.com/heben/p/8046726.html