-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.cs
54 lines (47 loc) · 1.62 KB
/
Grid.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
49
50
51
52
53
54
using Godot;
// The Grid class represents the spatial properties of a 2x2 grid.
// This class is a static, data-only resource and contains no game logic.
// Can be used in both orthogonal and isometric contexts.
[GlobalClass]
public partial class Grid : Resource
{
// Size of the grid in cells.
[Export]
public Vector2I Size = new(8, 8);
// Size of each cell in pixels.
[Export]
public Vector2I CellSize = new(16, 16);
// Convert grid cell coordinates to screen coordinates in pixels.
// Returns the center point of the given cell.
public Vector2 GridToScreen(Vector2I gridCoords)
{
return gridCoords * CellSize + (CellSize / 2);
}
// Convert screen coordinates in pixels to grid cell coordinates.
public Vector2I ScreenToGrid(Vector2 screenCoords)
{
Vector2 converted = (screenCoords / CellSize).Floor();
return new Vector2I((int)converted.X, (int)converted.Y);
}
// Returns 'true' if the given grid coordinates are valid.
public bool IsWithinBounds(Vector2I coords)
{
return (
coords.X >= 0 &&
coords.X < Size.X &&
coords.Y >= 0 &&
coords.Y < Size.Y
);
}
// Clamp the given coordinates to the bounds of the grid.
public Vector2I Clamp(Vector2I coords)
{
return coords.Clamp(Vector2I.Zero, new(Size.X - 1, Size.Y - 1));
}
// Serializes grid coordinates as integers such that each cell is uniquely
// represented. Useful when dealing with A* pathfinding.
public int ToIndex(Vector2I coords)
{
return coords.X + Size.X * coords.Y;
}
}