Multiplayer Basis

This commit is contained in:
2024-08-05 16:30:50 +08:00
parent fc6678f58b
commit c5387d7784
8 changed files with 235 additions and 4 deletions

71
Scripts/Multiplayer.cs Normal file
View File

@ -0,0 +1,71 @@
using System;
using Godot;
namespace CodingLand.Scripts;
public partial class Multiplayer : Node
{
[Export] public Node2D Playground;
private void GameFreeze()
{
Playground.Hide();
GetTree().Paused = true;
}
private void GameUnfreeze()
{
Playground.Show();
Playground.GetNode<Camera2D>("Camera2D").Enabled = true;
GetTree().Paused = false;
}
public override void _Ready()
{
GameFreeze();
if (DisplayServer.GetName() == "headless")
{
GD.Print("Starting server in headless mode...");
StartAsServer(4343);
}
}
public bool StartAsServer(int port)
{
var peer = new ENetMultiplayerPeer();
peer.CreateServer(port);
if (peer.GetConnectionStatus() == MultiplayerPeer.ConnectionStatus.Disconnected)
{
GD.PrintErr("Failed to start multiplayer server...");
OS.Alert("Failed to start multiplayer server...");
return false;
}
Multiplayer.MultiplayerPeer = peer;
GameUnfreeze();
return true;
}
public bool StartAsClient(string addr, int port)
{
if (string.IsNullOrEmpty(addr)) return false;
var peer = new ENetMultiplayerPeer();
peer.CreateClient(addr, port);
if (peer.GetConnectionStatus() == MultiplayerPeer.ConnectionStatus.Disconnected)
{
var info = $"Unable to connect multiplayer server {addr}:{port}";
GD.PrintErr(info);
OS.Alert(info);
return false;
}
Multiplayer.MultiplayerPeer = peer;
GameUnfreeze();
return true;
}
}

View File

@ -1,3 +1,4 @@
using System;
using Godot;
namespace CodingLand.Scripts;
@ -12,6 +13,9 @@ public partial class Player : CharacterBody2D
[Export] public float CameraSmoothness = 0.05f;
[Export] public Camera2D PlayerCamera;
[Export] public Timer PlayerDashCountdown;
[Export] public float PlayerDashAcceleration = 2f;
public override void _PhysicsProcess(double delta)
{
@ -39,6 +43,12 @@ public partial class Player : CharacterBody2D
{
Velocity = Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta);
}
if (Input.IsActionJustPressed("move_dash") && PlayerDashCountdown.IsStopped())
{
Velocity *= PlayerDashAcceleration;
PlayerDashCountdown.Start();
}
Position += Velocity * (float)delta;
MoveAndSlide();

View File

@ -0,0 +1,74 @@
using Godot;
namespace CodingLand.Scripts.UI;
public partial class MultiplayerUi : Control
{
[Export] public int DefaultServerPort = 4343;
[Export] public string DefaultServerAddr = "127.0.0.1";
[Export] public Multiplayer MultiplayerController;
[Export] public LineEdit ServerPortInput;
[Export] public LineEdit ServerAddrInput;
[Export] public Button StartAsServerButton;
[Export] public Button StartAsClientButton;
private void ApplySize()
{
var screenSize = GetViewportRect().Size;
Position = new Vector2(0, 0);
Size = screenSize;
GetNode<CenterContainer>("CenterContainer").Size = screenSize;
}
private bool DoValidation()
{
var addr = ServerAddrInput.Text;
var port = ServerPortInput.Text;
if (string.IsNullOrEmpty(addr)) return false;
if (string.IsNullOrEmpty(port) || !int.TryParse(port, out _)) return false;
return true;
}
public override void _Ready()
{
ApplySize();
ServerPortInput.Text = DefaultServerPort.ToString();
ServerAddrInput.Text = DefaultServerAddr;
StartAsServerButton.Pressed += () =>
{
if (!DoValidation())
{
OS.Alert("Invalid multiplayer configuration.");
return;
}
var port = ServerPortInput.Text;
var result = MultiplayerController.StartAsServer(int.Parse(port));
if (result)
Hide();
};
StartAsServerButton.Pressed += () =>
{
if (!DoValidation())
{
OS.Alert("Invalid multiplayer configuration.");
return;
}
var addr = ServerAddrInput.Text;
var port = ServerPortInput.Text;
var result = MultiplayerController.StartAsClient(addr, int.Parse(port));
if (result)
Hide();
};
}
}