Advertisement
Kenturrac

Untitled

Sep 15th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.69 KB | None | 0 0
  1. // Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
  2.  
  3. #pragma once
  4.  
  5. #include "ShooterWeapon.generated.h"
  6.  
  7. namespace EWeaponState
  8. {
  9. enum Type
  10. {
  11. Idle,
  12. Firing,
  13. Reloading,
  14. Equipping,
  15. };
  16. }
  17.  
  18. USTRUCT()
  19. struct FWeaponData
  20. {
  21. GENERATED_USTRUCT_BODY()
  22.  
  23. /** inifite ammo for reloads */
  24. UPROPERTY(EditDefaultsOnly, Category=Ammo)
  25. bool bInfiniteAmmo;
  26.  
  27. /** infinite ammo in clip, no reload required */
  28. UPROPERTY(EditDefaultsOnly, Category=Ammo)
  29. bool bInfiniteClip;
  30.  
  31. /** max ammo */
  32. UPROPERTY(EditDefaultsOnly, Category=Ammo)
  33. int32 MaxAmmo;
  34.  
  35. /** clip size */
  36. UPROPERTY(EditDefaultsOnly, Category=Ammo)
  37. int32 AmmoPerClip;
  38.  
  39. /** initial clips */
  40. UPROPERTY(EditDefaultsOnly, Category=Ammo)
  41. int32 InitialClips;
  42.  
  43. /** time between two consecutive shots */
  44. UPROPERTY(EditDefaultsOnly, Category=WeaponStat)
  45. float TimeBetweenShots;
  46.  
  47. /** failsafe reload duration if weapon doesn't have any animation for it */
  48. UPROPERTY(EditDefaultsOnly, Category=WeaponStat)
  49. float NoAnimReloadDuration;
  50.  
  51. /** defaults */
  52. FWeaponData()
  53. {
  54. bInfiniteAmmo = false;
  55. bInfiniteClip = false;
  56. MaxAmmo = 100;
  57. AmmoPerClip = 20;
  58. InitialClips = 4;
  59. TimeBetweenShots = 0.2f;
  60. NoAnimReloadDuration = 1.0f;
  61. }
  62. };
  63.  
  64. USTRUCT()
  65. struct FWeaponAnim
  66. {
  67. GENERATED_USTRUCT_BODY()
  68.  
  69. /** animation played on pawn (1st person view) */
  70. UPROPERTY(EditDefaultsOnly, Category=Animation)
  71. UAnimMontage* Pawn1P;
  72.  
  73. /** animation played on pawn (3rd person view) */
  74. UPROPERTY(EditDefaultsOnly, Category=Animation)
  75. UAnimMontage* Pawn3P;
  76. };
  77.  
  78. UCLASS(Abstract, Blueprintable)
  79. class AShooterWeapon : public AActor
  80. {
  81. GENERATED_UCLASS_BODY()
  82.  
  83. /** perform initial setup */
  84. virtual void PostInitializeComponents() override;
  85.  
  86. virtual void Destroyed() override;
  87.  
  88. //////////////////////////////////////////////////////////////////////////
  89. // Ammo
  90.  
  91. enum class EAmmoType
  92. {
  93. EBullet,
  94. ERocket,
  95. EMax,
  96. };
  97.  
  98. /** [server] add ammo */
  99. void GiveAmmo(int AddAmount);
  100.  
  101. /** consume a bullet */
  102. void UseAmmo();
  103.  
  104. /** query ammo type */
  105. virtual EAmmoType GetAmmoType() const
  106. {
  107. return EAmmoType::EBullet;
  108. }
  109.  
  110. //////////////////////////////////////////////////////////////////////////
  111. // Inventory
  112.  
  113. /** weapon is being equipped by owner pawn */
  114. virtual void OnEquip();
  115.  
  116. /** weapon is now equipped by owner pawn */
  117. virtual void OnEquipFinished();
  118.  
  119. /** weapon is holstered by owner pawn */
  120. virtual void OnUnEquip();
  121.  
  122. /** [server] weapon was added to pawn's inventory */
  123. virtual void OnEnterInventory(AShooterCharacter* NewOwner);
  124.  
  125. /** [server] weapon was removed from pawn's inventory */
  126. virtual void OnLeaveInventory();
  127.  
  128. /** check if it's currently equipped */
  129. bool IsEquipped() const;
  130.  
  131. /** check if mesh is already attached */
  132. bool IsAttachedToPawn() const;
  133.  
  134.  
  135. //////////////////////////////////////////////////////////////////////////
  136. // Input
  137.  
  138. /** [local + server] start weapon fire */
  139. virtual void StartFire();
  140.  
  141. /** [local + server] stop weapon fire */
  142. virtual void StopFire();
  143.  
  144. /** [all] start weapon reload */
  145. virtual void StartReload(bool bFromReplication = false);
  146.  
  147. /** [local + server] interrupt weapon reload */
  148. virtual void StopReload();
  149.  
  150. /** [server] performs actual reload */
  151. virtual void ReloadWeapon();
  152.  
  153. /** trigger reload from server */
  154. UFUNCTION(reliable, client)
  155. void ClientStartReload();
  156.  
  157.  
  158. //////////////////////////////////////////////////////////////////////////
  159. // Control
  160.  
  161. /** check if weapon can fire */
  162. bool CanFire() const;
  163.  
  164. /** check if weapon can be reloaded */
  165. bool CanReload() const;
  166.  
  167.  
  168. //////////////////////////////////////////////////////////////////////////
  169. // Reading data
  170.  
  171. /** get current weapon state */
  172. EWeaponState::Type GetCurrentState() const;
  173.  
  174. /** get current ammo amount (total) */
  175. // UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Game)
  176. int32 GetCurrentAmmo() const;
  177.  
  178. /** get current ammo amount (clip) */
  179. // UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Game)
  180. int32 GetCurrentAmmoInClip() const;
  181.  
  182. /** get clip size */
  183. int32 GetAmmoPerClip() const;
  184.  
  185. /** get max ammo amount */
  186. int32 GetMaxAmmo() const;
  187.  
  188. /** get weapon mesh (needs pawn owner to determine variant) */
  189. USkeletalMeshComponent* GetWeaponMesh() const;
  190.  
  191. /** get pawn owner */
  192. UFUNCTION(BlueprintCallable, Category="Game|Weapon")
  193. class AShooterCharacter* GetPawnOwner() const;
  194.  
  195. /** icon displayed on the HUD when weapon is equipped as primary */
  196. UPROPERTY(EditDefaultsOnly, Category=HUD)
  197. FCanvasIcon PrimaryIcon;
  198.  
  199. /** icon displayed on the HUD when weapon is secondary */
  200. UPROPERTY(EditDefaultsOnly, Category=HUD)
  201. FCanvasIcon SecondaryIcon;
  202.  
  203. /** bullet icon used to draw current clip (left side) */
  204. UPROPERTY(EditDefaultsOnly, Category=HUD)
  205. FCanvasIcon PrimaryClipIcon;
  206.  
  207. /** bullet icon used to draw secondary clip (left side) */
  208. UPROPERTY(EditDefaultsOnly, Category=HUD)
  209. FCanvasIcon SecondaryClipIcon;
  210.  
  211. /** how many icons to draw per clip */
  212. UPROPERTY(EditDefaultsOnly, Category=HUD)
  213. float AmmoIconsCount;
  214.  
  215. /** defines spacing between primary ammo icons (left side) */
  216. UPROPERTY(EditDefaultsOnly, Category=HUD)
  217. int32 PrimaryClipIconOffset;
  218.  
  219. /** defines spacing between secondary ammo icons (left side) */
  220. UPROPERTY(EditDefaultsOnly, Category=HUD)
  221. int32 SecondaryClipIconOffset;
  222.  
  223. /** crosshair parts icons (left, top, right, bottom and center) */
  224. UPROPERTY(EditDefaultsOnly, Category=HUD)
  225. FCanvasIcon Crosshair[5];
  226.  
  227. /** crosshair parts icons when targeting (left, top, right, bottom and center) */
  228. UPROPERTY(EditDefaultsOnly, Category=HUD)
  229. FCanvasIcon AimingCrosshair[5];
  230.  
  231. /** only use red colored center part of aiming crosshair */
  232. UPROPERTY(EditDefaultsOnly, Category=HUD)
  233. bool UseLaserDot;
  234.  
  235. /** false = default crosshair */
  236. UPROPERTY(EditDefaultsOnly, Category=HUD)
  237. bool UseCustomCrosshair;
  238.  
  239. /** false = use custom one if set, otherwise default crosshair */
  240. UPROPERTY(EditDefaultsOnly, Category=HUD)
  241. bool UseCustomAimingCrosshair;
  242.  
  243. /** true - crosshair will not be shown unless aiming with the weapon */
  244. UPROPERTY(EditDefaultsOnly, Category=HUD)
  245. bool bHideCrosshairWhileNotAiming;
  246.  
  247. /** check if weapon has infinite ammo (include owner's cheats) */
  248. bool HasInfiniteAmmo() const;
  249.  
  250. /** check if weapon has infinite clip (include owner's cheats) */
  251. bool HasInfiniteClip() const;
  252.  
  253. /** set the weapon's owning pawn */
  254. void SetOwningPawn(AShooterCharacter* AShooterCharacter);
  255.  
  256. /** gets last time when this weapon was switched to */
  257. float GetEquipStartedTime() const;
  258.  
  259. /** gets the duration of equipping weapon*/
  260. float GetEquipDuration() const;
  261.  
  262. protected:
  263.  
  264. /** pawn owner */
  265. UPROPERTY(Transient, ReplicatedUsing=OnRep_MyPawn)
  266. class AShooterCharacter* MyPawn;
  267.  
  268. /** weapon data */
  269. UPROPERTY(EditDefaultsOnly, Category=Config)
  270. FWeaponData WeaponConfig;
  271.  
  272. private:
  273. /** weapon mesh: 1st person view */
  274. UPROPERTY(VisibleDefaultsOnly, Category=Mesh)
  275. USkeletalMeshComponent* Mesh1P;
  276.  
  277. /** weapon mesh: 3rd person view */
  278. UPROPERTY(VisibleDefaultsOnly, Category=Mesh)
  279. USkeletalMeshComponent* Mesh3P;
  280. protected:
  281.  
  282. /** firing audio (bLoopedFireSound set) */
  283. UPROPERTY(Transient)
  284. UAudioComponent* FireAC;
  285.  
  286. /** name of bone/socket for muzzle in weapon mesh */
  287. UPROPERTY(EditDefaultsOnly, Category=Effects)
  288. FName MuzzleAttachPoint;
  289.  
  290. /** FX for muzzle flash */
  291. UPROPERTY(EditDefaultsOnly, Category=Effects)
  292. UParticleSystem* MuzzleFX;
  293.  
  294. /** spawned component for muzzle FX */
  295. UPROPERTY(Transient)
  296. UParticleSystemComponent* MuzzlePSC;
  297.  
  298. /** spawned component for second muzzle FX (Needed for split screen) */
  299. UPROPERTY(Transient)
  300. UParticleSystemComponent* MuzzlePSCSecondary;
  301.  
  302. /** camera shake on firing */
  303. UPROPERTY(EditDefaultsOnly, Category=Effects)
  304. TSubclassOf<UCameraShake> FireCameraShake;
  305.  
  306. /** force feedback effect to play when the weapon is fired */
  307. UPROPERTY(EditDefaultsOnly, Category=Effects)
  308. UForceFeedbackEffect *FireForceFeedback;
  309.  
  310. /** single fire sound (bLoopedFireSound not set) */
  311. UPROPERTY(EditDefaultsOnly, Category=Sound)
  312. USoundCue* FireSound;
  313.  
  314. /** looped fire sound (bLoopedFireSound set) */
  315. UPROPERTY(EditDefaultsOnly, Category=Sound)
  316. USoundCue* FireLoopSound;
  317.  
  318. /** finished burst sound (bLoopedFireSound set) */
  319. UPROPERTY(EditDefaultsOnly, Category=Sound)
  320. USoundCue* FireFinishSound;
  321.  
  322. /** out of ammo sound */
  323. UPROPERTY(EditDefaultsOnly, Category=Sound)
  324. USoundCue* OutOfAmmoSound;
  325.  
  326. /** reload sound */
  327. UPROPERTY(EditDefaultsOnly, Category=Sound)
  328. USoundCue* ReloadSound;
  329.  
  330. /** reload animations */
  331. UPROPERTY(EditDefaultsOnly, Category=Animation)
  332. FWeaponAnim ReloadAnim;
  333.  
  334. /** equip sound */
  335. UPROPERTY(EditDefaultsOnly, Category=Sound)
  336. USoundCue* EquipSound;
  337.  
  338. /** equip animations */
  339. UPROPERTY(EditDefaultsOnly, Category=Animation)
  340. FWeaponAnim EquipAnim;
  341.  
  342. /** fire animations */
  343. UPROPERTY(EditDefaultsOnly, Category=Animation)
  344. FWeaponAnim FireAnim;
  345.  
  346. /** is muzzle FX looped? */
  347. UPROPERTY(EditDefaultsOnly, Category=Effects)
  348. uint32 bLoopedMuzzleFX : 1;
  349.  
  350. /** is fire sound looped? */
  351. UPROPERTY(EditDefaultsOnly, Category=Sound)
  352. uint32 bLoopedFireSound : 1;
  353.  
  354. /** is fire animation looped? */
  355. UPROPERTY(EditDefaultsOnly, Category=Animation)
  356. uint32 bLoopedFireAnim : 1;
  357.  
  358. /** is fire animation playing? */
  359. uint32 bPlayingFireAnim : 1;
  360.  
  361. /** is weapon currently equipped? */
  362. uint32 bIsEquipped : 1;
  363.  
  364. /** is weapon fire active? */
  365. uint32 bWantsToFire : 1;
  366.  
  367. /** is reload animation playing? */
  368. UPROPERTY(Transient, ReplicatedUsing=OnRep_Reload)
  369. uint32 bPendingReload : 1;
  370.  
  371. /** is equip animation playing? */
  372. uint32 bPendingEquip : 1;
  373.  
  374. /** weapon is refiring */
  375. uint32 bRefiring;
  376.  
  377. /** current weapon state */
  378. EWeaponState::Type CurrentState;
  379.  
  380. /** time of last successful weapon fire */
  381. float LastFireTime;
  382.  
  383. /** last time when this weapon was switched to */
  384. float EquipStartedTime;
  385.  
  386. /** how much time weapon needs to be equipped */
  387. float EquipDuration;
  388.  
  389. /** current total ammo */
  390. UPROPERTY(Transient, Replicated)
  391. int32 CurrentAmmo;
  392.  
  393. /** current ammo - inside clip */
  394. UPROPERTY(Transient, Replicated)
  395. int32 CurrentAmmoInClip;
  396.  
  397. /** burst counter, used for replicating fire events to remote clients */
  398. UPROPERTY(Transient, ReplicatedUsing=OnRep_BurstCounter)
  399. int32 BurstCounter;
  400.  
  401. /** Handle for efficient management of OnEquipFinished timer */
  402. FTimerHandle TimerHandle_OnEquipFinished;
  403.  
  404. /** Handle for efficient management of StopReload timer */
  405. FTimerHandle TimerHandle_StopReload;
  406.  
  407. /** Handle for efficient management of ReloadWeapon timer */
  408. FTimerHandle TimerHandle_ReloadWeapon;
  409.  
  410. /** Handle for efficient management of HandleFiring timer */
  411. FTimerHandle TimerHandle_HandleFiring;
  412.  
  413. //////////////////////////////////////////////////////////////////////////
  414. // Input - server side
  415.  
  416. UFUNCTION(reliable, server, WithValidation)
  417. void ServerStartFire();
  418.  
  419. UFUNCTION(reliable, server, WithValidation)
  420. void ServerStopFire();
  421.  
  422. UFUNCTION(reliable, server, WithValidation)
  423. void ServerStartReload();
  424.  
  425. UFUNCTION(reliable, server, WithValidation)
  426. void ServerStopReload();
  427.  
  428.  
  429. //////////////////////////////////////////////////////////////////////////
  430. // Replication & effects
  431.  
  432. UFUNCTION()
  433. void OnRep_MyPawn();
  434.  
  435. UFUNCTION()
  436. void OnRep_BurstCounter();
  437.  
  438. UFUNCTION()
  439. void OnRep_Reload();
  440.  
  441. /** Called in network play to do the cosmetic fx for firing */
  442. virtual void SimulateWeaponFire();
  443.  
  444. /** Called in network play to stop cosmetic fx (e.g. for a looping shot). */
  445. virtual void StopSimulatingWeaponFire();
  446.  
  447.  
  448. //////////////////////////////////////////////////////////////////////////
  449. // Weapon usage
  450.  
  451. /** [local] weapon specific fire implementation */
  452. virtual void FireWeapon() PURE_VIRTUAL(AShooterWeapon::FireWeapon,);
  453.  
  454. /** [server] fire & update ammo */
  455. UFUNCTION(reliable, server, WithValidation)
  456. void ServerHandleFiring();
  457.  
  458. /** [local + server] handle weapon fire */
  459. void HandleFiring();
  460.  
  461. /** [local + server] firing started */
  462. virtual void OnBurstStarted();
  463.  
  464. /** [local + server] firing finished */
  465. virtual void OnBurstFinished();
  466.  
  467. /** update weapon state */
  468. void SetWeaponState(EWeaponState::Type NewState);
  469.  
  470. /** determine current weapon state */
  471. void DetermineWeaponState();
  472.  
  473.  
  474. //////////////////////////////////////////////////////////////////////////
  475. // Inventory
  476.  
  477. /** attaches weapon mesh to pawn's mesh */
  478. void AttachMeshToPawn();
  479.  
  480. /** detaches weapon mesh from pawn */
  481. void DetachMeshFromPawn();
  482.  
  483.  
  484. //////////////////////////////////////////////////////////////////////////
  485. // Weapon usage helpers
  486.  
  487. /** play weapon sounds */
  488. UAudioComponent* PlayWeaponSound(USoundCue* Sound);
  489.  
  490. /** play weapon animations */
  491. float PlayWeaponAnimation(const FWeaponAnim& Animation);
  492.  
  493. /** stop playing weapon animations */
  494. void StopWeaponAnimation(const FWeaponAnim& Animation);
  495.  
  496. /** Get the aim of the weapon, allowing for adjustments to be made by the weapon */
  497. virtual FVector GetAdjustedAim() const;
  498.  
  499. /** Get the aim of the camera */
  500. FVector GetCameraAim() const;
  501.  
  502. /** get the originating location for camera damage */
  503. FVector GetCameraDamageStartLocation(const FVector& AimDir) const;
  504.  
  505. /** get the muzzle location of the weapon */
  506. FVector GetMuzzleLocation() const;
  507.  
  508. /** get direction of weapon's muzzle */
  509. FVector GetMuzzleDirection() const;
  510.  
  511. /** find hit */
  512. FHitResult WeaponTrace(const FVector& TraceFrom, const FVector& TraceTo) const;
  513.  
  514. protected:
  515. /** Returns Mesh1P subobject **/
  516. FORCEINLINE USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; }
  517. /** Returns Mesh3P subobject **/
  518. FORCEINLINE USkeletalMeshComponent* GetMesh3P() const { return Mesh3P; }
  519. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement