You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.1 KiB
95 lines
2.1 KiB
using System.Collections.Generic;
|
|
using Godot.NativeInterop;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Fight;
|
|
|
|
public class FighterStack
|
|
{
|
|
private class Node
|
|
{
|
|
public Node next;
|
|
public FightWorld.Fighter fighter;
|
|
}
|
|
|
|
private Node? currentNode;
|
|
|
|
public FightWorld.Fighter Current => currentNode.fighter;
|
|
|
|
public void Next()
|
|
{
|
|
currentNode = currentNode.next;
|
|
}
|
|
|
|
public FightWorld.Fighter PeekNext()
|
|
{
|
|
return currentNode.next.fighter;
|
|
}
|
|
|
|
public void AddAsLast(FightWorld.Fighter value)
|
|
{
|
|
// if first node
|
|
if (currentNode == null)
|
|
{
|
|
currentNode = new Node { fighter = value };
|
|
currentNode.next = currentNode;
|
|
return;
|
|
}
|
|
|
|
var newNode = new Node { fighter = value, next = currentNode };
|
|
var node = currentNode;
|
|
while (node.next != currentNode)
|
|
{
|
|
node = node.next;
|
|
}
|
|
|
|
node.next = newNode;
|
|
}
|
|
|
|
public void AddAsNext(FightWorld.Fighter value)
|
|
{
|
|
// if first node
|
|
if (currentNode == null)
|
|
{
|
|
AddAsLast(value);
|
|
return;
|
|
}
|
|
|
|
var newNode = new Node { fighter = value, next = currentNode.next };
|
|
currentNode.next = newNode;
|
|
}
|
|
|
|
public bool Remove(FightWorld.Fighter value)
|
|
{
|
|
if (currentNode == null) return false;
|
|
|
|
// if only one node
|
|
if (currentNode.next == currentNode)
|
|
{
|
|
if (currentNode.fighter == value)
|
|
{
|
|
currentNode = null;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
var node = currentNode;
|
|
do
|
|
{
|
|
// next is the fighter to remove
|
|
if (node.next.fighter == value)
|
|
{
|
|
// if removing current, keep current
|
|
// it will be implicitly deleted on the next Next() call
|
|
|
|
node.next = node.next.next;
|
|
return true;
|
|
}
|
|
|
|
node = node.next;
|
|
} while (node != currentNode);
|
|
|
|
return false;
|
|
}
|
|
} |