Beispiel #1
0
        private List <Vector2> TracePath(MapPiece end)
        {
            List <Vector2> path         = new List <Vector2>();
            MapPiece       currentPiece = end;

            while (currentPiece != this)
            {
                path.Add(currentPiece.position);
                currentPiece = currentPiece.parent;
            }
            path.Reverse();
            return(path);
        }
Beispiel #2
0
        //public bool CheckCollisions()
        //{

        //    for (int b = 0; b < colliders.Count; b++)
        //    {
        //        for (int i = 1; i < colliders.Count -1; i++)
        //        {
        //            if (i != b &&
        //                colliders[b].BoxCollider[0] ==
        //                colliders[i].BoxCollider[0] &&
        //                colliders[b].BoxCollider[1] ==
        //                colliders[i].BoxCollider[1])
        //                return true;
        //        }
        //    }
        //    return false;
        //}
        public Type CheckCollisions(MapPiece col, int x = 0, int y = 0)
        {
            for (int i = 0; i < colliders.Count; i++)
            {
                if (colliders[i].GetType() != col.GetType())
                {
                    if (col.BoxCollider[0] + (x) >= colliders[i].BoxCollider[0] && col.BoxCollider[2] + (x) <= colliders[i].BoxCollider[2] &&
                        col.BoxCollider[1] + (y) >= colliders[i].BoxCollider[1] && col.BoxCollider[3] + (y) <= colliders[i].BoxCollider[3])
                    {
                        return(colliders[i].GetType());
                    }
                }
            }
            return(null);
        }
Beispiel #3
0
        private void Update()
        {
            if (direction != Dir.none)
            {
                timer++;
                if (timer > 1)
                {
                    timer = 0;
                    if (pathRed != null)
                    {
                        if (counter < pathRed.Count)
                        {
                            redGhost.position = pathRed[counter];

                            counter++;
                        }
                        else
                        {
                            counter = 0;
                        }
                    }
                    if (pathPink != null)
                    {
                        timer = 0;
                        if (counter2 < pathPink.Count)
                        {
                            pinkGhost.position = pathPink[counter2];

                            counter2++;
                        }
                        else
                        {
                            counter2 = 0;
                        }
                    }
                }
                switch (direction)
                {
                case Dir.up:
                    if (col.CheckCollisions(snake, 0, -1) != typeof(Map))
                    {
                        snake.position = new Vector2(snake.position.PosX, Math.Max(0, snake.position.PosY - 1));
                        int minus = 3;
                        do
                        {
                            ghostTarget.position = new Vector2(snake.position.PosX, Math.Max(snake.position.PosY, snake.position.PosY - minus));
                            minus--;
                        } while (col.CheckCollisions(ghostTarget, ghostTarget.position.PosX, ghostTarget.position.PosY) == typeof(Map));
                        lastDirection = direction;
                    }

                    break;

                case Dir.left:
                    if (col.CheckCollisions(snake, -1, 0) != typeof(Map))
                    {
                        snake.position = new Vector2(Math.Max(0, snake.position.PosX - 1), snake.position.PosY);
                        int minus = 3;
                        do
                        {
                            ghostTarget.position = new Vector2(Math.Max(0, snake.position.PosX - minus), snake.position.PosY);
                            minus--;
                        } while (col.CheckCollisions(ghostTarget, ghostTarget.position.PosX, ghostTarget.position.PosY) == typeof(Map));
                        lastDirection = direction;
                    }

                    break;

                case Dir.down:
                    if (col.CheckCollisions(snake, 0, 1) != typeof(Map))
                    {
                        snake.position = new Vector2(snake.position.PosX, Math.Min(db.YDim - 1, snake.position.PosY + 1));
                        int minus = 3;
                        do
                        {
                            ghostTarget.position = new Vector2(Math.Min(db.YDim - 1, snake.position.PosX - minus), snake.position.PosY);
                            minus--;
                        } while (col.CheckCollisions(ghostTarget, ghostTarget.position.PosX, ghostTarget.position.PosY) == typeof(Map));
                        lastDirection = direction;
                    }

                    break;

                case Dir.right:
                    if (col.CheckCollisions(snake, 1, 0) != typeof(Map))
                    {
                        snake.position = new Vector2(Math.Min(db.XDim - 1, snake.position.PosX + 1), snake.position.PosY);
                        int minus = 3;
                        do
                        {
                            ghostTarget.position = new Vector2(Math.Min(db.XDim - 1, snake.position.PosX - minus), snake.position.PosY);
                            minus--;
                        } while (col.CheckCollisions(ghostTarget, ghostTarget.position.PosX, ghostTarget.position.PosY) == typeof(Map));
                        lastDirection = direction;
                    }

                    break;
                }
                if (redGhost.position.Equals(snake.position))
                {
                    target = redCorner;
                }
                if (redGhost.position.Equals(redCorner.position))
                {
                    target = snake;
                }

                pathRed = redGhost.CalcuatePath(target);

                pathPink = pinkGhost.CalcuatePath(ghostTarget);

                counter2 = 0;
                counter  = 0;
            }

            snake.UpdatePhysics();
            redGhost.UpdatePhysics();
            pinkGhost.UpdatePhysics();

            if (col.CheckCollisions(snake) == typeof(Pellet))
            {
                for (int i = 0; i < physicsObjects.Count; i++)
                {
                    if (physicsObjects[i].position.Equals(snake.position))
                    {
                        if (physicsObjects[i] != snake)
                        {
                            physicsObjects[i] = new EmptySpace(snake.position.PosX, snake.position.PosY);
                        }

                        mapVisuals[snake.position.PosX, snake.position.PosY] = ' ';
                    }
                }
                Score++;
            }
            if (col.CheckCollisions(snake) == typeof(Teleporter))
            {
                int toPlace = snake.position.PosX == 0 ? db.XDim - 2 : 1;
                snake.position = new Vector2(toPlace, snake.position.PosY);
            }

            direction = Dir.none;
        }
