Beispiel #1
0
    Vector3 getNextPos()
    {
        float leftBoundary   = gs.getGridPosition(new Vector2(0, 0)).x;
        float rightBoundary  = gs.getGridPosition(new Vector2(gs.maxX, 0)).x;
        float topBoundary    = gs.getGridPosition(new Vector2(0, gs.maxY)).z;
        float bottomBoundary = gs.getGridPosition(new Vector2(0, 0)).z;

        direction = Quaternion.AngleAxis(Random.Range(0, 360), Vector3.up);
        Vector3 x = transform.forward * searchRadius;

        x.y = transform.position.y;


        //prevent agent from walking outside the landmass
        if (x.x < leftBoundary)
        {
            x.x = leftBoundary;
        }
        if (x.x > rightBoundary)
        {
            x.x = rightBoundary;
        }
        if (x.z < bottomBoundary)
        {
            x.z = bottomBoundary;
        }
        if (x.z > topBoundary)
        {
            x.z = topBoundary;
        }

        return(x);
    }
    //Move to the next position
    IEnumerator moveToNextPos()
    {
        nextPos = gs.getGridPosition(pos);
        yield return(StartCoroutine("rotate"));

        while (Vector3.Distance(transform.position, gs.getGridPosition(pos)) > 0.001)
        {
            this.transform.position = Vector3.MoveTowards(this.transform.position, gs.getGridPosition(pos), moveSpeed * Time.deltaTime);
            yield return(null);
        }
    }
Beispiel #3
0
 // Start is called before the first frame update
 void Start()
 {
     gs  = GameObject.FindGameObjectWithTag("SceneController").GetComponent <gridScript>();
     pos = new Vector2(Random.Range(0, gs.grid.GetLength(1)), Random.Range(0, gs.grid.GetLength(0)));
     // gs.occupySpace(pos);
     this.transform.position = gs.getGridPosition(pos);
 }
    // Update is called once per frame
    void Update()
    {
        gs.leaveSpace(pos);

        transform.position = gs.getGridPosition(pos);

        Debug.Log(safeToGiveBirth(pos) + " " + pos);


        if (Input.GetKeyDown(KeyCode.W) && pos.y < gs.maxY)
        {
            pos.y += 1;
        }
        if (Input.GetKeyDown(KeyCode.S) && pos.y > 0)
        {
            pos.y -= 1;
        }
        if (Input.GetKeyDown(KeyCode.D) && pos.x < gs.maxX)
        {
            pos.x += 1;
        }
        if (Input.GetKeyDown(KeyCode.A) && pos.x > 0)
        {
            pos.x -= 1;
        }

        gs.occupySpace(pos);
    }
Beispiel #5
0
    // Update is called once per frame
    IEnumerator walk()
    {
        while (true)
        {
            while (!getNextPos())
            {
                ;
            }
            transform.position = gs.getGridPosition(pos);
            yield return(new WaitForSeconds(1));

            if (path.Count > 15)
            {
                path = new List <Vector3>();
            }
        }
    }
Beispiel #6
0
 // Start is called before the first frame update
 void Start()
 {
     gs                 = GameObject.FindGameObjectWithTag("SceneController").GetComponent <gridScript>();
     pos                = new Vector2(0, 0);
     prevPos            = pos;
     transform.position = gs.getGridPosition(pos);
     StartCoroutine("walk");
 }
Beispiel #7
0
    IEnumerator deactivateForTime()
    {
        pos = new Vector2(Random.Range(0, gs.grid.GetLength(1)), Random.Range(0, gs.grid.GetLength(0)));
        this.transform.position = gs.getGridPosition(pos);
        gameObject.tag          = "Player";
        yield return(new WaitForSeconds(35));

        gameObject.tag = "Food";
    }
Beispiel #8
0
    // Update is called once per frame
    IEnumerator spawnTrees()
    {
        while (true)
        {
            yield return(new WaitForSeconds(secondsTillNextSpawn));

            Vector2 spawnPoint = pos;
            var     food       = (GameObject)Resources.Load("Prefabs/Food");
            //left
            spawnPoint.x -= 1;
            if (!gs.isOccupied(spawnPoint))
            {
                var left = Instantiate(food, gs.getGridPosition(spawnPoint), Quaternion.identity);
                left.transform.parent = GameObject.FindGameObjectWithTag("foodContainer").transform;
            }
            //top left
            spawnPoint.y += 1;
            if (!gs.isOccupied(spawnPoint))
            {
                var left = Instantiate(food, gs.getGridPosition(spawnPoint), Quaternion.identity);
                left.transform.parent = GameObject.FindGameObjectWithTag("foodContainer").transform;
            }
            //bottom left
            spawnPoint.y -= 2;
            if (!gs.isOccupied(spawnPoint))
            {
                var left = Instantiate(food, gs.getGridPosition(spawnPoint), Quaternion.identity);
                left.transform.parent = GameObject.FindGameObjectWithTag("foodContainer").transform;
            }
            //right
            spawnPoint    = pos;
            spawnPoint.x += 1;
            if (!gs.isOccupied(spawnPoint))
            {
                var right = Instantiate(food, gs.getGridPosition(spawnPoint), Quaternion.identity);
                right.transform.parent = GameObject.FindGameObjectWithTag("foodContainer").transform;
            }
            //top right
            spawnPoint.y += 1;
            if (!gs.isOccupied(spawnPoint))
            {
                var right = Instantiate(food, gs.getGridPosition(spawnPoint), Quaternion.identity);
                right.transform.parent = GameObject.FindGameObjectWithTag("foodContainer").transform;
            }
            //bottom right
            spawnPoint.y -= 2;
            if (!gs.isOccupied(spawnPoint))
            {
                var right = Instantiate(food, gs.getGridPosition(spawnPoint), Quaternion.identity);
                right.transform.parent = GameObject.FindGameObjectWithTag("foodContainer").transform;
            }
            //top
            spawnPoint    = pos;
            spawnPoint.y -= 1;
            if (!gs.isOccupied(spawnPoint))
            {
                var up = Instantiate(food, gs.getGridPosition(spawnPoint), Quaternion.identity);
                up.transform.parent = GameObject.FindGameObjectWithTag("foodContainer").transform;
            }
            //bottom
            spawnPoint.y += 2;
            if (!gs.isOccupied(spawnPoint))
            {
                var down = Instantiate(food, gs.getGridPosition(spawnPoint), Quaternion.identity);
                down.transform.parent = GameObject.FindGameObjectWithTag("foodContainer").transform;
            }
        }
    }