Files
Fishing2/Assets/Scripts/Fishing2~/Unit/Equipment/FlashlightComponent.cs

69 lines
2.3 KiB
C#

using Fantasy.Entitas;
using Fantasy.Entitas.Interface;
using UnityEngine;
namespace NBF.Fishing2
{
/// <summary>
/// 手电筒组件
/// </summary>
public class FlashlightComponent : Entity
{
private GameObject _fishingLight;
public class FlashlightComponentAwakeSystem : AwakeSystem<FlashlightComponent>
{
protected override void Awake(FlashlightComponent self)
{
var numericComponent = self.Parent.GetComponent<NumericComponent>();
var unityComponent = self.Parent.GetComponent<UnitUnityComponent>();
// self._fishingLight = unityComponent.Asset.FishingLight;
// self.Change(numericComponent[NumericType.Flashlight]);
var mapUnit = self.Parent as MapUnit;
if (mapUnit.IsSelf())
{
var inputComponent = self.Scene.GetComponent<InputComponent>();
inputComponent.OnPlayerPerformed += self.OnPlayerCanceled;
}
}
}
public class FlashlightComponentDestroySystem : DestroySystem<FlashlightComponent>
{
protected override void Destroy(FlashlightComponent self)
{
self._fishingLight = null;
var mapUnit = self.Parent as MapUnit;
if (mapUnit.IsSelf())
{
var inputComponent = self.Scene.GetComponent<InputComponent>();
if (inputComponent != null)
{
inputComponent.OnPlayerPerformed -= self.OnPlayerCanceled;
}
}
}
}
private void OnPlayerCanceled(string action)
{
if (action == InputDef.Player.UseTorch)
{
var numericComponent = Parent.GetComponent<NumericComponent>();
if (numericComponent != null)
{
var oldValue = numericComponent[NumericType.Flashlight];
numericComponent[NumericType.Flashlight] = oldValue > 0 ? 0 : 1;
}
}
}
public void Change(long value)
{
_fishingLight.SetActive(value > 0);
}
}
}