mirror of
https://codeberg.org/Ikatono/TierMaker.git
synced 2025-10-28 20:45:35 -05:00
232 lines
5.8 KiB
C#
232 lines
5.8 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 < 20; i++)
|
|
{
|
|
var c = card.MakeCard(GetTree());
|
|
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.IsActionPressed("OpenMenu"))
|
|
{
|
|
GetNode<settings_popup>("%SettingsPopup").Visible = true;
|
|
GetViewport().SetInputAsHandled();
|
|
}
|
|
}
|
|
// 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;
|
|
}
|
|
public void EditCardInMenu(card c)
|
|
{
|
|
GetNode<card_edit_popup>("%CardEditPopup").ActivateForCard(c);
|
|
}
|
|
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");
|
|
if (!deleteCards)
|
|
{
|
|
var unassigned = GetNode("%UnassignedCardContainer");
|
|
foreach (var c in r.Cards.ToArray())
|
|
c.Reparent(unassigned);
|
|
}
|
|
//GetNode("%RowContainer").RemoveChild(r);
|
|
r.QueueFree();
|
|
}
|
|
public void CreateCard(string title = null, ImageWithMetadata image = null)
|
|
{
|
|
var c = card.MakeCard(GetTree());
|
|
if (!string.IsNullOrWhiteSpace(title))
|
|
c.CardName = title;
|
|
if (image is not null)
|
|
{
|
|
c.SetTexture(ImageTexture.CreateFromImage(image.Image));
|
|
c.SetStretchMode(image.StretchMode);
|
|
}
|
|
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;
|
|
AddRow(r);
|
|
}
|
|
public void RenameCard(string cardId, string newName)
|
|
{
|
|
FindCard(cardId).CardName = newName;
|
|
}
|
|
public void RenameRow(string rowId, string newTitle)
|
|
{
|
|
GetRowById(rowId).RowText = newTitle;
|
|
}
|
|
public void RecolorRow(string rowId, Color color)
|
|
{
|
|
GetRowById(rowId).RowColor = color;
|
|
}
|
|
public void ChangeCardImage(string cardId, ImageWithMetadata image)
|
|
{
|
|
var c = FindCard(cardId);
|
|
if (c is null)
|
|
throw new Exception($"card {c.CardId} not found");
|
|
c.SetTexture(ImageTexture.CreateFromImage(image.Image));
|
|
c.SetStretchMode(image.StretchMode);
|
|
}
|
|
#endregion //Commands
|
|
}
|