Framework/Players/RpPlayer.cs

Component representing a networked player. Syncs connection id, SteamId, display name, a NetDictionary of NW vars and a move type from the host, and has host-only owner RPCs to teleport the body and add velocity.

using System;
using System.Collections.Generic;
using System.Linq;
using RoleplayFramework.Scripting;

namespace RoleplayFramework.Players;

/// <summary>
/// Identity and networked variables of a player's body. Everything here is written by the host only
/// (<see cref="SyncFlags.FromHost"/>); a client cannot change its own name, identity or NW vars.
/// Gameplay state (money, job, health, ...) lives in gamemode Lua as NW vars or server-side storage.
/// </summary>
public sealed class RpPlayer : Component, INetVarHost
{
	[Sync( SyncFlags.FromHost )] public Guid ConnectionId { get; set; }
	[Sync( SyncFlags.FromHost )] public string SteamId { get; set; }
	[Sync( SyncFlags.FromHost )] public string DisplayName { get; set; }
	[Sync( SyncFlags.FromHost )] public NetDictionary<string, string> NetVars { get; set; } = new();

	/// <summary>Lua ply:SetMoveType: 2 walk (default), 8 noclip, 0 frozen. Applied by <see cref="RpMoveMode"/> on the owner.</summary>
	[Sync( SyncFlags.FromHost )] public int MoveType { get; set; } = RpMoveMode.MoveTypeWalk;

	/// <summary>Adds velocity (knockback, jump pads). The owner simulates movement, so the host asks it.</summary>
	[Rpc.Owner( NetFlags.HostOnly )]
	public void AddVelocity( Vector3 velocity )
	{
		if ( velocity.Length > 10000 ) velocity = velocity.Normal * 10000;
		var pc = Controller;
		if ( pc?.Body is null ) return;
		// The controller re-grounds and overwrites the body velocity every step; Jump lifts the player off the
		// ground and applies the impulse the same way a jump does.
		pc.PreventGrounding( 0.3f );
		pc.Jump( velocity );
	}

	public Connection Connection => Connection.Find( ConnectionId );

	public PlayerController Controller => Components.Get<PlayerController>();

	protected override void OnStart()
	{
		// Logged on every machine, so each side's log shows which players it can see.
		Diagnostics.RpLog.Info( "players", $"{DisplayName} is in the world ({(IsLocalPlayer ? "local player" : "remote player")})" );
		if ( IsLocalPlayer ) Core.LoadingProgress.OnLocalPlayerSpawned();
	}

	/// <summary>True on the machine that controls this player.</summary>
	public bool IsLocalPlayer => Networking.IsActive
		? Connection.Local is not null && ConnectionId == Connection.Local.Id
		: ConnectionId == Guid.Empty; // offline: the only body is ours

	/// <summary>
	/// Moves the player. The owning client simulates its own movement, so the host asks the owner to move;
	/// only the host may call this.
	/// </summary>
	[Rpc.Owner( NetFlags.HostOnly )]
	public void Teleport( Vector3 position, Angles eyeAngles )
	{
		WorldPosition = position;
		var pc = Controller;
		if ( pc is not null )
		{
			pc.EyeAngles = eyeAngles;
			if ( pc.Body is not null ) pc.Body.Velocity = Vector3.Zero;
		}
		GameObject.Network.ClearInterpolation();
	}

	public static IEnumerable<RpPlayer> All( Scene scene ) =>
		scene is null ? Enumerable.Empty<RpPlayer>() : scene.GetAllComponents<RpPlayer>();

	public static RpPlayer Local( Scene scene ) => All( scene ).FirstOrDefault( p => p.IsLocalPlayer );
}