示例#1
0
    // ----------------------------------------------------------------
    public Vector2 GetDestination(MapInfo mi, ref PositionOnGrid newPos, EDirection d, bool doorOpened)
    {
        int x = newPos.X;
        int y = newPos.Y;

        int destX = newPos.X;
        int destY = newPos.Y;

        List <List <int> > map = mi.PuzzleMapExtended;

        PositionOnGrid nextPos = newPos.NextPos(d);

        // Ca not move if undef or void
        if (false == mi.IsDefined(nextPos) || mi.IsTileType(nextPos, ETile.Void))
        {
            return(GetDestinationFromTile(newPos));
        }

        // Can not move from board back to in tile
        if (mi.IsTileType(newPos, ETile.In) == false && mi.IsTileType(nextPos, ETile.In))
        {
            return(GetDestinationFromTile(newPos));
        }

        // If in, move just one tile
        if (mi.IsTileType(nextPos, ETile.In))
        {
            newPos = nextPos;
            return(GetDestinationFromTile(newPos));
        }

        // If out, move only if door opened
        if (mi.IsTileType(nextPos, ETile.Out))
        {
            if (doorOpened)
            {
                newPos = nextPos;
            }
            return(GetDestinationFromTile(newPos));
        }

        // Else inside map - move through all tiles with the same color
        switch (d)
        {
        case EDirection.Up:
            for (int i = y + 1; i < map.Count; i++)
            {
                if (map[y + 1][x] == map[i][x])
                {
                    destY = i;
                }
                else
                {
                    break;
                }
            }
            break;

        case EDirection.Right:
            for (int i = x + 1; i < map[y].Count; i++)
            {
                if (map[y][x + 1] == map[y][i])
                {
                    destX = i;
                }
                else
                {
                    break;
                }
            }
            break;

        case EDirection.Down:
            for (int i = y - 1; i >= 0; i--)
            {
                if (map[y - 1][x] == map[i][x])
                {
                    destY = i;
                }
                else
                {
                    break;
                }
            }
            break;

        case EDirection.Left:
            for (int i = x - 1; i >= 0; i--)
            {
                if (map[y][x - 1] == map[y][i])
                {
                    destX = i;
                }
                else
                {
                    break;
                }
            }
            break;
        }

        newPos.X = destX;
        newPos.Y = destY;

        return(GetDestinationFromTile(newPos));
    }