Player/FlashLight.cs
using Sandbox;
public sealed class FlashLight : Component
{
[Property] GameObject FlashLightObject { get; set; }
public bool IsActive => FlashLightObject.Enabled;
Opium.PlayerController controller;
Angles targetAngles;
Vector3 targetPosition;
bool enabled = true;
public bool HasFlashLight = false;
protected override void OnStart()
{
base.OnStart();
controller = GameObject.Components.Get<Opium.PlayerController>(FindMode.InSelf);
}
public void GiveFlashLight()
{
HasFlashLight = true;
enabled = true;
FlashLightObject.Enabled = true;
}
protected override void OnUpdate()
{
if(!HasFlashLight ) return;
if( Input.Pressed( "flashlight" ) )
{
enabled = !enabled;
FlashLightSound();
}
FlashLightObject.Enabled = enabled;
targetAngles = targetAngles.LerpTo( controller.EyeAngles, Time.Delta * 10.0f );
targetPosition = targetPosition.LerpTo( controller.Transform.Position + Vector3.Up * controller.EyeHeight + controller.GameObject.Transform.Rotation.Forward, Time.Delta * 10.0f );
FlashLightObject.Transform.Rotation = targetAngles;
FlashLightObject.Transform.Position = targetPosition;
}
void FlashLightSound()
{
if ( enabled )
{
Sound.Play( "flashlight.on", Transform.Position );
}
else
{
Sound.Play( "flashlight.off", Transform.Position );
}
}
}