Skip to content

Instantly share code, notes, and snippets.

@DDru
Created September 10, 2025 06:29
Show Gist options
  • Select an option

  • Save DDru/a07393736fc867bee0be2fdd2d48608c to your computer and use it in GitHub Desktop.

Select an option

Save DDru/a07393736fc867bee0be2fdd2d48608c to your computer and use it in GitHub Desktop.
Proto Entity Config
public class EntityConfig : ScriptableObject {
[SerializeField] private EntityConfig parentConfig;
[SerializeField] private bool replace;
[SerializeReference, SubclassSelector] private IConfigParam[] stats;
public void Apply(ProtoWorld world, ProtoEntity entity) {
if (parentConfig)
parentConfig.Apply(world, entity);
SetData(world, entity);
}
public void Release(ProtoWorld world, ProtoEntity entity) {
if (parentConfig)
parentConfig.Release(world, entity);
RemoveData(world, entity);
}
protected virtual void SetData(ProtoWorld world, ProtoEntity entity) {
for (var i = 0; i < stats.Length; i++) {
stats[i].Execute(world, entity, replace, false);
}
}
protected virtual void RemoveData(ProtoWorld world, ProtoEntity entity) {
for (var i = 0; i < stats.Length; i++) {
stats[i].Execute(world, entity, replace, true);
}
}
}
[System.Serializable]
public abstract class IConfigParam {
public abstract void Execute(ProtoWorld world, ProtoEntity entity, bool replace, bool remove);
}
[System.Serializable]
public abstract class ConfigParamBase<T, V> : IConfigParam where T : IProtoAspect where V : struct {
protected T aspect;
private V temp;
public override void Execute(ProtoWorld world, ProtoEntity entity, bool replace, bool remove) {
aspect = (T)world.Aspect(typeof(T));
Apply(entity, replace, remove);
}
protected abstract void Apply(ProtoEntity entity, bool replace, bool remove);
protected ref V SetPool(ProtoPool<V> pool, ProtoEntity entity, bool replace, bool remove) {
if (remove) {
pool.Del(entity);
return ref temp;
}
if (replace) {
return ref pool.GetOrAdd(entity);
}
return ref pool.Add(entity);
}
protected ProtoPool<V> GetPool() {
var pool = (ProtoPool<V>)aspect.World().Pool(typeof(V));
return pool;
}
}
// var 1 ---
[System.Serializable]
public class AutoConfigParam<T, V> : ConfigParamBase<T, V> where T : IProtoAspect where V : struct, IStatComponent {
[SerializeField] private int value;
protected override void Apply(ProtoEntity entity, bool replace, bool remove) {
SetPool(GetPool(), entity, replace, remove).Value = value;
}
}
[System.Serializable]
public class HealthMaxAutoConfigParam : AutoConfigParam<TestAspect, StatHealthMax> { }
// var 2 ---
[System.Serializable]
public class HealthConfigParam : ConfigParamBase<TestAspect, Health> {
[SerializeField] private int health;
protected override void Apply(ProtoEntity entity, bool replace, bool remove) {
SetPool(GetPool(), entity, replace, remove).value = health;
//SetPool(aspect.Healths, entity, replace, remove).value = health;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment