things/items/ReloadItem.cs

Defines a ReloadItem game item that spawns in the world, animates rotation and bounce, and when a living player collides with it it instantly reloads their weapon, recharges their dash, plays a collect effect, and removes itself. It sets lifetime, physics tweak values, and spawns a particle on start.

Native Interop
using System;
using Sandbox;

public class ReloadItem : Item
{
	private float _rotateTimeOffset;
	private float _personalRotateSpeed;
	private float _personalBounceSpeed;

	public override Vector3 SpawnScale => new Vector3( 2.3f );

	public override float BlinkTimeRemainingStart => 2.5f;

	protected override float AttractRangeFactor => 0.1f;
	protected override float AttractStrengthFactor => 1f;

	protected override void OnStart()
	{
		base.OnStart();

		Lifetime = 5f;
		ShouldCheckBounds = true;
		PushStrength = 500f;

		BaseZPos = 0f;
		WorldPosition = WorldPosition.WithZ( BaseZPos );

		Manager.Instance.SpawnParticle( "cloud", WorldPosition.WithZ( 10f ) );

		if ( IsProxy )
			return;

		Deceleration = 0.92f;

		_rotateTimeOffset = Game.Random.Float( 0f, 10f );
		_personalRotateSpeed = Game.Random.Float( 5f, 7f );
		_personalBounceSpeed = Game.Random.Float( 16f, 22f );
	}

	protected override void OnUpdate()
	{
		base.OnUpdate();

		if ( Manager.Instance.IsGameOver )
			return;

		if ( IsProxy )
			return;

		LocalRotation = Rotation.From( new Angles( -20f, -90f + Utils.FastSin( _rotateTimeOffset + Time.Now * _personalRotateSpeed * 0.75f ) * 25f, Utils.FastSin( _rotateTimeOffset + Time.Now * _personalRotateSpeed ) * 3f ) );

		if ( !IsInTheAir )
			WorldPosition = WorldPosition.WithZ( BaseZPos + 3f + Utils.FastSin( Time.Now * _personalBounceSpeed ) * 2f );
	}

	public override void Colliding( Thing other, float percent, float dt )
	{
		base.Colliding( other, percent, dt );

		if ( CantBeCollected )
			return;

		if ( other is Player player )
		{
			if ( !player.IsDead )
			{
				player.ReloadInstantly();
				player.RechargeDashInstantly();

				player.CollectItemEffect();

				Remove();
			}
		}
	}
}