✨ Usable multiplayer
This commit is contained in:
33
Scripts/Logic/PlayerInput.cs
Normal file
33
Scripts/Logic/PlayerInput.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using Godot;
|
||||
|
||||
namespace CodingLand.Scripts.Logic;
|
||||
|
||||
public partial class PlayerInput : MultiplayerSynchronizer
|
||||
{
|
||||
[Export] public bool IsDashing;
|
||||
|
||||
[Export] public Vector2 MovementDirection;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
if (GetMultiplayerAuthority() != Multiplayer.GetUniqueId())
|
||||
{
|
||||
SetProcess(false);
|
||||
SetPhysicsProcess(false);
|
||||
}
|
||||
}
|
||||
|
||||
[Rpc(CallLocal = true)]
|
||||
private void Dash()
|
||||
{
|
||||
IsDashing = true;
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
MovementDirection = Input.GetVector("move_left", "move_right", "move_up", "move_down");
|
||||
|
||||
if (Input.IsActionJustPressed("move_dash"))
|
||||
Rpc(nameof(Dash));
|
||||
}
|
||||
}
|
68
Scripts/Logic/World.cs
Normal file
68
Scripts/Logic/World.cs
Normal file
@ -0,0 +1,68 @@
|
||||
using System.Numerics;
|
||||
using Godot;
|
||||
using Vector2 = Godot.Vector2;
|
||||
|
||||
namespace CodingLand.Scripts.Logic;
|
||||
|
||||
public partial class World : Node2D
|
||||
{
|
||||
[Export] public PackedScene PlayerScene;
|
||||
|
||||
public void StartGame()
|
||||
{
|
||||
if (!Multiplayer.IsServer())
|
||||
return;
|
||||
|
||||
// Handling player connect / disconnect after this client connected
|
||||
Multiplayer.PeerDisconnected += RemovePlayer_Adaptor;
|
||||
Multiplayer.PeerConnected += AddPlayer_Adaptor;
|
||||
|
||||
// Handling player connected before this client
|
||||
foreach (var id in Multiplayer.GetPeers())
|
||||
AddPlayer(id);
|
||||
|
||||
// Add this client as a player if client isn't a dedicated server
|
||||
if (!OS.HasFeature("dedicated_server"))
|
||||
AddPlayer(1);
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (!Multiplayer.IsServer())
|
||||
return;
|
||||
|
||||
Multiplayer.PeerDisconnected -= RemovePlayer_Adaptor;
|
||||
Multiplayer.PeerConnected -= AddPlayer_Adaptor;
|
||||
}
|
||||
|
||||
private string BuildPlayerName(int id)
|
||||
{
|
||||
return $"Player#{id}";
|
||||
}
|
||||
|
||||
private void AddPlayer_Adaptor(long id)
|
||||
=> AddPlayer((int)id);
|
||||
|
||||
private void RemovePlayer_Adaptor(long id)
|
||||
=> RemovePlayer((int)id);
|
||||
|
||||
private void AddPlayer(int id)
|
||||
{
|
||||
var player = PlayerScene.Instantiate<Player>();
|
||||
player.SetPlayerId(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);
|
||||
|
||||
AddChild(player, true);
|
||||
}
|
||||
|
||||
private void RemovePlayer(int id)
|
||||
{
|
||||
var name = BuildPlayerName(id);
|
||||
if (!HasNode(name))
|
||||
return;
|
||||
|
||||
GetNode(name).QueueFree();
|
||||
}
|
||||
}
|
@ -1,23 +1,22 @@
|
||||
using System;
|
||||
using CodingLand.Scripts.Logic;
|
||||
using Godot;
|
||||
|
||||
namespace CodingLand.Scripts;
|
||||
|
||||
public partial class Multiplayer : Node
|
||||
{
|
||||
[Export] public Node2D Playground;
|
||||
[Export] public World World;
|
||||
|
||||
private void GameFreeze()
|
||||
{
|
||||
Playground.Hide();
|
||||
GetTree().Paused = true;
|
||||
World.Hide();
|
||||
}
|
||||
|
||||
private void GameUnfreeze()
|
||||
{
|
||||
Playground.Show();
|
||||
Playground.GetNode<Camera2D>("Camera2D").Enabled = true;
|
||||
GetTree().Paused = false;
|
||||
World.Show();
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
@ -41,9 +40,12 @@ public partial class Multiplayer : Node
|
||||
OS.Alert("Failed to start multiplayer server...");
|
||||
return false;
|
||||
}
|
||||
|
||||
GD.Print("Running game as server...");
|
||||
|
||||
Multiplayer.MultiplayerPeer = peer;
|
||||
GameUnfreeze();
|
||||
World.StartGame();
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -62,8 +64,11 @@ public partial class Multiplayer : Node
|
||||
return false;
|
||||
}
|
||||
|
||||
GD.Print("Running game as client...");
|
||||
|
||||
Multiplayer.MultiplayerPeer = peer;
|
||||
GameUnfreeze();
|
||||
World.StartGame();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -1,39 +1,54 @@
|
||||
using System;
|
||||
using CodingLand.Scripts.Logic;
|
||||
using Godot;
|
||||
|
||||
namespace CodingLand.Scripts;
|
||||
|
||||
public partial class Player : CharacterBody2D
|
||||
{
|
||||
private int _currentPlayerId = 1;
|
||||
|
||||
[Export] public float MaxSpeed = 400f;
|
||||
[Export] public float Acceleration = 500f;
|
||||
[Export] public float Deceleration = 500f;
|
||||
[Export] public float RotationSpeed = 5f;
|
||||
|
||||
[Export] public float CameraSmoothness = 0.05f;
|
||||
|
||||
[Export] public Camera2D PlayerCamera;
|
||||
|
||||
|
||||
[Export] public PlayerInput PlayerInput;
|
||||
|
||||
[Export] public Timer PlayerDashCountdown;
|
||||
[Export] public float PlayerDashAcceleration = 2f;
|
||||
|
||||
[Export] public int PlayerId
|
||||
{
|
||||
get => _currentPlayerId;
|
||||
set
|
||||
{
|
||||
_currentPlayerId = value;
|
||||
PlayerInput.SetMultiplayerAuthority(value);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetPlayerId(int id)
|
||||
{
|
||||
PlayerId = id;
|
||||
PlayerInput.SetMultiplayerAuthority(id);
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
if (PlayerId == Multiplayer.GetUniqueId())
|
||||
PlayerCamera.Enabled = true;
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
var input = Vector2.Zero;
|
||||
var input = PlayerInput.MovementDirection;
|
||||
|
||||
if (Input.IsActionPressed("move_right"))
|
||||
input.X += 1;
|
||||
if (Input.IsActionPressed("move_left"))
|
||||
input.X -= 1;
|
||||
if (Input.IsActionPressed("move_down"))
|
||||
input.Y += 1;
|
||||
if (Input.IsActionPressed("move_up"))
|
||||
input.Y -= 1;
|
||||
|
||||
input = input.Normalized();
|
||||
|
||||
if (input != Vector2.Zero)
|
||||
{
|
||||
input = input.Normalized();
|
||||
Velocity = Velocity.MoveToward(input * MaxSpeed, Acceleration * (float)delta);
|
||||
|
||||
var finalRotation = input.Angle() + Mathf.Pi / 2;
|
||||
@ -44,18 +59,14 @@ public partial class Player : CharacterBody2D
|
||||
Velocity = Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta);
|
||||
}
|
||||
|
||||
if (Input.IsActionJustPressed("move_dash") && PlayerDashCountdown.IsStopped())
|
||||
if (PlayerInput.IsDashing && PlayerDashCountdown.IsStopped())
|
||||
{
|
||||
PlayerInput.IsDashing = false;
|
||||
Velocity *= PlayerDashAcceleration;
|
||||
PlayerDashCountdown.Start();
|
||||
}
|
||||
|
||||
|
||||
Position += Velocity * (float)delta;
|
||||
MoveAndSlide();
|
||||
|
||||
if (PlayerCamera != null)
|
||||
{
|
||||
PlayerCamera.Position = GlobalPosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ public partial class MultiplayerUi : Control
|
||||
if (result)
|
||||
Hide();
|
||||
};
|
||||
StartAsServerButton.Pressed += () =>
|
||||
StartAsClientButton.Pressed += () =>
|
||||
{
|
||||
if (!DoValidation())
|
||||
{
|
||||
|
Reference in New Issue
Block a user