Beispiel #4
0
        public List <Vector2> CalcuatePath(MapPiece target)
        {
            open.Clear();
            closed.Clear();
            neibors.Clear();
            open.Add(this);

            while (open.Count > 0)
            {
                MapPiece current = open[0];

                for (int i = 1; i < open.Count; i++)
                {
                    if (open[i].f_Cost < current.f_Cost ||
                        open[i].f_Cost == current.f_Cost)
                    {
                        if (open[i].h_Cost < current.h_Cost)
                        {
                            current = open[i];
                        }
                    }
                }

                open.Remove(current);
                closed.Add(current);

                if (current.position.Equals(target.position))
                {
                    return(TracePath(current));
                }

                for (int c = 0; c < allPieces.Count; c++)
                {
                    if (allPieces[c].position.PosX == current.position.PosX + 1 &&
                        allPieces[c].position.PosY == current.position.PosY ||
                        allPieces[c].position.PosX == current.position.PosX - 1 &&
                        allPieces[c].position.PosY == current.position.PosY ||
                        allPieces[c].position.PosY == current.position.PosY + 1 &&
                        allPieces[c].position.PosX == current.position.PosX ||
                        allPieces[c].position.PosY == current.position.PosY - 1 &&
                        allPieces[c].position.PosX == current.position.PosX)
                    {
                        if (current.parent != allPieces[c] && allPieces[c].GetType() != typeof(Snake))
                        {
                            neibors.Add(allPieces[c]);
                        }
                    }
                }

                for (int b = 0; b < neibors.Count; b++)
                {
                    if (neibors[b].GetType() == typeof(Map) ||
                        closed.Contains(neibors[b]))
                    {
                        continue;
                    }

                    int newCostMov = current.g_Cost +
                                     GetDistace(current.position, neibors[b].position);
                    if (newCostMov < neibors[b].g_Cost ||
                        !open.Contains(neibors[b]))
                    {
                        neibors[b].g_Cost = newCostMov;
                        neibors[b].h_Cost =
                            GetDistace(neibors[b].position, target.position);
                        neibors[b].parent = current;

                        if (!open.Contains(neibors[b]))
                        {
                            open.Add(neibors[b]);
                        }
                    }
                }
                neibors.Clear();
            }
            return(null);
        }