Example #1
0
    private bool InAncestorNode(IntPoint2D tile)
    {
        bool       found   = false;
        SearchNode curNode = parent;

        while (!found && curNode != null)
        {
            if (tile.Equals(curNode.data))
            {
                found = true;
            }
            else
            {
                curNode = curNode.parent;
            }
        }
        return(found);
    }
Example #2
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
        }
    }
Example #3
0
    public List <SearchNode> GetChildren(IntPoint2D dest, ScenarioData scenario, bool openTravel)
    {
        List <SearchNode> children = new List <SearchNode>();
        int        childCost       = this.cost + 1;
        int        childHeuristic  = 0;
        IntPoint2D childData       = new IntPoint2D(data.xCoord - 1, data.yCoord);

        if (scenario.IsValidTile(childData) && !InAncestorNode(childData) &&
            (scenario.IsRoadTile(childData) || (openTravel && scenario.IsPassableTile(childData)) || childData.Equals(dest)))
        {
            childCost = this.cost + 1;
            if (!scenario.IsRoadTile(childData))
            {
                childCost = childCost + 2;
            }
            childHeuristic = Math.Abs(childData.xCoord - dest.xCoord) + Math.Abs(childData.yCoord - dest.yCoord);
            children.Add(new SearchNode(this, childData, childCost, childHeuristic));
        }
        childData = new IntPoint2D(data.xCoord + 1, data.yCoord);
        if (scenario.IsValidTile(childData) && !InAncestorNode(childData) &&
            (scenario.IsRoadTile(childData) || (openTravel && scenario.IsPassableTile(childData)) || childData.Equals(dest)))
        {
            childCost = this.cost + 1;
            if (!scenario.IsRoadTile(childData))
            {
                childCost = childCost + 2;
            }
            childHeuristic = Math.Abs(childData.xCoord - dest.xCoord) + Math.Abs(childData.yCoord - dest.yCoord);
            children.Add(new SearchNode(this, childData, childCost, childHeuristic));
        }
        childData = new IntPoint2D(data.xCoord, data.yCoord - 1);
        if (scenario.IsValidTile(childData) && !InAncestorNode(childData) &&
            (scenario.IsRoadTile(childData) || (openTravel && scenario.IsPassableTile(childData)) || childData.Equals(dest)))
        {
            childCost = this.cost + 1;
            if (!scenario.IsRoadTile(childData))
            {
                childCost = childCost + 2;
            }
            childHeuristic = Math.Abs(childData.xCoord - dest.xCoord) + Math.Abs(childData.yCoord - dest.yCoord);
            children.Add(new SearchNode(this, childData, childCost, childHeuristic));
        }
        childData = new IntPoint2D(data.xCoord, data.yCoord + 1);
        if (scenario.IsValidTile(childData) && !InAncestorNode(childData) &&
            (scenario.IsRoadTile(childData) || (openTravel && scenario.IsPassableTile(childData)) || childData.Equals(dest)))
        {
            childCost = this.cost + 1;
            if (!scenario.IsRoadTile(childData))
            {
                childCost = childCost + 2;
            }
            childHeuristic = Math.Abs(childData.xCoord - dest.xCoord) + Math.Abs(childData.yCoord - dest.yCoord);
            children.Add(new SearchNode(this, childData, childCost, childHeuristic));
        }
        return(children);
    }
Example #4
0
 public bool AtGoal(IntPoint2D dest)
 {
     return(data.Equals(dest));
 }
Example #5
0
    public List <SearchNode> GetChildren(IntPoint2D dest, ScenarioData scenario, bool openTravel)
    {
        // Get list of child nodes
        List <SearchNode> children = new List <SearchNode>();

        // Get this cost plus one
        int childCost = this.cost + 1;

        // Integer to hold child heuristic number
        int childHeuristic = 0;

        // Create child data
        IntPoint2D childData = new IntPoint2D(data.xCoord - 1, data.yCoord);

        if ((scenario.IsRoadTile(childData) || (openTravel && scenario.IsPassableTile(childData)) || childData.Equals(dest)) && scenario.IsValidTile(childData) && !InAncestorNode(childData))
        {
            childCost = this.cost + 1;
            if (!scenario.IsRoadTile(childData))
            {
                childCost = childCost + 2;
            }
            childHeuristic = Math.Abs(childData.xCoord - dest.xCoord) + Math.Abs(childData.yCoord - dest.yCoord);
            children.Add(new SearchNode(this, childData, childCost, childHeuristic));
        }

        childData = new IntPoint2D(data.xCoord + 1, data.yCoord);
        if ((scenario.IsRoadTile(childData) || (openTravel && scenario.IsPassableTile(childData)) || childData.Equals(dest)) && scenario.IsValidTile(childData) && !InAncestorNode(childData))
        {
            childCost = this.cost + 1;
            if (!scenario.IsRoadTile(childData))
            {
                childCost = childCost + 2;
            }
            childHeuristic = Math.Abs(childData.xCoord - dest.xCoord) + Math.Abs(childData.yCoord - dest.yCoord);
            children.Add(new SearchNode(this, childData, childCost, childHeuristic));
        }

        childData = new IntPoint2D(data.xCoord, data.yCoord - 1);
        if ((scenario.IsRoadTile(childData) || (openTravel && scenario.IsPassableTile(childData)) || childData.Equals(dest)) && scenario.IsValidTile(childData) && !InAncestorNode(childData))
        {
            childCost = this.cost + 1;
            if (!scenario.IsRoadTile(childData))
            {
                childCost = childCost + 2;
            }
            childHeuristic = Math.Abs(childData.xCoord - dest.xCoord) + Math.Abs(childData.yCoord - dest.yCoord);
            children.Add(new SearchNode(this, childData, childCost, childHeuristic));
        }

        childData = new IntPoint2D(data.xCoord, data.yCoord + 1);
        if ((scenario.IsRoadTile(childData) || (openTravel && scenario.IsPassableTile(childData)) || childData.Equals(dest)) && scenario.IsValidTile(childData) && !InAncestorNode(childData))
        {
            childCost = this.cost + 1;
            if (!scenario.IsRoadTile(childData))
            {
                childCost = childCost + 2;
            }
            childHeuristic = Math.Abs(childData.xCoord - dest.xCoord) + Math.Abs(childData.yCoord - dest.yCoord);
            children.Add(new SearchNode(this, childData, childCost, childHeuristic));
        }

        return(children);
    }