Наследование: MegaMan.Engine.Component
        public override Component Clone()
        {
            MovementComponent newone = new MovementComponent {
                Floating = this.Floating, CanMove = this.CanMove
            };

            return(newone);
        }
Пример #2
0
        private RectangleF CheckEntityCollisions(List <Collision> blockEntities, CollisionBox hitbox, RectangleF boundbox, bool pushAway = true, bool solidOnly = true)
        {
            foreach (var entity in Parent.Entities.GetAll())
            {
                if (entity == Parent)
                {
                    continue;
                }
                CollisionComponent coll = entity.GetComponent <CollisionComponent>();
                if (coll == null)
                {
                    continue;
                }

                IEnumerable <CollisionBox> collToCheck = (solidOnly) ? coll.HitByBoxes(hitbox).Where(box => box.Properties.Blocking) : coll.HitByBoxes(hitbox);

                foreach (CollisionBox targetBox in collToCheck)
                {
                    // if he's blocking, check for collision and maybe push me away

                    RectangleF rect       = targetBox.BoxAt(coll.PositionSrc.Position);
                    RectangleF adjustrect = rect;
                    adjustrect.X      -= Const.PixelEpsilon;
                    adjustrect.Y      -= Const.PixelEpsilon;
                    adjustrect.Width  += 2 * Const.PixelEpsilon;
                    adjustrect.Height += 2 - Const.PixelEpsilon;
                    RectangleF intersection = RectangleF.Intersect(boundbox, adjustrect);
                    if (intersection.Width != 0 || intersection.Height != 0)
                    {
                        blockEntities.Add(new Collision(hitbox, targetBox, coll));

                        if (hitbox.PushAway && pushAway)
                        {
                            float             vx, vy;
                            MovementComponent mov = entity.GetComponent <MovementComponent>();
                            vx = MovementSrc.VelocityX;
                            vy = MovementSrc.VelocityY;
                            if (mov != null)
                            {
                                vx -= mov.VelocityX;
                                vy -= mov.VelocityY;
                            }

                            PointF offset = hitbox.GetIntersectionOffset(rect, boundbox, vx, vy, false, false);
                            if (offset.X != 0 || offset.Y != 0)
                            {
                                PositionSrc.Offset(offset.X, offset.Y);
                                boundbox.Offset(offset.X, offset.Y);
                            }
                        }
                    }
                }
            }
            return(boundbox);
        }
Пример #3
0
 public override void RegisterDependencies(Component component)
 {
     if (component is PositionComponent)
     {
         PositionSrc = component as PositionComponent;
     }
     else if (component is MovementComponent)
     {
         MovementSrc = component as MovementComponent;
     }
 }
Пример #4
0
        public static Effect ParseEffect(XElement child)
        {
            Effect action = entity => { };

            foreach (XElement prop in child.Elements())
            {
                switch (prop.Name.LocalName)
                {
                case "Flying":
                    bool f = prop.GetValue <bool>();
                    action += entity =>
                    {
                        MovementComponent mov = entity.GetComponent <MovementComponent>();
                        if (mov != null)
                        {
                            mov.Flying = f;
                        }
                    };
                    break;

                case "FlipSprite":
                    bool flip = prop.GetValue <bool>();
                    action += entity =>
                    {
                        MovementComponent mov = entity.GetComponent <MovementComponent>();
                        if (mov != null)
                        {
                            mov.FlipSprite = flip;
                        }
                    };
                    break;

                case "X":
                    action += ParseMovementBehavior(prop, Axis.X);
                    break;

                case "Y":
                    action += ParseMovementBehavior(prop, Axis.Y);
                    break;

                case "Velocity":
                    action += ParseMovementBehavior(prop, Axis.Both);
                    break;
                }
            }
            return(action);
        }
Пример #5
0
        public override Component Clone()
        {
            MovementComponent newone = new MovementComponent {Flying = this.Flying, CanMove = this.CanMove};

            return newone;
        }
