Player name

This commit is contained in:
LittleSheep 2024-08-08 15:20:09 +08:00
parent f14a29d4e3
commit 91662a1c9f
8 changed files with 177 additions and 146 deletions

View File

@ -5,9 +5,15 @@
</component>
<component name="ChangeListManager">
<list default="true" id="c7bd02c7-bbb4-431f-81ca-d2f8b7b09b37" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/Scenes/UI/LaunchScreen.tscn" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.CodingLand/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.CodingLand/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Scenes/Player.tscn" beforeDir="false" afterPath="$PROJECT_DIR$/Scenes/Player.tscn" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Scenes/Root.tscn" beforeDir="false" afterPath="$PROJECT_DIR$/Scenes/Root.tscn" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Scenes/UI/StartScreen.tscn" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/Scripts/Launcher.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Scripts/Launcher.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Scripts/Logic/World.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Scripts/Logic/World.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Scripts/Player.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Scripts/Player.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Scripts/UI/StartScreen.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Scripts/UI/LaunchScreen.cs" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -110,7 +116,7 @@
<workItem from="1722846222698" duration="9239000" />
<workItem from="1722855558149" duration="11578000" />
<workItem from="1723040712804" duration="74000" />
<workItem from="1723040934916" duration="9355000" />
<workItem from="1723040934916" duration="11438000" />
</task>
<task id="LOCAL-00001" summary=":sparkles: Usable multiplayer">
<option name="closed" value="true" />

View File

@ -7,7 +7,7 @@
[ext_resource type="Script" path="res://Scripts/Effects/CameraShake.cs" id="4_fwngj"]
[sub_resource type="CircleShape2D" id="CircleShape2D_68yf8"]
radius = 20.48
radius = 25.6
[sub_resource type="FastNoiseLite" id="FastNoiseLite_cfnx7"]
@ -21,6 +21,27 @@ properties/1/replication_mode = 1
properties/2/path = NodePath("RotationCentre:rotation")
properties/2/spawn = true
properties/2/replication_mode = 1
properties/3/path = NodePath(".:PlayerName")
properties/3/spawn = true
properties/3/replication_mode = 1
properties/4/path = NodePath(".:Health")
properties/4/spawn = true
properties/4/replication_mode = 1
properties/5/path = NodePath(".:MaxHealth")
properties/5/spawn = true
properties/5/replication_mode = 1
properties/6/path = NodePath(".:ActionPoints")
properties/6/spawn = true
properties/6/replication_mode = 1
properties/7/path = NodePath(".:MaxActionPoints")
properties/7/spawn = true
properties/7/replication_mode = 1
properties/8/path = NodePath(".:AmmoAmount")
properties/8/spawn = true
properties/8/replication_mode = 1
properties/9/path = NodePath(".:MaxAmmoAmount")
properties/9/spawn = true
properties/9/replication_mode = 1
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_hojn2"]
properties/0/path = NodePath("InputSynchronizer:IsDashing")
@ -36,22 +57,22 @@ PlayerCamera = NodePath("Camera2D")
PlayerInput = NodePath("InputSynchronizer")
BulletScene = ExtResource("2_nmop0")
[node name="HUD" type="VBoxContainer" parent="."]
[node name="Overlay" type="VBoxContainer" parent="."]
offset_left = -90.0
offset_top = -80.0
offset_top = -86.0
offset_right = 90.0
offset_bottom = -20.0
offset_bottom = -26.0
theme_override_constants/separation = 4
alignment = 1
[node name="NameTag" type="Label" parent="HUD"]
[node name="NameTag" type="Label" parent="Overlay"]
layout_mode = 2
text = "Player"
horizontal_alignment = 1
vertical_alignment = 1
clip_text = true
[node name="HealthBar" type="ProgressBar" parent="HUD"]
[node name="HealthBar" type="ProgressBar" parent="Overlay"]
custom_minimum_size = Vector2(2.08165e-12, 8)
layout_mode = 2
rounded = true
@ -63,7 +84,7 @@ show_percentage = false
[node name="Sprite2D" type="Sprite2D" parent="RotationCentre"]
position = Vector2(2.08165e-12, 2.08165e-12)
scale = Vector2(0.04, 0.04)
scale = Vector2(0.05, 0.05)
texture = ExtResource("1_cqpqa")
[node name="Muzzle" type="Marker2D" parent="RotationCentre"]

