✨ Block placing
This commit is contained in:
		@@ -3,7 +3,7 @@ using Godot;
 | 
			
		||||
 | 
			
		||||
namespace CodingLand.Scripts;
 | 
			
		||||
 | 
			
		||||
public partial class Multiplayer : Node
 | 
			
		||||
public partial class Launcher : Node
 | 
			
		||||
{
 | 
			
		||||
	[Export] public World World;
 | 
			
		||||
	
 | 
			
		||||
@@ -30,6 +30,12 @@ public partial class Multiplayer : Node
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void StartAsSingle()
 | 
			
		||||
	{
 | 
			
		||||
		GameUnfreeze();
 | 
			
		||||
		World.StartGame();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public bool StartAsServer(int port)
 | 
			
		||||
	{
 | 
			
		||||
		var peer = new ENetMultiplayerPeer();
 | 
			
		||||
@@ -6,22 +6,26 @@ public partial class PlayerInput : MultiplayerSynchronizer
 | 
			
		||||
{
 | 
			
		||||
    [Export] public bool IsDashing;
 | 
			
		||||
 | 
			
		||||
    [Export] public Vector2 BuildingAt;
 | 
			
		||||
 | 
			
		||||
    [Export] public Vector2 MovementDirection;
 | 
			
		||||
 | 
			
		||||
    private bool IsCurrentPlayer => GetMultiplayerAuthority() == Multiplayer.GetUniqueId();
 | 
			
		||||
 | 
			
		||||
    public override void _Ready()
 | 
			
		||||
    {
 | 
			
		||||
        if (GetMultiplayerAuthority() != Multiplayer.GetUniqueId())
 | 
			
		||||
        {
 | 
			
		||||
            SetProcess(false);
 | 
			
		||||
            SetPhysicsProcess(false);
 | 
			
		||||
        }
 | 
			
		||||
        if (IsCurrentPlayer) return;
 | 
			
		||||
        SetProcess(false);
 | 
			
		||||
        SetPhysicsProcess(false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    [Rpc(CallLocal = true)]
 | 
			
		||||
    private void Dash()
 | 
			
		||||
    {
 | 
			
		||||
        IsDashing = true;
 | 
			
		||||
    }
 | 
			
		||||
        => IsDashing = true;
 | 
			
		||||
 | 
			
		||||
    [Rpc(CallLocal = true)]
 | 
			
		||||
    private void Build(Vector2 pos)
 | 
			
		||||
        => BuildingAt = pos;
 | 
			
		||||
 | 
			
		||||
    public override void _Process(double delta)
 | 
			
		||||
    {
 | 
			
		||||
@@ -30,4 +34,11 @@ public partial class PlayerInput : MultiplayerSynchronizer
 | 
			
		||||
        if (Input.IsActionJustPressed("move_dash"))
 | 
			
		||||
            Rpc(nameof(Dash));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public override void _Input(InputEvent evt)
 | 
			
		||||
    {
 | 
			
		||||
        if (!IsCurrentPlayer) return;
 | 
			
		||||
        if (evt is InputEventMouseButton { Pressed: true })
 | 
			
		||||
            Rpc(nameof(Build), GetViewport().GetMousePosition());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,4 +1,3 @@
 | 
			
		||||
using System.Numerics;
 | 
			
		||||
using Godot;
 | 
			
		||||
using Vector2 = Godot.Vector2;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -6,20 +6,22 @@ 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 int Reach = 5;
 | 
			
		||||
 | 
			
		||||
	[Export] public Camera2D PlayerCamera;
 | 
			
		||||
 | 
			
		||||
	[Export] public PlayerInput PlayerInput;
 | 
			
		||||
 | 
			
		||||
	[Export] public Timer PlayerDashCountdown;
 | 
			
		||||
	[Export] public float PlayerDashAcceleration = 2f;
 | 
			
		||||
 | 
			
		||||
	[Export] public int PlayerId
 | 
			
		||||
	[Export]
 | 
			
		||||
	public int PlayerId
 | 
			
		||||
	{
 | 
			
		||||
		get => _currentPlayerId;
 | 
			
		||||
		set
 | 
			
		||||
@@ -34,18 +36,34 @@ public partial class Player : CharacterBody2D
 | 
			
		||||
		PlayerId = id;
 | 
			
		||||
		PlayerInput.SetMultiplayerAuthority(id);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private TilesManager _tilesMgr;
 | 
			
		||||
 | 
			
		||||
	public override void _Ready()
 | 
			
		||||
	{
 | 
			
		||||
		if (PlayerId == Multiplayer.GetUniqueId())
 | 
			
		||||
			PlayerCamera.Enabled = true;
 | 
			
		||||
		_tilesMgr = GetNode<TilesManager>("../Tiles");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public override void _Process(double delta)
 | 
			
		||||
	{
 | 
			
		||||
		var vec = GetGlobalMousePosition();
 | 
			
		||||
		if (PlayerInput.BuildingAt == Vector2.Zero) return;
 | 
			
		||||
		var distance = Position.DistanceTo(vec);
 | 
			
		||||
		if (distance <= Reach * _tilesMgr.TileSize)
 | 
			
		||||
		{
 | 
			
		||||
			// Able to build
 | 
			
		||||
			Rpc(nameof(AddTile), vec);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		PlayerInput.BuildingAt = Vector2.Zero;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public override void _PhysicsProcess(double delta)
 | 
			
		||||
	{
 | 
			
		||||
		var input = PlayerInput.MovementDirection;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		if (input != Vector2.Zero)
 | 
			
		||||
		{
 | 
			
		||||
			input = input.Normalized();
 | 
			
		||||
@@ -59,14 +77,27 @@ public partial class Player : CharacterBody2D
 | 
			
		||||
			Velocity = Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (PlayerInput.IsDashing && PlayerDashCountdown.IsStopped())
 | 
			
		||||
		var dashCountdown = GetNode<Timer>("DashCountdown");
 | 
			
		||||
		if (PlayerInput.IsDashing && dashCountdown.IsStopped())
 | 
			
		||||
		{
 | 
			
		||||
			PlayerInput.IsDashing = false;
 | 
			
		||||
			Velocity *= PlayerDashAcceleration;
 | 
			
		||||
			PlayerDashCountdown.Start();
 | 
			
		||||
			dashCountdown.Start();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		Position += Velocity * (float)delta;
 | 
			
		||||
		MoveAndSlide();
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	[Rpc(mode: MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
 | 
			
		||||
	public void AddTile(Vector2 pos)
 | 
			
		||||
	{
 | 
			
		||||
		if (_tilesMgr.GetTileByPosition<Node2D>(pos) != null) return;
 | 
			
		||||
		
 | 
			
		||||
		var tileVec = new Vector2(_tilesMgr.TileSize, _tilesMgr.TileSize);
 | 
			
		||||
		var blueprint = GD.Load<PackedScene>("res://Scenes/Tiles/Brick.tscn");
 | 
			
		||||
		var instance = blueprint.Instantiate<Node2D>();
 | 
			
		||||
		instance.Position = pos.Snapped(tileVec);
 | 
			
		||||
		_tilesMgr.AddChild(instance);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										35
									
								
								Scripts/TilesManager.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								Scripts/TilesManager.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
using Godot;
 | 
			
		||||
 | 
			
		||||
namespace CodingLand.Scripts;
 | 
			
		||||
 | 
			
		||||
public partial class TilesManager : Node2D
 | 
			
		||||
{
 | 
			
		||||
    [Export] public float TileSize = 51.2f;
 | 
			
		||||
 | 
			
		||||
    [Rpc(CallLocal = true)]
 | 
			
		||||
    public void AddTile(Vector2 pos)
 | 
			
		||||
    {
 | 
			
		||||
        if (GetTileByPosition<Node2D>(pos) == null)
 | 
			
		||||
        {
 | 
			
		||||
            var tileVec = new Vector2(TileSize, TileSize);
 | 
			
		||||
            // TODO Replace the brick to player selection
 | 
			
		||||
            var blueprint = GD.Load<PackedScene>("res://Scenes/Tiles/Brick.tscn");
 | 
			
		||||
            var instance = blueprint.Instantiate<Node2D>();
 | 
			
		||||
            instance.Position = pos.Snapped(tileVec);
 | 
			
		||||
            AddChild(instance);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public T GetTileByPosition<T>(Vector2 position) where T : Node2D
 | 
			
		||||
    {
 | 
			
		||||
        foreach (var item in GetChildren())
 | 
			
		||||
        {
 | 
			
		||||
            if (item is T tile && tile.Position == position)
 | 
			
		||||
            {
 | 
			
		||||
                return tile;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -2,15 +2,16 @@ using Godot;
 | 
			
		||||
 | 
			
		||||
namespace CodingLand.Scripts.UI;
 | 
			
		||||
 | 
			
		||||
public partial class MultiplayerUi : Control
 | 
			
		||||
public partial class StartScreen : Control
 | 
			
		||||
{
 | 
			
		||||
	[Export] public int DefaultServerPort = 4343;
 | 
			
		||||
	[Export] public string DefaultServerAddr = "127.0.0.1";
 | 
			
		||||
 | 
			
		||||
	[Export] public Multiplayer MultiplayerController;
 | 
			
		||||
	[Export] public Launcher Launcher;
 | 
			
		||||
 | 
			
		||||
	[Export] public LineEdit ServerPortInput;
 | 
			
		||||
	[Export] public LineEdit ServerAddrInput;
 | 
			
		||||
	[Export] public Button StartAsSingleButton;
 | 
			
		||||
	[Export] public Button StartAsServerButton;
 | 
			
		||||
	[Export] public Button StartAsClientButton;
 | 
			
		||||
 | 
			
		||||
@@ -41,6 +42,11 @@ public partial class MultiplayerUi : Control
 | 
			
		||||
		ServerPortInput.Text = DefaultServerPort.ToString();
 | 
			
		||||
		ServerAddrInput.Text = DefaultServerAddr;
 | 
			
		||||
 | 
			
		||||
		StartAsSingleButton.Pressed += () =>
 | 
			
		||||
		{
 | 
			
		||||
			Launcher.StartAsSingle();
 | 
			
		||||
			Hide();
 | 
			
		||||
		};
 | 
			
		||||
		StartAsServerButton.Pressed += () =>
 | 
			
		||||
		{
 | 
			
		||||
			if (!DoValidation())
 | 
			
		||||
@@ -50,7 +56,7 @@ public partial class MultiplayerUi : Control
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			var port = ServerPortInput.Text;
 | 
			
		||||
			var result = MultiplayerController.StartAsServer(int.Parse(port));
 | 
			
		||||
			var result = Launcher.StartAsServer(int.Parse(port));
 | 
			
		||||
			
 | 
			
		||||
			if (result)
 | 
			
		||||
				Hide();
 | 
			
		||||
@@ -65,7 +71,7 @@ public partial class MultiplayerUi : Control
 | 
			
		||||
 | 
			
		||||
			var addr = ServerAddrInput.Text;
 | 
			
		||||
			var port = ServerPortInput.Text;
 | 
			
		||||
			var result = MultiplayerController.StartAsClient(addr, int.Parse(port));
 | 
			
		||||
			var result = Launcher.StartAsClient(addr, int.Parse(port));
 | 
			
		||||
 | 
			
		||||
			if (result)
 | 
			
		||||
				Hide();
 | 
			
		||||
		Reference in New Issue
	
	Block a user