using Godot; using System; using System.Collections.Generic; public partial class Desk : Node2D { public Vector2 _topLeft, _cellDimensions, _gridDimensions; public Sprite2D _light, _dark; Node2D _grid; public override void _Ready() { _grid = GetNode("Cells"); _light = GetNode("Light"); _dark = GetNode("Dark"); _cellDimensions = new Vector2(50, 50); _gridDimensions = new Vector2(10, 20); _topLeft = Globals.Instance._screenCenter - _cellDimensions * _gridDimensions / 2 + _cellDimensions / 2; GD.Print(_topLeft); Setup(); } public Vector2 GetPositionFromAddress(int ROW, int COLUMN) { if (_grid.HasNode("R" + ROW)) { Node2D row = _grid.GetNode("R" + ROW); if (row.HasNode("C" + COLUMN)) { Sprite2D column = row.GetNode("C" + COLUMN); return column.GlobalPosition; } } return new Vector2(-1, -1); } public void Setup() { Vector2 position; Node2D row; Sprite2D column; for (int i = 0; i < _gridDimensions.Y; i++) { if (i <= _grid.GetChildCount()) { row = new(); row.Name = "R" + (i + 1); _grid.AddChild(row); } for (int j = 0; j < _gridDimensions.X; j++) { position = _topLeft + new Vector2(_cellDimensions.X * j, _cellDimensions.Y * i); column = (Sprite2D)((i + j) % 2 == 0 ? _light : _dark).Duplicate(); column.Name = "C" + (j + 1); column.GlobalPosition = position; column.Visible = true; row = _grid.GetNode("R" + (i + 1)); row.AddChild(column); } } } }