50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
public partial class TileMapping : Node
|
|
{
|
|
/// <summary>
|
|
/// From tile coordinates to pixel coordinates. If null, use the offset instead.
|
|
/// </summary>
|
|
private Dictionary<Vector2I, Vector2I> _pixelMap;
|
|
/// <summary>
|
|
/// If pixelMap is null, use this offset to convert tile coordinates to pixel coordinates.
|
|
/// </summary>
|
|
private Vector2I _pixelOffset;
|
|
private Rect2I _tileRect;
|
|
|
|
public TileMapping(Dictionary<Vector2I, Vector2I> map) {
|
|
_pixelMap = map;
|
|
_pixelOffset = new Vector2I(-int.MaxValue, -int.MaxValue);
|
|
}
|
|
|
|
public TileMapping(Vector2I offset, Rect2I rect) {
|
|
_pixelMap = null;
|
|
_pixelOffset = offset;
|
|
_tileRect = rect;
|
|
}
|
|
|
|
public TileMapping() {}
|
|
|
|
public void Apply(TileMapLayer layer) {
|
|
// if pixelMap is not null, use all the pairs in pixelMap to set the tiles.
|
|
if (_pixelMap != null) {
|
|
var sourceId = layer.GetCellSourceId(_pixelMap.Keys.First());
|
|
foreach (var pair in _pixelMap) {
|
|
layer.SetCell(pair.Key, sourceId, pair.Value);
|
|
}
|
|
} else {
|
|
var sourceId = layer.GetCellSourceId(_tileRect.Position);
|
|
GD.Print(_tileRect);
|
|
for (int x = _tileRect.Position.X; x < _tileRect.End.X; x++) {
|
|
for (int y = _tileRect.Position.Y; y < _tileRect.End.Y; y++) {
|
|
var pos = new Vector2I(x, y);
|
|
layer.SetCell(pos, sourceId, pos + _pixelOffset);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|