♻️Code cleanup
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using Babushka.scripts.CSharp.Common.SceneManagement;
|
||||
using Godot;
|
||||
|
||||
@@ -7,9 +8,9 @@ namespace Babushka.scripts.CSharp.Common.Fight;
|
||||
|
||||
public partial class FightSceneSwitcher : Node
|
||||
{
|
||||
[Export] private Node sceneRoot;
|
||||
[Export] private string fightRoomScenePath;
|
||||
[Export] private string fightHappeningScene;
|
||||
[Export] private Node _sceneRoot = null!;
|
||||
[Export] private string _fightRoomScenePath = null!;
|
||||
[Export] private string _fightHappeningScene = null!;
|
||||
|
||||
private void LoadNext()
|
||||
{
|
||||
@@ -17,12 +18,12 @@ public partial class FightSceneSwitcher : Node
|
||||
Debug.Assert(nextRoom != null, "nextRoom!=null");
|
||||
var nextFightHappening = FightWorld.Instance.fightHappeningData;
|
||||
SceneTransitionThreaded.Instance.ChangeSceneToFile(nextFightHappening != null
|
||||
? fightHappeningScene
|
||||
: fightRoomScenePath);
|
||||
UnloadAfterDelay();
|
||||
? _fightHappeningScene
|
||||
: _fightRoomScenePath);
|
||||
_ = UnloadAfterDelay();
|
||||
}
|
||||
|
||||
private async void UnloadAfterDelay()
|
||||
private async Task UnloadAfterDelay()
|
||||
{
|
||||
await ToSignal(GetTree().CreateTimer(1.0f), "timeout"); // 1.0f seconds
|
||||
//sceneRoot.QueueFree();
|
||||
@@ -31,7 +32,7 @@ public partial class FightSceneSwitcher : Node
|
||||
public void SwitchRoom(int pathIndex)
|
||||
{
|
||||
Debug.Assert(FightWorld.Instance.currentRoom != null, "FightWorld.Instance.currentRoom!=null");
|
||||
|
||||
|
||||
if (!FightWorld.Instance.currentRoom.paths.TryGetValue(pathIndex, out var nextRoom))
|
||||
throw new Exception("Trying to go down a non-existent path");
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot.NativeInterop;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Fight;
|
||||
|
||||
@@ -7,37 +7,39 @@ public class FighterStack
|
||||
{
|
||||
private class Node
|
||||
{
|
||||
public Node next;
|
||||
public FightWorld.Fighter fighter;
|
||||
public required Node next;
|
||||
public required FightWorld.Fighter fighter;
|
||||
}
|
||||
|
||||
private Node? currentNode;
|
||||
private Node? _currentNode;
|
||||
|
||||
public FightWorld.Fighter Current => currentNode.fighter;
|
||||
public FightWorld.Fighter Current => _currentNode?.fighter ?? throw new InvalidOperationException("No current fighter");
|
||||
|
||||
public void Next()
|
||||
{
|
||||
currentNode = currentNode.next;
|
||||
Debug.Assert(_currentNode != null, "currentNode!=null");
|
||||
_currentNode = _currentNode.next;
|
||||
}
|
||||
|
||||
public FightWorld.Fighter PeekNext()
|
||||
{
|
||||
return currentNode.next.fighter;
|
||||
Debug.Assert(_currentNode != null, "currentNode!=null");
|
||||
return _currentNode.next.fighter;
|
||||
}
|
||||
|
||||
public void AddAsLast(FightWorld.Fighter value)
|
||||
{
|
||||
// if first node
|
||||
if (currentNode == null)
|
||||
if (_currentNode == null)
|
||||
{
|
||||
currentNode = new Node { fighter = value };
|
||||
currentNode.next = currentNode;
|
||||
_currentNode = new Node { fighter = value, next = null! };
|
||||
_currentNode.next = _currentNode;
|
||||
return;
|
||||
}
|
||||
|
||||
var newNode = new Node { fighter = value, next = currentNode };
|
||||
var node = currentNode;
|
||||
while (node.next != currentNode)
|
||||
var newNode = new Node { fighter = value, next = _currentNode };
|
||||
var node = _currentNode;
|
||||
while (node.next != _currentNode)
|
||||
{
|
||||
node = node.next;
|
||||
}
|
||||
@@ -48,47 +50,47 @@ public class FighterStack
|
||||
public void AddAsNext(FightWorld.Fighter value)
|
||||
{
|
||||
// if first node
|
||||
if (currentNode == null)
|
||||
if (_currentNode == null)
|
||||
{
|
||||
AddAsLast(value);
|
||||
return;
|
||||
}
|
||||
|
||||
var newNode = new Node { fighter = value, next = currentNode.next };
|
||||
currentNode.next = newNode;
|
||||
var newNode = new Node { fighter = value, next = _currentNode.next };
|
||||
_currentNode.next = newNode;
|
||||
}
|
||||
|
||||
public bool Remove(FightWorld.Fighter value)
|
||||
{
|
||||
if (currentNode == null) return false;
|
||||
if (_currentNode == null) return false;
|
||||
|
||||
// if only one node
|
||||
if (currentNode.next == currentNode)
|
||||
if (_currentNode.next == _currentNode)
|
||||
{
|
||||
if (currentNode.fighter == value)
|
||||
if (_currentNode.fighter == value)
|
||||
{
|
||||
currentNode = null;
|
||||
_currentNode = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
var node = currentNode;
|
||||
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
|
||||
// it will be implicitly deleted by loss of reference on the next Next() call
|
||||
|
||||
node.next = node.next.next;
|
||||
return true;
|
||||
}
|
||||
|
||||
node = node.next;
|
||||
} while (node != currentNode);
|
||||
} while (node != _currentNode);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user