View File

@ -1,13 +1,13 @@
[gd_scene load_steps=5 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://bvll23f5ibd4v" path="res://Scenes/UI/StartScreen.tscn" id="2_7o53i"]
[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/World.cs" id="3_xwguj"]
[node name="Node" type="Node"]
[node name="StartScreen" parent="." node_paths=PackedStringArray("Launcher") instance=ExtResource("2_7o53i")]
[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")]

View File

@ -1,8 +1,8 @@
[gd_scene load_steps=2 format=3 uid="uid://bvll23f5ibd4v"]
[ext_resource type="Script" path="res://Scripts/UI/StartScreen.cs" id="1_f8by8"]
[ext_resource type="Script" path="res://Scripts/UI/LaunchScreen.cs" id="1_hiet3"]
[node name="StartScreen" type="Control" node_paths=PackedStringArray("ServerPortInput", "ServerAddrInput", "StartAsSingleButton", "StartAsServerButton", "StartAsClientButton")]
[node name="LaunchScreen" type="Control" node_paths=PackedStringArray("PlayerNameInput", "ServerPortInput", "ServerAddrInput", "StartAsServerButton", "StartAsClientButton")]
process_mode = 3
layout_mode = 3
anchors_preset = 15
@ -10,10 +10,10 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_f8by8")
script = ExtResource("1_hiet3")
PlayerNameInput = NodePath("CenterContainer/VBoxContainer/NameEdit")
ServerPortInput = NodePath("CenterContainer/VBoxContainer/PortEdit")
ServerAddrInput = NodePath("CenterContainer/VBoxContainer/AddrEdit")
StartAsSingleButton = NodePath("CenterContainer/VBoxContainer/PlayAloneButton")
StartAsServerButton = NodePath("CenterContainer/VBoxContainer/MultiplayerActions/HostButton")
StartAsClientButton = NodePath("CenterContainer/VBoxContainer/MultiplayerActions/ConnectButton")
@ -25,18 +25,13 @@ offset_bottom = 648.0
[node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer"]
layout_mode = 2
[node name="SinglePlayerTitle" type="Label" parent="CenterContainer/VBoxContainer"]
[node name="NameEdit" type="LineEdit" parent="CenterContainer/VBoxContainer"]
layout_mode = 2
text = "Singleplayer"
horizontal_alignment = 1
[node name="PlayAloneButton" type="Button" parent="CenterContainer/VBoxContainer"]
layout_mode = 2
text = "Play Alone"
placeholder_text = "Player name"
[node name="MultiplayerTitle" type="Label" parent="CenterContainer/VBoxContainer"]
layout_mode = 2
text = "Mutiplayer"
text = "Play the Game"
horizontal_alignment = 1
[node name="PortEdit" type="LineEdit" parent="CenterContainer/VBoxContainer"]

View File

@ -30,13 +30,13 @@ public partial class Launcher : Node
}
}
public void StartAsSingle()
public void StartAsSingle(string currentPlayerName = null)
{
GameUnfreeze();
World.StartGame();
World.StartGame(currentPlayerName);
}
public bool StartAsServer(int port)
public bool StartAsServer(int port, string currentPlayerName = null)
{
var peer = new ENetMultiplayerPeer();
peer.CreateServer(port);
@ -51,12 +51,12 @@ public partial class Launcher : Node
Multiplayer.MultiplayerPeer = peer;
GameUnfreeze();
World.StartGame();
World.StartGame(currentPlayerName);
return true;
}
public bool StartAsClient(string addr, int port)
public bool StartAsClient(string addr, int port, string currentPlayerName = null)
{
if (string.IsNullOrEmpty(addr)) return false;
@ -74,7 +74,7 @@ public partial class Launcher : Node
Multiplayer.MultiplayerPeer = peer;
GameUnfreeze();
World.StartGame();
World.StartGame(currentPlayerName);
return true;
}

View File

@ -6,7 +6,7 @@ public partial class World : Node2D
{
[Export] public PackedScene PlayerScene;
public void StartGame()
public void StartGame(string currentPlayerName = null)
{
if (!Multiplayer.IsServer())
return;
@ -21,7 +21,7 @@ public partial class World : Node2D
// Add this client as a player if client isn't a dedicated server
if (!OS.HasFeature("dedicated_server"))
AddPlayer(1);
AddPlayer(1, currentPlayerName);
}
public override void _ExitTree()
@ -44,13 +44,14 @@ public partial class World : Node2D
private void RemovePlayer_Adaptor(long id)
=> RemovePlayer((int)id);
private void AddPlayer(int id)
private void AddPlayer(int id, string name = null)
{
var player = PlayerScene.Instantiate<AceField.Scripts.Player>();
player.SetPlayerId(id);
var player = PlayerScene.Instantiate<Player>();
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.Name = BuildPlayerName(id);
player.PlayerName = name;
AddChild(player, true);
}
@ -63,4 +64,17 @@ public partial class World : Node2D
GetNode(name).QueueFree();
}
public Player GetCurrentPlayer()
{
foreach(var child in GetChildren())
{
if (child is Player { IsCurrentPlayer: true } player)
{
return player;
}
}
return null;
}
}

View File

@ -19,6 +19,8 @@ public partial class Player : CharacterBody2D
[Export] public double MaxHealth = 100;
[Export] public double ActionPoints = 20;
[Export] public double MaxActionPoints = 20;
[Export] public double AmmoAmount = 30;
[Export] public double MaxAmmoAmount = 30;
[Export] public Camera2D PlayerCamera;
[Export] public PlayerInput PlayerInput;
@ -40,11 +42,7 @@ public partial class Player : CharacterBody2D
}
}
public void SetPlayerId(int id)
{
PlayerId = id;
PlayerInput.SetMultiplayerAuthority(id);
}
public bool IsCurrentPlayer => _currentPlayerId == Multiplayer.GetUniqueId();
public override void _Ready()
{
@ -54,10 +52,10 @@ public partial class Player : CharacterBody2D
if (PlayerId == Multiplayer.GetUniqueId())
PlayerCamera.Enabled = true;
PlayerName = $"Player#{PlayerId}"; // TODO Remove mock data
GetNode<Label>("HUD/NameTag").Text = PlayerName;
PlayerName ??= $"Player#{PlayerId}";
GetNode<Label>("Overlay/NameTag").Text = PlayerName;
GetNode<ProgressBar>("HUD/HealthBar").Value = Health / MaxHealth * 100;
GetNode<ProgressBar>("Overlay/HealthBar").Value = Health / MaxHealth * 100;
}
public override void _Process(double delta)
@ -114,7 +112,7 @@ public partial class Player : CharacterBody2D
Health -= damage;
var tween = CreateTween();
var bar = GetNode<ProgressBar>("HUD/HealthBar");
var bar = GetNode<ProgressBar>("Overlay/HealthBar");
tween.TweenProperty(bar, "value", Health / MaxHealth * 100, 0.3);
protection.Start();

