public IEnumerator eat_food_task()
        {
            Human human              = CreateHuman();
            Node  grassNode          = Utils.NodeAt(_spawnPosition + new Vector2Int(0, 10));
            Grass grass              = Factory.Create <Grass>("grass", grassNode.Position);
            float defaultHungerLevel = human.HungerComponent.HungerLevel;
            Node  targetNode         = SearchEngine.FindNodeNear(grassNode, human.MotionComponent.Node);
            var   task = new EatFoodTask(human, targetNode, grass);

            yield return(AddTaskAndWaitUntilFinished(task, human.CommandProcessor));

            yield return(null);

            //check position
            if (human.MotionComponent.Node != targetNode)
            {
                Assert.Fail();
            }
            //check hunger
            if (human.HungerComponent.HungerLevel == defaultHungerLevel)
            {
                Assert.Fail();
            }
            //check food destroy
            if (grass.gameObject != null)
            {
                Assert.Fail();
            }

            human.Die();
            Assert.Pass();
        }
Example #2
0
    public override void DisableComponent()
    {
        base.DisableComponent();

        _character   = null;
        _eatFoodTask = null;
    }
Example #3
0
    private IEnumerator StartFoodSearch()
    {
        if (_isSearchingForFood || Edibles.Count == 0)
        {
            yield break;
        }
        _isSearchingForFood = true;

        Grass grass = null;

        while (grass is null)
        {
            yield return(new WaitForSeconds(UnityEngine.Random.Range(0f, 2f)));

            grass = SearchEngine.FindClosestGrass(_character.MotionComponent.GridPosition);
        }

        _eatFoodTask = new EatFoodTask(_character, SearchEngine.FindNodeNear(Utils.NodeAt(grass.Position), _character.MotionComponent.Node), grass);
        _eatFoodTask.ResultHandler += HandleGetFoodResult;
        _character.AI.CommandProcessor.AddTask(_eatFoodTask);

        while (_eatFoodTask != null)
        {
            yield return(null);
        }
        _isSearchingForFood = false;
    }
Example #4
0
 private void HandleGetFoodResult(object source, EventArgs e)
 {
     if (_eatFoodTask is null)
     {
         return;
     }
     _eatFoodTask.ResultHandler -= HandleGetFoodResult;
     _eatFoodTask = null;
 }