Ejemplo n.º 1
0
        public static string Gen(string name, string upLevelContext)
        {
            CSharpWriter writer = new CSharpWriter(true);

            writer.Write($"public interface I{name}Component : LiteECS.IComponent");
            writer.EmptyScop();

            writer.Write($"public class {name}Entity : LiteECS.EntityT<I{name}Component>");
            using (new CSharpWriter.Scop(writer))
            {
                writer.Write($"public {name}Entity(LiteECS.Context context, int id) : base(context, id)");
                writer.EmptyScop(false);
            }

            writer.Write($"public static partial class {name}Components");
            using (new CSharpWriter.Scop(writer))
            {
                writer.Write($"public static System.Action<{name}Context> OnContextCreat;").NewLine();
                writer.Write($"public static int ComponentCount {{ get; private set; }}").NewLine();
            }

            writer.Write($"public class {name}Context : LiteECS.ContextT<{name}Entity>");
            using (new CSharpWriter.Scop(writer))
            {
                if (!string.IsNullOrEmpty(upLevelContext))
                {
                    writer.Write($"public {upLevelContext}Context {upLevelContext}{{ get; private set; }}").NewLine();
                }
                //Ctor
                writer.Write($"protected {name}Context(int componentTypeCount) : base(componentTypeCount, CreatFunc)");
                writer.EmptyScop();

                //CreatFunc
                writer.Write($"private static {name}Entity CreatFunc(LiteECS.Context context, int id)");
                using (new CSharpWriter.Scop(writer))
                {
                    writer.Write($"return new {name}Entity(context, id);");
                }

                //Creat
                writer.Write($"public static {name}Context Creat()");
                using (new CSharpWriter.Scop(writer))
                {
                    writer.Write($"var contxt = new {name}Context({name}Components.ComponentCount);").NewLine();
                    writer.Write($"{name}Components.OnContextCreat(contxt);").NewLine();
                    writer.Write("return contxt;");
                }
                if (!string.IsNullOrEmpty(upLevelContext))
                {
                    writer.Write($"public void Set{upLevelContext}( {upLevelContext}Context {upLevelContext.ToLower()} )");
                    using (new CSharpWriter.Scop(writer))
                    {
                        writer.Write($"{upLevelContext} = {upLevelContext.ToLower()};");
                    }
                }
            }


            return(writer.ToString());
        }
Ejemplo n.º 2
0
        public static string Gen(string className, string context, ECSSystemGenerateType type, string componentName = null)
        {
            CSharpWriter writer = new CSharpWriter(true);

            if (type < ECSSystemGenerateType.GroupExecute)
            {
                writer.Write($"public class {className} : LiteECS.I{type}System");
                using (new CSharpWriter.Scop(writer))
                {
                    writer.Write($"{context}Context context;").NewLine();
                    writer.Write($"public {className}({context}Context context)");
                    using (new CSharpWriter.Scop(writer))
                    {
                        writer.Write("this.context = context;");
                    }
                    writer.Write($"public void On{type}()");
                    writer.EmptyScop();
                }
            }
            else if (type == ECSSystemGenerateType.GroupExecute)
            {
                GenGroupExecuteSystem(writer, className, context, componentName);
            }
            else if (type == ECSSystemGenerateType.ReactiveExecute)
            {
                GenReactiveExecuteSystem(writer, className, context, componentName);
            }
            return(writer.ToString());
        }
Ejemplo n.º 3
0
 public static void GenReactiveExecuteSystem(CSharpWriter writer, string className, string context, string componentName)
 {
     if (string.IsNullOrEmpty(componentName))
     {
         componentName = $"I{context}Component";
     }
     writer.Write($"using TComponent = {componentName};").NewLine();
     writer.Write($"public class {className} : LiteECS.ReactiveExecuteSystem<{context}Entity, TComponent>");
     using (new CSharpWriter.Scop(writer))
     {
         writer.Write($" private {context}Context Context => context as {context}Context;").NewLine();
         writer.Write($"public {className}({context}Context context):base(context)");
         writer.EmptyScop();
         writer.Write($"protected override void OnExecuteEntity({context}Entity entity, TComponent component)");
         writer.EmptyScop();
     }
 }
Ejemplo n.º 4
0
        private void OnWizardCreate()
        {
            var cfg = ECSConfig.Instance.Contexts.Find(it => it.Name == contextNames[selectContextIdx]);

            if (cfg == null)
            {
                EditorUtility.DisplayDialog("错误", "选择的Context不存在,可能被移动位置或者删除\n,请重新创建", "ok");
                return;
            }
            int    typeIdx   = (int)createType;
            string dirPath   = Path.Combine(cfg.DirectoryPath, catalogs[typeIdx][selectCatalogIdx[typeIdx]]);
            string className = nameInput;

            if (createType == CreateType.Component)
            {
                if (!nameInput.StartsWith(cfg.Name))
                {
                    className = $"{cfg.Name}{nameInput}";
                }
            }
            else if (createType == CreateType.System)
            {
                if (!nameInput.StartsWith(cfg.Name))
                {
                    className = $"{cfg.Name}{nameInput}System";
                }
                else
                {
                    className = $"{nameInput}System";
                }
            }
            System.Reflection.Assembly assembly = ECSContextUtils.GetECSAssembly(cfg);
            if (assembly != null)
            {
                if (assembly.GetType(className) != null)
                {
                    EditorUtility.DisplayDialog("错误", $"创建失败,类型 {className} 已存在", "ok");
                    return;
                }
            }
            string filePath = Path.Combine(dirPath, $"{className}.cs");

            if (File.Exists(filePath))
            {
                EditorUtility.DisplayDialog("错误", "创建失败,目标文件已存在", "ok");
                return;
            }
            string content = null;

            if (createType == CreateType.Component)
            {
                CSharpWriter writer = new CSharpWriter(true);
                writer.Write($"public partial class {className} : I{cfg.Name}Component");
                writer.EmptyScop();
                content = writer.ToString();
            }
            else if (createType == CreateType.System)
            {
                string component = null;
                if (systemType >= ECSSystemGenerateType.GroupExecute && componentTypes != null)
                {
                    component = componentTypes[componentSelectIdx];
                }
                content = SystemGenertor.Gen(className, cfg.Name, systemType, component);
            }
            File.WriteAllText(filePath, content, System.Text.Encoding.UTF8);
            AssetDatabase.ImportAsset(filePath);
            if (OpenAfterCreate)
            {
                var obj = AssetDatabase.LoadMainAssetAtPath(filePath);
                if (obj)
                {
                    AssetDatabase.OpenAsset(obj);
                }
            }
        }