This commit is contained in:
CortexCore
2024-03-31 23:31:00 +08:00
parent e179d2eb53
commit b7b89ee71a
641 changed files with 31286 additions and 22134 deletions

View File

@@ -0,0 +1,17 @@
{
"name": "BITKit.Entities.Variable",
"rootNamespace": "",
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:709caf8d7fb6ef24bbba0ab9962a3ad0"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: f602b6f914f91d4499383d874af1decd
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View 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);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f0c6c2d406bfdaf46ba4ab9883f71066
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: