AceField/Scripts/TilesManager.cs
2024-08-05 23:54:22 +08:00

35 lines
917 B
C#

using Godot;
namespace CodingLand.Scripts;
public partial class TilesManager : Node2D
{
[Export] public float TileSize = 51.2f;
[Rpc(CallLocal = true)]
public void AddTile(Vector2 pos)
{
if (GetTileByPosition<Node2D>(pos) == null)
{
var tileVec = new Vector2(TileSize, TileSize);
// TODO Replace the brick to player selection
var blueprint = GD.Load<PackedScene>("res://Scenes/Tiles/Brick.tscn");
var instance = blueprint.Instantiate<Node2D>();
instance.Position = pos.Snapped(tileVec);
AddChild(instance);
}
}
public T GetTileByPosition<T>(Vector2 position) where T : Node2D
{
foreach (var item in GetChildren())
{
if (item is T tile && tile.Position == position)
{
return tile;
}
}
return null;
}
}