コード例 #1
0
    //When the AI has bricks, it will pick a point, take them there, and build
    void GoBricks()
    {
        //if we don't have a valid path, pick a location and generate a new path
        if (myPath == null || myPath.Count <= 0)
        {
            Vector3 ranTarget; //Initialize a holder for our target location
            do
            {
                float ranX = Random.Range(0, TheGrid.MapWidth);                                                                        //Randomize a x location within the grid's width
                float ranY = Random.Range(0, TheGrid.MapHeight);                                                                       //Randomize a y location within the grid's height

                ranTarget = TheGrid.NearestPointOnGrid(new Vector3(ranX, ranY, 0));                                                    //Turn those elements into a Grid position
            } while (TheGrid.OccupiedGrid[(int)ranTarget.x, (int)ranTarget.y]);                                                        //Check if that spot is occupied, and do it again if it is
            Vector3 curpoint = TheGrid.NearestPointOnGrid(new Vector3(ranTarget.x, ranTarget.y, ranTarget.z));                         // Once the target is finalized, pass along the target

            myPath = TheGrid.GeneratePath(new point((int)curpoint.x, (int)curpoint.y), new point((int)ranTarget.x, (int)ranTarget.y)); //Generate path to target
        }
        else
        {
            //Set navtarget to current path target.
            Vector3 curNav = new Vector3(myPath[0].x * TheGrid.GridDimension, myPath[0].y * TheGrid.GridDimension, 0);
            //Check distance to curNav, if its below checkDIst, build a thing, reset path, and switch modes.
            if (Vector2.Distance(transform.position, curNav) < checkDist)
            {
                hasBricks = false;
                myPath    = null;
                //Instantiate(toBuild, TheGrid.NearestPointOnGrid(transform.position), transform.rotation);
            }
            StepForward(); //Move towards nav target

            /*else
             * {
             *  StepForward();
             * }*/
        }
    }