コード例 #1
0
        public static IEnumerable <SyntaxNode> BuildSetTranslation(this RoslynEcsTranslator translator, SetPositionNodeModel model, IPortModel portModel)
        {
            IPortModel entityOrComponentPort = model.InstancePort;

            if (!translator.GetComponentFromEntityOrComponentPort(model, entityOrComponentPort, out _, out ExpressionSyntax setValue, RoslynEcsTranslator.AccessMode.Write))
            {
                yield break;
            }

            switch (model.Mode)
            {
            case SetPositionNodeModel.TranslationMode.Float3:
                yield return(RoslynBuilder.SetProperty(
                                 model.Add ? RoslynBuilder.AssignmentKind.Add : RoslynBuilder.AssignmentKind.Set,
                                 setValue,
                                 translator.BuildPort(model.GetInput(SetPositionNodeModel.InputType.Value)).FirstOrDefault() as ExpressionSyntax,
                                 nameof(Translation.Value)));

                break;

            case SetPositionNodeModel.TranslationMode.Axis:
                var inputTypes = new[]
                {
                    Tuple.Create(SetPositionNodeModel.InputType.X, nameof(float3.x)),
                    Tuple.Create(SetPositionNodeModel.InputType.Y, nameof(float3.y)),
                    Tuple.Create(SetPositionNodeModel.InputType.Z, nameof(float3.z))
                };
                foreach (var inputType in inputTypes)
                {
                    IPortModel axisPort = model.GetInput(inputType.Item1);
                    yield return(RoslynBuilder.SetProperty(
                                     model.Add ? RoslynBuilder.AssignmentKind.Add : RoslynBuilder.AssignmentKind.Set,
                                     setValue,
                                     translator.BuildPort(axisPort).FirstOrDefault() as ExpressionSyntax,
                                     nameof(Translation.Value), inputType.Item2));
                }
                break;
            }
        }
コード例 #2
0
        public static IEnumerable <SyntaxNode> BuildSetTranslation(this RoslynEcsTranslator translator, SetRotationNodeModel model, IPortModel portModel)
        {
            ExpressionSyntax BuildPortForInput(SetRotationNodeModel.InputType inputType)
            {
                return(translator.BuildPort(model.GetInput(inputType)).FirstOrDefault() as ExpressionSyntax);
            }

            ExpressionSyntax value;

            switch (model.Mode)
            {
            case SetRotationNodeModel.RotationMode.Axis:
                var axisValue  = BuildPortForInput(SetRotationNodeModel.InputType.Axis);
                var angleValue = BuildPortForInput(SetRotationNodeModel.InputType.Angle);
                axisValue = InvocationExpression(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
                                                                        IdentifierName(nameof(math)), IdentifierName(nameof(math.normalize)))).WithArgumentList(ArgumentList(SingletonSeparatedList(Argument(axisValue))));
                value = InvocationExpression(
                    MemberAccessExpression(
                        SyntaxKind.SimpleMemberAccessExpression,
                        IdentifierName(nameof(quaternion)),
                        IdentifierName(nameof(quaternion.AxisAngle))))
                        .WithArgumentList(
                    ArgumentList(
                        SeparatedList(new[]
                {
                    Argument(axisValue),
                    Argument(angleValue)
                })));
                break;

            case SetRotationNodeModel.RotationMode.Quaternion:
                value = BuildPortForInput(SetRotationNodeModel.InputType.Quaternion);
                break;

            case SetRotationNodeModel.RotationMode.Euler:
                value = InvocationExpression(
                    MemberAccessExpression(
                        SyntaxKind.SimpleMemberAccessExpression,
                        IdentifierName(nameof(quaternion)),
                        IdentifierName(nameof(quaternion.EulerXYZ))))
                        .WithArgumentList(
                    ArgumentList(
                        SeparatedList(new[]
                {
                    Argument(BuildPortForInput(SetRotationNodeModel.InputType.X)),
                    Argument(BuildPortForInput(SetRotationNodeModel.InputType.Y)),
                    Argument(BuildPortForInput(SetRotationNodeModel.InputType.Z))
                })));
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            if (!translator.GetComponentFromEntityOrComponentPort(model, model.InstancePort, out _, out ExpressionSyntax setValue, RoslynEcsTranslator.AccessMode.Write))
            {
                yield break;
            }
            var finalValue = !model.Add
                ? value
                : InvocationExpression( // rot.value = math.mul(rot.value, <input>)
                MemberAccessExpression(
                    SyntaxKind.SimpleMemberAccessExpression,
                    IdentifierName(nameof(math)),
                    IdentifierName(nameof(math.mul))))
                             .WithArgumentList(
                ArgumentList(
                    SeparatedList(new[]
            {
                Argument(
                    MemberAccessExpression(
                        SyntaxKind.SimpleMemberAccessExpression,
                        setValue,
                        IdentifierName(nameof(Rotation.Value)))),
                Argument(value)
            })));

            yield return(RoslynBuilder.SetProperty(
                             RoslynBuilder.AssignmentKind.Set,
                             setValue,
                             finalValue,
                             nameof(Rotation.Value)
                             ));
        }