LEAP Documentation 40220
Documentation for the LEAP project
GlobalAmmoComponent.h
Go to the documentation of this file.
1// Copyright Blue Isle Studios Inc 2018. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Components/ActorComponent.h"
7#include "GameFramework/Actor.h"
8#include "GlobalAmmoComponent.generated.h"
9
10class APlayerState;
11class UWeaponInstance;
12
13//Globally held ammo for a weapon for persistance across pawns.
14USTRUCT()
16{
17 GENERATED_USTRUCT_BODY()
18
19 FGlobalWeaponAmmo(TSubclassOf<UWeaponInstance> InWeapon, float InAmmo, float Modifier = 1.0f)
20 {
21 Weapon = InWeapon;
22 Ammo = InAmmo;
23 RestockModifier = Modifier;
24 }
25
27 {
28 Weapon = NULL;
29 Ammo = 0.f;
30 }
31
32public:
33 UPROPERTY()
34 TSubclassOf<UWeaponInstance> Weapon = NULL;
35
36 UPROPERTY()
37 float Ammo = 0.f;
38 UPROPERTY()
39 float RestockTimerStart = -1.f;
40 UPROPERTY()
41 float RestockModifier = 0.0f;
42
43public:
44 bool IsEqualTo(const FGlobalWeaponAmmo& Other) const;
45 bool HasEnoughAmmo(float Amount) const;
46 bool ConsumeAmmo(float Amount, class UGlobalAmmoComponent* OwningComponent);
47
48 void UpdateProgressAmmoRestock(class UGlobalAmmoComponent* OwningComponent);
49
50 //Used by clients for when the server updates the onreps and the client thinks it's still restocking.
51 void ClientCheckRestockTimer(class UGlobalAmmoComponent* OwningComponent);
52
53 void StartRestockTimer(class UGlobalAmmoComponent* OwningComponent);
54
55 bool operator==(const FGlobalWeaponAmmo& Other) const
56 {
57 return this->IsEqualTo(Other);
58 }
59
60 bool operator!=(const FGlobalWeaponAmmo& Other) const
61 {
62 return !(*this == Other);
63 }
64
65public:
67};
68
69DECLARE_DYNAMIC_MULTICAST_DELEGATE(FGlobalAmmoChangedSignature);
70DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FGlobalAmmoRestockTimerChangedSignature, TSubclassOf<UWeaponInstance>, Weapon, FTimerHandle, Timer);
71DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FGlobalAmmoRestockCompleteSignature, TSubclassOf<UWeaponInstance>, Weapon, bool, bLastRestock);
72
73UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
74class PROJECTX_API UGlobalAmmoComponent : public UActorComponent
75{
76 GENERATED_BODY()
77
78 UGlobalAmmoComponent();
79
80//~ Begin UActorComponent Interface
81public:
82 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
83//~ End UActorComponent Interface
84
85public:
86 UFUNCTION()
87 virtual FGlobalWeaponAmmo& GetGlobalAmmoEntry(const UWeaponInstance* const Weapon);
88
89 UFUNCTION(BlueprintCallable, Category = PlayerState)
90 virtual float GetGlobalWeaponAmmo(TSubclassOf<UWeaponInstance> Weapon) const;
91
92 UFUNCTION(BlueprintCallable, Category = PlayerState)
93 virtual float GetTimeUntilNextRestock(TSubclassOf<UWeaponInstance> Weapon) const;
94
95 UFUNCTION()
96 virtual void GlobalWeaponAmmoRestock(TSubclassOf<UWeaponInstance> Weapon);
97 UFUNCTION()
98 virtual void GlobalWeaponAmmoReset(TSubclassOf<UWeaponInstance> Weapon);
99
100 TMap<TSubclassOf<UWeaponInstance>, FTimerHandle>& GetRestockTimerMap() { return GlobalAmmoTimerMap; }
101 TMap<TSubclassOf<UWeaponInstance>, int32>& GetRestockInterpolatorMap() { return GlobalAmmoInterpolatorMap; }
102
103 UFUNCTION()
104 void ForceNetUpdate() { if (GetOwner()) { GetOwner()->ForceNetUpdate(); } }
105
106 //Meant to run through APlayerState::OverrideWith and APlayerState::CopyProperties.
107 virtual void OverrideWith(UGlobalAmmoComponent* GlobalAmmoComponent);
108 virtual void CopyProperties(UGlobalAmmoComponent* GlobalAmmoComponent);
109
110public:
111 UPROPERTY(BlueprintAssignable, Category = PlayerState)
112 FGlobalAmmoChangedSignature OnGlobalAmmoChanged;
113 UPROPERTY(BlueprintAssignable, Category = PlayerState)
114 FGlobalAmmoRestockTimerChangedSignature OnGlobalAmmoRestockTimerChanged;
115 UPROPERTY(BlueprintAssignable, Category = PlayerState)
116 FGlobalAmmoRestockCompleteSignature OnGlobalAmmoRestockComplete;
117
118 void MarkGlobalAmmoDirty();
119
120protected:
121 friend struct FGlobalWeaponAmmo;
122
123 /* List of weapons that have globally synchronized ammo */
124 UPROPERTY(Replicated)
125 TArray<FGlobalWeaponAmmo> GlobalAmmoList;
126 UPROPERTY(ReplicatedUsing = OnRep_GlobalAmmoList)
127 uint8 Counter = 0; // For some reason the array is not triggering the onreps, so we're using this instead
128 TArray<FGlobalWeaponAmmo> PreviousGlobalAmmoList;
129
130 TMap<TSubclassOf<UWeaponInstance>, FTimerHandle> GlobalAmmoTimerMap; //Global ammo timer handles kept separate to make it easier to manage prediction.
131 TMap<TSubclassOf<UWeaponInstance>, int32> GlobalAmmoInterpolatorMap; //Global ammo timer handles kept separate to make it easier to manage prediction.
132
133 UFUNCTION()
134 virtual void OnRep_GlobalAmmoList();
135};
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FGlobalAmmoRestockTimerChangedSignature, TSubclassOf< UWeaponInstance >, Weapon, FTimerHandle, Timer)
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FGlobalAmmoChangedSignature)
Definition: WeaponInstance.h:220
virtual bool ConsumeAmmo(float Amount, uint8 Mode=0)
Definition: WeaponInstance.cpp:2073
virtual bool HasEnoughAmmo(float Amount, uint8 Mode=0) const
Definition: WeaponInstance.cpp:2052
UWeaponComponent * OwningComponent
Definition: WeaponInstance.h:1182
Definition: GlobalAmmoComponent.h:16
FGlobalWeaponAmmo()
Definition: GlobalAmmoComponent.h:26
bool operator!=(const FGlobalWeaponAmmo &Other) const
Definition: GlobalAmmoComponent.h:60
static FGlobalWeaponAmmo InvalidEntry
Definition: GlobalAmmoComponent.h:66