2026-02-14 14:35:29 +01:00
namespace GlobalClassLib ;
public abstract class GlobalMapTile < TCon , TTile > where TCon : GlobalTileConnector < TTile , TCon > where TTile : GlobalMapTile < TCon , TTile > { // TTile should be the inheriting class
public int Id { get ; private set ; }
public bool Lit { get ; set ; } = false ;
2026-02-16 21:48:59 +01:00
public ( int x , int y ) GridPosition { get ; private set ; }
2026-02-14 14:35:29 +01:00
private List < TCon > connectors = new ( ) ;
public GlobalMapTile ( int id ) {
Id = id ;
2026-02-16 21:48:59 +01:00
GridPosition = IdToCoords ( id ) ;
2026-02-14 14:35:29 +01:00
}
public void AddConnector ( TCon connector ) { // tile1 is ignored when provided
2026-02-16 21:48:59 +01:00
connector = connector . Clone ( ) ;
2026-02-14 14:35:29 +01:00
connector . Tiles . tile1 = ( TTile ) this ;
connectors . Add ( connector ) ;
connector . Tiles . tile2 . _AddConnectorNoRepeat ( connector . Clone ( ) ) ;
// connectors.Add(new TCon(this, tile, type));
// tile.connectors.Add(new GlobalTileConnector(tile, this, type));
}
private void _AddConnectorNoRepeat ( TCon connector ) {
( connector . Tiles . tile1 , connector . Tiles . tile2 ) = ( connector . Tiles . tile2 , connector . Tiles . tile1 ) ;
connectors . Add ( connector ) ;
}
public void AddConnectors ( TCon [ ] connectors ) = >
Array . ForEach ( connectors , AddConnector ) ;
public override string ToString ( ) = > $"{PositionAsString} -> {string.Join(" , ", connectors.Select(c => c.Tiles.Item2.PositionAsString))}" ;
public string PositionAsString = > $"[{Id}]" ; // for debug purposes
public TCon ? GetConnector ( int id ) {
foreach ( var con in connectors ) {
if ( con . Tiles . Item2 . Id = = id ) return con ;
}
return null ;
}
2026-02-16 21:48:59 +01:00
public TCon [ ] GetAllConnectors ( ) = > connectors . ToArray ( ) ;
2026-02-14 14:35:29 +01:00
2026-02-16 21:48:59 +01:00
public const int ID_X_OFFSET = 5 ; // map grid height
public static int CoordsToId ( int x , int y ) = > x * ID_X_OFFSET + y ;
public static ( int , int ) IdToCoords ( int id ) = > ( id / ID_X_OFFSET , id % ID_X_OFFSET ) ;
2026-02-14 14:35:29 +01:00
}