Skip to content

Commit

Permalink
Syntax Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
knowlife4 committed Jan 25, 2023
1 parent e479b55 commit 5952e1a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 70 deletions.
29 changes: 5 additions & 24 deletions ECT/Assets/Runtime/ECTComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace ECT
{
public abstract class ECTComponent<MyRoot, MyParent, MyComponent> : ScriptableObject, IComponent where MyRoot : class, IRoot where MyParent : class, IParent where MyComponent : class, IComponent
public abstract class ECTComponent<MyRoot, MyParent, MyComponent> : ScriptableObject, IComponent
where MyRoot : class, IRoot
where MyParent : class, IParent
where MyComponent : class, IComponent
{
protected MyComponent ThisComponent => this as MyComponent;

Expand All @@ -15,7 +18,6 @@ public IReference CreateReference(IParent root, IParent parent)
ISystem system = System;
IReference reference = CreateReference((MyRoot)root, (MyParent)parent, system);
system.SetReference(reference);
system.Initialize();

return reference;
}
Expand All @@ -29,28 +31,7 @@ public class ComponentReference : ECTReference<MyRoot, MyParent, MyComponent>
public ComponentReference(MyComponent reference, MyRoot root, MyParent parent, ISystem system) : base(reference, root, parent, system) {}
}

public abstract class ComponentGroup : ECTComponent<MyRoot, MyParent, MyComponent>, IParent
{
public ECTBranch<ChildComponent> Components;

protected override IReference CreateReference(MyRoot root, MyParent parent, ISystem system) => new ComponentGroupReference(this, root, parent, system, Components);

public abstract class ChildComponent : ECTComponent<MyRoot, ComponentGroup, ChildComponent> {}

public class ComponentGroupReference : ECTReference<MyRoot, MyParent, ComponentGroup>
{
public ECTReferenceBranch ReferenceBranch { get; }

public ComponentGroupReference(ComponentGroup reference, MyRoot root, MyParent parent, ISystem system, ECTBranch<ChildComponent> branch) : base(reference, root, parent, system) => ReferenceBranch = new(branch.Components);

public override FindSystem Get<FindSystem>() => System is FindSystem system ? system : ReferenceBranch.Get<FindSystem>();
}

public new abstract class ComponentSystem<Component> : ECTSystem<ComponentGroupReference, Component, MyRoot, MyParent> where Component : class, IComponent
{
protected override void OnUpdate() => Reference.ReferenceBranch.Update(Reference.Root, Reference.Component);
}
}
public abstract class ComponentGroup : ECTComponentGroup<MyRoot, MyParent, MyComponent, ComponentGroup> {}
}

public interface IComponent
Expand Down
6 changes: 5 additions & 1 deletion ECT/Assets/Runtime/ECTComponentGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

