From 33c6762ed365f866039a89edfd113defa4604826 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 9 Aug 2024 15:45:40 +0800 Subject: [PATCH] :sparkles: End game --- Scenes/Root.tscn | 19 +++++++++---- Scenes/UI/GameOverScreen.tscn | 50 +++++++++++++++++++++++++++++++++ Scenes/UI/PlayerDiedScreen.tscn | 21 ++++++++++++-- Scripts/Logic/World.cs | 15 ++++++---- Scripts/UI/BootScreen.cs | 3 +- Scripts/UI/GameOverScreen.cs | 29 +++++++++++++++++++ Scripts/UI/HUD.cs | 2 +- Scripts/UI/Leaderboard.cs | 18 +++++++----- 8 files changed, 134 insertions(+), 23 deletions(-) create mode 100644 Scenes/UI/GameOverScreen.tscn create mode 100644 Scripts/UI/GameOverScreen.cs diff --git a/Scenes/Root.tscn b/Scenes/Root.tscn index d8d7c53..0c4dc0a 100644 --- a/Scenes/Root.tscn +++ b/Scenes/Root.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=11 format=3 uid="uid://bjhmjrldq4lkt"] +[gd_scene load_steps=12 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"] @@ -6,6 +6,7 @@ [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"] [ext_resource type="Script" path="res://Scripts/Logic/World.cs" id="3_xwguj"] +[ext_resource type="PackedScene" uid="uid://cnf80yioktsvj" path="res://Scenes/UI/GameOverScreen.tscn" id="5_441wx"] [ext_resource type="PackedScene" uid="uid://bb704b0kpwwkr" path="res://Scenes/UI/PlayerDiedScreen.tscn" id="5_pimes"] [ext_resource type="PackedScene" uid="uid://c7w5sgq0bshk0" path="res://Scenes/UI/HUD.tscn" id="5_qvun1"] [ext_resource type="PackedScene" uid="uid://dfoy57v3q4of5" path="res://Scenes/UI/Leaderboard.tscn" id="7_j24m7"] @@ -17,6 +18,9 @@ properties/0/replication_mode = 1 properties/1/path = NodePath(".:RoundTimeLeft") properties/1/spawn = true properties/1/replication_mode = 1 +properties/2/path = NodePath(".:RoundCount") +properties/2/spawn = true +properties/2/replication_mode = 1 [node name="Node" type="Node"] @@ -38,7 +42,16 @@ script = ExtResource("3_as2wg") [node name="OverlayLayer" type="CanvasLayer" parent="."] +[node name="PlayerDiedScreen" parent="OverlayLayer" instance=ExtResource("5_pimes")] +visible = false + +[node name="GameOverScreen" parent="OverlayLayer" node_paths=PackedStringArray("Scoreboard") instance=ExtResource("5_441wx")] +process_mode = 3 +visible = false +Scoreboard = NodePath("../../ScoreboardNode") + [node name="Leaderboard" parent="OverlayLayer" node_paths=PackedStringArray("Scoreboard") instance=ExtResource("7_j24m7")] +process_mode = 3 visible = false Scoreboard = NodePath("../../ScoreboardNode") @@ -46,14 +59,10 @@ Scoreboard = NodePath("../../ScoreboardNode") visible = false World = NodePath("../../World") -[node name="PlayerDiedScreen" parent="OverlayLayer" instance=ExtResource("5_pimes")] -visible = false - [node name="World" type="Node2D" parent="." node_paths=PackedStringArray("Scoreboard")] script = ExtResource("3_xwguj") Scoreboard = NodePath("../ScoreboardNode") PlayerScene = ExtResource("1_vby0g") -RoundDuration = 10.0 [node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."] root_path = NodePath("../World") diff --git a/Scenes/UI/GameOverScreen.tscn b/Scenes/UI/GameOverScreen.tscn new file mode 100644 index 0000000..35c842e --- /dev/null +++ b/Scenes/UI/GameOverScreen.tscn @@ -0,0 +1,50 @@ +[gd_scene load_steps=2 format=3 uid="uid://cnf80yioktsvj"] + +[ext_resource type="Script" path="res://Scripts/UI/GameOverScreen.cs" id="1_j12ja"] + +[node name="GameOverScreen" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1_j12ja") + +[node name="CenterContainer" type="CenterContainer" parent="."] +offset_right = 1152.0 +offset_bottom = 648.0 + +[node name="Panel" type="Panel" parent="CenterContainer"] +custom_minimum_size = Vector2(340, 180) +layout_mode = 2 + +[node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer/Panel"] +layout_mode = 1 +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -129.5 +offset_top = -46.5 +offset_right = 129.5 +offset_bottom = 46.5 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="Title" type="Label" parent="CenterContainer/Panel/VBoxContainer"] +layout_mode = 2 +theme_override_font_sizes/font_size = 28 +text = "Game over" +horizontal_alignment = 1 + +[node name="Caption" type="Label" parent="CenterContainer/Panel/VBoxContainer"] +layout_mode = 2 +text = "You're in the 1st place" +horizontal_alignment = 1 + +[node name="Caption2" type="Label" parent="CenterContainer/Panel/VBoxContainer"] +layout_mode = 2 +text = "Hold Tab key to view leaderboard" +horizontal_alignment = 1 diff --git a/Scenes/UI/PlayerDiedScreen.tscn b/Scenes/UI/PlayerDiedScreen.tscn index 3a7cf6e..2854b31 100644 --- a/Scenes/UI/PlayerDiedScreen.tscn +++ b/Scenes/UI/PlayerDiedScreen.tscn @@ -13,16 +13,31 @@ layout_mode = 0 offset_right = 1152.0 offset_bottom = 648.0 -[node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer"] +[node name="Panel" type="Panel" parent="CenterContainer"] +custom_minimum_size = Vector2(340, 120) layout_mode = 2 -[node name="Title" type="Label" parent="CenterContainer/VBoxContainer"] +[node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer/Panel"] +layout_mode = 1 +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -92.0 +offset_top = -33.0 +offset_right = 92.0 +offset_bottom = 33.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="Title" type="Label" parent="CenterContainer/Panel/VBoxContainer"] layout_mode = 2 theme_override_font_sizes/font_size = 28 text = "You died" horizontal_alignment = 1 -[node name="Caption" type="Label" parent="CenterContainer/VBoxContainer"] +[node name="Caption" type="Label" parent="CenterContainer/Panel/VBoxContainer"] layout_mode = 2 text = "Respawn in Next Round" horizontal_alignment = 1 diff --git a/Scripts/Logic/World.cs b/Scripts/Logic/World.cs index 053d920..cbf8b80 100644 --- a/Scripts/Logic/World.cs +++ b/Scripts/Logic/World.cs @@ -81,8 +81,7 @@ public partial class World : Node2D { var player = PlayerScene.Instantiate(); player.PlayerId = id; - var position = Vector2.FromAngle(GD.Randf() * 2 * Mathf.Pi); - player.Position = new Vector2(position.X * 5f * GD.Randf(), position.Y * 5f * GD.Randf()); + player.Position = new Vector2(GD.RandRange(-100, 100), GD.RandRange(-100, 100)); player.Name = BuildPlayerName(id); player.PlayerDied += (killerId) => { @@ -109,7 +108,7 @@ public partial class World : Node2D { if (RoundCount >= RoundTotalCount) { - // TODO End this game + Rpc(nameof(EndGame)); return; } @@ -129,9 +128,7 @@ public partial class World : Node2D foreach (var child in GetChildren()) { if (child is Player { IsCurrentPlayer: true } player) - { return player; - } } return null; @@ -147,4 +144,12 @@ public partial class World : Node2D return null; } + + [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] + private void EndGame() + { + GetParent().GetNode("OverlayLayer/Hud").Hide(); + GetParent().GetNode("OverlayLayer/GameOverScreen").Show(); + GetTree().Paused = true; + } } diff --git a/Scripts/UI/BootScreen.cs b/Scripts/UI/BootScreen.cs index f8ce002..3ead802 100644 --- a/Scripts/UI/BootScreen.cs +++ b/Scripts/UI/BootScreen.cs @@ -24,7 +24,7 @@ public partial class BootScreen : Control var count = Multiplayer.GetPeers().Length; PlayerCountLabel.Text = $"{(count + 1):00}/{16}"; - StartGameButton.Disabled = !Multiplayer.IsServer(); + StartGameButton.Disabled = !Multiplayer.IsServer() || Multiplayer.GetPeers().Length == 0; } [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] @@ -32,7 +32,6 @@ public partial class BootScreen : Control { 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 ?? $"Player#{Multiplayer.GetUniqueId()}"); World.StartGame(currentPlayerName: name); Hide(); diff --git a/Scripts/UI/GameOverScreen.cs b/Scripts/UI/GameOverScreen.cs new file mode 100644 index 0000000..bd443a6 --- /dev/null +++ b/Scripts/UI/GameOverScreen.cs @@ -0,0 +1,29 @@ +using System.Linq; +using AceField.Scripts.Logic; +using Godot; + +namespace AceField.Scripts.UI; + +public partial class GameOverScreen : Control +{ + [Export] public Scoreboard Scoreboard; + + public override void _Process(double delta) + { + if (!Visible) return; + + var data = Scoreboard.GetData(Multiplayer.GetUniqueId()); + if (data == null) return; + + var place = 1; + var list = Scoreboard.Players.ToList(); + list.Sort((a, b) => b.Value.Score.CompareTo(a.Value.Score)); + foreach (var item in list) + { + if (Multiplayer.GetUniqueId() == item.Key) break; + place++; + } + + GetNode