예제 #1
0
    public static bool CheckValidMove(Vector2Int aOffset, List <BlockObject> aSelectedList)
    {
        // print("CHECKING VALID MOVE");
        HashSet <Vector2Int> checkTopPositions = new HashSet <Vector2Int>();
        HashSet <Vector2Int> checkBotPositions = new HashSet <Vector2Int>();

        foreach (BlockObject block in aSelectedList)
        {
            // for each position inside block
            for (int x = 0; x < block.size.x; x++)
            {
                for (int y = 0; y < block.size.y; y++)
                {
                    Vector2Int currentPos = block.pos + aOffset + new Vector2Int(x, y);
                    // check if in bounds of level
                    if (!GameUtil.IsInside(currentPos, Vector2Int.zero, BoardManager.Instance.levelData.boardSize))
                    {
                        // print("IS BLOCKED! NOT IN LEVEL BOUNDS");
                        return(false);
                    }
                    BlockObject maybeABlock = GetBlockOnPosition(currentPos);
                    // check if this position is occupied by something not itself
                    if (maybeABlock != null && !aSelectedList.Contains(maybeABlock))
                    {
                        // print("IS BLOCKED! INVALID MOVE!!!");
                        return(false);
                    }
                    // check if a mob exists on this position
                    MobObject maybeAMob = GetMobOnPosition(currentPos);
                    if (maybeAMob != null)
                    {
                        // print("IS BLOCKED! MOB EXISTS HERE");
                        return(false);
                    }
                }
            }
            // fill in checktop/checkbot positions as a list of pos that need to be checked
            for (int x = block.ghostPos.x; x < block.ghostPos.x + block.size.x; x++)
            {
                int        aboveY = block.ghostPos.y + block.size.y;
                int        belowY = block.ghostPos.y - 1;
                Vector2Int topPos = new Vector2Int(x, aboveY);
                Vector2Int botPos = new Vector2Int(x, belowY);
                foreach (BlockObject otherBlock in aSelectedList)
                {
                    if (PlayingManager.GetSelectedBlockOnPosition(topPos) == null)
                    {
                        // print("toppos does not contain a selected block");
                        Vector2Int checkPos = new Vector2Int(x, aboveY);
                        checkTopPositions.Add(checkPos);
                    }
                    if (PlayingManager.GetSelectedBlockOnPosition(botPos) == null)
                    {
                        // print("botpos does not contain a selected block");
                        Vector2Int checkPos = new Vector2Int(x, belowY);
                        checkBotPositions.Add(checkPos);
                    }
                }
            }
        }
        bool connectedOnTop = false;
        bool connectedOnBot = false;

        foreach (Vector2Int pos in checkTopPositions)
        {
            // print("examining pos " + pos);
            if (GetBlockOnPosition(pos) != null && !aSelectedList.Contains(GetBlockOnPosition(pos)))
            {
                connectedOnTop = true;
                // print("connected on top at" + pos);
            }
        }
        foreach (Vector2Int pos in checkBotPositions)
        {
            // print("examining pos " + pos);
            if (GetBlockOnPosition(pos) != null && !aSelectedList.Contains(GetBlockOnPosition(pos)))
            {
                connectedOnBot = true;
                // print("connected on bot at" + pos);
            }
        }
        if (connectedOnTop == true && connectedOnBot == true)
        {
            // print("IS SANDWICHED! INVALID MOVE!!!");
            return(false);
        }
        else if (connectedOnTop == false && connectedOnBot == false)
        {
            // print("IS FLOATING! INVALID MOVE!!!");
            return(false);
        }
        else
        {
            // print ("VALID MOVE");
            return(true);
        }
    }