|
| 1 | +using System; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using System.Runtime.CompilerServices; |
| 4 | +using DungeonRoyale.Modules.Tiles.Scripts; |
| 5 | +using DungeonRoyale.Shared.Scripts.Constants; |
| 6 | + |
| 7 | +namespace DungeonRoyale.Modules.GameManagers.Scripts; |
| 8 | + |
| 9 | +public partial class TilesManager : Node2D |
| 10 | +{ |
| 11 | + public static TilesManager? Instance { get; private set; } |
| 12 | + |
| 13 | + public DRTileData[,] Tiles { get; private set; } = new DRTileData[0, 0]; |
| 14 | + |
| 15 | + private int _width; |
| 16 | + private int _height; |
| 17 | + |
| 18 | + public override void _Ready() |
| 19 | + { |
| 20 | + if (Instance is null) |
| 21 | + { |
| 22 | + Instance = this; |
| 23 | + } |
| 24 | + else |
| 25 | + { |
| 26 | + GD.PrintErr("There is already an instance of TilesManager in the scene."); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public void SetUpTiles(int width, int height) |
| 31 | + { |
| 32 | + if (width <= 0 || height <= 0) |
| 33 | + { |
| 34 | + throw new ArgumentException("Width and height must be greater than 0."); |
| 35 | + } |
| 36 | + |
| 37 | + _width = width; |
| 38 | + _height = height; |
| 39 | + |
| 40 | + Tiles = new DRTileData[_width, _height]; |
| 41 | + } |
| 42 | + |
| 43 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 44 | + private bool IsOutOfMapBounds(int x, int y) => |
| 45 | + x < 0 || x >= _width || y < 0 || y >= _height; |
| 46 | + |
| 47 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 48 | + public DRTileData? GetTileAt(int x, int y) => |
| 49 | + IsOutOfMapBounds(x, y) ? null : Tiles[x, y]; |
| 50 | + |
| 51 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 52 | + public bool TryGetTileAt(int x, int y, [NotNullWhen(returnValue: true)] out DRTileData? tileData) => |
| 53 | + (tileData = GetTileAt(x, y)) is not null; |
| 54 | + |
| 55 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 56 | + public bool TryGetTileAt((int x, int y) position, [NotNullWhen(returnValue: true)] out DRTileData? tileData) => |
| 57 | + TryGetTileAt(position.x, position.y, out tileData); |
| 58 | + |
| 59 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 60 | + public bool TryGetTileAt(Vector2I position, [NotNullWhen(returnValue: true)] out DRTileData? tileData) => |
| 61 | + TryGetTileAt(position.X, position.Y, out tileData); |
| 62 | + |
| 63 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 64 | + public bool TryGetTileAtGlobalCoords(Vector2 position, [NotNullWhen(returnValue: true)] out DRTileData? tileData) => |
| 65 | + (tileData = GetTileAt((int) position.X / TileConstants.TILE_SIZE, (int) position.Y / TileConstants.TILE_SIZE)) is not null; |
| 66 | +} |
0 commit comments