A Razor UI component for the player's HUD. It finds a PlayerController by name, reads a TacitPlayerSample health component, updates a TutorialHealthArmorPanel and an array of BaseCrosshair instances to show/hide crosshairs and health each frame.
@using Sandbox;
@using Sandbox.UI;
@using Sandbox.TacitPlayer;
@using CrosshairMaker.Abstract;
@inherits PanelComponent
@namespace Sandbox
<root>
<div class="column">
<div class="row top"></div>
<div class="row middle"></div>
<div class="row bottom">
<TutorialHealthArmorPanel @ref="HPPanel"/>
</div>
</div>
</root>
@code
{
[Property] public string ExpectedReferedPlayer {get;set;} = "player controller edito";
[Property] public PlayerController ReferedPlayer {get;set;}
[Property,ReadOnly] public TacitPlayerSample HealthSystem {get;set;}
string hashPlayerHp {get;set;} = "";
[Property] public BaseCrosshair[] anCrosshairs {get;set;}
[Property, TextArea] public string MyStringValue { get; set; } = "Hello World!";
[Property] public bool isThirdPerson {get; internal set;} = false;
TutorialHealthArmorPanel HPPanel {get;set;}
protected override void OnStart()
{
if(!ReferedPlayer.IsValid())
{
// Obtain Player now!
ReferedPlayer = Scene.Directory.FindByName( ExpectedReferedPlayer ).First().GetComponent<PlayerController>();
}
if(ReferedPlayer.IsValid())
{
HealthSystem = ReferedPlayer.GetComponent<TacitPlayerSample>();
}
}
protected override void OnUpdate()
{
if(ReferedPlayer.IsValid())
{
// Check first person now!
if(ReferedPlayer.ThirdPerson)
{
// Player is in 3rd person!
}
if(isThirdPerson != ReferedPlayer.ThirdPerson)
{
foreach(var crosshair in anCrosshairs)
{
if(crosshair.IsValid())
{
var isAlive = false;
if(HealthSystem.IsValid()) isAlive = !HealthSystem.IsDead;
else isAlive = false;
crosshair.IsVisible = !ReferedPlayer.ThirdPerson && isAlive;
}
}
isThirdPerson = ReferedPlayer.ThirdPerson;
}
//HPPanel.Health = ReferedPlayer.Local.Health;
} else {
foreach(var crosshair in anCrosshairs)
{
if(crosshair.IsValid())
{
crosshair.IsVisible = false;
}
}
}
if(HealthSystem.IsValid())
{
hashPlayerHp = $"{HealthSystem.Health}";
HPPanel.Health = HealthSystem.Health;
//HPPanel.Armor = 12f;
} else {
hashPlayerHp = $"0";
HPPanel.Health = 0f;
}
}
/// <summary>
/// the hash determines if the system should be rebuilt. If it changes, it will be rebuilt
/// </summary>
protected override int BuildHash() => System.HashCode.Combine(
ReferedPlayer,
hashPlayerHp,
ExpectedReferedPlayer,
MyStringValue
);
}