LEAP Documentation 40220
Documentation for the LEAP project
WeaponComponent.h
Go to the documentation of this file.
1// Copyright Blue Isle Studios Inc 2017. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "PlayerStructs.h"
7#include "GameFramework/GameStateBase.h"
8#include "WeaponComponent.generated.h"
9
10class USkeletalMeshComponent;
11class UWeaponInstance;
12
13UENUM(BlueprintType)
14enum class EWeaponError : uint8
15{
17 OutOfAmmo = 1,
18 OnCoolDown = 2,
19 NoFlashlight = 3,
21 FullyLoaded = 5,
23 NotReady = 7, //used by weapon instances, generically signifies the weapon is busy doing something that doesn't let it perform the requested action.
25 OutOfRange = 9,
26 Overheated = 10,
27 AltitudeTooLow = 11,
28 OutOfEnergy = 12
29};
30
31DECLARE_DYNAMIC_MULTICAST_DELEGATE(FWeaponRemovedSignature);
32DECLARE_DYNAMIC_MULTICAST_DELEGATE(FWeaponAnimationSignature);
33DECLARE_DYNAMIC_MULTICAST_DELEGATE(FWeaponReloadSignature);
35DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FWeaponAmmoChangeSignature, int32, Amount);
36DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FReloadEventSignature, UWeaponComponent*, WeaponComponent);
37DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDeployableSpawnedSignature, class ADeployable*, Deployable);
38DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDeployableDestroyedSignature, class ADeployable*, Deployable);
39DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FReticleVisibilityChangedSignature, bool, bVisible);
45DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FPersonalVehicleEventSignature, bool, bHold, float, HoldDuration);
47DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FLockOnStrengthChangedSignature, class UWeaponInstanceProjectile*, Weapon, float, DeltaStrength);
48
49UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
50class PROJECTX_API UWeaponComponent : public UActorComponent
51{
52 GENERATED_UCLASS_BODY()
53
54public:
55 UFUNCTION(BlueprintPure, Category = WeaponComponent)
56 FORCEINLINE bool IsLocallyControlled() const { return GetController() && GetController()->IsLocalController(); }
57
58 UFUNCTION(BlueprintPure, Category = WeaponComponent)
59 FORCEINLINE bool IsHoldingAltFire() const { return bHoldingAltFire;}
60
61 UFUNCTION(BlueprintPure, Category = WeaponComponent)
62 FORCEINLINE bool IsHoldingFire() const { return bHoldingFire; }
63
64 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
65 virtual bool UsesAmmo() const;
66 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
67 virtual float GetMagAmmo() const;
68 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
69 virtual float GetReserveAmmo() const;
70 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
71 virtual bool IsSwitchingWeapons() const;
72
73 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
74 TArray<UWeaponInstance*> GetWeaponInventory() const;
75 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
76 FORCEINLINE UWeaponInstance* GetWeaponFromIndex(int32 Index) const { return WeaponInventory.IsValidIndex(Index) ? WeaponInventory[Index].Get() : NULL; }
77 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
78 UWeaponInstance* GetWeaponFromClass(TSubclassOf<UWeaponInstance> WeaponClass) const;
79 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
80 FORCEINLINE UWeaponInstance* GetWeaponFromSlot(EEquipSlot Slot) const { return WeaponInventory.IsValidIndex((uint8)Slot) ? WeaponInventory[(uint8)Slot].Get() : NULL; }
81 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
82 EEquipSlot GetSlotFromWeapon(const UWeaponInstance* const Weapon) const;
83
84 UFUNCTION(BlueprintPure, Category = WeaponComponent)
85 UCameraComponent* GetFirstPersonCamera() const { return FPCamera; }
86 void UpdateFirstPersonCamera(UCameraComponent* CameraComponent);
87
88 template <class T>
89 T* GetWeaponFromClass() const { return Cast<T>(GetWeaponFromClass(T::StaticClass())); }
90
91 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
92 int32 GetWeaponIndex(UWeaponInstance* Weapon) const;
93 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
94 FORCEINLINE UWeaponInstance* GetCurrentWeapon() const { return EquippedWeapon.IsValid() ? EquippedWeapon.Get() : NULL; }
95
96 UFUNCTION(BlueprintPure, Category = WeaponComponent)
97 virtual float GetWeaponSpread() const;
98 UFUNCTION(BlueprintPure, Category = WeaponComponent)
99 virtual float GetReticleSpreadMultiplier() const;
100
101 virtual bool CanPlayAnimations1P() const;
102 virtual bool CanPlayAnimations3P() const;
103
104 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
105 FORCEINLINE USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; }
106 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
107 FORCEINLINE USkeletalMeshComponent* GetMesh3P() const { return Mesh3P; }
108
109 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
110 FORCEINLINE USkeletalMeshComponent* GetWeaponMesh1P() const { return WeaponMesh1P; }
111 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
112 FORCEINLINE USkeletalMeshComponent* GetWeaponMesh3P() const { return WeaponMesh3P; }
113
114 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
115 FORCEINLINE bool IsCurrentWeapon(UWeaponInstance* Weapon) const { return GetCurrentWeapon() == Weapon; }
116
117 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
118 virtual bool IsCurrentWeaponByClass(TSubclassOf<UWeaponInstance> Weapon) const;
119 FORCEINLINE bool GetCanFire() { return bCanFire; }
120
121 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
122 virtual float GetAmmoPickupMultiplier() const { return AmmoPickupMultiplier;}
123 virtual void UpdateAmmoPickUpMultiplier(float NewPickupMultiplier) { AmmoPickupMultiplier = NewPickupMultiplier; }
124
125 //This allows weapons to call the OnWeaponStartFire event locally to drive cosmetic changes on the weapon ABP
126 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
127 virtual void CosmeticPrimaryFire() const { OnWeaponStartFire.Broadcast(0); }
128
129 class AController* GetController() const;
130 class ACharacter* GetPlayerCharacter() const;
131
132 virtual void ChargedFire(uint8 Index);
133 /* Run a check for fire buttons being held and if applicable, ask for a fire */
134 virtual void FireIfAvailable();
135
136 void UpdateCanFire(bool bNewCanFire);
137
138public:
139 virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent);
140
141 virtual void BeginPlay() override;
142 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
143 UFUNCTION(BlueprintCallable)
144 virtual UWeaponInstance* AddWeapon(TSubclassOf<UWeaponInstance> WeaponClass,EEquipSlot Slot, bool bIsPartOfBatch = false);
145 virtual void RemoveWeapon(TSubclassOf<UWeaponInstance> WeaponClass);
146 virtual void RemoveWeapon(UWeaponInstance* Weapon);
147 virtual void ForceWeaponInventoryCheck();
148 virtual void IncrementBlockSemaphore();
149 virtual void DecrementBlockSemaphore();
150
151 template<uint8 Index>
152 void EquipWeapon();
153
154 void EquipMelee();
155 void EquipVehicle();
156
157 virtual void RemoveAllWeapons();
158 UFUNCTION()
159 void WeaponAutoFire();
160 UFUNCTION()
161 virtual void StartVehicleAbility();
162 UFUNCTION()
163 virtual void StopVehicleAbility();
164 UFUNCTION()
165 virtual void UseVehicleAbility();
166
167 //do not abuse this function. this should only be called by weapon instances attempted to manually perform weapon switching (eg attempting perform a queued weapon switch on their own).
168 void SetPendingWeapon(UWeaponInstance* Weapon);
169 UFUNCTION(Server, Reliable, WithValidation)
170 virtual void Server_Reliable_SetPendingWeapon(UWeaponInstance* Weapon);
171 UFUNCTION(BlueprintCallable)
172 virtual void EquipWeapon(UWeaponInstance* Weapon, bool bCanUnequipExclusive = false, bool bForce = false);
173
174 UFUNCTION()
175 virtual void EquipPreviousWeapon(bool bCanUnequipExclusive = false);
176 UFUNCTION()
177 virtual void UnEquipComplete();
178 UFUNCTION()
179 virtual void OwnerDied();
180 UFUNCTION()
181 virtual void OnRep_WeaponInventory();
182 UFUNCTION()
183 virtual void OnFOVSettingChanged(const float Delta);
184 virtual void ToggleWeaponActive(bool bNewActiveState, UWeaponInstance* WeaponClass);
185
186 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
187 virtual void CancelWeaponActions();
188 UFUNCTION(BlueprintCallable, Category = WeaponComponent)
189 virtual void CancelInputConstrainedWeaponActions();
190
191 UFUNCTION(BlueprintCallable, Category = "Damage")
192 FORCEINLINE float GetDamageModifier() const { return DamageModifier; }
193
194 void UpdateDamageModifier(float NewModifier);
195 void UpdateWeaponSpreadMultiplier(float NewMultiplier);
196
197public:
198 //We will keep these bindable events here so that listeners do not need to bind to every weapon.
199 UPROPERTY(BlueprintAssignable, Category = "Weapon Events")
200 FWeaponAddedSignature OnWeaponAdded;
201 UPROPERTY(BlueprintAssignable, Category = "Weapon Events")
202 FWeaponRemovedSignature OnWeaponRemoved;
203 UPROPERTY(BlueprintAssignable, Category = "Weapon Events")
204 FWeaponEquippedSignature OnWeaponEquipped;
205 UPROPERTY(BlueprintAssignable, Category = "Weapon Events")
206 FWeaponSkinsChangedSignature OnWeaponSkinsChanged;
207 UPROPERTY(BlueprintAssignable, Category = "Weapon Events")
208 FWeaponErrorSignature OnWeaponError;
209 UPROPERTY(BlueprintAssignable, Category = "Weapon Events")
210 FWeaponAnimationSignature OnTightAimStart;
211 UPROPERTY(BlueprintAssignable, Category = "Weapon Events")
212 FWeaponAnimationSignature OnTightAimStop;
213 UPROPERTY(BlueprintAssignable, Category = "Weapon Events")
214 FWeaponAmmoChangeSignature OnAmmoChanged;
215 UPROPERTY(BlueprintAssignable, Category = "Weapon Events")
216 FWeaponAmmoChangeSignature OnAmmoReserveChanged;
217 UPROPERTY(BlueprintAssignable, Category = "Weapon Events")
218 FReloadEventSignature OnReloadStart;
219 UPROPERTY(BlueprintAssignable, Category = "Weapon Events")
220 FReloadEventSignature OnReloadStop;
221 UPROPERTY(BlueprintAssignable, Category = "Weapon Events")
222 FWeaponAnimationSignature OnFiringStart;
223 UPROPERTY(BlueprintAssignable, Category = "Weapon Events")
224 FWeaponAnimationSignature OnFiringStop;
225
226 UPROPERTY(BlueprintAssignable)
227 FDeployableSpawnedSignature OnDeployableSpawned;
228 UPROPERTY(BlueprintAssignable)
229 FDeployableDestroyedSignature OnDeployableDestroyed;
230
231 UPROPERTY(BlueprintAssignable)
232 FLockedOnSignature OnLockOnStart;
233 UPROPERTY(BlueprintAssignable)
234 FLockedOnSignature OnLockedOn;
235 UPROPERTY(BlueprintAssignable)
236 FLockOnSignature OnLockOnLost;
237 UPROPERTY(BlueprintAssignable)
238 FLockOnSignature OnLockOnStopped;
239 UPROPERTY(BlueprintAssignable)
240 FLockOnStrengthChangedSignature OnLockOnStrengthChanged;
241
242 UPROPERTY(BlueprintAssignable)
243 FWeaponFireSignature OnWeaponStartFire;
244 UPROPERTY(BlueprintAssignable)
245 FWeaponFireSignature OnWeaponAutoFire;
246 UPROPERTY(BlueprintAssignable)
247 FWeaponFireSignature OnWeaponStopFire;
248 UPROPERTY(BlueprintAssignable)
249 FWeaponFireSignature OnWeaponChargedFire;
250 UPROPERTY(BlueprintAssignable)
251 FWeaponFireSignature OnWeaponShotFired;
252 UPROPERTY(BlueprintAssignable)
253 FWeaponReloadSignature OnWeaponReload;
254 UPROPERTY(BlueprintAssignable)
255 FPersonalVehicleEventSignature OnPersonalVehicleInputEvent;
256
257 /* How long you would have to hold to trigger the vehicle*/
258 UPROPERTY(EditDefaultsOnly,Category = Vehicle, Config)
259 float PersonalVehicleHoldTriggerTimeDefault = 0.2;
260 /* A curve that can be used to dynamically evaluate a forward offset for our arms mesh based on the current FOV */
261 UPROPERTY(EditDefaultsOnly, Category = Jetpack = "Character|Camera")
262 class UCurveFloat* FOVOffsetControlCurve = NULL;
263 float FoundWeaponAmmoBonus = 0.0f;
264
265 UFUNCTION(Server, Reliable, WithValidation)
266 virtual void Server_Reliable_EquipWeapon(UWeaponInstance* Weapon, bool bCanUnequipExclusive = false);
267
268 UFUNCTION()
269 virtual void QuickWeaponSwitch();
270 UFUNCTION()
271 virtual void QuickWeaponSwitchPrevious();
272 UFUNCTION()
273 virtual void CycleWeapon(bool bReverse = false);
274 UFUNCTION()
275 virtual void OnRep_EquippedWeapon();
276
277 template<int32 Index>
278 void Fire() { Fire(Index); }
279 void Fire(int32 Index);
280
281 template<int32 Index>
282 void FireReleased() { FireReleased(Index); }
283 void FireReleased(int32 Index);
284
285 void Reload();
286
287 UPROPERTY(ReplicatedUsing = OnRep_EquippedWeapon)
288 TWeakObjectPtr<UWeaponInstance> EquippedWeapon = NULL;
289 UPROPERTY()
290 TWeakObjectPtr<UWeaponInstance> PendingWeapon = NULL;
291 UPROPERTY()
292 TWeakObjectPtr<UWeaponInstance> PreviousWeapon = NULL;
293
294 UPROPERTY(ReplicatedUsing = OnRep_WeaponInventory)
295 TArray<TWeakObjectPtr<UWeaponInstance>> WeaponInventory;
296
297 TArray<TWeakObjectPtr<UWeaponInstance>> PreviousInventory;
298
299 UPROPERTY()
300 USkeletalMeshComponent* Mesh1P;
301 UPROPERTY()
302 USkeletalMeshComponent* Mesh3P;
303 UPROPERTY()
304 USkeletalMeshComponent* WeaponMesh1P;
305 UPROPERTY()
306 USkeletalMeshComponent* WeaponMesh3P;
307
308
309 UPROPERTY()
310 class UCameraComponent* FPCamera;
311
312 UPROPERTY()
313 float DamageModifier = 1.0f;
314 UPROPERTY()
315 float AmmoPickupMultiplier = 1.0f;
316 UPROPERTY()
317 float WeaponSpreadMultiplier = 1.0f;
318
319private:
320 mutable AController* Controller;
321 mutable ACharacter* PlayerCharacter;
322
323 FTimerHandle PVHoldTimer;
324
325 UPROPERTY(Replicated)
326 bool bCanFire = true;
327
328 bool bHoldingAltFire = false;
329
330 bool bHoldingFire = false;
331 /* In the case that we mis predict this value, the client might get locked out of doing input-based actions. Replicating it makes this safer to have as we don't have a lot of supporting systems when we mis predict some stuff to modify this locally.
332 This value is currently only being manipulated on Equips/Unequips, and getting this out of sync won't probably happen, but it's 1 byte to be on the safe side and future-proof it */
333 UPROPERTY(Replicated)
334 uint8 InputBlockSemaphore = 0;
335};
EEquipSlot
Definition: PlayerStructs.h:25
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FPersonalVehicleEventSignature, bool, bHold, float, HoldDuration)
EWeaponError
Definition: WeaponComponent.h:15
@ CantFireWhileSwitchingWeapons
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FWeaponErrorSignature, EWeaponError, Error)
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FWeaponRemovedSignature)
Definition: Deployable.h:47
Definition: WeaponInstance.h:220
friend class UWeaponComponent
Definition: WeaponInstance.h:1197
Definition: WeaponInstanceProjectile.h:36