@@ -72,6 +72,16 @@ private void OnRejuvenate(EntityUid uid, HungerComponent component, RejuvenateEv
72
72
SetHunger ( uid , component . Thresholds [ HungerThreshold . Okay ] , component ) ;
73
73
}
74
74
75
+ /// <summary>
76
+ /// Gets the current hunger value of the given <see cref="HungerComponent"/>.
77
+ /// </summary>
78
+ public float GetHunger ( HungerComponent component )
79
+ {
80
+ var dt = _timing . CurTime - component . LastAuthoritativeHungerChangeTime ;
81
+ var value = component . LastAuthoritativeHungerValue - ( float ) dt . TotalSeconds * component . ActualDecayRate ;
82
+ return ClampHungerWithinThresholds ( component , value ) ;
83
+ }
84
+
75
85
/// <summary>
76
86
/// Adds to the current hunger of an entity by the specified value
77
87
/// </summary>
@@ -82,7 +92,7 @@ public void ModifyHunger(EntityUid uid, float amount, HungerComponent? component
82
92
{
83
93
if ( ! Resolve ( uid , ref component ) )
84
94
return ;
85
- SetHunger ( uid , component . CurrentHunger + amount , component ) ;
95
+ SetHunger ( uid , GetHunger ( component ) + amount , component ) ;
86
96
}
87
97
88
98
/// <summary>
@@ -95,11 +105,23 @@ public void SetHunger(EntityUid uid, float amount, HungerComponent? component =
95
105
{
96
106
if ( ! Resolve ( uid , ref component ) )
97
107
return ;
98
- component . CurrentHunger = Math . Clamp ( amount ,
99
- component . Thresholds [ HungerThreshold . Dead ] ,
100
- component . Thresholds [ HungerThreshold . Overfed ] ) ;
108
+
109
+ SetAuthoritativeHungerValue ( ( uid , component ) , amount ) ;
101
110
UpdateCurrentThreshold ( uid , component ) ;
102
- Dirty ( uid , component ) ;
111
+ }
112
+
113
+ /// <summary>
114
+ /// Sets <see cref="HungerComponent.LastAuthoritativeHungerValue"/> and
115
+ /// <see cref="HungerComponent.LastAuthoritativeHungerChangeTime"/>, and dirties this entity. This "resets" the
116
+ /// starting point for <see cref="GetHunger"/>'s calculation.
117
+ /// </summary>
118
+ /// <param name="entity">The entity whose hunger will be set.</param>
119
+ /// <param name="value">The value to set the entity's hunger to.</param>
120
+ private void SetAuthoritativeHungerValue ( Entity < HungerComponent > entity , float value )
121
+ {
122
+ entity . Comp . LastAuthoritativeHungerChangeTime = _timing . CurTime ;
123
+ entity . Comp . LastAuthoritativeHungerValue = ClampHungerWithinThresholds ( entity . Comp , value ) ;
124
+ DirtyField ( entity . Owner , entity . Comp , nameof ( HungerComponent . LastAuthoritativeHungerChangeTime ) ) ;
103
125
}
104
126
105
127
private void UpdateCurrentThreshold ( EntityUid uid , HungerComponent ? component = null )
@@ -167,7 +189,7 @@ component.StarvationDamage is { } damage &&
167
189
/// <returns></returns>
168
190
public HungerThreshold GetHungerThreshold ( HungerComponent component , float ? food = null )
169
191
{
170
- food ??= component . CurrentHunger ;
192
+ food ??= GetHunger ( component ) ;
171
193
var result = HungerThreshold . Dead ;
172
194
var value = component . Thresholds [ HungerThreshold . Overfed ] ;
173
195
foreach ( var threshold in component . Thresholds )
@@ -229,16 +251,23 @@ public bool TryGetStatusIconPrototype(HungerComponent component, [NotNullWhen(tr
229
251
return prototype != null ;
230
252
}
231
253
254
+ private static float ClampHungerWithinThresholds ( HungerComponent component , float hungerValue )
255
+ {
256
+ return Math . Clamp ( hungerValue ,
257
+ component . Thresholds [ HungerThreshold . Dead ] ,
258
+ component . Thresholds [ HungerThreshold . Overfed ] ) ;
259
+ }
260
+
232
261
public override void Update ( float frameTime )
233
262
{
234
263
base . Update ( frameTime ) ;
235
264
236
265
var query = EntityQueryEnumerator < HungerComponent > ( ) ;
237
266
while ( query . MoveNext ( out var uid , out var hunger ) )
238
267
{
239
- if ( _timing . CurTime < hunger . NextUpdateTime )
268
+ if ( _timing . CurTime < hunger . NextThresholdUpdateTime )
240
269
continue ;
241
- hunger . NextUpdateTime = _timing . CurTime + hunger . UpdateRate ;
270
+ hunger . NextThresholdUpdateTime = _timing . CurTime + hunger . NextThresholdUpdateTime ;
242
271
243
272
ModifyHunger ( uid , - hunger . ActualDecayRate , hunger ) ;
244
273
DoContinuousHungerEffects ( uid , hunger ) ;
0 commit comments