我很赞同ET的ECS写法,毕竟自己的项目也遇到了重构困难的问题。不过,社区有没有大佬可以提供一些ECS写法的实例,比方说把一个OOP继承的写法,改成ECS的。
ET的代码中,我看了MailBox的和NumericWatcher的。
下面按自己的理解写了一份,感觉不太对,又不知道这种要怎么写。请问以下大家遇到这样的要怎么写呢?
我觉得如果有更多的ECS指导示例,对我这种菜鸟来说能更好的学习并且使用ET。非常感谢!!
using System;
using ET;
namespace BattleCore
{
#region OOP
public abstract class Test_A
{
public virtual void Func1(){}
public virtual void Func2(){}
public virtual void Func3(){}
public virtual void Func4(){}
public virtual void Func5(){}
}
public class Test_B : Test_A
{
private string propB;
public override void Func1()
{
Log.Info($"Func1 {this.propB}");
}
public override void Func2()
{
Log.Info($"Func2 {this.propB}");
}
public override void Func3()
{
Log.Info($"Func3 {this.propB}");
}
public override void Func4()
{
Log.Info($"Func4 {this.propB}");
}
public override void Func5()
{
Log.Info($"Func5 {this.propB}");
}
}
public class Test_C : Test_B
{
private string propC;
public override void Func1()
{
base.Func1();
Log.Info($"Func1 {this.propC}");
}
public override void Func2()
{
Log.Info($"Func2 {this.propC}");
base.Func1();
}
}
public class Obj
{
private Test_A a;
void Test()
{
a = new Test_C();
this.a.Func1();
this.a.Func2();
}
}
#endregion
#region ET ECS
public class Obj_ET : Entity, IAwake
{
void Awake()
{
}
public void TestFunc()
{
}
}
public enum Test_Com_Type
{
Test_B,
Test_C,
}
public struct FuncTestEvent_1
{
public Test_Com com;
}
public struct FuncTestEvent_2
{
public Test_Com com;
}
public struct FuncTestEvent_3
{
public Test_Com com;
}
// 有更多需要继续添加
public class Test_Com : Entity,IAwake<Test_Com_Type>
{
public Test_Com_Type ComType;
}
[EntitySystemOf(typeof(Test_Com))]
public static partial class Test_ComSystem
{
[EntitySystem]
private static void Awake(this BattleCore.Test_Com self, BattleCore.Test_Com_Type args2)
{
self.ComType = args2;
switch (args2)
{
case Test_Com_Type.Test_B:
{
self.AddComponent<Test_Com_B>();
}
break;
case Test_Com_Type.Test_C:
{
self.AddComponent<Test_Com_C>();
}
break;
default:
throw new ArgumentOutOfRangeException(nameof(args2), args2, null);
}
}
public static void FuncTest1(this Test_Com self)
{
EventSystem.Instance.Invoke((long)self.ComType, new FuncTestEvent_1(){com = self});
}
public static void FuncTest2(this Test_Com self)
{
EventSystem.Instance.Invoke((long)self.ComType, new FuncTestEvent_2(){com = self});
}
public static void FuncTest3(this Test_Com self)
{
EventSystem.Instance.Invoke((long)self.ComType, new FuncTestEvent_3(){com = self});
}
}
public class Test_Com_B : Entity, IAwake
{
public string propB;
}
[EntitySystemOf(typeof(Test_Com_B))]
public static partial class Test_Com_BSystem
{
[EntitySystem]
private static void Awake(this BattleCore.Test_Com_B self)
{
}
public static void Func1(this Test_Com_B self)
{
Log.Info($"Func1 {self.propB}");
}
public static void Func2(this Test_Com_B self)
{
Log.Info($"Func2 {self.propB}");
}
public static void Func3(this Test_Com_B self)
{
Log.Info($"Func3 {self.propB}");
}
}
public class Test_Com_C : Entity, IAwake
{
public string propC;
}
[EntitySystemOf(typeof(Test_Com_C))]
public static partial class Test_Com_CSystem
{
[EntitySystem]
private static void Awake(this BattleCore.Test_Com_C self)
{
self.Parent.AddComponent<Test_Com_B>();
}
public static void Func1(this Test_Com_C self)
{
Log.Info($"Func1 {self.propC}");
}
public static void Func2(this Test_Com_C self)
{
Log.Info($"Func2 {self.propC}");
}
public static void Func3(this Test_Com_C self)
{
Log.Info($"Func3 {self.propC}");
}
}
[Invoke((long)Test_Com_Type.Test_B)]
public partial class Test_Com_B_FuncTest_1 : AInvokeHandler<FuncTestEvent_1>
{
public override void Handle(FuncTestEvent_1 args)
{
Test_Com_B comB = args.com.GetComponent<Test_Com_B>();
comB.Func1();
}
}
[Invoke((long)Test_Com_Type.Test_B)]
public partial class Test_Com_B_FuncTest_2 : AInvokeHandler<FuncTestEvent_2>
{
public override void Handle(FuncTestEvent_2 args)
{
Test_Com_B comB = args.com.GetComponent<Test_Com_B>();
comB.Func2();
}
}
[Invoke((long)Test_Com_Type.Test_B)]
public partial class Test_Com_B_FuncTest_3 : AInvokeHandler<FuncTestEvent_3>
{
public override void Handle(FuncTestEvent_3 args)
{
Test_Com_B comB = args.com.GetComponent<Test_Com_B>();
comB.Func3();
}
}
[Invoke((long)Test_Com_Type.Test_C)]
public partial class Test_Com_C_FuncTest_1 : AInvokeHandler<FuncTestEvent_1>
{
public override void Handle(FuncTestEvent_1 args)
{
Test_Com_B comB = args.com.GetComponent<Test_Com_B>();
comB.Func1();
Test_Com_C comC = args.com.GetComponent<Test_Com_C>();
comC.Func1();
}
}
[Invoke((long)Test_Com_Type.Test_C)]
public partial class Test_Com_C_FuncTest_2 : AInvokeHandler<FuncTestEvent_2>
{
public override void Handle(FuncTestEvent_2 args)
{
Test_Com_C comC = args.com.GetComponent<Test_Com_C>();
comC.Func1();
Test_Com_B comB = args.com.GetComponent<Test_Com_B>();
comB.Func1();
}
}
#endregion
}