Example #1
0
    IEnumerable <int> PriestWork()
    {
        Worker worker = null;

        while (worker == null)
        {
            foreach (int i in EatSleep())
            {
                yield return(i);
            }

            Worker[] workers = FindObjectsOfType <Worker>();
            if (workers.Length == 1)
            {
                // The only worker is ourselves!
                yield return(0);

                continue;
            }

            // Sample one random worker
            worker = this;
            while (worker == this)
            {
                // <= 2 iterations in expectation
                worker = workers[Random.Range(0, workers.Length)];
            }

            // Now try to catch him
            while ((transform.position - worker.transform.position).sqrMagnitude >= 1)
            {
                bool gotPath = false;
                int  steps   = 10;
                foreach (int i in MoveTo(WorldGrid.instance.GridPos(worker.transform.position)))
                {
                    yield return(0);

                    if (worker == null || !worker.gameObject.activeSelf)
                    {
                        worker = null;
                        break;
                    }

                    gotPath = true;
                    if (--steps <= 0)
                    {
                        break;
                    }
                }
                if (worker == null || abortMoveTo)
                {
                    // For some reason the target became unwalkable, just choose another
                    worker = null;
                    break;
                }

                if (!gotPath)
                {
                    // There is no path to the target.
                    if (WorldGrid.instance.GridPos(worker.transform.position) == WorldGrid.instance.GridPos(transform.position))
                    {
                        // We are on the same cell, in which case we just move in direction of the target to reach its real world pos
                        DirectMoveTo(worker.transform.position);
                        yield return(0);

                        if (worker == null || !worker.gameObject.activeSelf)
                        {
                            worker = null;
                            break;
                        }
                    }
                    else
                    {
                        // The target is unreachable, in which case we should choose another
                        worker = null;
                        break;
                    }
                }
            }
        }

        if (Random.value > .1f)
        {
            animation.Act(.5f, 1, worker.transform.position);
            while (animation.IsActing)
            {
                yield return(0);
            }
            worker.speedBonus = 10;
            yield break;
        }

        // Get him!
        worker.isSacrificed       = true;
        worker.hexingTimeOfPriest = hexingTime / speedup;
        worker.actions            = worker.Actions().GetEnumerator();
        animation.Act(hexingTime / speedup, 3, worker.transform.position);
        while (animation.IsActing)
        {
            yield return(0);
        }

        // Follow the poor guy to the temple
        // NICE COPY
        while (!worker.isReadyForSacrifice || (transform.position - worker.transform.position).sqrMagnitude >= 1)
        {
            bool gotPath = false;
            int  steps   = 10;
            foreach (int i in MoveTo(WorldGrid.instance.GridPos(worker.transform.position)))
            {
                yield return(0);

                if (worker == null || !worker.gameObject.activeSelf)
                {
                    worker = null;
                    break;
                }

                gotPath = true;
                if (--steps <= 0)
                {
                    break;
                }
            }
            if (worker == null || abortMoveTo)
            {
                // For some reason the target became unwalkable, just choose another
                worker = null;
                break;
            }

            if (!gotPath)
            {
                // There is no path to the target.
                if (WorldGrid.instance.GridPos(worker.transform.position) == WorldGrid.instance.GridPos(transform.position))
                {
                    // We are on the same cell, in which case we just move in direction of the target to reach its real world pos
                    DirectMoveTo(worker.transform.position);
                    yield return(0);

                    if (worker == null || !worker.gameObject.activeSelf)
                    {
                        worker = null;
                        break;
                    }
                }
                else
                {
                    // The target is unreachable, in which case we should choose another
                    worker = null;
                    break;
                }
            }
        }
        // Make sure the worker reached the temple
        if (worker == null || worker.temple == null)
        {
            yield break;
        }
        temple = worker.temple;

        // NOW KILL HIM
        temple.StartSacrifice();
        animation.Act(sacrificingTime / speedup, 15, worker.transform.position);
        while (animation.IsActing)
        {
            yield return(0);
        }
        if (worker)
        {
            worker.Die("was sacrificed by your Priest");
        }
        if (temple)
        {
            temple.EndSacrifice();
        }
        yield return(0);
    }