コード例 #1
0
    // Use this for initialization
    void Start()
    {
        thisUnit   = GetComponent <Unit>();
        thisPlayer = GetComponent <Player>();
        grid       = FindObjectOfType <GridContructor>();

        kdb = new MinotaurMaze.State(grid.initialState);


        planSteps = new Queue <string>();

        kdb.Add("visited", thisUnit.unitPositionText);
        if (thisPlayer.haveBomb)
        {
            grid.problemObjects += "\n\t\t" + "bomb - bomb" + "\n\t\t";
            kdb.Add("haveObject", "bomb");
        }

        problemGoal = "(and (atTile " + gameObject.name + " exit))";
        //StartPlanner();
    }
コード例 #2
0
    //Gerador do peças do grid;
    void SpawnTileAt(Color32 c, int x, int y)
    {
        //Verifica se é um pixel transparente, resultando em um espaço vazio.
        if (c.a <= 0)
        {
            return;
        }

        //Note: Não otimizado, usar dicionário para melhorar a velociade;
        foreach (PrefabLibrary pl in prefabLibrary)
        {
            if (c.Equals(pl.color))
            {
                Vector3    ObjPosition = (CalcWorldPos(x, pl.height, y));
                GameObject gameObj     = Instantiate(pl.prefab, ObjPosition, Quaternion.identity) as GameObject;
                gameObj.transform.parent = this.transform;

                if (gameObj.GetComponent <MazeExit> ())
                {
                    gameObj.name = "exit";
                }
                else if (gameObj.GetComponent <Passage> ())
                {
                    gameObj.name = "passage";
                }
                else
                {
                    gameObj.name = "l" + x + "c" + y;
                }
                gameObj.GetComponent <Tile> ().tileX = x;
                gameObj.GetComponent <Tile> ().tileY = y;
                gameObj.GetComponent <Tile> ().grid  = this;


                graph [x, y] = new Node();

                graph [x, y].x = x;
                graph [x, y].y = y;
                if (gameObj.name == "exit")
                {
                    graph [x, y].lXcY = "exit";
                }
                else if (gameObj.name == "passage")
                {
                    graph [x, y].lXcY = "passage";
                }
                else
                {
                    graph [x, y].lXcY = "l" + x + "c" + y;
                }
                graph [x, y].movementCost = pl.movementCost;
                graph [x, y].moveable     = pl.moveable;
                if (graph [x, y].moveable)
                {
                    if (objNumber % 5 == 0)
                    {
                        problemObjects += graph [x, y].lXcY + " - tile\n\t\t";
                    }
                    else
                    {
                        problemObjects += graph [x, y].lXcY + " - tile\t\t";
                    }
                    initialState.Add("tileSafe", graph [x, y].lXcY);
                    objNumber++;
                }

                if (pl.spawnPoint)
                {
                    GetComponent <Spawner>().SpawnUnit(pl.color, gameObj.GetComponent <Tile>());
                    GetComponent <Spawner> ().SpawnItem(pl.color, gameObj.GetComponent <Tile> ());
                }
                return;
            }
        }
        //Se essa linha for executada, significa que a função não achou um obj para a cor.
        Debug.LogError("No color to prefab found for: " + c.ToString());
    }
