-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTileHitbox.cs
More file actions
55 lines (47 loc) · 1.82 KB
/
TileHitbox.cs
File metadata and controls
55 lines (47 loc) · 1.82 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
using System;
using System.Linq;
using Microsoft.Xna.Framework;
using OriMod.Utilities;
using Terraria;
namespace OriMod;
/// <summary>
/// Primarily used for <see cref="Abilities.Burrow"/>, stores an array of points as a template, and retrieves tiles of that template when updated.
/// </summary>
public class TileHitbox {
/// <summary>
/// Instantiate a <see cref="TileHitbox"/> with local-space <paramref name="template"/>.
/// </summary>
/// <param name="template"><see cref="Template"/>. Must have at least 1 item.</param>
public TileHitbox(params Point[] template) {
ArgumentNullException.ThrowIfNull(template);
if (template.Length == 0) {
throw new ArgumentException("Template must have at least one item.", nameof(template));
}
Template = template;
Points = new Point[Template.Length];
UpdateHitbox(Point.Zero);
}
public TileHitbox(params (int x, int y)[] template) : this(template?.Select(tuple => new Point(tuple.x, tuple.y)).ToArray()) { }
/// <summary>
/// Current world position of points in the hitbox, in tile coordinates.
/// </summary>
public Point[] Points { get; }
/// <summary>
/// Local position of points in the hitbox, in tile coordinates.
/// </summary>
public Point[] Template { get; }
/// <summary>
/// Updates the position of the hitbox based on the given world position.
/// </summary>
/// <param name="origin">World-space position to use.</param>
public void UpdateHitbox(Vector2 origin) => UpdateHitbox(origin.ToTileCoordinates());
/// <summary>
/// Updates the world position of the hitbox based on the given tile position.
/// </summary>
/// <param name="origin">Tile-space position to use.</param>
public void UpdateHitbox(Point origin) {
for (int i = 0; i < Points.Length; i++) {
Points[i] = Template[i].Add(origin);
}
}
}