Example #1
0
    private void AssignGridType(int[] gridID, string gridType, string direction)
    {
        LaserGameBoardGrid targetGrid = gameBoard.transform.GetChild(gridID[0]).GetChild(gridID[1]).gameObject.GetComponent <LaserGameBoardGrid>();

        targetGrid.gameGridType = gridType;
        targetGrid.isFixedItem  = true;
        switch (gridType)
        {
        case ("Start"):
            targetGrid.startNode.Add(new StartNode((int[])gridID.Clone(), new int[3] {
                0, 1, 0
            }, direction));
            //startNodes.Add(new StartNode((int[])gridID.Clone(), new int[3] {0, 1, 0}, direction));
            break;

        case ("Goal"):
            targetGrid.goalNode = new GoalNode((int[])gridID.Clone(), new int[3] {
                1, 1, 0
            }, direction);
            //goalNodes.Add(new GoalNode((int[])gridID.Clone(), new int[3] {1, 1, 0}, direction));
            break;

        default:
            break;
        }
    }
    private void ClearInfoOnGrid(int[] gridID)
    {
        if (gridID[0] < 0 || gridID[1] < 0 || gridID[0] >= canvasManager.gameBoardSizeWithBoarder[0] || gridID[1] >= canvasManager.gameBoardSizeWithBoarder[1])
        {
            return;
        }
        LaserGameBoardGrid currentGrid = GameObject.Find("grid_" + gridID[0].ToString() + "_" + gridID[1].ToString()).GetComponent <LaserGameBoardGrid>();

        currentGrid.isGoalNodeIgnited = false;
        string gridType = currentGrid.gameGridType;

        for (int i = 0; i < GridInfo.laserPattern.Length; i++)
        {
            currentGrid.transform.Find("Laser").Find(GridInfo.laserPattern[i]).gameObject.GetComponent <Image>().enabled = false;
            currentGrid.GetComponent <LaserGameBoardGrid>().LaserPattern[GridInfo.laserPattern[i]] = new int[3] {
                0, 0, 0
            };
        }
    }
