2D multiplayer platformer using Godot Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

55 lines
1.8 KiB

using Godot;
public class Player : KinematicBody2D, IInitializable
{
[Export] public NodePath DisplayNamePath { get; set; }
[Export] public NodePath SpritePath { get; set; }
public Label DisplayNameLabel { get; private set; }
public Sprite Sprite { get; private set; }
public int NetworkID { get => int.Parse(Name); set => Name = value.ToString(); }
public string DisplayName { get => DisplayNameLabel.Text; set => DisplayNameLabel.Text = value; }
public Color Color { get => Sprite.Modulate; set => Sprite.Modulate = value; }
public void Initialize()
{
DisplayNameLabel = GetNode<Label>(DisplayNamePath);
Sprite = GetNode<Sprite>(SpritePath);
RsetConfig("position", MultiplayerAPI.RPCMode.Puppetsync);
RsetConfig(nameof(NetworkID), MultiplayerAPI.RPCMode.Puppetsync);
RsetConfig(nameof(DisplayName), MultiplayerAPI.RPCMode.Puppetsync);
RsetConfig(nameof(Color), MultiplayerAPI.RPCMode.Puppetsync);
}
public override void _Process(float delta)
{
if ((Position.y > 9000) && (this.GetGame() is Server))
RpcId(NetworkID, nameof(LocalPlayer.ResetPosition), Vector2.Zero);
}
[Master]
public void Move(Vector2 position)
{
if (GetTree().GetRpcSenderId() != NetworkID) return;
// TODO: Somewhat verify the movement of players.
Position = position;
foreach (var player in this.GetWorld().Players)
if (player != this)
RsetId(player.NetworkID, "position", Position);
}
[Master]
public void ChangeAppearance(string displayName, Color color)
{
if (GetTree().GetRpcSenderId() != NetworkID) return;
// TODO: Validate input.
Rset(nameof(DisplayName), displayName);
Rset(nameof(Color), color);
}
}