using Godot; using System; using System.Collections; using System.Collections.Generic; using System.Linq; public partial class game : Control { [Export] private Vector2 _CardSize = new(200, 200); public Vector2 CardSize { get => _CardSize; set { _CardSize = value; if (IsNodeReady()) PropogateCardSize(); } } protected void PropogateCardSize() { foreach (var r in GetNode("%RowContainer").GetChildren().OfType()) r.CardSize = _CardSize; foreach (var c in GetNode("%UnassignedCardContainer").GetChildren().OfType()) c.CustomMinimumSize = _CardSize; SetContainerMinima(); } public IEnumerable Rows => GetNode("%RowContainer").GetChildren().OfType(); // Called when the node enters the scene tree for the first time. public override void _Ready() { var rows = this.GetAllDescendents().ToArray(); for (int i = 0; i < 50; i++) { var c = card.MakeCard(GetTree()); GD.Print(c.CardId); c.CardName = $"Card {i}"; if (GD.RandRange(0, 1) == 1) { //add to a row var r = rows[GD.RandRange(0, rows.Length - 1)]; r.AddCard(c); } else { AddUnassignedCard(c); } } PropogateCardSize(); } // public override void _UnhandledInput(InputEvent @event) // { // if (@event is InputEventMouseButton iemb) // { // if (iemb.ButtonIndex == MouseButton.Right) // { // } // } // else if (@event is InputEventKey iek) // { // } // } // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(double delta) { } public void SetContainerMinima() { var node = GetNode("%RowContainer"); node.CustomMinimumSize = new Vector2(0, _CardSize.Y * node.GetChildCount()); } public row GetRowById(string id) => GetTree().GetNodesInGroup("RowGroup").OfType().FirstOrDefault(r => r.RowId == id); public void AddRow(row row, string after = null) { if (after is not null) { var r = GetRowById(after) ?? throw new Exception("row id does not exist"); r.AddSibling(row); } else { var node = GetNode("%RowContainer"); node.AddChild(row); node.MoveChild(row, 0); } SetContainerMinima(); } public void RemoveRow(string id) { var r = GetRowById(id); GetNode("%RowContainer").RemoveChild(r); SetContainerMinima(); } public void AddUnassignedCard(card c) { c.CustomMinimumSize = _CardSize; var node = GetNode("%UnassignedCardContainer"); node.AddChild(c); } public card RemoveUnassignedCard(string id) { var node = GetNode("%UnassignedCardContainer"); var c = node.GetAllDescendents().FirstOrDefault(x => x.CardId == id); if (c is not null) { node.RemoveChild(c); } return c; } /// /// /// /// /// null to unassign card /// false if something goes wrong // public bool MoveCard(string id, row target_row, float horizontal) // { // foreach (row r in Rows) // { // if (r.TryRemoveCard(id) is card c) // { // //add to row // if (target_row is not null) // { // target_row.AddCard(c); // return true; // } // //unassign // else // { // AddUnassignedCard(c); // return true; // } // } // } // if (RemoveUnassignedCard(id) is card cc) // { // //add to row // if (target_row is not null) // { // target_row.AddCard(cc); // return true; // } // //unassign // else // { // AddUnassignedCard(cc); // return true; // } // } // return false; // } public card ClaimCard(string id) { foreach (row r in Rows) { if (r.TryRemoveCard(id) is card c) return c; } return RemoveUnassignedCard(id); } }