Пример #6
0
        private static Effect ParseMovementBehavior(XElement prop, Axis axis)
        {
            Effect action;

            float?mag = prop.TryAttribute <float?>("magnitude");

            if (mag != 0)
            {
                XAttribute dirattr = prop.Attribute("direction");
                string     direction;
                direction = dirattr == null ? "Same" : dirattr.Value;
                switch (direction)
                {
                case "Up":
                    action = entity =>
                    {
                        MovementComponent mov = entity.GetComponent <MovementComponent>();
                        if (mov != null)
                        {
                            mov.VelocityY = -1 * (mag ?? Math.Abs(mov.VelocityY));
                        }
                    };
                    break;

                case "Down":
                    action = entity =>
                    {
                        MovementComponent mov = entity.GetComponent <MovementComponent>();
                        if (mov != null)
                        {
                            mov.VelocityY = (mag ?? Math.Abs(mov.VelocityY));
                        }
                    };
                    break;

                case "Left":
                    action = entity =>
                    {
                        MovementComponent mov = entity.GetComponent <MovementComponent>();
                        if (mov != null)
                        {
                            mov.VelocityX = -mag ?? -1 * Math.Abs(mov.VelocityX);
                        }
                    };
                    break;

                case "Right":
                    action = entity =>
                    {
                        MovementComponent mov = entity.GetComponent <MovementComponent>();
                        if (mov != null)
                        {
                            mov.VelocityX = mag ?? Math.Abs(mov.VelocityX);
                        }
                    };
                    break;

                case "Same":
                    action = entity =>
                    {
                        if (mag == null)
                        {
                            return;
                        }
                        float fmag = mag ?? 0;

                        MovementComponent mov = entity.GetComponent <MovementComponent>();
                        if (mov == null)
                        {
                            return;
                        }
                        Direction dir = mov.Direction;

                        if (axis != Axis.Y)
                        {
                            mov.VelocityX = (dir == Direction.Right) ? fmag : ((dir == Direction.Left) ? -fmag : 0);
                        }
                        if (axis != Axis.X)
                        {
                            mov.VelocityY = (dir == Direction.Down) ? fmag : ((dir == Direction.Up) ? -fmag : 0);
                        }
                    };
                    break;

                case "Reverse":
                    action = entity =>
                    {
                        if (mag == null)
                        {
                            return;
                        }
                        float fmag = mag ?? 0;

                        MovementComponent mov = entity.GetComponent <MovementComponent>();
                        if (mov == null)
                        {
                            return;
                        }
                        Direction dir = mov.Direction;

                        if (axis != Axis.Y)
                        {
                            mov.VelocityX = (dir == Direction.Left) ? fmag : ((dir == Direction.Right) ? -fmag : 0);
                        }
                        if (axis != Axis.X)
                        {
                            mov.VelocityY = (dir == Direction.Up) ? fmag : ((dir == Direction.Down) ? -fmag : 0);
                        }
                    };
                    break;

                case "Inherit":
                    action = entity =>
                    {
                        MovementComponent mov = entity.GetComponent <MovementComponent>();
                        if (mov == null)
                        {
                            return;
                        }
                        if (entity.Parent != null)
                        {
                            Direction dir = entity.Parent.Direction;

                            if (axis != Axis.Y)
                            {
                                mov.VelocityX = (dir == Direction.Right) ? (mag ?? Math.Abs(mov.VelocityX)) : ((dir == Direction.Left) ? (-mag ?? -1 * Math.Abs(mov.VelocityX)) : 0);
                            }
                            if (axis != Axis.X)
                            {
                                mov.VelocityY = (dir == Direction.Down) ? (mag ?? Math.Abs(mov.VelocityY)) : ((dir == Direction.Up) ? (-mag ?? -1 * Math.Abs(mov.VelocityY)) : 0);
                            }
                        }
                        else
                        {
                            mov.VelocityY = 0;
                        }
                    };
                    break;

                case "Input":
                    action = entity =>
                    {
                        MovementComponent mov   = entity.GetComponent <MovementComponent>();
                        InputComponent    input = entity.GetComponent <InputComponent>();
                        if (mov == null || input == null)
                        {
                            return;
                        }

                        if (axis != Axis.Y)
                        {
                            if (input.Left)
                            {
                                mov.VelocityX = -mag ?? -1 * Math.Abs(mov.VelocityX);
                            }
                            else if (input.Right)
                            {
                                mov.VelocityX = mag ?? Math.Abs(mov.VelocityX);
                            }
                        }
                        if (axis != Axis.X)
                        {
                            if (input.Down)
                            {
                                mov.VelocityY = mag ?? Math.Abs(mov.VelocityY);
                            }
                            else if (input.Up)
                            {
                                mov.VelocityY = -mag ?? -1 * Math.Abs(mov.VelocityY);
                            }
                            else
                            {
                                mov.VelocityY = 0;
                            }
                        }
                    };
                    break;

                case "Player":
                    action = entity =>
                    {
                        MovementComponent mov = entity.GetComponent <MovementComponent>();
                        PositionComponent pos = entity.GetComponent <PositionComponent>();
                        if (mov == null || pos == null)
                        {
                            return;
                        }

                        GameEntity        player    = entity.Entities.GetEntityById("Player");
                        PositionComponent playerPos = player.GetComponent <PositionComponent>();

                        if (axis == Axis.X)
                        {
                            if (pos.Position.X > playerPos.Position.X)
                            {
                                mov.VelocityX = -mag ?? -1 * Math.Abs(mov.VelocityX);
                            }
                            else if (pos.Position.X < playerPos.Position.X)
                            {
                                mov.VelocityX = mag ?? Math.Abs(mov.VelocityX);
                            }
                        }
                        else if (axis == Axis.Y)
                        {
                            if (pos.Position.Y > playerPos.Position.Y)
                            {
                                mov.VelocityY = -mag ?? -1 * Math.Abs(mov.VelocityY);
                            }
                            else if (pos.Position.Y < playerPos.Position.Y)
                            {
                                mov.VelocityY = mag ?? Math.Abs(mov.VelocityY);
                            }
                        }
                        else
                        {
                            float  dx  = playerPos.Position.X - pos.Position.X;
                            float  dy  = playerPos.Position.Y - pos.Position.Y;
                            double hyp = Math.Pow(dx, 2) + Math.Pow(dy, 2);
                            hyp = Math.Pow(hyp, 0.5);

                            mov.VelocityX = (float)(mag * dx / hyp);
                            mov.VelocityY = (float)(mag * dy / hyp);
                        }
                    };
                    break;

                default: action = new Effect(entity => { }); break;
                }
            }
            else
            {
                if (axis == Axis.X)
                {
                    action = entity =>
                    {
                        MovementComponent mov = entity.GetComponent <MovementComponent>();
                        if (mov != null)
                        {
                            mov.VelocityX = 0;
                        }
                    }
                }
                ;
                else
                {
                    action = entity =>
                    {
                        MovementComponent mov = entity.GetComponent <MovementComponent>();
                        if (mov != null)
                        {
                            mov.VelocityY = 0;
                        }
                    }
                };
            }

            return(action);
        }
    }