1
+ // Copyright (c) Yevhenii Selivanov
2
+
3
+ #include " UI/ViewModel/MVVM_MyHUD.h"
4
+ // ---
5
+ #include " GameFramework/MyGameStateBase.h"
6
+ #include " LevelActors/PlayerCharacter.h"
7
+ #include " Subsystems/GlobalEventsSubsystem.h"
8
+ #include " UtilityLibraries/MyBlueprintFunctionLibrary.h"
9
+ // ---
10
+ #include UE_INLINE_GENERATED_CPP_BY_NAME(MVVM_MyHUD)
11
+
12
+ /* ********************************************************************************************
13
+ * Countdown timers
14
+ ********************************************************************************************* */
15
+
16
+ // Setter about the summary seconds of launching 'Three-two-one-GO' timer that is used on game starting
17
+ void UMVVM_MyHUD::SetStartingTimerSecRemain (const FText& NewStartingTimerSecRemain)
18
+ {
19
+ UE_MVVM_SET_PROPERTY_VALUE (StartingTimerSecRemain, NewStartingTimerSecRemain);
20
+ }
21
+
22
+ // Setter about the seconds to the end of the round
23
+ void UMVVM_MyHUD::SetInGameTimerSecRemain (const FText& NewInGameTimerSecRemain)
24
+ {
25
+ UE_MVVM_SET_PROPERTY_VALUE (InGameTimerSecRemain, NewInGameTimerSecRemain);
26
+ }
27
+
28
+ // Called when the 'Three-two-one-GO' timer was updated
29
+ void UMVVM_MyHUD::OnStartingTimerSecRemainChanged_Implementation (float NewStartingTimerSecRemain)
30
+ {
31
+ const int32 Value = FMath::CeilToInt (NewStartingTimerSecRemain);
32
+ SetStartingTimerSecRemain (FText::AsNumber (Value));
33
+ }
34
+
35
+ // Called when remain seconds to the end of the match timer was updated
36
+ void UMVVM_MyHUD::OnInGameTimerSecRemainChanged_Implementation (float NewInGameTimerSecRemain)
37
+ {
38
+ const int32 Value = FMath::CeilToInt (NewInGameTimerSecRemain);
39
+ SetInGameTimerSecRemain (FText::AsNumber (Value));
40
+ }
41
+
42
+ /* ********************************************************************************************
43
+ * PowerUps
44
+ ********************************************************************************************* */
45
+
46
+ // Setter about current amount of speed power-ups
47
+ void UMVVM_MyHUD::SetPowerUpSkate (const FText& NewPowerUpSkate)
48
+ {
49
+ UE_MVVM_SET_PROPERTY_VALUE (PowerUpSkate, NewPowerUpSkate);
50
+ }
51
+
52
+ // Setter about current amount of max bomb power-ups
53
+ void UMVVM_MyHUD::SetPowerUpBomb (const FText& NewPowerUpBomb)
54
+ {
55
+ UE_MVVM_SET_PROPERTY_VALUE (PowerUpBomb, NewPowerUpBomb);
56
+ }
57
+
58
+ // Setter about current amount of blast radius power-ups
59
+ void UMVVM_MyHUD::SetPowerUpFire (const FText& NewPowerUpFire)
60
+ {
61
+ UE_MVVM_SET_PROPERTY_VALUE (PowerUpFire, NewPowerUpFire);
62
+ }
63
+
64
+ // Called when power-ups were updated
65
+ void UMVVM_MyHUD::OnPowerUpsChanged_Implementation (const FPowerUp& NewPowerUps)
66
+ {
67
+ SetPowerUpSkate (FText::AsNumber (NewPowerUps.SkateN ));
68
+ SetPowerUpBomb (FText::AsNumber (NewPowerUps.BombN ));
69
+ SetPowerUpFire (FText::AsNumber (NewPowerUps.FireN ));
70
+ }
71
+
72
+ /* ********************************************************************************************
73
+ * Events
74
+ ********************************************************************************************* */
75
+
76
+ // Is called when the view is constructed
77
+ void UMVVM_MyHUD::OnViewModelConstruct_Implementation (const UUserWidget* UserWidget)
78
+ {
79
+ Super::OnViewModelConstruct_Implementation (UserWidget);
80
+
81
+ BIND_AND_CALL_ON_LOCAL_PLAYER_READY (this , ThisClass::OnLocalPlayerReady);
82
+ }
83
+
84
+ // Is called when this View Model is destructed
85
+ void UMVVM_MyHUD::OnViewModelDestruct_Implementation ()
86
+ {
87
+ Super::OnViewModelDestruct_Implementation ();
88
+
89
+ if (AMyGameStateBase* MyGameState = UMyBlueprintFunctionLibrary::GetMyGameState ())
90
+ {
91
+ MyGameState->OnStartingTimerSecRemainChanged .RemoveAll (this );
92
+ MyGameState->OnInGameTimerSecRemainChanged .RemoveAll (this );
93
+ }
94
+
95
+ if (APlayerCharacter* Player = UMyBlueprintFunctionLibrary::GetLocalPlayerCharacter ())
96
+ {
97
+ Player->OnPowerUpsChanged .RemoveAll (this );
98
+ }
99
+ }
100
+
101
+ // Called when local player character was spawned and possessed, so we can bind to data
102
+ void UMVVM_MyHUD::OnLocalPlayerReady (APlayerCharacter* PlayerCharacter)
103
+ {
104
+ checkf (PlayerCharacter, TEXT (" ERROR: [%i] %s:\n 'PlayerCharacter' is null!" ), __LINE__, *FString (__FUNCTION__));
105
+ PlayerCharacter->OnPowerUpsChanged .AddUniqueDynamic (this , &ThisClass::OnPowerUpsChanged);
106
+
107
+ AMyGameStateBase& MyGameState = AMyGameStateBase::Get ();
108
+ MyGameState.OnStartingTimerSecRemainChanged .AddUniqueDynamic (this , &ThisClass::OnStartingTimerSecRemainChanged);
109
+ MyGameState.OnInGameTimerSecRemainChanged .AddUniqueDynamic (this , &ThisClass::OnInGameTimerSecRemainChanged);
110
+ }
0 commit comments