-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSpriteSheet.cs
More file actions
64 lines (58 loc) · 2.43 KB
/
SpriteSheet.cs
File metadata and controls
64 lines (58 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
namespace OpenWheels.Rendering
{
/// <summary>
/// An image containing multiple sprites.
/// Retrieve a <see cref="Sprite"/> with it's key.
/// </summary>
/// <typeparam name="T">Type of <see cref="Sprite"/> keys.</typeparam>
public class SpriteSheet<T>
{
/// <summary>
/// The texture of this <see cref="SpriteSheet{T}"/>.
/// </summary>
public int Texture { get; }
private readonly Dictionary<T, Rectangle> _spriteMap;
/// <summary>
/// Create a <see cref="SpriteSheet{T}"/>.
/// </summary>
/// <param name="texture">Texture identifier.</param>
/// <param name="spriteMap">Dictionary of sprite coordinates in pixels.</param>
public SpriteSheet(int texture, Dictionary<T, Rectangle> spriteMap)
{
Texture = texture;
_spriteMap = spriteMap;
}
/// <summary>
/// Get the <see cref="Sprite"/> associated with the given key.
/// </summary>
/// <param name="key">The key of the <see cref="Sprite"/> to retrieve.</param>
/// <exception cref="ArgumentNullException">If <paramref name="key"/> is <code>null</code>.</exception>
/// <exception cref="KeyNotFoundException">If the key does not exist in the sprite map.</exception>
public Sprite this[T key] => new Sprite(Texture, _spriteMap[key]);
/// <summary>
/// Get the <see cref="Sprite"/> associated with the given key.
/// </summary>
/// <param name="key">The key of the <see cref="Sprite"/> to retrieve.</param>
/// <param name="sprite">
/// The retrieved <see cref="Sprite"/> or the default sprite value if the
/// key does not exist in the sprite map.
/// </param>
/// <returns>
/// <code>True</code> if <paramref name="key"/> exists in the sprite map and
/// the sprite was succesfully retrieved, <code>false</code> otherwise.
/// </returns>
/// <exception cref="ArgumentNullException">If <paramref name="key"/> is <code>null</code>.</exception>
public bool TryGetSprite(T key, out Sprite sprite)
{
if (_spriteMap.TryGetValue(key, out var rect))
{
sprite = new Sprite(Texture, rect);
return true;
}
sprite = default;
return false;
}
}
}