Files
hotdesking/Gameplay/Desk.cs

79 lines
1.9 KiB
C#

using Godot;
using System;
using System.Collections.Generic;
public partial class Desk : Node2D
{
public Vector2 _topLeft, _cellDimensions, _gridDimensions;
public Cell _light, _dark;
Node2D _grid;
public override void _Ready()
{
_grid = GetNode<Node2D>("Cells");
_light = GetNode<Cell>("Light");
_dark = GetNode<Cell>("Dark");
}
public Cell GetCellFromAddress(int ROW, int COLUMN)
{
if (_grid.HasNode("R" + ROW))
{
Node2D row = _grid.GetNode<Node2D>("R" + ROW);
if (row.HasNode("C" + COLUMN))
{
Cell column = row.GetNode<Cell>("C" + COLUMN);
return column;
}
}
return null;
}
public Vector2 GetPositionFromAddress(int ROW, int COLUMN)
{
Cell cell = GetCellFromAddress(ROW, COLUMN);
if (cell != null)
{
return cell.GlobalPosition;
}
return new Vector2(-1, -1);
}
public void Setup(int ROWS, int COLUMNS, int CELLSIZE)
{
_gridDimensions = new Vector2(COLUMNS, ROWS);
_cellDimensions = new Vector2(CELLSIZE, CELLSIZE);
_light.Scale = _cellDimensions / _light.Texture.GetSize();
_dark.Scale = _cellDimensions / _dark.Texture.GetSize();
_topLeft = Globals.Instance._screenCenter - _cellDimensions * _gridDimensions / 2 + _cellDimensions / 2;
Vector2 position;
Node2D row;
Cell 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 = (Cell)((i + j) % 2 == 0 ? _light : _dark).Duplicate();
column._size = (int)(column.Texture.GetSize().Y * column.Scale.Y);
column.Name = "C" + (j + 1);
column.GlobalPosition = position;
column.Visible = true;
column._row = i + 1;
column._column = j + 1;
row = _grid.GetNode<Node2D>("R" + (i + 1));
row.AddChild(column);
}
}
}
}