コード例 #1
0
        private Effect ParsePositionBehavior(PositionEffectAxisInfo axisInfo, Axis axis)
        {
            Effect action = e => { };

            var baseVar   = axisInfo.BaseVar;
            var offsetVar = axisInfo.OffsetVar;

            if (baseVar != null)
            {
                if (axis == Axis.X)
                {
                    action = entity => {
                        var x = CheckNumericVar(entity, baseVar);
                        if (x.HasValue)
                        {
                            PositionComponent pos = entity.GetComponent <PositionComponent>();
                            if (pos != null)
                            {
                                pos.SetX(x.Value);
                            }
                        }
                    }
                }
                ;
                else
                {
                    action = entity => {
                        var y = CheckNumericVar(entity, baseVar);
                        if (y.HasValue)
                        {
                            PositionComponent pos = entity.GetComponent <PositionComponent>();
                            if (pos != null)
                            {
                                pos.SetY(y.Value);
                            }
                        }
                    }
                };
            }
            else if (axisInfo.Base == null)
            {
                action = entity => {
                    PositionComponent pos = entity.GetComponent <PositionComponent>();
                    if (pos != null && entity.Parent != null)
                    {
                        PositionComponent parentPos = entity.Parent.GetComponent <PositionComponent>();
                        if (parentPos != null)
                        {
                            if (axis == Axis.X)
                            {
                                pos.SetX(parentPos.X);
                            }
                            else if (axis == Axis.Y)
                            {
                                pos.SetY(parentPos.Y);
                            }
                        }
                    }
                };
            }
            else
            {
                if (axis == Axis.X)
                {
                    action = entity => {
                        PositionComponent pos = entity.GetComponent <PositionComponent>();
                        if (pos != null)
                        {
                            pos.SetPosition(new PointF(axisInfo.Base.Value, pos.Position.Y));
                        }
                    }
                }
                ;
                else
                {
                    action = entity => {
                        PositionComponent pos = entity.GetComponent <PositionComponent>();
                        if (pos != null)
                        {
                            pos.SetPosition(new PointF(pos.Position.X, axisInfo.Base.Value));
                        }
                    }
                };
            }

            if (axisInfo.Offset != null || offsetVar != null)
            {
                switch (axisInfo.OffsetDirection)
                {
                case OffsetDirection.Inherit:
                    action += entity => {
                        var offset            = axisInfo.Offset ?? CheckNumericVar(entity, offsetVar) ?? 0;
                        PositionComponent pos = entity.GetComponent <PositionComponent>();
                        if (pos != null && entity.Parent != null)
                        {
                            Direction offdir = entity.Parent.Direction;
                            switch (offdir)
                            {
                            case Direction.Down: pos.Offset(0, offset); break;

                            case Direction.Up: pos.Offset(0, -offset); break;

                            case Direction.Left: pos.Offset(-offset, 0); break;

                            case Direction.Right: pos.Offset(offset, 0); break;
                            }
                        }
                    };
                    break;

                case OffsetDirection.Input:
                    action += entity => {
                        var offset              = axisInfo.Offset ?? CheckNumericVar(entity, offsetVar) ?? 0;
                        PositionComponent pos   = entity.GetComponent <PositionComponent>();
                        InputComponent    input = entity.GetComponent <InputComponent>();
                        if (input != null && pos != null)
                        {
                            if (axis == Axis.Y)
                            {
                                if (input.Down)
                                {
                                    pos.Offset(0, offset);
                                }
                                else if (input.Up)
                                {
                                    pos.Offset(0, -offset);
                                }
                            }
                            else
                            {
                                if (input.Left)
                                {
                                    pos.Offset(-offset, 0);
                                }
                                else if (input.Right || (!input.Up && !input.Down))
                                {
                                    pos.Offset(offset, 0);
                                }
                            }
                        }
                    };
                    break;

                case OffsetDirection.Left:
                    action += entity => {
                        var offset            = axisInfo.Offset ?? CheckNumericVar(entity, offsetVar) ?? 0;
                        PositionComponent pos = entity.GetComponent <PositionComponent>();
                        if (pos != null)
                        {
                            pos.Offset(-offset, 0);
                        }
                    };
                    break;

                case OffsetDirection.Right:
                    action += entity => {
                        var offset            = axisInfo.Offset ?? CheckNumericVar(entity, offsetVar) ?? 0;
                        PositionComponent pos = entity.GetComponent <PositionComponent>();
                        if (pos != null)
                        {
                            pos.Offset(offset, 0);
                        }
                    };
                    break;

                case OffsetDirection.Down:
                    action += entity => {
                        var offset            = axisInfo.Offset ?? CheckNumericVar(entity, offsetVar) ?? 0;
                        PositionComponent pos = entity.GetComponent <PositionComponent>();
                        if (pos != null)
                        {
                            pos.Offset(0, offset);
                        }
                    };
                    break;

                case OffsetDirection.Up:
                    action += entity => {
                        var offset            = axisInfo.Offset ?? CheckNumericVar(entity, offsetVar) ?? 0;
                        PositionComponent pos = entity.GetComponent <PositionComponent>();
                        if (pos != null)
                        {
                            pos.Offset(0, -offset);
                        }
                    };
                    break;
                }
            }
            return(action);
        }