namespace ECT
{
public abstract class ECTComponentGroup<MyRoot, MyParent, MyComponent, MyComponentGroup> : ECTComponent<MyRoot, MyParent, MyComponent>, IParent where MyRoot : class, IRoot where MyParent : class, IParent where MyComponent : class, IComponent where MyComponentGroup : class, IComponent, IParent
public abstract class ECTComponentGroup<MyRoot, MyParent, MyComponent, MyComponentGroup> : ECTComponent<MyRoot, MyParent, MyComponent>, IParent
where MyRoot : class, IRoot
where MyParent : class, IParent
where MyComponent : class, IComponent
where MyComponentGroup : class, IComponent, IParent
{
public ECTBranch<ChildComponent> Components;

Expand Down
84 changes: 48 additions & 36 deletions ECT/Assets/Runtime/ECTReferenceBranch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,21 @@ namespace ECT
{
public class ECTReferenceBranch : IReferenceBranch
{
public ECTReferenceBranch(IComponent[] components)
{
Components = components;
}
public ECTReferenceBranch(IComponent[] components) => Components = components;

public IComponent[] Components { get; }

public IReference[] References { get; private set; }

public ECTAction OnPreUpdate { get; }
public ECTAction OnPostUpdate { get; }

void CreateReferences (IParent root, IParent parent)
public void Update (IRoot root, IParent parent)
{
List<IReference> references = new();
foreach (var component in Components)
{
references.Add(component.CreateReference(root, parent));
}
if(References == null || Components.Length != References.Length) SetupReferences(root, parent);

References = references.ToArray();
}

void UpdateReferences ()
{
foreach (var reference in References)
{
ISystem system = reference.System;
bool passed = true;

foreach (var validation in system.GetValidations())
{
if(validation.Successful == true) continue;
passed = false;
break;
}

if(passed) reference.System.Update();
}
OnPreUpdate.Execute();
UpdateReferences();
OnPostUpdate.Execute();
}

public FindSystem Get<FindSystem>() where FindSystem : class, ISystem
Expand All @@ -61,16 +37,52 @@ void UpdateReferences ()
public ECTValidation QuerySystem<FindSystem> (out FindSystem find) where FindSystem : class, ISystem
{
find = Get<FindSystem>();
return new ECTValidation(find != null);
return new ECTValidation(Get<FindSystem>() != null);
}

public void Update (IParent root, IParent parent)
void SetupReferences (IRoot root, IParent parent)
{
if(References == null || Components.Length != References.Length) CreateReferences(root, parent);
References = CreateReferences(root, parent);

OnPreUpdate.Execute();
UpdateReferences();
OnPostUpdate.Execute();
InitializeReferences();
}

IReference[] CreateReferences (IRoot root, IParent parent)
{
List<IReference> references = new();
foreach (var component in Components)
{
references.Add(component.CreateReference(root, parent));
}
return references.ToArray();
}

void InitializeReferences ()
{
foreach (var reference in References)
{
InitializeReference(reference);
}
}

void UpdateReferences ()
{
foreach (var reference in References)
{
UpdateReference(reference);
}
}

void InitializeReference (IReference reference)
{
ISystem system = reference.System;
if(system.IsValid()) system.Initialize();
}

void UpdateReference (IReference reference)
{
ISystem system = reference.System;
if(system.IsValid()) system.Update();
}
}

Expand Down
25 changes: 24 additions & 1 deletion ECT/Assets/Runtime/ECTSystem.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
namespace ECT
{
public abstract class ECTSystem<MyReference, MyComponent, MyRoot, MyParent> : ISystem, IReferenceParent where MyReference : IReference where MyComponent : class, IComponent where MyRoot : class, IRoot where MyParent : class, IParent
public abstract class ECTSystem<MyReference, MyComponent, MyRoot, MyParent> : ISystem, IReferenceParent
where MyReference : IReference
where MyComponent : class, IComponent
where MyRoot : class, IRoot
where MyParent : class, IParent
{
public MyReference Reference { get; private set; }

protected virtual ECTValidation[] Validations => System.Array.Empty<ECTValidation>();
public ECTValidation[] GetValidations() => Validations;

public bool IsValid()
{
bool passed = true;
foreach (var validation in GetValidations())
{
if(validation.Successful == true) continue;
passed = false;
break;
}

return passed;
}

protected virtual void OnInitialize() {}
public void Initialize() => OnInitialize();

Expand All @@ -17,6 +34,11 @@ public abstract class ECTSystem<MyReference, MyComponent, MyRoot, MyParent> : IS

public ECTValidation QuerySystem<FindSystem>(out FindSystem find) where FindSystem : class, ISystem => Root.QuerySystem(out find);
public ECTValidation Validate<T>(T input, out T output) where T : class => ECTValidation.Validate(input, out output);
public ECTValidation Subscribe(ECTAction ectAction, System.Action action)
{
ectAction.Subscribe(action);
return new(true);
}

public MyComponent Component => Reference.Component as MyComponent;
public MyRoot Root => Reference.Root as MyRoot;
Expand All @@ -26,6 +48,7 @@ public abstract class ECTSystem<MyReference, MyComponent, MyRoot, MyParent> : IS
public interface ISystem
{
public ECTValidation[] GetValidations();
public bool IsValid();
public void Initialize();
public void Update();
public void SetReference(IReference reference);
Expand Down
4 changes: 2 additions & 2 deletions ECT/Assets/Samples/Platformer/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -27301,8 +27301,8 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 963194225}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 1, z: -10}
m_LocalRotation: {x: 0.27676514, y: -0.53306615, z: 0.18991841, w: 0.7766419}
m_LocalPosition: {x: 174.43591, y: 82.536736, z: 35.45572}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class PlayerEntity : ECTEntity<PlayerEntity>

public void Update()
{
if(Vector3.Distance(transform.position, Target.position) > 10) return;
UpdateChildren();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ public struct ParallelData : IParallelData<ParallelData>

public ParallelData Execute(NativeArray<ParallelData> dataArray)
{
foreach (var item in dataArray)
{

}

float3 direction = Target.position - Transform.position;

float rotationSpeed = Speed * DeltaTime;
Expand Down

0 comments on commit 5952e1a

Please sign in to comment.