diff --git a/Scenes/Root.tscn b/Scenes/Root.tscn index aa1196e..654793f 100644 --- a/Scenes/Root.tscn +++ b/Scenes/Root.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=9 format=3 uid="uid://bjhmjrldq4lkt"] +[gd_scene load_steps=10 format=3 uid="uid://bjhmjrldq4lkt"] [ext_resource type="PackedScene" uid="uid://b3gx0bl43lku3" path="res://Scenes/Player.tscn" id="1_vby0g"] +[ext_resource type="PackedScene" uid="uid://dk8x2aeq1eevf" path="res://Scenes/UI/BootScreen.tscn" id="2_7aede"] [ext_resource type="PackedScene" uid="uid://bvll23f5ibd4v" path="res://Scenes/UI/LaunchScreen.tscn" id="2_7o53i"] [ext_resource type="Script" path="res://Scripts/Launcher.cs" id="2_u5cms"] [ext_resource type="Script" path="res://Scripts/Logic/Scoreboard.cs" id="3_as2wg"] @@ -21,9 +22,14 @@ properties/1/replication_mode = 1 [node name="LaunchScreen" parent="." node_paths=PackedStringArray("Launcher") instance=ExtResource("2_7o53i")] Launcher = NodePath("../LauncherNode") -[node name="LauncherNode" type="Node" parent="." node_paths=PackedStringArray("World", "Overlay")] +[node name="BootScreen" parent="." node_paths=PackedStringArray("World") instance=ExtResource("2_7aede")] +visible = false +World = NodePath("../World") + +[node name="LauncherNode" type="Node" parent="." node_paths=PackedStringArray("World", "BootMenu", "Overlay")] script = ExtResource("2_u5cms") World = NodePath("../World") +BootMenu = NodePath("../BootScreen") Overlay = NodePath("../OverlayLayer") [node name="ScoreboardNode" type="Node" parent="."] @@ -32,6 +38,7 @@ script = ExtResource("3_as2wg") [node name="OverlayLayer" type="CanvasLayer" parent="."] [node name="Hud" parent="OverlayLayer" node_paths=PackedStringArray("World") instance=ExtResource("5_qvun1")] +visible = false World = NodePath("../../World") [node name="PlayerDiedScreen" parent="OverlayLayer" instance=ExtResource("5_pimes")] diff --git a/Scenes/UI/BootScreen.tscn b/Scenes/UI/BootScreen.tscn new file mode 100644 index 0000000..39fb90b --- /dev/null +++ b/Scenes/UI/BootScreen.tscn @@ -0,0 +1,49 @@ +[gd_scene load_steps=2 format=3 uid="uid://dk8x2aeq1eevf"] + +[ext_resource type="Script" path="res://Scripts/UI/BootScreen.cs" id="1_ajoaa"] + +[node name="BootScreen" type="Control" node_paths=PackedStringArray("StartGameButton", "PlayerNameInput", "PlayerCountLabel")] +process_mode = 3 +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1_ajoaa") +StartGameButton = NodePath("CenterContainer/VBoxContainer/StartButton") +PlayerNameInput = NodePath("CenterContainer/VBoxContainer/NameEdit") +PlayerCountLabel = NodePath("CenterContainer/VBoxContainer/PlayerCountLabel") + +[node name="CenterContainer" type="CenterContainer" parent="."] +layout_mode = 0 +offset_right = 1152.0 +offset_bottom = 648.0 + +[node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer"] +layout_mode = 2 + +[node name="PlayerCountLabel" type="Label" parent="CenterContainer/VBoxContainer"] +layout_mode = 2 +theme_override_font_sizes/font_size = 28 +text = "01/16" +horizontal_alignment = 1 + +[node name="PlayerCountHint" type="Label" parent="CenterContainer/VBoxContainer"] +layout_mode = 2 +text = "Waiting for Players" +horizontal_alignment = 1 + +[node name="Spacer" type="BoxContainer" parent="CenterContainer/VBoxContainer"] +custom_minimum_size = Vector2(2.08165e-12, 8) +layout_mode = 2 + +[node name="NameEdit" type="LineEdit" parent="CenterContainer/VBoxContainer"] +layout_mode = 2 +placeholder_text = "Player name" +alignment = 1 + +[node name="StartButton" type="Button" parent="CenterContainer/VBoxContainer"] +layout_mode = 2 +disabled = true +text = "Start the Game" diff --git a/Scenes/UI/LaunchScreen.tscn b/Scenes/UI/LaunchScreen.tscn index 48221a3..1c828e9 100644 --- a/Scenes/UI/LaunchScreen.tscn +++ b/Scenes/UI/LaunchScreen.tscn @@ -2,7 +2,7 @@ [ext_resource type="Script" path="res://Scripts/UI/LaunchScreen.cs" id="1_hiet3"] -[node name="LaunchScreen" type="Control" node_paths=PackedStringArray("PlayerNameInput", "ServerPortInput", "ServerAddrInput", "StartAsServerButton", "StartAsClientButton")] +[node name="LaunchScreen" type="Control" node_paths=PackedStringArray("ServerPortInput", "ServerAddrInput", "StartAsServerButton", "StartAsClientButton")] process_mode = 3 layout_mode = 3 anchors_preset = 15 @@ -11,7 +11,6 @@ anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 script = ExtResource("1_hiet3") -PlayerNameInput = NodePath("CenterContainer/VBoxContainer/NameEdit") ServerPortInput = NodePath("CenterContainer/VBoxContainer/PortEdit") ServerAddrInput = NodePath("CenterContainer/VBoxContainer/AddrEdit") StartAsServerButton = NodePath("CenterContainer/VBoxContainer/MultiplayerActions/HostButton") @@ -31,9 +30,9 @@ text = "AceField Prototype ยท Closed test" horizontal_alignment = 1 -[node name="NameEdit" type="LineEdit" parent="CenterContainer/VBoxContainer"] +[node name="Spacer" type="BoxContainer" parent="CenterContainer/VBoxContainer"] +custom_minimum_size = Vector2(2.08165e-12, 16) layout_mode = 2 -placeholder_text = "Player name" [node name="PlayTitle" type="Label" parent="CenterContainer/VBoxContainer"] layout_mode = 2 diff --git a/Scripts/Launcher.cs b/Scripts/Launcher.cs index 205ea1f..83ea1f0 100644 --- a/Scripts/Launcher.cs +++ b/Scripts/Launcher.cs @@ -1,4 +1,5 @@ using AceField.Scripts.Logic; +using AceField.Scripts.UI; using Godot; namespace AceField.Scripts; @@ -6,6 +7,9 @@ namespace AceField.Scripts; public partial class Launcher : Node { [Export] public World World; + + [Export] public BootScreen BootMenu; + [Export] public CanvasLayer Overlay; private void GameFreeze() @@ -20,6 +24,7 @@ public partial class Launcher : Node GetTree().Paused = false; World.Show(); Overlay.Show(); + BootMenu.StartGame -= GameUnfreeze; } public override void _Ready() @@ -33,13 +38,12 @@ public partial class Launcher : Node } } - public void StartAsSingle(string currentPlayerName = null) + public void StartAsSingle() { GameUnfreeze(); - World.StartGame(currentPlayerName); } - public bool StartAsServer(int port, string currentPlayerName = null) + public bool StartAsServer(int port) { var peer = new ENetMultiplayerPeer(); peer.CreateServer(port); @@ -53,13 +57,13 @@ public partial class Launcher : Node GD.Print("Running game as server..."); Multiplayer.MultiplayerPeer = peer; - GameUnfreeze(); - World.StartGame(currentPlayerName); + BootMenu.StartGame += GameUnfreeze; + BootMenu.Show(); return true; } - public bool StartAsClient(string addr, int port, string currentPlayerName = null) + public bool StartAsClient(string addr, int port) { if (string.IsNullOrEmpty(addr)) return false; @@ -76,8 +80,8 @@ public partial class Launcher : Node GD.Print("Running game as client..."); Multiplayer.MultiplayerPeer = peer; - GameUnfreeze(); - World.StartGame(currentPlayerName); + BootMenu.StartGame += GameUnfreeze; + BootMenu.Show(); return true; } diff --git a/Scripts/Logic/World.cs b/Scripts/Logic/World.cs index 303d20f..bde4be9 100644 --- a/Scripts/Logic/World.cs +++ b/Scripts/Logic/World.cs @@ -27,6 +27,15 @@ public partial class World : Node2D AddChild(_roundTimer); _roundTimer.Start(); + // Add players into the game + PutPlayers(currentPlayerName); + } + + public override void _Ready() + { + if (!Multiplayer.IsServer()) + return; + // Handling player connect / disconnect after this client connected Multiplayer.PeerDisconnected += RemovePlayer_Adaptor; Multiplayer.PeerConnected += AddPlayer_Adaptor; @@ -37,10 +46,7 @@ public partial class World : Node2D // Add this client as a player if client isn't a dedicated server if (!OS.HasFeature("dedicated_server")) - Scoreboard.AddPlayer(1, currentPlayerName); - - // Add players into the game - PutPlayers(currentPlayerName); + Scoreboard.AddPlayer(1); } public override void _ExitTree() diff --git a/Scripts/UI/BootScreen.cs b/Scripts/UI/BootScreen.cs new file mode 100644 index 0000000..fd0811f --- /dev/null +++ b/Scripts/UI/BootScreen.cs @@ -0,0 +1,40 @@ +using AceField.Scripts.Logic; +using Godot; + +namespace AceField.Scripts.UI; + +public partial class BootScreen : Control +{ + [Export] public World World; + + [Export] public Button StartGameButton; + [Export] public LineEdit PlayerNameInput; + [Export] public Label PlayerCountLabel; + + [Signal] + public delegate void StartGameEventHandler(); + + public override void _Ready() + { + StartGameButton.Pressed += () => { Rpc(nameof(Start)); }; + } + + public override void _Process(double delta) + { + var count = Multiplayer.GetPeers().Length; + PlayerCountLabel.Text = $"{(count + 1):00}/{16}"; + + StartGameButton.Disabled = !Multiplayer.IsServer(); + } + + [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] + private void Start() + { + EmitSignal(SignalName.StartGame); + var name = string.IsNullOrEmpty(PlayerNameInput.Text) ? null : PlayerNameInput.Text; + // TODO Fix this I don't know why the first round player's name won't fully apply + World.Scoreboard.SetName(Multiplayer.GetUniqueId(), name); + World.StartGame(currentPlayerName: name); + Hide(); + } +} diff --git a/Scripts/UI/LaunchScreen.cs b/Scripts/UI/LaunchScreen.cs index 5c547d1..876fb31 100644 --- a/Scripts/UI/LaunchScreen.cs +++ b/Scripts/UI/LaunchScreen.cs @@ -8,8 +8,7 @@ public partial class LaunchScreen : Control [Export] public string DefaultServerAddr = "127.0.0.1"; [Export] public Launcher Launcher; - - [Export] public LineEdit PlayerNameInput; + [Export] public LineEdit ServerPortInput; [Export] public LineEdit ServerAddrInput; [Export] public Button StartAsServerButton; @@ -51,8 +50,7 @@ public partial class LaunchScreen : Control } var port = ServerPortInput.Text; - var name = string.IsNullOrEmpty(PlayerNameInput.Text) ? null : PlayerNameInput.Text; - var result = Launcher.StartAsServer(int.Parse(port), currentPlayerName: name); + var result = Launcher.StartAsServer(int.Parse(port)); if (result) Hide(); @@ -67,8 +65,7 @@ public partial class LaunchScreen : Control var addr = ServerAddrInput.Text; var port = ServerPortInput.Text; - var name = string.IsNullOrEmpty(PlayerNameInput.Text) ? null : PlayerNameInput.Text; - var result = Launcher.StartAsClient(addr, int.Parse(port), currentPlayerName: name); + var result = Launcher.StartAsClient(addr, int.Parse(port)); if (result) Hide();