1using System.Collections.ObjectModel;
15public sealed class SpriteManager : IDisposable
17 [Newtonsoft.Json.JsonIgnore]
internal DefinitionProvenance? DefinitionProvenance {
get;
set; }
19 private static readonly Lazy<SpriteManager> _instance =
new(() =>
new SpriteManager());
25 public static SpriteManager
Instance => _instance.Value;
27 internal readonly List<Sprite> _spriteList =
new();
28 private readonly
object _spriteListLock =
new();
30 private long _lastTick = EngineSimulationClock.GetCurrentTick();
37 private SpriteManager() { }
46 lock (_spriteListLock)
47 return new List<Sprite>(_spriteList).AsReadOnly();
56 private string _defaultCollisionProfile = CollisionProfileNames.Actor;
64 get => _defaultCollisionProfile;
67 if (
string.IsNullOrWhiteSpace(value))
68 throw new ArgumentException(
"Default collision profile cannot be empty.", nameof(value));
70 _defaultCollisionProfile = value;
74 #region public methods
90 string? collisionProfileName =
null)
92 var profileName =
string.IsNullOrWhiteSpace(collisionProfileName)
94 : collisionProfileName;
96 var sprite =
new Sprite(sceneLayer, frame, profileName);
98 PublishCreatedSprite(sprite);
119 PublishCreatedSprite(newSprite);
165 DefinitionProvenance =
null;
166 List<Sprite> tempSprites;
168 lock (_spriteListLock)
169 tempSprites =
new List<Sprite>(_spriteList);
171 foreach (
Sprite sprite
in tempSprites)
182 List<Sprite> snapshot;
184 lock (_spriteListLock)
185 snapshot =
new List<Sprite>(_spriteList);
187 foreach (
Sprite sprite
in snapshot)
196 internal bool HasVisibleSpriteInWorldRect(
200 lock (_spriteListLock)
202 for (
int i = 0; i < _spriteList.Count; i++)
204 var sprite = _spriteList[i];
206 if (sprite.Visible &&
207 ReferenceEquals(sprite.SceneLayer, sceneLayer) &&
208 sprite.VisualBoundsWorld.IntersectsWith(worldRect))
229 bool fullEnclosures =
false)
231 List<Sprite> retSprites =
new();
233 List<Sprite> snapshot;
235 lock (_spriteListLock)
236 snapshot =
new List<Sprite>(_spriteList);
238 foreach (
Sprite sprite
in snapshot)
240 if ((sceneLayer is
null) || (sprite.
SceneLayer == sceneLayer))
245 retSprites.Add(sprite);
250 retSprites.Add(sprite);
269 Rectangle viewRectPx,
271 bool fullEnclosures =
false)
273 var retSprites =
new List<Sprite>();
275 List<Sprite> snapshot;
277 lock (_spriteListLock)
278 snapshot =
new List<Sprite>(_spriteList);
280 foreach (var sprite
in snapshot)
282 if ((sceneLayer is
null) || (sprite.
SceneLayer == sceneLayer))
288 if (viewRectPx.Contains(rectScreen))
289 retSprites.Add(sprite);
293 if (rectScreen.IntersectsWith(viewRectPx))
294 retSprites.Add(sprite);
314 var retSprites =
new List<Sprite>();
316 List<Sprite> snapshot;
318 lock (_spriteListLock)
319 snapshot =
new List<Sprite>(_spriteList);
321 foreach (
Sprite sprite
in snapshot)
323 if ((sceneLayer is
null) || (sprite.
SceneLayer == sceneLayer))
326 retSprites.Add(sprite);
333 #endregion public methods
335 #region internal methods
337 internal void AddSprite(
Sprite sprite)
339 lock (_spriteListLock)
340 _spriteList.Add(sprite);
348 internal void RefreshCollisionProfiles(
SceneLayer sceneLayer)
350 ArgumentNullException.ThrowIfNull(sceneLayer);
352 List<Sprite> snapshot;
354 lock (_spriteListLock)
355 snapshot =
new List<Sprite>(_spriteList);
357 foreach (var sprite
in snapshot)
362 if (ReferenceEquals(sprite.SceneLayer, sceneLayer))
363 sprite.RefreshCollisionProfile();
367 internal void MoveSprites(
long tick)
369 if (tick <= _lastTick)
372 float duration = HighResTimer.GetDuration(_lastTick, tick);
374 lock (_spriteListLock)
376 for (
int i = 0; i < _spriteList.Count; i++)
378 var sprite = _spriteList[i];
380 if (sprite._pendingDispose)
383 sprite.Movement.AdvanceMovement(duration);
384 sprite.AdvanceResize(duration);
385 sprite.AdvanceJiggle(duration);
388 SweepDisposedSprites();
394 private void SweepDisposedSprites()
398 while (i < _spriteList.Count)
400 if (!_spriteList[i]._pendingDispose)
406 _spriteList[i].DisposeImmediate();
408 int last = _spriteList.Count - 1;
409 _spriteList[i] = _spriteList[last];
410 _spriteList.RemoveAt(last);
414 private void PublishCreatedSprite(Sprite sprite)
423 lock (_spriteListLock)
424 _spriteList.Remove(sprite);
426 sprite.DisposeImmediate();
431 #endregion internal methods
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.
override void Dispose()
Releases the resources owned by this instance.
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.
override SceneLayer SceneLayer
string? Nickname
Gets or sets the optional lookup name.
Represents a rendered view of a scene, combining a camera position with a viewport configuration to c...
Represents a single layer within a Scene, containing a grid of tiles and supporting various coordinat...
Represents the source Tilesheet and its coordinates to render on a destination.