1
This commit is contained in:
105
Src/Unity/Scripts/Entity/Variable/EntityVariable.cs
Normal file
105
Src/Unity/Scripts/Entity/Variable/EntityVariable.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Pool;
|
||||
|
||||
namespace BITKit.Entities.Variables
|
||||
{
|
||||
public interface IVariable
|
||||
{
|
||||
string Variable { get; set; }
|
||||
string[] Variables { get; }
|
||||
event Action<string,string> OnVariableChanged;
|
||||
event Func<string,bool> TryChangeVariable;
|
||||
event Func<string> CollectVariables;
|
||||
|
||||
}
|
||||
[Serializable]
|
||||
public sealed class IVariableFromDI : InjectFromDI<IVariable>,IVariable
|
||||
{
|
||||
private IVariable _variableImplementation => Value;
|
||||
public string Variable
|
||||
{
|
||||
get => _variableImplementation.Variable;
|
||||
set => _variableImplementation.Variable = value;
|
||||
}
|
||||
|
||||
public string[] Variables => _variableImplementation.Variables;
|
||||
|
||||
public event Action<string, string> OnVariableChanged
|
||||
{
|
||||
add => _variableImplementation.OnVariableChanged += value;
|
||||
remove => _variableImplementation.OnVariableChanged -= value;
|
||||
}
|
||||
|
||||
public event Func<string, bool> TryChangeVariable
|
||||
{
|
||||
add => _variableImplementation.TryChangeVariable += value;
|
||||
remove => _variableImplementation.TryChangeVariable -= value;
|
||||
}
|
||||
|
||||
public event Func<string> CollectVariables
|
||||
{
|
||||
add => _variableImplementation.CollectVariables += value;
|
||||
remove => _variableImplementation.CollectVariables -= value;
|
||||
}
|
||||
}
|
||||
|
||||
[CustomType(typeof(IVariable),true)]
|
||||
public sealed class EntityVariable:MonoBehaviour,IVariable
|
||||
{
|
||||
public string Variable
|
||||
{
|
||||
get => _variable;
|
||||
set
|
||||
{
|
||||
if (TryChangeVariable is null || TryChangeVariable.CastAsFunc().All(Yes))
|
||||
{
|
||||
OnVariableChanged?.Invoke(_variable, _variable = value);
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
bool Yes(Func<string, bool> arg)
|
||||
{
|
||||
return arg.Invoke(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string[] Variables {
|
||||
get
|
||||
{
|
||||
if (CollectVariables is null)
|
||||
{
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
var listPool=ListPool<string>.Get();
|
||||
foreach (var x in CollectVariables.CastAsFunc())
|
||||
{
|
||||
listPool.Add(x.Invoke());
|
||||
}
|
||||
var result = listPool.ToArray();
|
||||
ListPool<string>.Release(listPool);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public event Action<string,string> OnVariableChanged;
|
||||
public event Func<string,bool> TryChangeVariable;
|
||||
public event Func<string> CollectVariables;
|
||||
|
||||
private string _variable="Default";
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (GetType().GetCustomAttribute<CustomTypeAttribute>() is { AsGlobal: true })
|
||||
{
|
||||
DI.Register<IVariable>(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user