93 lines
1.8 KiB
C#
93 lines
1.8 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
public class TileGrid : Resource, IReadOnlyList<Tile>
|
|
{
|
|
[Export]
|
|
public int Size { get; private set; } = 10;
|
|
|
|
[Export]
|
|
private Resource _startTileTypeResource;
|
|
public TileType StartTileType => (TileType)_startTileTypeResource;
|
|
|
|
public int Count => Size * Size;
|
|
|
|
public Tile this[int index]
|
|
{
|
|
get => Tiles[index];
|
|
set => Tiles[index] = value;
|
|
}
|
|
|
|
public Tile this[int x, int y]
|
|
{
|
|
get
|
|
{
|
|
MapPosition(x, y, out int idx);
|
|
return Tiles[idx];
|
|
}
|
|
set
|
|
{
|
|
MapPosition(x, y, out int idx);
|
|
Tiles[idx] = value;
|
|
}
|
|
}
|
|
|
|
private List<Tile> Tiles
|
|
{
|
|
get
|
|
{
|
|
if (_tiles == null)
|
|
{
|
|
_tiles = new List<Tile>();
|
|
Generate(_tiles);
|
|
}
|
|
|
|
return _tiles;
|
|
}
|
|
}
|
|
private List<Tile> _tiles = null;
|
|
|
|
private void Generate(List<Tile> tiles)
|
|
{
|
|
tiles.Clear();
|
|
for (int i = 0; i < Count; i++)
|
|
{
|
|
var tile = new Tile();
|
|
tile.isHighlighted = false;
|
|
tile.temperature = 0.0f;
|
|
tile.type = StartTileType;
|
|
|
|
tiles.Add(tile);
|
|
}
|
|
}
|
|
|
|
public IEnumerator<Tile> GetEnumerator()
|
|
{
|
|
return Tiles.GetEnumerator();
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return Tiles.GetEnumerator();
|
|
}
|
|
|
|
public void MapIndex(int idx, out int x, out int y)
|
|
{
|
|
y = idx / Size;
|
|
x = idx % Size;
|
|
}
|
|
|
|
public void MapPosition(int x, int y, out int idx)
|
|
{
|
|
idx = y * Size + x;
|
|
}
|
|
|
|
public bool IsInBounds(int x, int y)
|
|
{
|
|
return x >= 0 && x < Size && y >= 0 && y < Size;
|
|
}
|
|
}
|
|
|