Advertisement
Guest User

Untitled

a guest
May 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #pragma once
  4.  
  5. #include "CoreMinimal.h"
  6. #include "GameFramework/Actor.h"
  7. #include "SWeapon.generated.h"
  8.  
  9. class USkeletalMeshComponent;
  10. class UDamageType;
  11. class UParticleSystem;
  12.  
  13.  
  14. UCLASS()
  15. class CPPSHOOTER_API ASWeapon : public AActor
  16. {
  17. GENERATED_BODY()
  18.  
  19.  
  20. public:
  21. // Sets default values for this actor's properties
  22. ASWeapon();
  23.  
  24.  
  25. protected:
  26. virtual void BeginPlay() override;
  27.  
  28. UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
  29. USkeletalMeshComponent* MeshComp;
  30.  
  31. void PlayFireEffects(FVector TraceEnd);
  32.  
  33. UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  34. TSubclassOf<UDamageType> DamageType;
  35.  
  36. UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  37. FName MuzzleSocketName;
  38.  
  39. UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  40. FName TracerTargetName;
  41.  
  42. UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  43. UParticleSystem* MuzzleEffect;
  44.  
  45. UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  46. UParticleSystem* DefaultImpactEffect;
  47.  
  48. UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  49. UParticleSystem* FleshImpactEffect;
  50.  
  51. UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  52. UParticleSystem* TracerEffect;
  53.  
  54. UPROPERTY(EditDefaultsOnly, Category = "Weapon")
  55. TSubclassOf<UCameraShake> FireCamShake;
  56.  
  57. UPROPERTY(EditDefaultsOnly, Category = "Weapon")
  58. float BaseDamage;
  59.  
  60. FTimerHandle TimerHandle_Reloading;
  61.  
  62. UPROPERTY(Replicated, EditAnywhere, BlueprintReadWrite, Category = "Weapon", meta = (ToolTip = "Number of bullets available to shoot"))
  63. int32 TotalAmmo = 1000;
  64.  
  65. UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon", meta = (ToolTip = "Max number of bullets in the chamber"))
  66. int32 MaxChamberAmmo = 10;
  67.  
  68. UPROPERTY(Replicated, EditAnywhere, BlueprintReadWrite, Category = "Weapon", meta = (ToolTip = "Current number of bullets in the chamber"))
  69. int32 CurrentChamberAmmo = 10;
  70.  
  71. UPROPERTY(Replicated, VisibleAnywhere, BlueprintReadOnly, Category = "Weapon", meta = (ToolTip = "Flag to know when the weapon is in reloading time"))
  72. bool IsReloading = false;
  73.  
  74. UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon", meta = (ToolTip = "Time in seconds to reload the weapon"))
  75. float TimeToReload = 0.9f;
  76.  
  77. void Fire();
  78.  
  79. FTimerHandle TimerHandle_TimeBetweenShots;
  80.  
  81. float LastFireTime;
  82.  
  83. /*RPM - Bullets per minute fired by weapon */
  84. UPROPERTY(EditDefaultsOnly, Category = "Weapon")
  85. float RateOfFire;
  86.  
  87. // Derived from rate of fire
  88. float TimeBetweenShots;
  89.  
  90. UFUNCTION(BlueprintCallable, Category = "Weapons")
  91. virtual bool CheckAmmo() {
  92. if (IsReloading) return false;
  93.  
  94. if (!HasAmmo())
  95. {
  96. if (CanReloadAmmo())
  97. {
  98. Reload();
  99. return false;
  100. }
  101. else
  102. {
  103. //No ammo
  104. StopFire();
  105. return false;
  106. }
  107.  
  108. return true;
  109.  
  110. UFUNCTION(BlueprintCallable, Category = "Weapons")
  111. virtual bool HasAmmo() const { return CurrentChamberAmmo > 0 && TotalAmmo > 0; }
  112.  
  113. UFUNCTION(BlueprintCallable, Category = "Weapons")
  114. virtual bool CanReloadAmmo() const { return TotalAmmo > 0 && CurrentChamberAmmo < MaxChamberAmmo; }
  115.  
  116. UFUNCTION(BlueprintCallable, Category = "Weapons")
  117. virtual void Reload() {
  118. if (IsReloading) return;
  119. IsReloading = true;
  120. int32 oldammo = CurrentChamberAmmo;
  121. int32 newammo = MaxChamberAmmo - oldammo;
  122. CurrentChamberAmmo = TotalAmmo >= newammo ? newammo : TotalAmmo;
  123. TotalAmmo -= newammo;
  124.  
  125. GetWorldTimerManager().SetTimer(
  126. TimerHandle_Reloading, [this]() {
  127. IsReloading = false; }, TimeToReload, false, TimeToReload);
  128. }
  129.  
  130. public:
  131.  
  132. void StartFire();
  133.  
  134. void StopFire();
  135.  
  136. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement