supervisor-simulator/scripts/TileMapping.cs
2024-12-01 23:47:55 +08:00

49 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 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);
GD.Print($"{pos} -> {pos + pixelOffset}");
layer.SetCell(pos, sourceId, pos + pixelOffset);
}
}
}
}
}