Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
SpriteManager.cs
1using System.Collections.ObjectModel;
2using System.Drawing;
9
11
15public sealed class SpriteManager : IDisposable
16{
17 [Newtonsoft.Json.JsonIgnore] internal DefinitionProvenance? DefinitionProvenance { get; set; }
18
19 private static readonly Lazy<SpriteManager> _instance = new(() => new SpriteManager());
20
25 public static SpriteManager Instance => _instance.Value;
26
27 internal readonly List<Sprite> _spriteList = new();
28 private readonly object _spriteListLock = new();
29
30 private long _lastTick = EngineSimulationClock.GetCurrentTick();
31
35 public event Action<Sprite>? SpriteCreated;
36
37 private SpriteManager() { }
38
42 public ReadOnlyCollection<Sprite> AllSprites
43 {
44 get
45 {
46 lock (_spriteListLock)
47 return new List<Sprite>(_spriteList).AsReadOnly();
48 }
49 }
50
54 public bool SizeNewSpritesToSceneLayer { get; set; } = true;
55
56 private string _defaultCollisionProfile = CollisionProfileNames.Actor;
57
63 {
64 get => _defaultCollisionProfile;
65 set
66 {
67 if (string.IsNullOrWhiteSpace(value))
68 throw new ArgumentException("Default collision profile cannot be empty.", nameof(value));
69
70 _defaultCollisionProfile = value;
71 }
72 }
73
74 #region public methods
75
87 SceneLayer sceneLayer,
88 Frame frame,
89 string? id = null,
90 string? collisionProfileName = null)
91 {
92 var profileName = string.IsNullOrWhiteSpace(collisionProfileName)
94 : collisionProfileName;
95
96 var sprite = new Sprite(sceneLayer, frame, profileName);
97 sprite.Nickname = id;
98 PublishCreatedSprite(sprite);
99 return sprite;
100 }
101
107 public Sprite CloneSprite(Sprite sprite) => CloneSprite(sprite, sprite.SceneLayer);
108
115 public Sprite CloneSprite(Sprite sprite, SceneLayer sceneLayer)
116 {
117 Sprite newSprite = new Sprite(sprite, sceneLayer);
118
119 PublishCreatedSprite(newSprite);
120 return newSprite;
121 }
122
129 public Sprite? CloneSprite(string id, SceneLayer sceneLayer)
130 {
131 Sprite? sprite = GetSpriteByID(id);
132
133 if (sprite != null)
134 return CloneSprite(sprite, sceneLayer);
135
136 return null;
137 }
138
143 public void Remove(Sprite sprite)
144 {
145 sprite.Dispose();
146 }
147
152 public void Remove(string ID)
153 {
154 Sprite? sprite = GetSpriteByID(ID);
155
156 if (sprite != null)
157 Remove(sprite);
158 }
159
163 public void Clear()
164 {
165 DefinitionProvenance = null;
166 List<Sprite> tempSprites;
167
168 lock (_spriteListLock)
169 tempSprites = new List<Sprite>(_spriteList);
170
171 foreach (Sprite sprite in tempSprites)
172 Remove(sprite);
173 }
174
180 public Sprite? GetSpriteByID(string ID)
181 {
182 List<Sprite> snapshot;
183
184 lock (_spriteListLock)
185 snapshot = new List<Sprite>(_spriteList);
186
187 foreach (Sprite sprite in snapshot)
188 {
189 if (sprite.Nickname == ID)
190 return sprite;
191 }
192
193 return null;
194 }
195
196 internal bool HasVisibleSpriteInWorldRect(
197 Rectangle worldRect,
198 SceneLayer sceneLayer)
199 {
200 lock (_spriteListLock)
201 {
202 for (int i = 0; i < _spriteList.Count; i++)
203 {
204 var sprite = _spriteList[i];
205
206 if (sprite.Visible &&
207 ReferenceEquals(sprite.SceneLayer, sceneLayer) &&
208 sprite.VisualBoundsWorld.IntersectsWith(worldRect))
209 {
210 return true;
211 }
212 }
213 }
214
215 return false;
216 }
217
218 // world
226 public List<Sprite> GetSpritesInWorldRectRange(
227 Rectangle worldRect,
228 SceneLayer? sceneLayer = null,
229 bool fullEnclosures = false)
230 {
231 List<Sprite> retSprites = new();
232
233 List<Sprite> snapshot;
234
235 lock (_spriteListLock)
236 snapshot = new List<Sprite>(_spriteList);
237
238 foreach (Sprite sprite in snapshot)
239 {
240 if ((sceneLayer is null) || (sprite.SceneLayer == sceneLayer))
241 {
242 if (fullEnclosures)
243 {
244 if (worldRect.Contains(sprite.VisualBoundsWorld))
245 retSprites.Add(sprite);
246 }
247 else
248 {
249 if (sprite.VisualBoundsWorld.IntersectsWith(worldRect))
250 retSprites.Add(sprite);
251 }
252 }
253 }
254
255 return retSprites;
256 }
257
258 // screen
267 public List<Sprite> GetSpritesInViewRectRange(
268 View view,
269 Rectangle viewRectPx,
270 SceneLayer? sceneLayer = null,
271 bool fullEnclosures = false)
272 {
273 var retSprites = new List<Sprite>();
274
275 List<Sprite> snapshot;
276
277 lock (_spriteListLock)
278 snapshot = new List<Sprite>(_spriteList);
279
280 foreach (var sprite in snapshot)
281 {
282 if ((sceneLayer is null) || (sprite.SceneLayer == sceneLayer))
283 {
284 var rectScreen = sprite.GetVisualBoundsScreen(view).ToPixelAlignedRect();
285
286 if (fullEnclosures)
287 {
288 if (viewRectPx.Contains(rectScreen))
289 retSprites.Add(sprite);
290 }
291 else
292 {
293 if (rectScreen.IntersectsWith(viewRectPx))
294 retSprites.Add(sprite);
295 }
296 }
297 }
298
299 return retSprites;
300 }
301
309 public List<Sprite> GetSpritesAtViewPixel(
310 View view,
311 Point viewPxlPt,
312 SceneLayer? sceneLayer = null)
313 {
314 var retSprites = new List<Sprite>();
315
316 List<Sprite> snapshot;
317
318 lock (_spriteListLock)
319 snapshot = new List<Sprite>(_spriteList);
320
321 foreach (Sprite sprite in snapshot)
322 {
323 if ((sceneLayer is null) || (sprite.SceneLayer == sceneLayer))
324 {
325 if (sprite.GetVisualBoundsScreen(view).Contains(viewPxlPt))
326 retSprites.Add(sprite);
327 }
328 }
329
330 return retSprites;
331 }
332
333 #endregion public methods
334
335 #region internal methods
336
337 internal void AddSprite(Sprite sprite)
338 {
339 lock (_spriteListLock)
340 _spriteList.Add(sprite);
341 }
342
348 internal void RefreshCollisionProfiles(SceneLayer sceneLayer)
349 {
350 ArgumentNullException.ThrowIfNull(sceneLayer);
351
352 List<Sprite> snapshot;
353
354 lock (_spriteListLock)
355 snapshot = new List<Sprite>(_spriteList);
356
357 foreach (var sprite in snapshot)
358 {
359 if (sprite is null)
360 continue;
361
362 if (ReferenceEquals(sprite.SceneLayer, sceneLayer))
363 sprite.RefreshCollisionProfile();
364 }
365 }
366
367 internal void MoveSprites(long tick)
368 {
369 if (tick <= _lastTick)
370 return;
371
372 float duration = HighResTimer.GetDuration(_lastTick, tick);
373
374 lock (_spriteListLock)
375 {
376 for (int i = 0; i < _spriteList.Count; i++)
377 {
378 var sprite = _spriteList[i];
379
380 if (sprite._pendingDispose)
381 continue;
382
383 sprite.Movement.AdvanceMovement(duration);
384 sprite.AdvanceResize(duration);
385 sprite.AdvanceJiggle(duration);
386 }
387
388 SweepDisposedSprites();
389 }
390
391 _lastTick = tick;
392 }
393
394 private void SweepDisposedSprites()
395 {
396 int i = 0;
397
398 while (i < _spriteList.Count)
399 {
400 if (!_spriteList[i]._pendingDispose)
401 {
402 i++;
403 continue;
404 }
405
406 _spriteList[i].DisposeImmediate();
407
408 int last = _spriteList.Count - 1;
409 _spriteList[i] = _spriteList[last];
410 _spriteList.RemoveAt(last);
411 }
412 }
413
414 private void PublishCreatedSprite(Sprite sprite)
415 {
416 try
417 {
418 SpriteCreated?.Invoke(sprite);
419 }
420 catch
421 {
422 // The caller cannot roll back an instance that never returned from CreateSprite/CloneSprite.
423 lock (_spriteListLock)
424 _spriteList.Remove(sprite);
425
426 sprite.DisposeImmediate();
427 throw;
428 }
429 }
430
431 #endregion internal methods
432
436 public void Dispose()
437 {
438 Clear();
439 }
440}
Sprite? GetSpriteByID(string ID)
Retrieves a sprite by its ID/nickname.
ReadOnlyCollection< Sprite > AllSprites
Gets a read-only collection of all sprites currently managed by the sprite manager.
List< Sprite > GetSpritesInViewRectRange(View view, Rectangle viewRectPx, SceneLayer? sceneLayer=null, bool fullEnclosures=false)
Gets all sprites within the specified view rectangle range.
Sprite CreateSprite(SceneLayer sceneLayer, Frame frame, string? id=null, string? collisionProfileName=null)
Creates a new sprite on the specified scene layer with the given frame.
string DefaultCollisionProfile
Gets or sets the scene collision profile assigned to newly created sprites when no profile is specifi...
List< Sprite > GetSpritesInWorldRectRange(Rectangle worldRect, SceneLayer? sceneLayer=null, bool fullEnclosures=false)
Gets all sprites within the specified world rectangle range.
Sprite CloneSprite(Sprite sprite)
Creates a copy of the specified sprite, preserving its current scene layer.
void Clear()
Removes and disposes all sprites currently managed by the sprite manager.
Sprite CloneSprite(Sprite sprite, SceneLayer sceneLayer)
Creates a clone of the specified sprite on the given scene layer.
void Dispose()
Releases all sprites currently managed by this instance.
bool SizeNewSpritesToSceneLayer
Gets or sets a value indicating whether new sprites should be automatically sized to their scene laye...
void Remove(string ID)
Removes and disposes the sprite with the specified ID.
Action< Sprite >? SpriteCreated
Event raised when a new sprite is created.
void Remove(Sprite sprite)
Removes and disposes the specified sprite.
static SpriteManager Instance
Gets the singleton instance of the sprite manager.
Sprite? CloneSprite(string id, SceneLayer sceneLayer)
Creates a clone of the sprite with the specified ID on the given scene layer.
List< Sprite > GetSpritesAtViewPixel(View view, Point viewPxlPt, SceneLayer? sceneLayer=null)
Gets all sprites at the specified view pixel coordinate.
Represents a movable visual element on a scene layer.
Definition Sprite.cs:21
override void Dispose()
Releases the resources owned by this instance.
Definition Sprite.cs:687
RectangleF GetVisualBoundsScreen(View view)
Gets the axis-aligned screen-pixel bounds enclosing the rotated sprite.
Rectangle VisualBoundsWorld
Gets the axis-aligned world-pixel bounds enclosing the rotated sprite.
Definition Sprite.cs:367
override SceneLayer SceneLayer
Definition Sprite.cs:642
string? Nickname
Gets or sets the optional lookup name.
Definition Tile.cs:116
Represents a rendered view of a scene, combining a camera position with a viewport configuration to c...
Definition View.cs:14
Represents a single layer within a Scene, containing a grid of tiles and supporting various coordinat...
Definition SceneLayer.cs:40
Represents the source Tilesheet and its coordinates to render on a destination.
Definition Frame.cs:12