Fight happening base setup
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user