View File

@ -2,16 +2,16 @@ using Godot;
namespace AceField.Scripts.UI;
public partial class StartScreen : Control
public partial class LaunchScreen : Control
{
[Export] public int DefaultServerPort = 4343;
[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 StartAsSingleButton;
[Export] public Button StartAsServerButton;
[Export] public Button StartAsClientButton;
@ -42,11 +42,6 @@ public partial class StartScreen : Control
ServerPortInput.Text = DefaultServerPort.ToString();
ServerAddrInput.Text = DefaultServerAddr;
StartAsSingleButton.Pressed += () =>
{
Launcher.StartAsSingle();
Hide();
};
StartAsServerButton.Pressed += () =>
{
if (!DoValidation())
@ -56,7 +51,8 @@ public partial class StartScreen : Control
}
var port = ServerPortInput.Text;
var result = Launcher.StartAsServer(int.Parse(port));
var name = string.IsNullOrEmpty(PlayerNameInput.Text) ? null : PlayerNameInput.Text;
var result = Launcher.StartAsServer(int.Parse(port), currentPlayerName: name);
if (result)
Hide();
@ -71,7 +67,8 @@ public partial class StartScreen : Control
var addr = ServerAddrInput.Text;
var port = ServerPortInput.Text;
var result = Launcher.StartAsClient(addr, int.Parse(port));
var name = string.IsNullOrEmpty(PlayerNameInput.Text) ? null : PlayerNameInput.Text;
var result = Launcher.StartAsClient(addr, int.Parse(port), currentPlayerName: name);
if (result)
Hide();