Ejemplo n.º 1
0
        public override void Update()
        {
            if (Keyboard.IsKeyDown(Keys.Space) && GetMousePressed(Mouse.LeftButton))
            {
                if (GridSnapMouse != SnapToGrid(X, Y))
                {
                    Path = AStarGrid.FindPath(SnapToGrid(X, Y), GridSnapMouse);
                }
            }
            if (Keyboard.IsKeyDown(Keys.LeftShift) && GetMousePressed(Mouse.LeftButton))
            {
                bool canMove = true;
                foreach (Solid obj in Solid.Solids.ToList())
                {
                    if (obj.X == GridSnapMouse.X && obj.Y == GridSnapMouse.Y)
                    {
                        canMove = false;
                    }
                }
                if (canMove)
                {
                    X = GridSnapMouse.X;
                    Y = GridSnapMouse.Y;
                }
            }
            if (Path != null && Path.Count > 0)
            {
                xTarget = Path.Peek().Parent.Center.X;
                yTarget = Path.Peek().Parent.Center.Y;
                if (Path.Count == 1)
                {
                    if (X == Path.Peek().Parent.Center.X&& Y == Path.Peek().Parent.Center.Y)
                    {
                        walkToLastNode = true;
                    }
                    if (walkToLastNode)
                    {
                        xTarget = Path.Peek().Center.X;
                        yTarget = Path.Peek().Center.Y;
                    }
                    if (X == Path.Peek().Center.X&& Y == Path.Peek().Center.Y)
                    {
                        walkToLastNode = false;
                        Path.Pop();
                    }
                }
                direction = G.PointDirection(X, Y, xTarget, yTarget);
                xSpeed    = (float)Math.Cos(direction) * movementSpeed;
                ySpeed    = (float)Math.Sin(direction) * movementSpeed;

                X += Math.Abs(X - xTarget) < Math.Abs(xSpeed) ? 0 : xSpeed;
                Y += Math.Abs(Y - yTarget) < Math.Abs(ySpeed) ? 0 : ySpeed;

                if (Math.Abs(X - xTarget) < Math.Abs(xSpeed))
                {
                    X = xTarget;
                }
                if (Math.Abs(Y - yTarget) < Math.Abs(ySpeed))
                {
                    Y = yTarget;
                }
                if (X == xTarget && Y == yTarget)
                {
                    if (Path.Count != 1 && Path.Count != 0)
                    {
                        Path.Pop();
                    }
                }
            }

            base.Update();
        }
Ejemplo n.º 2
0
 public void NewPath(int targetX, int targetY)
 {
     Path = AStarGrid.FindPath(SnapToGrid(X, Y), SnapToGrid(targetX, targetY));
 }