mirror of
https://codeberg.org/Ikatono/TierMaker.git
synced 2025-10-28 20:45:35 -05:00
265 lines
6.2 KiB
C#
265 lines
6.2 KiB
C#
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();
|
|
}
|
|
}
|
|
public void SetCardSize(Vector2 size)
|
|
{
|
|
CardSize = size;
|
|
}
|
|
protected void PropogateCardSize()
|
|
{
|
|
foreach (var r in GetNode("%RowContainer").GetChildren().OfType<row>())
|
|
r.CardSize = _CardSize;
|
|
foreach (var c in GetNode("%UnassignedCardContainer").GetChildren().OfType<card>())
|
|
c.CustomMinimumSize = _CardSize;
|
|
SetContainerMinima();
|
|
}
|
|
public IEnumerable<row> Rows
|
|
=> GetNode<VBoxContainer>("%RowContainer").GetChildren().OfType<row>();
|
|
// Called when the node enters the scene tree for the first time.
|
|
public override void _Ready()
|
|
{
|
|
var rows = this.GetAllDescendents<row>().ToArray();
|
|
for (int i = 0; i < 50; i++)
|
|
{
|
|
var c = card.MakeCard(GetTree());
|
|
GD.Print(c.CardId);
|
|
c.CardName = $"Card {c.CardId}";
|
|
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<VBoxContainer>("%RowContainer");
|
|
node.CustomMinimumSize = new Vector2(0, _CardSize.Y * node.GetChildCount());
|
|
}
|
|
public row GetRowById(string id)
|
|
=> GetTree().GetNodesInGroup("RowGroup").OfType<row>().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<VBoxContainer>("%RowContainer");
|
|
node.AddChild(row);
|
|
node.MoveChild(row, 0);
|
|
}
|
|
SetContainerMinima();
|
|
}
|
|
public void RemoveRow(string id)
|
|
{
|
|
var r = GetRowById(id);
|
|
GetNode<VBoxContainer>("%RowContainer").RemoveChild(r);
|
|
SetContainerMinima();
|
|
}
|
|
public void AddUnassignedCard(card c)
|
|
{
|
|
c.CustomMinimumSize = _CardSize;
|
|
var node = GetNode<HFlowContainer>("%UnassignedCardContainer");
|
|
node.AddChild(c);
|
|
}
|
|
public card RemoveUnassignedCard(string id)
|
|
{
|
|
var node = GetNode<HFlowContainer>("%UnassignedCardContainer");
|
|
var c = node.GetAllDescendents<card>().FirstOrDefault(x => x.CardId == id);
|
|
if (c is not null)
|
|
{
|
|
node.RemoveChild(c);
|
|
}
|
|
return c;
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="target_row">null to unassign card</param>
|
|
/// <returns>false if something goes wrong</returns>
|
|
// 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);
|
|
}
|
|
public card FindCard(string id)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(id))
|
|
return null;
|
|
foreach (var c in this.GetAllDescendents<card>())
|
|
if (c.CardId == id)
|
|
return c;
|
|
return null;
|
|
}
|
|
public row FindRow(string id)
|
|
{
|
|
foreach (var r in this.GetAllDescendents<row>())
|
|
if (r.RowId == id)
|
|
return r;
|
|
return null;
|
|
}
|
|
|
|
#region Commands
|
|
public void MoveCard(string cardId, string targetRowId, int? toIndex = null)
|
|
{
|
|
var r = FindRow(targetRowId);
|
|
if (r is null)
|
|
throw new Exception($"row {r.RowId} not found");
|
|
var c = ClaimCard(cardId);
|
|
if (c is null)
|
|
throw new Exception($"card {c.CardId} not found");
|
|
r.AddCard(c, toIndex);
|
|
}
|
|
public void MoveRow(string rowId, int toIndex)
|
|
{
|
|
var r = FindRow(rowId);
|
|
if (r is null)
|
|
throw new Exception($"row {r.RowId} not found");
|
|
//TODO what if out of range?
|
|
r.GetParent().MoveChild(r, toIndex);
|
|
}
|
|
public void DeleteCards(params string[] cardId)
|
|
{
|
|
//don't claim any cards unless all of them are found
|
|
if (!cardId.Select(FindCard).All(x => x is not null))
|
|
throw new Exception("not all cards found");
|
|
foreach (var c in cardId.Select(ClaimCard))
|
|
{
|
|
c.QueueFree();
|
|
}
|
|
}
|
|
public void DeleteRow(string rowId, bool deleteCards = false)
|
|
{
|
|
var r = FindRow(rowId);
|
|
if (r is null)
|
|
throw new Exception($"row {r.RowId} not found");
|
|
}
|
|
public void CreateCard(string title = null, Image image = null)
|
|
{
|
|
var scn = GD.Load<PackedScene>("res://card.tscn");
|
|
var c = scn.Instantiate() as card;
|
|
if (!string.IsNullOrWhiteSpace(title))
|
|
c.CardName = title;
|
|
if (image is not null)
|
|
c.SetTexture(ImageTexture.CreateFromImage(image));
|
|
AddUnassignedCard(c);
|
|
}
|
|
public void CreateRow(Color? color = null, string title = null)
|
|
{
|
|
var scn = GD.Load<PackedScene>("res://row.tscn");
|
|
var r = scn.Instantiate() as row;
|
|
if (!string.IsNullOrWhiteSpace(title))
|
|
r.RowText = title;
|
|
if (color is Color color1)
|
|
r.RowColor = color1;
|
|
}
|
|
public void RenameCard(string cardId, string newName)
|
|
{
|
|
|
|
}
|
|
public void RenameRow(string rowId, string newTitle)
|
|
{
|
|
|
|
}
|
|
public void RecolorRow(string rowId, Color color)
|
|
{
|
|
|
|
}
|
|
public void ChangeCardImage(string cardId, Image image)
|
|
{
|
|
var c = FindCard(cardId);
|
|
if (c is null)
|
|
throw new Exception($"card {c.CardId} not found");
|
|
c.SetTexture(ImageTexture.CreateFromImage(image));
|
|
}
|
|
#endregion //Commands
|
|
}
|