Components/Player/Player.cs
using Sandbox;
using Sandbox.Services;
[Icon("person")]
[Category("Citizen")]
[Tint( EditorTint.Pink )]
public partial class Player : Component
{
[Property, ReadOnly, Feature( "Data" )]
public float SessionTime = 0f;
[ConVar( "debug_text_info", ConVarFlags.Cheat )]
public static bool DrawText { get; set; } = false;
[ConVar( "debug_traces_info", ConVarFlags.Cheat )]
public static bool DrawTraces { get; set; } = false;
public static Player LocalPlayer
{
get
{
if( !_local.IsValid() )
{
_local = Game.ActiveScene.GetAllComponents<Player>().FirstOrDefault( x => x.Network.IsOwner );
}
return _local;
}
}
private static Player _local;
private Vector3 CameraOrigin;
private float currentDistance = 0f;
protected override void OnAwake()
{
if ( !IsProxy )
SessionTime = (float)Stats.LocalPlayer.Get("time_played").Max;
base.OnAwake();
}
protected override void OnFixedUpdate()
{
if ( IsProxy ) return;
CameraControl();
CheckDistanceTrace();
if ( DrawText )
DebugOverlay.ScreenText
(
new Vector2( .1f * Screen.Width, .1f * Screen.Height ) + Screen.DesktopScale,
$"Camera Distance: {CameraDistance}\nTime:{SessionTime}"
);
CheckSessionTime();
base.OnFixedUpdate();
}
bool isLogged = false;
void CheckSessionTime()
{
if ( SessionTime < 1 ) return;
if ( (int)SessionTime % 60 == 0 )
{
if ( !isLogged )
{
Stats.SetValue( "time_played", (int)SessionTime );
isLogged = true;
}
}
else
{
isLogged = false;
}
}
}