A component that represents a floating perk visual. It stores icon, rarity, timing, lifetime, velocity, opacity and updates position and opacity over time, destroying the GameObject when its lifetime elapses.
using System;
using Sandbox;
public sealed class PerkFloater : Component
{
public string IconPath { get; set; }
public Rarity Rarity { get; set; }
public bool Curse { get; set; }
public float Progress { get; set; }
public float Opacity { get; set; }
private TimeSince _timeSinceSpawn;
private RealTimeSince _realTimeSinceSpawn;
public ParticleEffect.TimingMode Timing { get; set; }
public float Lifetime { get; set; }
public Vector3 Velocity { get; set; }
public bool WasRemoved { get; set; }
public float RemovedRotateSpeed { get; set; }
protected override void OnEnabled()
{
base.OnEnabled();
_timeSinceSpawn = 0f;
_realTimeSinceSpawn = 0f;
Lifetime = Game.Random.Float(0.9f, 1.2f);
}
protected override void OnUpdate()
{
base.OnUpdate();
var elapsedTime = Timing == ParticleEffect.TimingMode.GameTime ? _timeSinceSpawn.Relative : _realTimeSinceSpawn.Relative;
if ( elapsedTime > Lifetime )
{
GameObject.Destroy();
}
else
{
Opacity = Utils.Map( elapsedTime, 0f, Lifetime, 1f, 0f, EasingType.QuadIn );
var dt = Timing == ParticleEffect.TimingMode.GameTime ? Time.Delta : RealTime.Delta;
WorldPosition += Velocity * dt;
Velocity *= MathF.Max( 1f - 3f * dt, 0f );
}
}
}