コード例 #3
0
    // Executa os passos da solução que estão no Queue
    IEnumerator ExecutePlanningProblemSolution()
    {
        while (planSteps.Count > 0)
        {
            yield return(new WaitUntil(() => thisUnit.waitingTurn == false));

            if (GameObject.Find("Step-By-Step Toggle").GetComponent <Toggle>().isOn)
            {
                yield return(new WaitUntil(() => FindObjectOfType <StepsController>().nextStep == true));
            }

            FindObjectOfType <StepsController>().nextStep = false;

            if (!SurroundingOk())
            {
                Replan();
            }

            yield return(new WaitUntil(() => thisUnit.unitBusy == false));

            string thisStep = planSteps.Dequeue();
            string action   = thisStep.Split(' ')[0];
            switch (action)
            {
                #region action moveTo
            case "MOVETO":
                //action moveTo
                //parameters: (?player - player ?from - tile ?to - tile ?minotaur - minotaur)
                //precondition:	(atTile ?player ?from) (neighbour ?from ?to)
                //				(or (not (isAlive ?minotaur))
                //					(not (atTile ?minotaur ?to))
                //				)
                //effect:	-(atTile ?from)
                //			+(atTile ?to)
                //			se (not (visited ?to))
                //				+(visited ?to)
                string thisTile          = thisStep.Split(' ')[2];
                string tileToMove        = thisStep.Split(' ')[3];
                Tile   gameObjTileToMove = GameObject.Find(tileToMove.ToLower()).GetComponent <Tile>();

                if ((thisUnit.unitPositionText.ToLower() == thisTile.ToLower()) && (!(gameObjTileToMove.minotaurNear == 0) || !(GameObject.Find(thisStep.Split(' ')[4].ToLower()))))
                {
                    kdb.Remove("atTile", gameObject.name, thisTile.ToLower());
                    thisUnit.MoveUnitTo(gameObjTileToMove.tileX, gameObjTileToMove.tileY);
                    kdb.Add("atTile", gameObject.name, tileToMove.ToLower());
                    if (!kdb.CheckVar("visited", tileToMove.ToLower()))
                    {
                        kdb.Add("visited", tileToMove.ToLower());
                    }
                    yield return(new WaitUntil(() => thisUnit.unitBusy == false));
                }
                else
                {
                    Replan();
                }
                break;
                #endregion

                #region action pick
            case "PICK":
                //action pick
                //parameters: (?player - player ?object - object ?location - tile)
                //precondition: (atTile ?player ?location) (objectOnMap ?object ?location)
                //effect:		-(objectOnMap ?object ?location)
                //				+(haveObject ?object)
                if (thisUnit.unitPositionText.ToLower() == thisStep.Split(' ')[3].ToLower())
                {
                    string itemName = thisStep.Split(' ')[2];
                    // Procura a chave que está no mesmo tile do agente
                    Collider[] itemInSpace = Physics.OverlapSphere(transform.position, 0.5f);
                    for (int i = 0; i < itemInSpace.Length; i++)
                    {
                        if (itemInSpace[i].name == itemName.ToLower())
                        {
                            kdb.Remove("objectOnMap", itemName.ToLower(), thisStep.Split(' ')[3].ToLower());
                            itemInSpace[i].GetComponent <Key>().Pick();
                            kdb.Add("haveObject", itemName.ToLower());
                        }
                    }
                }
                else
                {
                    Replan();
                }
                break;
                #endregion

                #region action usePassage
            case "USEPASSAGE":
                //action usePassage
                //parameters (?player - player ?from - tile ?to - tile ?key - key)
                //precondition (and (haveObject ?key) (atTile ?agent ?from) (passage ?from ?to))
                //effect	-(atTile ?agent ?from) -(haveObject ?key)
                //			+(atTile ?agent ?to)
                string tileFromName = thisStep.Split(' ')[2];
                string tileToName   = thisStep.Split(' ')[3];
                Tile   tileFrom     = GameObject.Find(tileFromName.ToLower()).GetComponent <Tile>();
                Tile   tileTo       = GameObject.Find(tileToName.ToLower()).GetComponent <Tile>();

                if ((thisUnit.haveKey) && (thisUnit.unitPositionText.ToLower() == tileFromName.ToLower()))
                {
                    // Adiciona conexão entre os tiles da passagem
                    grid.graph[tileFrom.tileX, tileFrom.tileY].neighbours.Add(grid.graph[tileTo.tileX, tileTo.tileY]);
                    // Move o agente pela passagem
                    kdb.Remove("atTile", gameObject.name, tileFromName.ToLower());
                    kdb.Remove("haveObject", thisStep.Split(' ')[4].ToLower());
                    thisUnit.MoveUnitTo(tileTo.tileX, tileTo.tileY);
                    kdb.Add("atTile", gameObject.name, tileToName.ToLower());
                    if (!kdb.CheckVar("visited", tileToName.ToLower()))
                    {
                        kdb.Add("visited", tileToName.ToLower());
                    }
                    yield return(new WaitUntil(() => thisUnit.unitBusy == false));

                    // Remove a conexão entre os tiles pois gastou a chave
                    grid.graph[tileFrom.tileX, tileFrom.tileY].neighbours.Remove(grid.graph[tileTo.tileX, tileTo.tileY]);
                }
                else
                {
                    Replan();
                }
                break;
                #endregion

                #region action throwBomb
            case "THROWBOMB":
                //action throwBomb
                //parameters (?tile - tile ?bomb - bomb ?minotaur - minotaur ?player - player ?pTile - tile)
                //precondition	(haveObject ?bomb)
                //				(isAlive ?minotaur)
                //				(atTile ?player ?pTile)
                //				(exists (?pNeighbour - tile)
                //						(and	(neighbour ?pTile ?pNeighbour)
                //								(neighbour ?pNeighbour ?tile)
                //						)
                //				)
                //				(or (atTile ?minotaur ?tile)
                //					(exists (?neighbours - tile)
                //							(and	(neighbour ?tile ?neighbours)
                //									(atTile ?minotaur ?neighbours)
                //							)
                //					)
                //				)
                //effect	-(haveObject ?bomb)
                //			-(isAlive ?minotaur)
                string tileToThrowBomb        = thisStep.Split(' ')[1];
                Tile   gameObjTileToThrowBomb = GameObject.Find(tileToThrowBomb.ToLower()).GetComponent <Tile>();
                if (thisPlayer.haveBomb && (GameObject.Find(thisStep.Split(' ')[3].ToLower())))
                {
                    thisPlayer.ThrowBomb(gameObjTileToThrowBomb);
                    yield return(new WaitUntil(() => thisUnit.unitBusy == false));

                    kdb.Remove("haveObject", thisStep.Split(' ')[2].ToLower());
                    kdb.Remove("isAlive", thisStep.Split(' ')[3].ToLower());
                }
                else
                {
                    Replan();
                }
                break;
                #endregion

            default:
                Replan();
                break;
            }
        }
        GameObject.Find("Play Canvas").GetComponent <Canvas>().enabled          = false;
        GameObject.Find("Plan Finished Canvas").GetComponent <Canvas>().enabled = true;
    }