Example #3
0
    public void LeftClickedGrid(int[] gameGridID)
    {
        LaserGameBoardGrid targetGrid     = GameObject.Find("grid_" + gameGridID[0].ToString() + "_" + gameGridID[1].ToString()).GetComponent <LaserGameBoardGrid>();
        string             targetGridType = targetGrid.gameGridType;

        if (targetGridType == "Disabled")
        {
            Debug.Log("Disabled Grid");
            return;
        }

        if (loadedLevel == 0)
        {
            switch (pointerDebugMode)
            {
            case ("Start"):
                if (targetGridType == "Goal")
                {
                    break;
                }
                if (targetGridType == "Start")
                {
                    targetGrid.RotateSelectedGrid();
                }
                else
                {
                    // COLOR SWITCH
                    int   indexRgb = ddStartColor.value + 1;
                    int[] startRgb = new int[3] {
                        indexRgb % 2, (indexRgb / 2) % 2, indexRgb / 4
                    };
                    //startNodes.Add(new StartNode((int[])gameGridID.Clone(), startRgb, "CT"));
                    targetGrid.startNode.Add(new StartNode((int[])gameGridID.Clone(), startRgb, "CT"));
                    targetGrid.gameGridType = "Start";
                }
                break;

            case ("Goal"):
                if (targetGridType == "Start")
                {
                    break;
                }
                if (targetGridType == "Goal")
                {
                    targetGrid.RotateSelectedGrid();
                }
                else
                {
                    int   indexRgb = ddGoalColor.value + 1;
                    int[] goalRgb  = new int[3] {
                        indexRgb % 2, (indexRgb / 2) % 2, indexRgb / 4
                    };
                    //goalNodes.Add(new GoalNode((int[])gameGridID.Clone(), goalRgb, "T"));
                    targetGrid.goalNode     = new GoalNode((int[])gameGridID.Clone(), goalRgb, "T");
                    targetGrid.gameGridType = "Goal";
                }
                break;

            case ("Glass"):
                if (targetGridType == "Start" || targetGridType == "Goal" || targetGridType == "Invalid")
                {
                    break;
                }
                targetGrid.gameGridType = "Glass";
                break;

            case ("Blackbody"):
                if (targetGridType == "Start" || targetGridType == "Goal" || targetGridType == "Invalid")
                {
                    break;
                }
                targetGrid.gameGridType = "Blackbody";
                break;

            case ("Mirror"):
                if (targetGridType == "Start" || targetGridType == "Goal" || targetGridType == "Invalid")
                {
                    break;
                }
                if (targetGridType == "Mirror")
                {
                    targetGrid.RotateSelectedGrid();
                }
                else
                {
                    targetGrid.gameGridType  = "Mirror";
                    targetGrid.mirrorPattern = "BR";
                }
                break;

            case ("MirrorBlock"):
                if (targetGridType == "Start" || targetGridType == "Goal" || targetGridType == "Invalid")
                {
                    break;
                }
                targetGrid.gameGridType = "MirrorBlock";
                break;

            case ("Empty"):
                // Setting starting or ending grid as obstacles is not allowed.
                if (targetGridType == "Start" || targetGridType == "Goal" || targetGridType == "Invalid")
                {
                    break;
                }
                targetGrid.gameGridType = "Empty";
                break;

            case ("Invalid"):
                // Setting starting or ending grid as obstacles is not allowed.
                if (targetGridType == "Start" || targetGridType == "Goal")
                {
                    break;
                }
                targetGrid.gameGridType = "Invalid";
                break;

            default:
                break;
            }
        }
        else if (loadedLevel != 0)
        {
            int itemIndex = Array.IndexOf(inventoryGameItemName, pointerGameMode);

            if (targetGrid.isFixedItem)
            {
                return;
            }

            if (pointerGameMode == "")
            {
                if (targetGridType != "Empty")
                {
                    targetGrid.gameGridType = "Empty";
                    inventoryGameItemNum[Array.IndexOf(inventoryGameItemName, targetGridType)] += 1;
                }
                return;
            }

            if (inventoryGameItemNum[itemIndex] <= 0)
            {
                if (pointerGameMode == "Mirror" && targetGridType == "Mirror")
                {
                    targetGrid.RotateSelectedGrid();
                }
                return;
            }

            targetGrid.gameGridType          = pointerGameMode;
            inventoryGameItemNum[itemIndex] -= 1;
            if (targetGridType != "Empty")
            {
                inventoryGameItemNum[Array.IndexOf(inventoryGameItemName, targetGridType)] += 1;
            }

            switch (pointerGameMode)
            {
            case ("Glass"):
                if (targetGridType == "Start" || targetGridType == "Goal" || targetGridType == "Invalid")                          // || inventoryGameItemNum[Array.IndexOf(inventoryGameItemName, "Glass")]  <= 0)
                {
                    break;
                }
                targetGrid.gameGridType = "Glass";
                break;

            case ("Blackbody"):
                if (targetGridType == "Start" || targetGridType == "Goal" || targetGridType == "Invalid")
                {
                    break;
                }
                targetGrid.gameGridType = "Blackbody";
                break;

            case ("Mirror"):
                if (targetGridType == "Start" || targetGridType == "Goal" || targetGridType == "Invalid")
                {
                    break;
                }
                if (targetGridType == "Mirror")
                {
                    targetGrid.RotateSelectedGrid();
                }
                else
                {
                    targetGrid.gameGridType  = "Mirror";
                    targetGrid.mirrorPattern = "BR";
                }
                break;

            case ("MirrorBlock"):
                if (targetGridType == "Start" || targetGridType == "Goal" || targetGridType == "Invalid")
                {
                    break;
                }
                targetGrid.gameGridType = "MirrorBlock";
                break;

            default:
                break;
            }
        }
    }
