mirror of
https://codeberg.org/Ikatono/TierMaker.git
synced 2025-10-28 20:45:35 -05:00
forgot to push for a while, got drag-and-drop mostly working
This commit is contained in:
169
game.cs
Normal file
169
game.cs
Normal file
@@ -0,0 +1,169 @@
|
||||
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<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 {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<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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user