sasalka/Code/Weapons/Flashlight.cs
2025-06-28 18:13:47 +03:00

72 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Sandbox;
using Sasalka;
namespace Sandbox.Weapons;
/// <summary>
/// Пример фонарика - используемого предмета с собственной логикой
/// Показывает универсальность системы IUseable
/// </summary>
public sealed class Flashlight : Component, IUseable
{
[Property] public GameObject LightSource { get; set; }
[Property] public SoundPointComponent ToggleSound { get; set; }
[Property] public float Cooldown { get; set; } = 0.5f;
private bool _isOn = false;
private TimeSince _lastUseTime;
protected override void OnStart()
{
base.OnStart();
// Изначально фонарик выключен
if (LightSource != null)
{
LightSource.Enabled = false;
}
}
/// <summary>
/// Проверка возможности использования
/// </summary>
public bool CanUse()
{
return IsValid && _lastUseTime >= Cooldown;
}
/// <summary>
/// Использование фонарика (включение/выключение)
/// </summary>
public void Use()
{
if (!CanUse()) return;
_lastUseTime = 0;
ToggleLight();
}
/// <summary>
/// Переключение света
/// </summary>
private void ToggleLight()
{
_isOn = !_isOn;
if (LightSource != null)
{
LightSource.Enabled = _isOn;
}
// Воспроизводим звук
ToggleSound?.StartSound();
// Можно добавить дополнительные эффекты
Log.Info($"Фонарик {( _isOn ? "включен" : "выключен" )}");
}
/// <summary>
/// Получить состояние фонарика
/// </summary>
public bool IsOn => _isOn;
}