From 719b1fa8000551d66ab13f1079e26d89b3f94fa6 Mon Sep 17 00:00:00 2001 From: jonathan Date: Tue, 7 Oct 2025 15:29:39 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8FCode=20cleanup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prefabs/fight/fight_scene_switcher.tscn | 8 +-- scenes/Babushka_scene_fight_happening.tscn | 4 +- scenes/Babushka_scene_fight_world_room.tscn | 3 +- .../CSharp/Common/Fight/FightSceneSwitcher.cs | 17 ++++--- scripts/CSharp/Common/Fight/FighterStack.cs | 50 ++++++++++--------- 5 files changed, 43 insertions(+), 39 deletions(-) diff --git a/prefabs/fight/fight_scene_switcher.tscn b/prefabs/fight/fight_scene_switcher.tscn index 7a89541..0449045 100644 --- a/prefabs/fight/fight_scene_switcher.tscn +++ b/prefabs/fight/fight_scene_switcher.tscn @@ -2,8 +2,8 @@ [ext_resource type="Script" uid="uid://cql8mt5jsmcdl" path="res://scripts/CSharp/Common/Fight/FightSceneSwitcher.cs" id="1_5dt1r"] -[node name="FightSceneSwitcher" type="Node" node_paths=PackedStringArray("sceneRoot")] +[node name="FightSceneSwitcher" type="Node" node_paths=PackedStringArray("_sceneRoot")] script = ExtResource("1_5dt1r") -sceneRoot = NodePath("") -fightRoomScenePath = "res://scenes/Babushka_scene_fight_world_room.tscn" -fightHappeningScene = "res://scenes/Babushka_scene_fight_happening.tscn" +_sceneRoot = NodePath("") +_fightRoomScenePath = "res://scenes/Babushka_scene_fight_world_room.tscn" +_fightHappeningScene = "res://scenes/Babushka_scene_fight_happening.tscn" diff --git a/scenes/Babushka_scene_fight_happening.tscn b/scenes/Babushka_scene_fight_happening.tscn index b8ce694..0ebaa51 100644 --- a/scenes/Babushka_scene_fight_happening.tscn +++ b/scenes/Babushka_scene_fight_happening.tscn @@ -59,8 +59,8 @@ visible = false script = ExtResource("10_qqd8u") _fightSceneSwitcher = NodePath("FightSceneSwitcher") -[node name="FightSceneSwitcher" parent="SwitchSceneOnFightEnd" node_paths=PackedStringArray("sceneRoot") instance=ExtResource("2_phrlx")] -sceneRoot = NodePath("../..") +[node name="FightSceneSwitcher" parent="SwitchSceneOnFightEnd" node_paths=PackedStringArray("_sceneRoot") instance=ExtResource("2_phrlx")] +_sceneRoot = NodePath("../..") [node name="ActionSelect" type="CanvasLayer" parent="." node_paths=PackedStringArray("_attackActionButton", "_summonActionButton", "_talkActionButton", "_fleeActionButton")] visible = false diff --git a/scenes/Babushka_scene_fight_world_room.tscn b/scenes/Babushka_scene_fight_world_room.tscn index f7bfc7f..5665420 100644 --- a/scenes/Babushka_scene_fight_world_room.tscn +++ b/scenes/Babushka_scene_fight_world_room.tscn @@ -2148,8 +2148,9 @@ position = Vector2(1560, 422) [node name="Spawn4" type="Node2D" parent="YSorted/EnemyGroupSpawns"] position = Vector2(-1127, 671) -[node name="FightSceneSwitcher" parent="." instance=ExtResource("40_elhbh")] +[node name="FightSceneSwitcher" parent="." node_paths=PackedStringArray("_sceneRoot") instance=ExtResource("40_elhbh")] unique_name_in_owner = true +_sceneRoot = NodePath("..") [node name="FightSceneSetup" type="Node" parent="." node_paths=PackedStringArray("_enemyGroupSpawns", "_fightSceneSwitcher")] script = ExtResource("40_cvg1r") diff --git a/scripts/CSharp/Common/Fight/FightSceneSwitcher.cs b/scripts/CSharp/Common/Fight/FightSceneSwitcher.cs index 0a84d4b..2bc99ca 100644 --- a/scripts/CSharp/Common/Fight/FightSceneSwitcher.cs +++ b/scripts/CSharp/Common/Fight/FightSceneSwitcher.cs @@ -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"); diff --git a/scripts/CSharp/Common/Fight/FighterStack.cs b/scripts/CSharp/Common/Fight/FighterStack.cs index aceb67e..8d2049e 100644 --- a/scripts/CSharp/Common/Fight/FighterStack.cs +++ b/scripts/CSharp/Common/Fight/FighterStack.cs @@ -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; }