AmmoPickup.cs
using Sandbox;
public sealed class AmmoPickup : Component, Component.ITriggerListener
{
[Property] public int AmmoAmount { get; set; } = 30; // Скільки патронів дає ця коробка
[Property] public SoundEvent PickupSound { get; set; }
protected override void OnStart()
{
// Налаштовуємо коллайдер на тригер, якщо це ще не зроблено в інспекторі
var collider = Components.Get<Collider>();
if ( collider.IsValid() )
{
collider.IsTrigger = true;
}
}
protected override void OnUpdate()
{
// Просте обертання для краси
WorldRotation *= Rotation.FromYaw( 90f * Time.Delta );
}
public void OnTriggerEnter( Collider other )
{
// Перевіряємо тільки на сервері
if ( !Networking.IsHost ) return;
// Шукаємо гравця та його зброю
var paintGun = other.GameObject.Components.GetAll<PaintGun>( FindMode.EverythingInSelfAndAncestors ).FirstOrDefault();
if ( paintGun.IsValid() )
{
// Якщо у гравця ще немає максимальної амуніції
if ( paintGun.AmmoReserve < paintGun.MaxReserve )
{
paintGun.AmmoReserve = System.Math.Min( paintGun.MaxReserve, paintGun.AmmoReserve + AmmoAmount );
// Якщо є звук - граємо
if ( PickupSound != null )
{
Sound.Play( PickupSound, WorldPosition );
}
// Знищуємо об'єкт (коробку з амуніцією)
GameObject.Destroy();
}
}
}
public void OnTriggerExit( Collider other ) { }
}