예제 #1
0
파일: Helper.cs 프로젝트: jsjtxietian/Agent
    public bool CheckBox(bool condition)
    {
        //todo check box anyway or ? see Mr du's example
        Coordinate nextPos = RobiController.GetNextPos();

        if (CheckOutOfIndex(nextPos))
        {
            return(false);
        }

        bool real = WorldMap[nextPos.x, nextPos.y] == RoadType.Box;

        return(condition ? real : !real);
    }
예제 #2
0
    private IEnumerator Gameloop()
    {
        //get ready
        HUDController.ClockStartRecord();
        Indicator.SetActive(true);

        //main loop
        while (!IsEnterExit()) // a single round
        {
            hasEnergy = true;
            bool hasMoved = false;

            //robi part
            for (int i = 0; i < InstructionController.GetHeight(); i++)
            {
                if (InstructionController.instructions[i, 0] == SensorAndAction.None)
                {
                    break;
                }

                //break when don't have energy any more
                if (!hasEnergy)
                {
                    break;
                }

                for (int j = 0; j < InstructionController.GetLength(); j++)
                {
                    var currentIns = InstructionController.instructions[i, j];

                    if (currentIns == SensorAndAction.None)
                    {
                        break;
                    }

                    if (InstructionController.IsSensor(currentIns))
                    {
                        if (currentIns == SensorAndAction.Box)
                        {
                            if (!Helper.CheckBox(true))
                            {
                                break;
                            }
                        }
                        else if (currentIns == SensorAndAction._Box)
                        {
                            if (!Helper.CheckBox(false))
                            {
                                break;
                            }
                        }
                        else if (currentIns == SensorAndAction.Obstacle)
                        {
                            if (!Helper.CheckObstale(true))
                            {
                                break;
                            }
                        }
                        else if (currentIns == SensorAndAction._Obstacle)
                        {
                            if (!Helper.CheckObstale(false))
                            {
                                break;
                            }
                        }
                        else if (currentIns == SensorAndAction.Route)
                        {
                            if (!Helper.CheckRoute(true))
                            {
                                break;
                            }
                        }
                        else if (currentIns == SensorAndAction._Route)
                        {
                            if (!Helper.CheckRoute(false))
                            {
                                break;
                            }
                        }
                        else if (currentIns == SensorAndAction.Monster)
                        {
                            if (!Helper.CheckMonster(true))
                            {
                                break;
                            }
                        }
                        else if (currentIns == SensorAndAction._Monster)
                        {
                            if (!Helper.CheckMonster(false))
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        SetIndicatorPos(i);

                        switch (currentIns)
                        {
                        case SensorAndAction.Forward:

                            hasMoved = true;

                            if (Helper.CheckOutOfIndex(RobiController.GetNextPos()))
                            {
                                GameCrash();
                                yield return(new WaitForSeconds(RoundTime));
                            }
                            if (hasEnergy)
                            {
                                if (CheckMeet(true))
                                {
                                    StartCoroutine(ReduceBlood());
                                    yield return(new WaitForSeconds(RoundTime));
                                }
                                else
                                {
                                    RobiController.MoveForward();
                                    hasEnergy = false;
                                }
                            }
                            break;

                        case SensorAndAction.Open:
                            hasMoved = true;
                            if (hasEnergy)
                            {
                                OpenChest();
                                hasEnergy = false;
                                hasMoved  = true;
                            }
                            break;

                        case SensorAndAction.Left:
                            hasMoved = true;
                            RobiController.TurnLeft();
                            break;

                        case SensorAndAction.Right:
                            hasMoved = true;
                            RobiController.TurnRight();
                            break;

                        case SensorAndAction.Back:
                            hasMoved = true;
                            RobiController.TurnBack();
                            break;
                        }

                        yield return(new WaitForSeconds(RoundTime));
                    }
                }
            }

            if (!hasMoved)
            {
                GameCrash();
            }

            //in case that game crash doesn't work
            yield return(new WaitForSeconds(0.1f));

            //monster part
            foreach (var monster in MonsterControllers)
            {
                if (monster.CheckNextOutBorder())
                {
                    monster.TurnBack();
                    yield return(new WaitForSeconds(RoundTime));
                }

                if (CheckMeet(false, monster))
                {
                    StartCoroutine(ReduceBlood());
                    yield return(new WaitForSeconds(RoundTime));
                }
                else
                {
                    monster.MoveForward();
                }

                yield return(new WaitForSeconds(RoundTime));
            }
        }

        #region SetPlayerPref

        //success
        PlayerPrefs.SetInt("minute" + Level.ToString(), HUDController.currentMinute);
        PlayerPrefs.SetInt("second" + Level.ToString(), HUDController.currentSecond);
        PlayerPrefs.SetInt("chest" + Level.ToString(), HUDController.currentChest);

        int chestState = GetChestState(HUDController.currentChest);
        PlayerPrefs.SetInt("chestState" + Level.ToString(), chestState);

        int oldLevel = PlayerPrefs.GetInt("level");
        if (Level > oldLevel)
        {
            PlayerPrefs.SetInt("level", Level);
        }

        #endregion

        SuccessUI.SetActive(true);
        SuccesSource.Play();

        yield return(new WaitForSeconds(RoundTime));

        LevelSelect.Play();
        yield return(new WaitForSeconds(RoundTime));

        if (Level == 3)
        {
            SceneManager.LoadScene("Congratulation");
        }
        else
        {
            SceneManager.LoadScene("LevelSelection");
        }
    }