public void TileCheck()
    {
        byte dirX = (byte)tileX;
        byte dirY = (byte)tileY;

        switch (keyInput)
        {
        case 0:
            dirY += 1;
            break;

        case 1:
            dirX += 1;
            break;

        case 2:
            dirY -= 1;
            break;

        case 3:
            dirX -= 1;
            break;
        }
        if (map.IsClear(dirX, dirY) == true)
        {
            isMoving  = true;
            direction = keyInput;
        }
    }
    private void DumbDirection(int x)
    {
        targetX = (byte)Mathf.RoundToInt(gameObject.transform.position.x);
        targetY = (byte)Mathf.Abs(Mathf.RoundToInt(gameObject.transform.position.z));



        switch (x)
        {
        case 0:
            targetY += 1;
            break;

        case 1:
            targetX += 1;
            break;

        case 2:
            targetY -= 1;
            break;

        case 3:
            targetX -= 1;
            break;
        }

        if (map.IsClear(targetX, targetY) == true && x != behind)
        {
            mover.keyInput = x;
            behind         = mover.keyInput + 2;
            if (behind >= 4)
            {
                behind -= 4;
            }
        }
        else
        {
            if (x < 3)
            {
                x += 1;
            }
            else
            {
                x = 0;
            }

            xx += 1;
            if (xx < 4)
            {
                DumbDirection(x);
            }
            else
            {
                DumbBack(behind);
            }
        }
    }