Example #4
0
    private void SaveAsLevel()
    {
        // Debug mode
        if (loadedLevel != 0)
        {
            return;
        }

        int inputLevel = Convert.ToInt32(inputSaveAsLevel.text);

        LaserLevel newLevel = new LaserLevel();

        newLevel.level     = Convert.ToInt32(inputSaveAsLevel.text);
        newLevel.boardSize = (int[])gameBoardSize.Clone();

        LaserAvailableItems glass = new LaserAvailableItems();

        glass.type     = "Glass";
        glass.quantity = Convert.ToInt32(inputGlassNum.text);
        LaserAvailableItems mirror = new LaserAvailableItems();

        mirror.type     = "Mirror";
        mirror.quantity = Convert.ToInt32(inputMirrorNum.text);
        LaserAvailableItems mirrorBlock = new LaserAvailableItems();

        mirrorBlock.type     = "MirrorBlock";
        mirrorBlock.quantity = Convert.ToInt32(inputMirrorBlockNum.text);
        LaserAvailableItems blackbody = new LaserAvailableItems();

        blackbody.type     = "Blackbody";
        blackbody.quantity = Convert.ToInt32(inputBlackbodyNum.text);

        newLevel.availableItems = new List <LaserAvailableItems>();
        newLevel.availableItems.Add(glass);
        newLevel.availableItems.Add(mirror);
        newLevel.availableItems.Add(mirrorBlock);
        newLevel.availableItems.Add(blackbody);

        newLevel.gameBoardItems = new List <LaserGameBoardItems>();
        string[] searchTypes = new string[7] {
            "Start", "Goal", "Glass", "Mirror", "MirrorBlock", "Blackbody", "Invalid"
        };
        for (int index = 0; index < searchTypes.Length; index++)
        {
            LaserGameBoardItems fixedItem = new LaserGameBoardItems();
            fixedItem.type = searchTypes[index];
            for (int row = 0; row < gameBoardSize[0]; row++)
            {
                for (int col = 0; col < gameBoardSize[1]; col++)
                {
                    LaserGameBoardGrid targetGrid = GameObject.Find("grid_" + (row + extraRows).ToString() + "_" + (col + extraCols).ToString()).GetComponent <LaserGameBoardGrid>();
                    string             gridType   = targetGrid.gameGridType;
                    if (gridType == searchTypes[index])
                    {
                        LaserGameBoardItemParameters parameter = new LaserGameBoardItemParameters();
                        parameter.location = (int[])targetGrid.gameGridID.Clone();

                        switch (searchTypes[index])
                        {
                        case ("Start"):
                            parameter.direction = targetGrid.startNode[0].Direction;
                            break;

                        case ("Goal"):
                            parameter.direction = targetGrid.goalNode.Direction;
                            break;

                        case ("Mirror"):
                            parameter.direction = targetGrid.mirrorPattern;
                            break;

                        default:
                            break;
                        }
                        fixedItem.parameters.Add(parameter);
                    }
                }
            }
            newLevel.gameBoardItems.Add(fixedItem);
        }

        gameConfiguration.SaveGameConfiguration(newLevel);
    }
