Example #1
0
    public void AddGrownTree(TreeControl tree)
    {
        //create and add to list
        GrownTree gt = new GrownTree(tree);

        GrownTrees.Add(gt);

        //deploy new bird and reset timer
        DeployBird(gt);
        timer = 0f;
    }
Example #2
0
    public void RemoveABird(GameObject bird)
    {
        BirdAI ba = bird.GetComponent <BirdAI>();

        //find the tree this bird is on
        GrownTree gt = GrownTrees.Concat(OccupiedTrees).ToList().Find(t => t.tree == ba.TargetTree.tree);

        gt.RemoveBird(bird);

        if (OccupiedTrees.Contains(gt))
        {
            OccupiedTrees.Remove(gt);
            GrownTrees.Add(gt);
        }
    }
Example #3
0
    public void RemoveGrownTree(TreeControl tc)
    {
        //find the tree and remove form either list
        GrownTree gt = GrownTrees.Concat(OccupiedTrees).ToList().Find(t => t.tree == tc.gameObject);

        //make each bird on that tree fly away
        GameObject[] tempBirds = new GameObject[gt.birds.Count];
        gt.birds.CopyTo(tempBirds);
        foreach (GameObject bird in tempBirds)
        {
            gt.RemoveBird(bird);
            bird.GetComponent <BirdAI>().FlyAway();
        }

        //remove the tree
        GrownTrees.Remove(gt);
        OccupiedTrees.Remove(gt);
    }
Example #4
0
    // Update is called once per frame
    void Update()
    {
        //deploying birds
        if (GrownTrees.Count > 0)
        {
            timer += Time.deltaTime;
            if (timer > newDeployTime)
            {
                //deploy a new bird to a random grown tree
                int       index = rnd.Next(GrownTrees.Count);
                GrownTree gt    = GrownTrees[index];
                DeployBird(gt);

                timer -= newDeployTime;
            }
        }

        //send birds to traps
    }
Example #5
0
    void DeployBird(GrownTree tree)
    {
        //find a spot that is not occupied
        int idx = -1;

        for (int i = 0; i < tree.occupied.Length; i++)
        {
            if (!tree.occupied[i])
            {
                idx = i;
            }
        }

        //if all occupied, return
        if (idx == -1)
        {
            return;
        }

        //instantiate a bird
        GameObject bird = Instantiate(birdPrefab);

        //set this bird's target position to the rest spot position, target tree to this tree
        BirdAI ba = bird.GetComponent <BirdAI>();

        ba.TargetRestPosition = tree.tree.transform.position + tree.restPositions[idx];
        ba.RestRotation       = tree.restRotations[idx];
        ba.TargetTree         = tree;
        ba.RestPosIdx         = idx;

        //increase this tree's birdcount and add this bird to this tree
        tree.birdCount++;
        tree.birds.Add(bird);
        tree.occupied[idx] = true;

        //if this tree's birdcount reaches max, move this tree from grown list to occupied list
        if (tree.birdCount >= tree.maxBird)
        {
            GrownTrees.Remove(tree);
            OccupiedTrees.Add(tree);
        }
    }
Example #6
0
    public bool FindTargetTreeForPoacher(out Vector3 trapPosition, out Vector3 poacherPosition, out Vector3 hidePosition, out GrownTree grownTree)
    {
        List <GrownTree> fullTrees = OccupiedTrees.ToList().FindAll(t => t.birdOnTree == t.maxBird);

        //find a random tree that is not targeted by a poacher yet
        foreach (GrownTree gt in fullTrees.OrderBy(a => rnd.Next()).ToList())
        {
            //check if there is empty grids around the tree
            if (gridCon.FindTrapPosition(gt.tree, gt.scale, out trapPosition, out poacherPosition, out hidePosition))
            {
                grownTree = gt;
                return(true);
            }
        }

        trapPosition = poacherPosition = hidePosition = Vector3.zero;
        grownTree    = null;
        return(false);
    }