-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTileUtils.cs
48 lines (41 loc) · 1.88 KB
/
TileUtils.cs
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
using Terraria;
using Terraria.DataStructures;
namespace Eggpack;
public static class TileUtils
{
/// <summary>
/// Gets the top-left tile of a multitile
/// </summary>
/// <param name="i">The tile X-coordinate</param>
/// <param name="j">The tile Y-coordinate</param>
public static Point16 GetTileOrigin(int i, int j)
{
//Framing.GetTileSafely ensures that the returned Tile instance is not null
//Do note that neither this method nor Framing.GetTileSafely check if the wanted coordiates are in the world!
Tile tile = Framing.GetTileSafely(i, j);
Point16 coord = new(i, j);
Point16 frame = new(tile.TileFrameX / 18, tile.TileFrameY / 18);
return coord - frame;
}
/// <summary>
/// Uses <seealso cref="GetTileOrigin(int, int)"/> to try to get the entity bound to the multitile at (<paramref name="i"/>, <paramref name="j"/>).
/// </summary>
/// <typeparam name="T">The type to get the entity as</typeparam>
/// <param name="i">The tile X-coordinate</param>
/// <param name="j">The tile Y-coordinate</param>
/// <param name="entity">The found <typeparamref name="T"/> instance, if there was one.</param>
/// <returns><see langword="true"/> if there was a <typeparamref name="T"/> instance, or <see langword="false"/> if there was no entity present OR the entity was not a <typeparamref name="T"/> instance.</returns>
public static bool TryGetTileEntityAs<T>(int i, int j, out T entity) where T : TileEntity
{
Point16 origin = GetTileOrigin(i, j);
//TileEntity.ByPosition is a Dictionary<Point16, TileEntity> which contains all placed TileEntity instances in the world
//TryGetValue is used to both check if the dictionary has the key, origin, and get the value from that key if it's there
if (TileEntity.ByPosition.TryGetValue(origin, out TileEntity existing) && existing is T existingAsT)
{
entity = existingAsT;
return true;
}
entity = null;
return false;
}
}