コード例 #1
0
ファイル: CollectFoodAction.cs プロジェクト: fuboss/aiProject
        public override IEnumerator Execute()
        {
            bool success = false;
            LookForFoodAround();
            foreach (var foodItem in _visibleFood)
            {
                if (_agent.Character.BackPack.IsFull)
                {
                    break;
                }

                //двигаемся к следующему объекту
                var moveTo = new MoveToAction(_agent, foodItem.transform.position);
                yield return _agent.StartCoroutine(moveTo.Execute());

                //TODO: нужно еще раз проверить дистанцию до цели
                //подбераем объект, к которому подошли
                var pickUpAction = new PickupItemAction(_agent, foodItem);
                pickUpAction.Finished = (finished) =>success = finished;
                yield return _agent.StartCoroutine(pickUpAction.Execute());

                yield return new WaitForSeconds(0.5f);

                if (_visibleFood.Count < 2)
                    LookForFoodAround();

                if (!success) break;
            }
            yield return new WaitForSeconds(0.5f);

            Finished(true);
        }
コード例 #2
0
ファイル: GetWoodAction.cs プロジェクト: fuboss/aiProject
        public override IEnumerator Execute()
        {
            if (_tree == null)
            {
                //look for trees
                var trees = _agent.GetItems<Tree>(tree => tree.Alive);

                //if nearby there are not trees, go to place from momory
                if (trees.Count == 0)
                {
                    var memoryTrees = _agent.GetItemsFromMemory(typeof (Tree));
                    memoryTrees.SortActorsByDisntace(_agent.Character.Position);

                    var treeInfo = memoryTrees.FirstOrDefault();
                    if (treeInfo == null)
                    {
                        Debug.Log("Nearby there are no trees");
                        Cancel();
                        yield break;
                    }

                    var moveTo = new MoveToAction(_agent, treeInfo.Position);
                    yield return _agent.StartCoroutine(moveTo.Execute());
                    yield return new WaitForSeconds(1f);

                    trees = _agent.GetItems<Tree>(tree => tree.Alive);
                }

                //go to closest  visible tree
                trees.SortActorsByDisntace(_agent.Character.Position);
                _tree = trees.FirstOrDefault();
            }

            if (_tree == null)
            {
                Debug.LogError("targetTree == null, BUG");
                Cancel();
                yield break;
            }

            var goTo = new MoveToAction(_agent, _tree.Position);
            yield return _agent.StartCoroutine(goTo.Execute());
            yield return new WaitForSeconds(1f);

            _agent.Memory.Forget(_tree);
            //chop the tree
            var chopTree = new ChopTreeAction(_agent, _tree);
            yield return _agent.StartCoroutine(chopTree.Execute());
            yield return new WaitForSeconds(0.5f);

            //cut the log
            var cutLog = new CutLogAction(_agent);
            yield return _agent.StartCoroutine(cutLog.Execute());
            yield return new WaitForSeconds(1f);

            //collect woodbeams

            var woodBeams =
                _agent.GetItems<WoodBeam>(wb => (wb.Position - _agent.Character.Position).sqrMagnitude < 12);
            if (woodBeams.Count == 0)
            {
                yield return new WaitForSeconds(0.1f);
                woodBeams =_agent.GetItems<WoodBeam>(wb => (wb.Position - _agent.Character.Position).sqrMagnitude < 12);
            }
            if (woodBeams == null || woodBeams.Count == 0)
            {
                Debug.LogError("Nearby there are no woodbeams");
                Cancel();
                yield break;
            }

            foreach (var woodBeam in woodBeams)
            {
                var collectWoodBeam = new PickupItemAction(_agent, woodBeam);
                yield return _agent.StartCoroutine(collectWoodBeam.Execute());
                yield return new WaitForSeconds(0.5f);
            }

            Finished(true);
        }