An Item subclass representing a healing/lock pickup. It spawns scaled, floats/rotates, is attracted to nearby damaged players based on their stats, plays a sound, triggers a collect effect, and removes itself on pickup.
using System;
using Sandbox;
public class LockItem : Item
{
private float _rotateTimeOffset;
private float _personalRotateSpeed;
public override Vector3 SpawnScale => new Vector3( 5f );
protected override float AttractRangeFactor => 1f;
protected override float AttractStrengthFactor => 0.8f;
protected override void OnStart()
{
base.OnStart();
Lifetime = 70f;
ShouldCheckBounds = true;
PushStrength = 500f;
BaseZPos = 24f;
WorldPosition = WorldPosition.WithZ( BaseZPos );
if ( IsProxy )
return;
Deceleration = 0.92f;
_rotateTimeOffset = Game.Random.Float( 0f, 10f );
_personalRotateSpeed = Game.Random.Float( 3f, 5f );
}
protected override void OnUpdate()
{
base.OnUpdate();
if ( Manager.Instance.IsGameOver )
return;
if ( IsProxy )
return;
LocalRotation = Rotation.From( new Angles( Utils.FastSin( _rotateTimeOffset + Time.Now * _personalRotateSpeed ) * 7f, 180f, 0f ) );
foreach ( var player in Manager.Instance.AlivePlayers )
{
if( player.Health < player.GetSyncStat(PlayerStat.MaxHp) )
{
var dist_sqr = (Position2D - player.Position2D).LengthSquared;
var req_dist_sqr = MathF.Pow( player.GetSyncStat(PlayerStat.CoinAttractRange), 2f );
if ( dist_sqr < req_dist_sqr )
{
if ( (player.Position2D - Position2D).LengthSquared > Manager.TOUCH_DIST_REQUIRED_SQR )
Velocity += (player.Position2D - Position2D).Normal * Utils.Map( dist_sqr, req_dist_sqr, 0f, 0f, 1f, EasingType.Linear ) * player.GetSyncStat(PlayerStat.CoinAttractStrength) * Time.Delta;
}
}
}
if ( !IsInTheAir )
WorldPosition = WorldPosition.WithZ( BaseZPos + 3f + Utils.MapReturn( Utils.FastSin( _rotateTimeOffset + Time.Now * _personalRotateSpeed ), -1f, 1f, -1f, 1f, EasingType.Linear ) * 3f );
}
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 )
{
// todo: different sfx
Manager.Instance.PlaySfxNearbyRpc( "heal", player.Position2D, pitch: Game.Random.Float( 1.6f, 1.7f ), volume: 1.1f, maxDist: 350f );
player.CollectItemEffect();
Remove();
}
}
}
}