This is a Blazor component that displays the game's kill feed. It iterates over historical entries, rendering killer and victim names, weapon icons, and indicating special events like headshots or suicides.
@using System;
@using System.Collections.Generic;
@using System.Globalization;
@using System.Linq;
@using Sandbox;
@using Sandbox.UI;
@inherits PanelComponent
@namespace Sandbox
<root class="@(IsWorldPanel ? "full" : "")">
<div class="feed">
@foreach ( var entry in VisibleEntries )
{
var opacity = EntryOpacity( entry ).ToString( "0.###", CultureInfo.InvariantCulture );
<div class="entry" style="opacity: @opacity;">
@if ( !entry.IsSuicide )
{
<label class="name @(IsLocal( entry.KillerId ) ? "local" : "")">@entry.KillerName</label>
@if ( !string.IsNullOrEmpty( entry.WeaponIconPath ) )
{
<img class="weapon" src="@entry.WeaponIconPath" />
}
else
{
<label class="separator">»</label>
}
}
else
{
<label class="separator suicide">☠</label>
}
<label class="name @(IsLocal( entry.VictimId ) ? "local" : "")">@entry.VictimName</label>
@if ( entry.Headshot )
{
<label class="headshot">HS</label>
}
</div>
}
</div>
</root>
@code
{
private const float FadeStart = 4f;
private static KillFeedHistory History => DeathmatchGame.Instance?.KillFeedHistory;
/// <summary>When true, layout fills a WorldPanel (VR); when false, uses ScreenPanel HUD offsets.</summary>
[Property] bool IsWorldPanel { get; set; }
private IEnumerable<KillFeedHistory.Entry> VisibleEntries =>
(History?.Entries ?? Array.Empty<KillFeedHistory.Entry>())
.Where( e => Time.Now - e.CreatedAt < KillFeedHistory.EntryLifetime );
public static void Add( Guid attackerId, Guid victimId, bool headshot, bool isSuicide, string weaponIconPath )
{
var attacker = NetworkManager.Instance?.GetPlayerSession( attackerId );
var victim = NetworkManager.Instance?.GetPlayerSession( victimId );
PushEntry(
attackerId,
victimId,
attacker?.DisplayName ?? "Unknown",
victim?.DisplayName ?? "Unknown",
headshot,
isSuicide,
weaponIconPath
);
}
public void AddEntry( Guid attackerId, Guid victimId, bool headshot, bool isSuicide, string weaponIconPath ) =>
Add( attackerId, victimId, headshot, isSuicide, weaponIconPath );
/// <summary>Inserts a sample killfeed row (cycles kill / headshot / suicide).</summary>
[Button( "Add Test Entry" )]
public void AddTestEntry()
{
var localId = Connection.Local?.Id ?? Guid.Empty;
var variant = (History?.Version ?? 0) % 3;
switch ( variant )
{
case 0:
PushEntry( localId, Guid.NewGuid(), "TestKiller", "TestVictim", false, false, "/materials/m4a1.png" );
break;
case 1:
PushEntry( localId, Guid.NewGuid(), "TestKiller", "TestVictim", true, false, "/materials/usp.webp" );
break;
default:
PushEntry( localId, localId, "TestVictim", "TestVictim", false, true, "" );
break;
}
}
private static void PushEntry( Guid killerId, Guid victimId, string killerName, string victimName, bool headshot, bool isSuicide, string weaponIconPath ) =>
History?.Add( killerId, victimId, killerName, victimName, headshot, isSuicide, weaponIconPath );
private static bool IsLocal( Guid connectionId ) =>
Connection.Local is not null && Connection.Local.Id == connectionId;
private static float EntryOpacity( KillFeedHistory.Entry entry )
{
var age = Time.Now - entry.CreatedAt;
if ( age < FadeStart )
return 1f;
return Math.Clamp( 1f - (age - FadeStart) / (KillFeedHistory.EntryLifetime - FadeStart), 0f, 1f );
}
protected override int BuildHash()
{
var fadeTick = (int)(Time.Now * 10f);
return HashCode.Combine( History?.Version ?? 0, fadeTick, History?.Entries.Count ?? 0, IsWorldPanel );
}
}