예제 #1
0
        static ExpressionSyntax InstantiateEntity(RoslynEcsTranslator translator, InstantiateNodeModel model)
        {
            var entityTranslator = translator.context.GetEntityManipulationTranslator();
            var entitySyntax     = translator.BuildPort(model.EntityPort).First() as ExpressionSyntax;

            return(entityTranslator.Instantiate(translator.context, entitySyntax).First() as ExpressionSyntax);
        }
예제 #2
0
        public static IEnumerable <SyntaxNode> BuildInstantiateEntityTranslator(this RoslynEcsTranslator translator,
                                                                                InstantiateNodeModel model, IPortModel portModel)
        {
            bool             hasAnyComponentInstruction = false;
            ExpressionSyntax newInstance = null;

            if (model.InstancePort.Connected)
            {
                newInstance = translator.BuildPort(model.InstancePort).SingleOrDefault() as ExpressionSyntax;
                hasAnyComponentInstruction = true;
            }

            // always instantiate
            var newEntity = InstantiateEntity(translator, model);

            if (hasAnyComponentInstruction) // == if connected. assignment of implicit variable is done only if it's required in the foreach loop below
            {
                yield return(RoslynBuilder.Assignment(newInstance, newEntity));
            }

            var entityTranslator = translator.context.GetEntityManipulationTranslator();

            foreach (ComponentOperation compOperation in model.GetEditableComponents())
            {
                var componentType = compOperation.Type.Resolve(model.GraphModel.Stencil);
                IEnumerable <SyntaxNode> instructions;
                // first instruction forces the declaration of an implicit variable if not already done
                if (!hasAnyComponentInstruction)
                {
                    hasAnyComponentInstruction = true;
                    string entityVariableName = translator.MakeUniqueName("entity");
                    yield return(RoslynBuilder.DeclareLocalVariable(typeof(Entity), entityVariableName, newEntity));

                    newInstance = SyntaxFactory.IdentifierName(entityVariableName);
                }
                switch (compOperation.OperationType)
                {
                case ComponentOperationType.AddComponent:
                    instructions = entityTranslator.AddComponent(translator.context, newInstance, componentType,
                                                                 BuildNewComponent(compOperation, componentType));
                    break;

                case ComponentOperationType.RemoveComponent:
                    instructions = entityTranslator.RemoveComponent(translator.context, newInstance, componentType);
                    break;

                case ComponentOperationType.SetComponent:
                    instructions = entityTranslator.SetComponent(translator.context, newInstance, componentType,
                                                                 BuildNewComponent(compOperation, componentType));
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                foreach (var instruction in instructions)
                {
                    yield return(instruction);
                }
            }

            if (!hasAnyComponentInstruction) // implicit variable, never used in the loop
            {
                yield return(SyntaxFactory.ExpressionStatement(newEntity));
            }

            ExpressionSyntax BuildNewComponent(ComponentOperation compOperation, Type componentType)
            {
                var componentInput = model.GetPortsForComponent(compOperation.Type).ToArray();
                var newComponent   = translator.BuildComponentFromInput(componentType, componentInput);

                return(newComponent);
            }
        }