예제 #1
0
    IEnumerator TakePath()
    {
        bool stillTraveling = true;

        Vector3    startPos = gameObject.transform.position, endPos = startPos;
        Quaternion startAngle = gameObject.transform.rotation;
        Quaternion endAngle   = startAngle;

        while (stillTraveling)
        {
            if (goingToHouse && curTile.Equals(destTile) || goingToStore && havePath && path.Count == 0)
            {
                stillTraveling = false;
            }
            else
            {
                if (!havePath)
                {
                    // compute path from curTile to dest
                    if (goingToHouse)
                    {
                        path     = scenarioInfo.ComputePath(curTile, this.destTile, true);
                        havePath = true;
                    }
                    else if (goingToStore)
                    {
                        havePath = GetPathToAllStores();
                    }
                    else
                    {
                        stillTraveling = false;
                    }
                    yield return(0);
                }
                if (path.Count == 0)                 // got no path: wait for the world to change
                {
                    yield return(new WaitForSeconds(1));
                }
                else
                {
                    IntPoint2D nextTile = path.Pop();
                    startPos = gameObject.transform.position;
                    ScenarioMgr.Direction nextFacing = facing;
                    //check validity of tile
                    if (!scenarioInfo.IsPassableTile(nextTile))
                    {
                        if (goingToHouse && nextTile.Equals(destTile) || goingToStore && havePath && path.Count == 0)
                        {
                            stillTraveling = false;
                        }
                        else if (goingToStore)
                        {
                            havePath = GetPathToStore();
                        }
                        else
                        {
                            havePath = false;
                            yield return(new WaitForSeconds(1));
                        }
                    }
                    else
                    {
                        // compute direction
                        if (curTile.xCoord > nextTile.xCoord)
                        {
                            nextFacing = ScenarioMgr.Direction.Left;
                        }
                        else if (curTile.xCoord < nextTile.xCoord)
                        {
                            nextFacing = ScenarioMgr.Direction.Right;
                        }
                        else if (curTile.yCoord > nextTile.yCoord)
                        {
                            nextFacing = ScenarioMgr.Direction.Up;
                        }
                        else if (curTile.yCoord < nextTile.yCoord)
                        {
                            nextFacing = ScenarioMgr.Direction.Down;
                        }
                        else
                        {
                            stillTraveling = false;
                        }
                        if (stillTraveling)
                        {
                            endPos = scenarioInfo.ComputeTopLeftPointOfTile(nextTile) + citizenTileOffset;
                            // compute ending position
                            switch (nextFacing)
                            {
                            case ScenarioMgr.Direction.Left:
                                endAngle = Quaternion.Euler(0, 180, 0);
                                break;

                            case ScenarioMgr.Direction.Up:
                                endAngle = Quaternion.Euler(0, 90, 0);
                                break;

                            case ScenarioMgr.Direction.Down:
                                endAngle = Quaternion.Euler(0, 270, 0);
                                break;

                            case ScenarioMgr.Direction.Right:
                                endAngle = Quaternion.Euler(0, 0, 0);
                                break;
                            }
                            float elapsedTime = 0;
                            if (facing != nextFacing)
                            {
                                // first handle the rotation
                                while (elapsedTime < this.turnTime)
                                {
                                    this.gameObject.transform.rotation = Quaternion.Slerp(startAngle, endAngle, (elapsedTime / this.turnTime));
                                    elapsedTime += Time.deltaTime;
                                    yield return(0);
                                }
                            }
                            // move to tile
                            float moveTime = this.tileMoveTime;
                            if (!scenarioInfo.IsRoadTile(curTile))
                            {
                                moveTime = moveTime * 2;
                            }
                            elapsedTime = 0;
                            while (elapsedTime < moveTime)
                            {
                                this.gameObject.transform.position = Vector3.Lerp(startPos, endPos, (elapsedTime / moveTime));
                                elapsedTime += Time.deltaTime;
                                yield return(0);
                            }
                            // fix data for next tile
                            this.facing  = nextFacing;
                            this.curTile = nextTile;
                            startPos     = gameObject.transform.position;
                            startAngle   = endAngle;
                        }
                    }
                }
            }
            yield return(0);
        }
        Debug.Log("at destination");
        if (movingIn && myHouse != null)
        {
            myHouse.RecordArrival();
        }
        else if (goingToHouse && myHouse != null && goods > 0)
        {
            myHouse.ReceiveResource2(goods);
        }
        if (goingToStore && destStore != null)
        {
            DoStoreStuff();
        }
        else
        {
            // done with this citizen
            basket.SetActive(false);
            Destroy(gameObject); // destroy me
        }
    }