namespace Data.Enum;
[GlobalClass]
public partial class Level : Node2D
{
[Export]
LevelResolution resolution;
[Export]
bool HideCollision = false;
TileMapLayer CollisionLayer { get { return (TileMapLayer)FindChild("Collision"); } }
Array<TileMapLayer> Layers
{
get
{
Array<TileMapLayer> layers = new Array<TileMapLayer>();
Array<Node> children = GetChildren();
foreach (Node item in children)
{
if (item.GetType() != typeof(TileMapLayer))
continue;
if (item == CollisionLayer)
continue;
layers.Add((TileMapLayer)item);
}
return layers;
}
}
#region ConsoleCommands
[ConsoleCommand]
public int GetResolution()
{
return (int)resolution;
}
[ConsoleCommand]
public Vector2I GetMapSize()
{
Vector2I foundSize = Vector2I.Zero;
Array<TileMapLayer> layers = Layers;
foreach (TileMapLayer layer in layers)
{
Rect2I rect = layer.GetUsedRect();
if (rect.Size.X > foundSize.X)
foundSize = new Vector2I(rect.Size.X, foundSize.Y);
if (rect.Size.Y > foundSize.Y)
foundSize = new Vector2I(foundSize.X, rect.Size.Y);
}
return foundSize;
}
[ConsoleCommand]
public Vector2I GetPixelMapSize()
{
Vector2I mapSize = GetMapSize();
return mapSize * GetResolution();
}
[ConsoleCommand]
public Vector2I MouseToTile()
{
Vector2I mousepos = (Vector2I)GetLocalMousePosition();
return PixelToTile(mousepos);
}
public Vector2I MouseToPixel()
{
Vector2I mousepos = (Vector2I)GetLocalMousePosition();
Vector2I mouseTile = PixelToTile(mousepos);
Vector2I tilePixel = TileToPixel(mouseTile);
return tilePixel;
}
[ConsoleCommand]
public Vector2I PixelToTile(int pixelX, int pixelY)
{
return PixelToTile(new Vector2I(pixelX, pixelY));
}
[ConsoleCommand]
public Vector2I PixelToTile(Vector2I pixelCoordinates)
{
return (Vector2I)new Vector2(Mathf.Floor(pixelCoordinates.X / GetResolution()), Mathf.Floor(pixelCoordinates.Y / GetResolution()));
}
[ConsoleCommand]
public Vector2I TileToPixel(int TileX, int TileY)
{
return TileToPixel(TileX, TileY);
}
[ConsoleCommand]
public bool IsWalkableTile(int TileX, int TileY)
{
return IsWalkableTile(new Vector2I(TileX, TileY));
}
[ConsoleCommand]
public Vector2I ClampTile(int TileX, int TileY)
{
return ClampTile(new Vector2I(TileX, TileY));
}
[ConsoleCommand]
public bool IsInBounds(int TileX, int TileY)
{
return IsInBounds(new Vector2I(TileX, TileY));
}
#endregion
//Returns the pixel coordinates of the tile based off the center of the tile
public Vector2I TileToPixel(Vector2I TileCoordinates)
{
Vector2I halfSize = new Vector2I(GetResolution() / 2, GetResolution() / 2);
return TileCoordinates * GetResolution() + halfSize;
}
public bool IsWalkableTile(Vector2I position)
{
return CollisionLayer.GetCellSourceId(position) != -1;
}
public Vector2I ClampTile(Vector2I tileCoordinate)
{
int X = tileCoordinate.X;
int Y = tileCoordinate.Y;
Vector2I mapSize = GetMapSize();
if (X < 0)
X = 0;
if (X > mapSize.X)
X = mapSize.X;
if (Y < 0)
Y = 0;
if (Y > mapSize.Y)
Y = mapSize.Y;
return new Vector2I(X, Y);
}
public bool IsInBounds(Vector2I tileCoordinate)
{
int X = tileCoordinate.X;
int Y = tileCoordinate.Y;
Vector2I mapSize = GetMapSize();
if (X < 0 || Y < 0)
return false;
if (X > mapSize.X || Y > mapSize.Y)
return false;
return true;
}
//Converts a 2d coordinate to an array index
public int ToIndex(Vector2I tileCoordinate)
{
return (int)(tileCoordinate.X + GetMapSize().X * tileCoordinate.Y);
}
//converts an array index to a 2d coordinate
public Vector2I IndexToCell(int index)
{
int mapWidth = GetMapSize().X;
int x = index % mapWidth;
int y = index / mapWidth;
return new Vector2I(x, y);
}
public override void _Ready()
{
base._Ready();
if (HideCollision)
{
Visible = false;
}
}
}