Exemplo n.º 3
0
    private void AStar()       // calc new path
    {
        loopCount = 0;
        openList  = new List <NodeScript>();
        openXY    = new List <Vector2>();
        closed    = new List <Vector2>();
        nodeMap   = new NodeScript[maze.mapWidth, maze.mapHeight];
        targetX   = (byte)gM.playerPos.x;
        targetY   = (byte)gM.playerPos.y;

        int hereX  = Mathf.RoundToInt(gameObject.transform.position.x);
        int hereY  = Mathf.Abs(Mathf.RoundToInt(gameObject.transform.position.z));
        int miscH  = (int)((Mathf.Pow(Mathf.Abs(targetX - hereX), 2f)) + (Mathf.Pow(Mathf.Abs(targetY - hereY), 2)));
        int deltaX = targetX - hereX;
        int deltaY = targetY - hereY;

        //add start to list
        current = new NodeScript(0, (deltaX * deltaX) + (deltaY * deltaY), hereX, hereY);
        openList.Add(current);
        openXY.Add(current.XY);
        nodeMap[(int)current.XY.x, (int)current.XY.y] = current;

        while (openList.Count > 0 && loopCount <= (maze.mapHeight * maze.mapWidth))
        {
            loopCount++;
            //find Current
            int indexOfLowest = GetIndexOfLowestCost(openList);
            current = openList[indexOfLowest];

            //move to closed
            closed.Add(openList[indexOfLowest].XY);
            openList.RemoveAt(indexOfLowest);
            openXY.RemoveAt(indexOfLowest);


            //if current = goal, backtrack from goal.
            if (current.XY == new Vector2(targetX, targetY))
            {
                //	int miscPC = nodeMap[(int)current.XY.x, (int)current.XY.y].g; //Path cost

                misc        = new NodeScript(current.g, 0, targetX, targetY);
                misc.parent = current.XY;
                nodeMap[(int)current.XY.x, (int)current.XY.y] = current;
                openList.Clear();
                FindPath();
                return;
            }
            else
            {
                // Check and gen. children
                for (int dir = 0; dir <= 3; dir++)
                {
                    //if within maze size (this was a problem, not sure how exactly) //Actually..
                    if ((current.XY.x + AdjacentDirections[dir].x) <= maze.mapWidth - 1 && (current.XY.y + AdjacentDirections[dir].y) <= maze.mapWidth - 1 && (current.XY.x + AdjacentDirections[dir].x) >= 0 && (current.XY.y + AdjacentDirections[dir].y) >= 0)
                    {
                        // if not on closed and not a wall
                        if (maze.IsClear((byte)(current.XY.x + AdjacentDirections[dir].x), (byte)(current.XY.y + AdjacentDirections[dir].y)) == true && !closed.Contains(new Vector2((byte)(current.XY.x + AdjacentDirections[dir].x), (byte)(current.XY.y + AdjacentDirections[dir].y))))
                        {
                            int miscPC = nodeMap[(int)current.XY.x, (int)current.XY.y].pathCost;
                            miscH       = (int)((Mathf.Pow(Mathf.Abs(targetX - (current.XY.x + AdjacentDirections[dir].x)), 2f)) + (Mathf.Pow(Mathf.Abs(targetY - (current.XY.y + AdjacentDirections[dir].y)), 2)));
                            misc        = new NodeScript(miscPC, miscH, (int)(current.XY.x + AdjacentDirections[dir].x), (int)(current.XY.y + AdjacentDirections[dir].y));
                            misc.parent = current.XY;

                            //if on openlist
                            if (openXY.Contains(new Vector2(current.XY.x + AdjacentDirections[dir].x, current.XY.y + AdjacentDirections[dir].y)))
                            {
                                indexOfLowest = openXY.IndexOf(new Vector2(current.XY.x + AdjacentDirections[dir].x, current.XY.y + AdjacentDirections[dir].y));


                                //if path is better, use that instead.
                                if (openList[indexOfLowest].g >= misc.g)
                                {
                                    openList[indexOfLowest] = misc;
                                    nodeMap[(int)misc.XY.x, (int)misc.XY.y] = misc;
                                }                                // else { Debug.Log("Nope"); }
                            }
                            else
                            {
                                //add to list
                                openList.Add(misc);
                                openXY.Add(misc.XY);
                                nodeMap[(int)misc.XY.x, (int)misc.XY.y] = misc;
                            }
                        }
                    }                     //else { throw new System.Exception("Why!~?!?" + current.XY); }
                }
            }
        }
    }
    void Update()
    {
        for (int e = 0; e < gM.enemyPos.Length; e++)
        {
            if (gM.enemyPos[e] == gM.playerPos)
            {
                gM.RecreateMaze();
            }
        }
        // state / direction changer. check if aligned. X

        // set tile x and y. this is the current tile.
        tileX        = Mathf.RoundToInt(gameObject.transform.position.x);
        tileY        = Mathf.Abs(Mathf.RoundToInt(gameObject.transform.position.z));
        gM.playerPos = new Vector2(tileX, tileY);

        //is aligned to grid
        if (gameObject.transform.position.x == Mathf.Floor(gameObject.transform.position.x) && gameObject.transform.position.z == Mathf.Floor(gameObject.transform.position.z))
        {
            aligned = true;
        }
        else
        {
            aligned = false;
        }


        // input value
        if (Input.GetAxisRaw("Horizontal") != 0 && Input.GetAxisRaw("Vertical") == 0)
        {
            //keyInput = 2 + Mathf.RoundToInt(Input.GetAxis("Horizontal"));
            if (Input.GetAxisRaw("Horizontal") == 1)
            {
                keyInput = 3;
            }
            else
            {
                keyInput = 1;
            }
        }
        if (Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") != 0)
        {
            //keyInput = 1 + Mathf.RoundToInt(Input.GetAxis("Vertical"));
            if (Input.GetAxisRaw("Vertical") == 1)
            {
                keyInput = 0;
            }
            else
            {
                keyInput = 2;
            }
        }
        if (Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") == 0)
        {
            keyInput = 5;
        }


        // if is aligned and axys = 0, set not moving.
        if (aligned && keyInput == 5)
        {
            isMoving = false;
        }

        //if is aligned and axys isn't 0 check walls in that direction.
        // if no walls, set direction. move = true.
        if (aligned && keyInput != 5)
        {
            byte dirX = (byte)tileX;
            byte dirY = (byte)tileY;
            switch (keyInput)
            {
            case 0:
                dirY += 1;
                break;

            case 1:
                dirX += 1;
                break;

            case 2:
                dirY -= 1;
                break;

            case 3:
                dirX -= 1;
                break;
            }
            if (map.IsClear(dirX, dirY) == true)
            {
                isMoving  = true;
                direction = keyInput;
            }
            //if(map.WallCheck(keyInput, tileX, tileY)) {
        }
    }