Example #5
0
    private void LoadLevel()
    {
        if (loadedLevel == 0)
        {
            controlPanel.SetActive(true);
            gameItemPanel.SetActive(false);
            if (debugBoardSize[0] != 0 && debugBoardSize[1] != 0)
            {
                gameBoardSize = (int[])debugBoardSize.Clone();
            }
            else
            {
                gameBoardSize = (int[])gameConfiguration.Config.properties[0].sandboxModeBoardSize.Clone();
            }
        }
        else
        {
            controlPanel.SetActive(false);
            gameItemPanel.SetActive(true);

            foreach (LaserLevel levelToSearch in gameConfiguration.Config.levels)
            {
                if (levelToSearch.level == loadedLevel)
                {
                    gameBoardSize = (int[])levelToSearch.boardSize.Clone();
                }
            }
        }

        gameBoardSizeWithBoarder = new int[2] {
            gameBoardSize[0] + extraRows * 2, gameBoardSize[1] + extraCols * 2
        };

        gameBoard.GetComponent <RectTransform>().sizeDelta = new Vector2(gameBoardSizeWithBoarder[1] * 70, gameBoardSizeWithBoarder[0] * 70);

        GameObject gameBoardRow;
        GameObject gameBoardCol;

        for (int row = 0; row < gameBoardSizeWithBoarder[0]; row++)
        {
            gameBoardRow      = Instantiate(gameBoardRowPrefab, gameBoard.transform);
            gameBoardRow.name = "row_" + row.ToString();
            for (int col = 0; col < gameBoardSizeWithBoarder[1]; col++)
            {
                gameBoardCol      = Instantiate(gameBoardColumnPrefab, gameBoardRow.transform);
                gameBoardCol.name = "grid_" + row.ToString() + "_" + col.ToString();
                gameBoardCol.GetComponent <LaserGameBoardGrid>().gameGridID = new int[] { row, col };
                if (row < extraRows || row >= gameBoardSizeWithBoarder[0] - 2 || col < extraCols || col >= gameBoardSizeWithBoarder[1] - 2)
                {
                    gameBoardCol.GetComponent <LaserGameBoardGrid>().gameGridType = "Invalid";
                }
                else
                {
                    gameBoardCol.GetComponent <LaserGameBoardGrid>().gameGridType = "Empty";
                }
            }
        }

        if (loadedLevel == 0)
        {
            AssignGridType(new int[] { extraRows, extraCols }, "Start", "CR");
            AssignGridType(new int[] { gameBoardSizeWithBoarder[0] - extraRows - 1, gameBoardSizeWithBoarder[1] - extraCols - 1 }, "Goal", "T");
        }
        else
        {
            foreach (LaserLevel levelToSearch in gameConfiguration.Config.levels)
            {
                if (levelToSearch.level == loadedLevel)
                {
                    foreach (LaserGameBoardItems item in levelToSearch.gameBoardItems)
                    {
                        foreach (LaserGameBoardItemParameters parameter in item.parameters)
                        {
                            LaserGameBoardGrid targetGrid = gameBoard.transform.GetChild(parameter.location[0]).GetChild(parameter.location[1]).gameObject.GetComponent <LaserGameBoardGrid>();
                            targetGrid.gameGridType = item.type;
                            targetGrid.isFixedItem  = true;
                            switch (item.type)
                            {
                            case ("Start"):
                                targetGrid.startNode.Add(new StartNode((int[])parameter.location.Clone(), (int[])parameter.rgb.Clone(), parameter.direction));
                                //startNodes.Add(new StartNode((int[])parameter.location.Clone(), (int[])parameter.rgb.Clone(), parameter.direction));
                                break;

                            case ("Goal"):
                                targetGrid.goalNode = new GoalNode((int[])parameter.location.Clone(), (int[])parameter.rgb.Clone(), parameter.direction);
                                //goalNodes.Add(new GoalNode((int[])parameter.location.Clone(), (int[])parameter.rgb.Clone(), parameter.direction));
                                break;

                            case ("Mirror"):
                                targetGrid.mirrorPattern = parameter.direction;
                                break;

                            default:
                                break;
                            }
                        }
                    }
                    inventoryGameItemNum = new int[4] {
                        0, 0, 0, 0
                    };
                    inventoryGameItemNumMax = new int[4] {
                        0, 0, 0, 0
                    };
                    foreach (LaserAvailableItems item in levelToSearch.availableItems)
                    {
                        int itemIndex = Array.IndexOf(inventoryGameItemName, item.type);
                        inventoryGameItemNum[itemIndex]    = item.quantity;
                        inventoryGameItemNumMax[itemIndex] = item.quantity;
                    }
                }
            }
            RefreshGameItemDisplay();
        }

        RunGame();
        RefreshNodeDisplay();
    }
    public void RunGame()
    {
        for (int row = 0; row < canvasManager.gameBoardSizeWithBoarder[0]; row++)
        {
            for (int col = 0; col < canvasManager.gameBoardSizeWithBoarder[1]; col++)
            {
                ClearInfoOnGrid(new int[] { row, col });
                LaserGameBoardGrid currentGrid = GameObject.Find("grid_" + row.ToString() + "_" + col.ToString()).GetComponent <LaserGameBoardGrid>();
                foreach (StartNode startNode in currentGrid.startNode)
                {
                    startNodes.Add(startNode);
                }
                if (currentGrid.goalNode != null)
                {
                    goalNodes.Add(currentGrid.goalNode);
                }
            }
        }


        // Create a Laser from each starting nodes
        foreach (StartNode startNode in startNodes)
        {
            LaserGameBoardGrid StartGrid    = GameObject.Find("grid_" + startNode.Location[0].ToString() + "_" + startNode.Location[1].ToString()).GetComponent <LaserGameBoardGrid>();
            string             startPattern = StartGrid.startNode[0].Direction;
            int[] rgb = StartGrid.startNode[0].Rgb;
            if (startPattern == "")
            {
                return;
            }
            else
            {
                int[] nextGridID = new int[] {
                    GridInfo.NextGrid[startPattern][0] + startNode.Location[0],
                    GridInfo.NextGrid[startPattern][1] + startNode.Location[1]
                };
                CreateLaserOnGrid(nextGridID, startPattern, canvasManager.maxBounces, rgb);
            }
        }

        // Check each goal nodes if the incoming laser is in the corresponding color
        foreach (GoalNode goalNode in goalNodes)
        {
            int[] previousGridID = new int[2] {
                goalNode.Location[0] + GridInfo.PreviousGrid[goalNode.Direction][0],
                goalNode.Location[1] + GridInfo.PreviousGrid[goalNode.Direction][1]
            };
            LaserGameBoardGrid previousGrid = GameObject.Find("grid_" + previousGridID[0].ToString() + "_" + previousGridID[1].ToString()).GetComponent <LaserGameBoardGrid>();
            LaserGameBoardGrid goalGrid     = GameObject.Find("grid_" + goalNode.Location[0].ToString() + "_" + goalNode.Location[1].ToString()).GetComponent <LaserGameBoardGrid>();

            foreach (LaserDirectionInfo directionInfo in previousGrid.CombinedLaserDirection)
            {
                foreach (string pattern in GridInfo.laserPattern)
                {
                    string previousPattern = (directionInfo.Heading == 1) ? directionInfo.Location : new string(directionInfo.Location.ToCharArray().Reverse().ToArray());

                    if (IsLaserReachGoal(previousPattern, goalNode.Direction, directionInfo.Rgb, goalNode.Rgb))
                    {
                        goalGrid.isGoalNodeIgnited = true;
                        Debug.Log("IGN");
                    }
                    else
                    {
                        goalGrid.isGoalNodeIgnited = false;
                    }
                    int[] previousGridRgb = (int[])previousGrid.LaserPattern[pattern].Clone();
                }



                int[] previousGridRgb = (int[])previousGrid.LaserPattern[pattern].Clone();
                if (!previousGridRgb.SequenceEqual(new int[3] {
                    0, 0, 0
                }))
                {
                    if (IsLaserReachGoal(pattern, goalNode.Direction, previousGridRgb, goalNode.Rgb))
                    {
                        goalGrid.isGoalNodeIgnited = true;
                        Debug.Log("IGN");
                    }
                    else
                    {
                        goalGrid.isGoalNodeIgnited = false;
                    }
                }
            }

            foreach (string pattern in GridInfo.laserPattern)
            {
                int[] previousGridRgb = (int[])previousGrid.LaserPattern[pattern].Clone();
                if (!previousGridRgb.SequenceEqual(new int[3] {
                    0, 0, 0
                }))
                {
                    if (IsLaserReachGoal(pattern, goalNode.Direction, previousGridRgb, goalNode.Rgb))
                    {
                        goalGrid.isGoalNodeIgnited = true;
                        Debug.Log("IGN");
                    }
                    else
                    {
                        goalGrid.isGoalNodeIgnited = false;
                    }
                }
            }
        }
    }
    private void CreateLaserOnGrid(int[] gridID, string previousPattern, int bounce, int[] rgb)
    {
        // Return when the maximum bounces is reached
        if (bounce < 0)
        {
            return;
        }

        // Return when the beam goes out of boundary
        if (gridID[0] < 0 || gridID[1] < 0 || gridID[0] >= canvasManager.gameBoardSizeWithBoarder[0] || gridID[1] >= canvasManager.gameBoardSizeWithBoarder[1])
        {
            return;
        }
        GameObject         currentGirdGO = GameObject.Find("grid_" + gridID[0].ToString() + "_" + gridID[1].ToString());
        LaserGameBoardGrid currentGrid   = currentGirdGO.GetComponent <LaserGameBoardGrid>();
        string             gridType      = currentGrid.gameGridType;

        if (gridType == "Goal")
        {
            return;
        }

        if (gridType == "Start")
        {
            gridType = "Empty";
        }

        if (gridType == "Mirror")
        {
            gridType = "Mirror_" + currentGrid.mirrorPattern;
        }

        if (GridInfo.Patterns[previousPattern][gridType]["NextPattern"].Length == 0)
        {
            return;
        }

        string[] gridLaserPattern = GridInfo.Patterns[previousPattern][gridType]["Laser"];
        for (int i = 0; i < gridLaserPattern.Length; i++)
        {
            //foreach(string pattern in GridInfo.laserPattern){
            //}
            //LaserDirectionInfo newLaserOnGrid = new LaserDirectionInfo(rgb, laserLocation,

            //if (previousPattern == gridLaserPattern[i]) ;
            int    laserHeading  = LaserHeadingCheck(gridLaserPattern[i]);
            string laserLocation = (laserHeading == 1) ? gridLaserPattern[i] : new string(gridLaserPattern[i].ToCharArray().Reverse().ToArray());


            //LaserDirectionInfo newLaserOnGrid = new LaserDirectionInfo(rgb, laserLocation,
            //currentGrid.LaserDirection.Add

            //Image targetLaserLoactionImage = currentGrid.transform.Find("Laser").Find(laserLocation).gameObject.GetComponent<Image>();
            //targetLaserLoactionImage.enabled = true;
            //int[] combineRgb = CombineLaserInfo(currentGirdGO, laserLocation, rgb);
            //int combineRgbIndex = combineRgb[0] + combineRgb[1] * 2 + combineRgb[2] * 4 - 1;
            //if (laserLocation[0] == 'C') {
            //	targetLaserLoactionImage.sprite = canvasManager.spriteLaserLineHorizontal[combineRgbIndex];
            //} else {
            //	targetLaserLoactionImage.sprite = canvasManager.spriteLaserLineSlanted[combineRgbIndex];
            //}
        }

        string nextPattern = GridInfo.Patterns[previousPattern][gridType]["NextPattern"][0];

        int[] nextGridID = new int[] {
            GridInfo.NextGrid[nextPattern][0] + gridID[0],
            GridInfo.NextGrid[nextPattern][1] + gridID[1]
        };
        CreateLaserOnGrid(nextGridID, nextPattern, bounce - 1, rgb);
    }