Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Commit 534d854

Browse files
Encapsulate Notifier
1 parent 411864e commit 534d854

5 files changed

Lines changed: 61 additions & 53 deletions

File tree

Scenes/Main.tscn

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[ext_resource path="res://Scripts/Netcode/Common/Utils/NetworkManager.cs" type="Script" id=4]
77
[ext_resource path="res://Scripts/Scenes/Main/SceneManager.cs" type="Script" id=5]
88
[ext_resource path="res://Themes/Main.tres" type="Theme" id=6]
9-
[ext_resource path="res://Scripts/Scenes/Main/ErrorNotifier.cs" type="Script" id=7]
9+
[ext_resource path="res://Scripts/Scenes/Main/Notifier.cs" type="Script" id=7]
1010
[ext_resource path="res://Scripts/Msc/UIGameConsole.cs" type="Script" id=8]
1111
[ext_resource path="res://Scenes/Prefabs/ServerSimulation.tscn" type="PackedScene" id=9]
1212

@@ -20,8 +20,11 @@
2020
bg_color = Color( 0, 0, 0, 0.784314 )
2121

2222
[node name="Main" type="Node"]
23+
24+
[node name="GameManager" type="Node" parent="."]
2325
script = ExtResource( 1 )
24-
NodePathGameConsole = NodePath("CanvasLayer/Game Console")
26+
NodePathGameConsole = NodePath("../CanvasLayer/Game Console")
27+
NodePathNotifier = NodePath("../Notifier")
2528

2629
[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."]
2730
volume_db = -10.0
@@ -37,7 +40,7 @@ NodePathServerSimulation = NodePath("../CanvasLayer/Server Simulation/ViewportCo
3740
[node name="SceneManager" type="Node" parent="."]
3841
script = ExtResource( 5 )
3942

40-
[node name="ErrorNotifier" type="Node" parent="."]
43+
[node name="Notifier" type="Node" parent="."]
4144
script = ExtResource( 7 )
4245

4346
[node name="CanvasLayer" type="CanvasLayer" parent="."]

Scripts/Netcode/Common/Utils/Logger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void Dequeue()
9090
Console.ResetColor();
9191

9292
#if CLIENT
93-
ErrorNotifier.IncrementErrorCount();
93+
GM.IncrementErrorCount();
9494
GM.LogConsoleMessage(errorText);
9595
#endif
9696
break;

Scripts/Scenes/Main/ErrorNotifier.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

Scripts/Scenes/Main/GM.cs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace GodotModules
99
public class GM : Node
1010
{
1111
[Export] public readonly NodePath NodePathGameConsole;
12+
[Export] public readonly NodePath NodePathNotifier;
1213

1314
public static ModLoader ModLoader { get; set; }
1415
public static string GameName = "Godot Modules";
@@ -18,13 +19,16 @@ public class GM : Node
1819
private static UIGameConsole GameConsole { get; set; }
1920
private static GodotCommands GodotCommands { get; set; }
2021
private static Logger Logger { get; set; }
22+
private static Notifier Notifier { get; set; }
23+
private static GTimer TimerNotifyErrors { get; set; }
2124

2225
public override void _Ready()
2326
{
2427
PersistentTree = GetTree();
2528
GameConsole = GetNode<UIGameConsole>(NodePathGameConsole);
2629
GodotCommands = new GodotCommands();
2730
Logger = new Logger();
31+
Notifier = GetNode<Notifier>(NodePathNotifier);
2832
}
2933

3034
public override async void _Process(float delta)
@@ -37,32 +41,17 @@ public override async void _Process(float delta)
3741
public override void _Input(InputEvent @event)
3842
{
3943
if (Input.IsActionJustPressed("ui_debug"))
40-
{
4144
GameConsole.ToggleVisibility();
42-
}
4345

4446
if (Input.IsActionJustPressed("ui_fullscreen"))
4547
UtilOptions.ToggleFullscreen();
4648
}
4749

4850
public static string GetGameDataPath() => System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), GM.GameName);
4951

50-
public static void SpawnPopupMessage(string message)
51-
{
52-
var popupMessage = Prefabs.PopupMessage.Instance<UIPopupMessage>();
53-
PersistentTree.CurrentScene.AddChild(popupMessage);
54-
popupMessage.Init(message);
55-
popupMessage.PopupCentered();
56-
}
57-
58-
public static void SpawnPopupError(Exception e)
59-
{
60-
ErrorNotifier.IncrementErrorCount();
61-
var popupError = Prefabs.PopupError.Instance<UIPopupError>();
62-
PersistentTree.CurrentScene.AddChild(popupError);
63-
popupError.Init(e.Message, e.StackTrace);
64-
popupError.PopupCentered();
65-
}
52+
public static void SpawnPopupMessage(string message) => Notifier.SpawnPopupMessage(message);
53+
public static void SpawnPopupError(Exception e) => Notifier.SpawnPopupError(e);
54+
public static void IncrementErrorCount() => Notifier.IncrementErrorCount();
6655

6756
public static void LogConsoleMessage(string message) => GameConsole.AddMessage(message);
6857
public static void ToggleConsoleVisibility() => GameConsole.ToggleVisibility();

Scripts/Scenes/Main/Notifier.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Godot;
2+
using System;
3+
4+
namespace GodotModules
5+
{
6+
public class Notifier : Node
7+
{
8+
private int _errorCount;
9+
10+
public override void _Ready()
11+
{
12+
var timer = new GTimer(1500);
13+
timer.Connect(this, nameof(SpawnErrorNotification));
14+
}
15+
16+
public void IncrementErrorCount() => _errorCount++;
17+
18+
public void SpawnErrorNotification()
19+
{
20+
if (_errorCount == 0)
21+
return;
22+
23+
var notifyError = Prefabs.NotifyError.Instance<UINotifyError>();
24+
notifyError.Count = _errorCount;
25+
GM.PersistentTree.CurrentScene.AddChild(notifyError);
26+
27+
_errorCount = 0;
28+
}
29+
30+
public void SpawnPopupMessage(string message)
31+
{
32+
var popupMessage = Prefabs.PopupMessage.Instance<UIPopupMessage>();
33+
GM.PersistentTree.CurrentScene.AddChild(popupMessage);
34+
popupMessage.Init(message);
35+
popupMessage.PopupCentered();
36+
}
37+
38+
public void SpawnPopupError(Exception e)
39+
{
40+
IncrementErrorCount();
41+
var popupError = Prefabs.PopupError.Instance<UIPopupError>();
42+
GM.PersistentTree.CurrentScene.AddChild(popupError);
43+
popupError.Init(e.Message, e.StackTrace);
44+
popupError.PopupCentered();
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)