@@ -12,58 +12,76 @@ namespace Intersect.Client.Entities;
1212
1313public partial class Resource : Entity , IResource
1414{
15- private bool _waitingForTilesets ;
16-
17- public ResourceBase ? BaseResource { get ; set ; }
15+ private FloatRect _renderBoundsDest = FloatRect . Empty ;
16+ private FloatRect _renderBoundsSrc = FloatRect . Empty ;
1817
19- bool IResource . IsDepleted => IsDead ;
18+ private bool _recalculateRenderBounds ;
19+ private bool _waitingForTilesets ;
2020
21- public bool IsDead { get ; set ; }
21+ private bool _isDead ;
22+ private ResourceBase ? _descriptor ;
2223
23- FloatRect mDestRectangle = FloatRect . Empty ;
24+ public Resource ( Guid id , ResourceEntityPacket packet ) : base ( id , packet , EntityType . Resource )
25+ {
26+ mRenderPriority = 0 ;
27+ }
2428
25- private bool mHasRenderBounds ;
29+ public ResourceBase ? Descriptor
30+ {
31+ get => _descriptor ;
32+ set => _descriptor = value ;
33+ }
2634
27- FloatRect mSrcRectangle = FloatRect . Empty ;
35+ public bool IsDepleted => IsDead ;
2836
29- public Resource ( Guid id , ResourceEntityPacket packet ) : base ( id , packet , EntityType . Resource )
37+ public bool IsDead
3038 {
31- mRenderPriority = 0 ;
39+ get => _isDead ;
40+ set
41+ {
42+ if ( value == _isDead )
43+ {
44+ return ;
45+ }
46+
47+ _isDead = value ;
48+ _recalculateRenderBounds = true ;
49+ }
3250 }
3351
3452 public override string Sprite
3553 {
36- get => mMySprite ;
54+ get => _sprite ;
3755 set
3856 {
39- if ( value == mMySprite )
57+ if ( value == _sprite )
4058 {
4159 return ;
4260 }
4361
44- if ( BaseResource == null )
62+ if ( Descriptor == null )
4563 {
4664 return ;
4765 }
4866
49- mMySprite = value ;
67+ _sprite = value ;
5068 ReloadSpriteTexture ( ) ;
5169 }
5270 }
5371
5472 private void ReloadSpriteTexture ( )
5573 {
56- if ( BaseResource == null )
74+ if ( Descriptor == null )
5775 {
5876 return ;
5977 }
6078
61- if ( IsDead && BaseResource . Exhausted . GraphicFromTileset ||
62- ! IsDead && BaseResource . Initial . GraphicFromTileset )
79+ if ( IsDead && Descriptor . Exhausted . GraphicFromTileset ||
80+ ! IsDead && Descriptor . Initial . GraphicFromTileset )
6381 {
6482 if ( GameContentManager . Current . TilesetsLoaded )
6583 {
66- Texture = Globals . ContentManager . GetTexture ( Framework . Content . TextureType . Tileset , mMySprite ) ;
84+ Texture = Globals . ContentManager . GetTexture ( Framework . Content . TextureType . Tileset , _sprite ) ;
6785 }
6886 else
6987 {
@@ -72,40 +90,43 @@ private void ReloadSpriteTexture()
7290 }
7391 else
7492 {
75- Texture = Globals . ContentManager . GetTexture ( Framework . Content . TextureType . Resource , mMySprite ) ;
93+ Texture = Globals . ContentManager . GetTexture ( Framework . Content . TextureType . Resource , _sprite ) ;
7694 }
7795
78- mHasRenderBounds = false ;
96+ _recalculateRenderBounds = true ;
7997 }
8098
8199 public override void Load ( EntityPacket ? packet )
82100 {
83101 base . Load ( packet ) ;
84- var pkt = packet as ResourceEntityPacket ;
85102
86- if ( pkt == default )
103+ _recalculateRenderBounds = true ;
104+
105+ if ( packet is not ResourceEntityPacket resourceEntityPacket )
87106 {
88107 return ;
89108 }
90109
91- IsDead = pkt . IsDead ;
92- var baseId = pkt . ResourceId ;
93- BaseResource = ResourceBase . Get ( baseId ) ;
110+ IsDead = resourceEntityPacket . IsDead ;
94111
95- if ( BaseResource == default )
112+ if ( ! ResourceBase . TryGet ( resourceEntityPacket . ResourceId , out _descriptor ) )
96113 {
97114 return ;
98115 }
99116
100- HideName = true ;
101- if ( IsDead )
102- {
103- Sprite = BaseResource . Exhausted . Graphic ;
104- }
105- else
117+ UpdateFromDescriptor ( _descriptor ) ;
118+ }
119+
120+ private void UpdateFromDescriptor ( ResourceBase ? descriptor )
121+ {
122+ if ( descriptor == null )
106123 {
107- Sprite = BaseResource . Initial . Graphic ;
124+ return ;
108125 }
126+
127+ var updatedSprite = IsDead ? descriptor . Exhausted . Graphic : descriptor . Initial . Graphic ;
128+ _sprite = updatedSprite ;
129+ ReloadSpriteTexture ( ) ;
109130 }
110131
111132 public override void Dispose ( )
@@ -130,6 +151,12 @@ public override bool Update()
130151 return false ;
131152 }
132153
154+ if ( Descriptor is { IsDeleted : true } deletedDescriptor )
155+ {
156+ _ = ResourceBase . TryGet ( deletedDescriptor . Id , out _descriptor ) ;
157+ UpdateFromDescriptor ( _descriptor ) ;
158+ }
159+
133160 if ( ! Maps . MapInstance . TryGet ( MapId , out var map ) || ! map . InView ( ) )
134161 {
135162 LatestMap = map ;
@@ -138,12 +165,12 @@ public override bool Update()
138165 return false ;
139166 }
140167
141- if ( ! mHasRenderBounds )
168+ if ( _recalculateRenderBounds )
142169 {
143170 CalculateRenderBounds ( ) ;
144171 }
145172
146- if ( ! Graphics . WorldViewport . IntersectsWith ( mDestRectangle ) )
173+ if ( ! Graphics . WorldViewport . IntersectsWith ( _renderBoundsDest ) )
147174 {
148175 if ( RenderList != null )
149176 {
@@ -170,9 +197,9 @@ public override bool Update()
170197
171198 public override HashSet < Entity > ? DetermineRenderOrder ( HashSet < Entity > ? renderList , IMapInstance ? map )
172199 {
173- if ( BaseResource == default ||
174- ( IsDead && ! BaseResource . Exhausted . RenderBelowEntities ) ||
175- ( ! IsDead && ! BaseResource . Initial . RenderBelowEntities )
200+ if ( Descriptor == default ||
201+ ( IsDead && ! Descriptor . Exhausted . RenderBelowEntities ) ||
202+ ( ! IsDead && ! Descriptor . Initial . RenderBelowEntities )
176203 )
177204 {
178205 return base . DetermineRenderOrder ( renderList , map ) ;
@@ -249,7 +276,7 @@ public override bool Update()
249276
250277 private void CalculateRenderBounds ( )
251278 {
252- if ( BaseResource == default )
279+ if ( Descriptor == default )
253280 {
254281 return ;
255282 }
@@ -278,43 +305,43 @@ private void CalculateRenderBounds()
278305 return ;
279306 }
280307
281- mSrcRectangle . X = 0 ;
282- mSrcRectangle . Y = 0 ;
283- if ( IsDead && BaseResource . Exhausted . GraphicFromTileset )
308+ _renderBoundsSrc . X = 0 ;
309+ _renderBoundsSrc . Y = 0 ;
310+ if ( IsDead && Descriptor . Exhausted . GraphicFromTileset )
284311 {
285- mSrcRectangle . X = BaseResource . Exhausted . X * Options . TileWidth ;
286- mSrcRectangle . Y = BaseResource . Exhausted . Y * Options . TileHeight ;
287- mSrcRectangle . Width = ( BaseResource . Exhausted . Width + 1 ) * Options . TileWidth ;
288- mSrcRectangle . Height = ( BaseResource . Exhausted . Height + 1 ) * Options . TileHeight ;
312+ _renderBoundsSrc . X = Descriptor . Exhausted . X * Options . TileWidth ;
313+ _renderBoundsSrc . Y = Descriptor . Exhausted . Y * Options . TileHeight ;
314+ _renderBoundsSrc . Width = ( Descriptor . Exhausted . Width + 1 ) * Options . TileWidth ;
315+ _renderBoundsSrc . Height = ( Descriptor . Exhausted . Height + 1 ) * Options . TileHeight ;
289316 }
290- else if ( ! IsDead && BaseResource . Initial . GraphicFromTileset )
317+ else if ( ! IsDead && Descriptor . Initial . GraphicFromTileset )
291318 {
292- mSrcRectangle . X = BaseResource . Initial . X * Options . TileWidth ;
293- mSrcRectangle . Y = BaseResource . Initial . Y * Options . TileHeight ;
294- mSrcRectangle . Width = ( BaseResource . Initial . Width + 1 ) * Options . TileWidth ;
295- mSrcRectangle . Height = ( BaseResource . Initial . Height + 1 ) * Options . TileHeight ;
319+ _renderBoundsSrc . X = Descriptor . Initial . X * Options . TileWidth ;
320+ _renderBoundsSrc . Y = Descriptor . Initial . Y * Options . TileHeight ;
321+ _renderBoundsSrc . Width = ( Descriptor . Initial . Width + 1 ) * Options . TileWidth ;
322+ _renderBoundsSrc . Height = ( Descriptor . Initial . Height + 1 ) * Options . TileHeight ;
296323 }
297324 else
298325 {
299- mSrcRectangle . Width = Texture . Width ;
300- mSrcRectangle . Height = Texture . Height ;
326+ _renderBoundsSrc . Width = Texture . Width ;
327+ _renderBoundsSrc . Height = Texture . Height ;
301328 }
302329
303- mDestRectangle . Width = mSrcRectangle . Width ;
304- mDestRectangle . Height = mSrcRectangle . Height ;
305- mDestRectangle . Y = ( int ) ( map . Y + Y * Options . TileHeight + OffsetY ) ;
306- mDestRectangle . X = ( int ) ( map . X + X * Options . TileWidth + OffsetX ) ;
307- if ( mSrcRectangle . Height > Options . TileHeight )
330+ _renderBoundsDest . Width = _renderBoundsSrc . Width ;
331+ _renderBoundsDest . Height = _renderBoundsSrc . Height ;
332+ _renderBoundsDest . Y = ( int ) ( map . Y + Y * Options . TileHeight + OffsetY ) ;
333+ _renderBoundsDest . X = ( int ) ( map . X + X * Options . TileWidth + OffsetX ) ;
334+ if ( _renderBoundsSrc . Height > Options . TileHeight )
308335 {
309- mDestRectangle . Y -= mSrcRectangle . Height - Options . TileHeight ;
336+ _renderBoundsDest . Y -= _renderBoundsSrc . Height - Options . TileHeight ;
310337 }
311338
312- if ( mSrcRectangle . Width > Options . TileWidth )
339+ if ( _renderBoundsSrc . Width > Options . TileWidth )
313340 {
314- mDestRectangle . X -= ( mSrcRectangle . Width - Options . TileWidth ) / 2 ;
341+ _renderBoundsDest . X -= ( _renderBoundsSrc . Width - Options . TileWidth ) / 2 ;
315342 }
316343
317- mHasRenderBounds = true ;
344+ _recalculateRenderBounds = false ;
318345 }
319346
320347 //Rendering Resources
@@ -327,7 +354,7 @@ public override void Draw()
327354
328355 if ( Texture != null )
329356 {
330- Graphics . DrawGameTexture ( Texture , mSrcRectangle , mDestRectangle , Intersect . Color . White ) ;
357+ Graphics . DrawGameTexture ( Texture , _renderBoundsSrc , _renderBoundsDest , Intersect . Color . White ) ;
331358 }
332359 }
333360}
0 commit comments