示例#1
0
        public void createMap(int loopNum)
        {
            // generate junctions
            Vector2      center        = new Vector2(0, 0);
            Vector2      junctionIndex = Vector2.zero;
            JunctionSize size          = new JunctionSize();
            int          dirIndex      = 0;

            indexer           = new JunctionIndexer(loopNum);
            roads             = new List <Road>();
            sidewalks         = new List <Block>();
            roadGenerator     = new RoadGenerator();
            junctionGenerator = new JunctionGenerator();

            // generate junction
            size = junctionGenerator.setRandomParam(size);
            junctionGenerator.createJunction(center, size, junctionIndex, ref junctions, ref indexer, ref areaSet, ref sidewalks);
            for (int i = 1; i < loopNum; i++)
            {
                junctionGenerator.setAndCreateJunction(i, ref size, ref center, ref junctionIndex, ref dirIndex, ref junctions, ref indexer, ref areaSet, ref sidewalks);
                junctionGenerator.setAndCreateJunction(i, ref size, ref center, ref junctionIndex, ref dirIndex, ref junctions, ref indexer, ref areaSet, ref sidewalks);

                if (i == loopNum - 1)
                {
                    junctionGenerator.setAndCreateJunction(i, ref size, ref center, ref junctionIndex, ref dirIndex, ref junctions, ref indexer, ref areaSet, ref sidewalks);
                }
            }

            // link junctions using road
            linkJunctions(GameObject.Find("Road"));

            // generate area
            if (buildingGenerator == null)
            {
                buildingGenerator = UnityEngine.Object.Instantiate((Resources.Load("Prefab/DummyObjects/BuildingGenerator") as GameObject).GetComponent <BuildingGenerator>(), transform);
            }
            buildingGenerator.generate(areaSet);
            isReady = true;
        }
        public void connectByRoad(Junction source, AbsDirection connectDirection, GameObject roadParent, ref List <Junction> roadEnds,
                                  ref List <Road> roads, ref List <Junction> junctions, ref JunctionIndexer indexer, ref JunctionGenerator junctionGenerator)
        {
            int coDirection = (int)connectDirection;
            int opDirection = ((int)connectDirection + 2) % 4;

            if (indexer.getIndex(source.coordIndex + directions[coDirection]) >= 0)
            {
                Junction target = junctions[indexer.getIndex(source.coordIndex + directions[coDirection])];
                //Debug.Log(source.ToString() + "\t" + target.ToString() + "\r" + source.connectRoadIdx[coDirection] + "\t" + target.connectRoadIdx[opDirection]);
                if (source.connectRoadIdx[coDirection] < 0 && target.connectRoadIdx[opDirection] < 0)
                {
                    createRoads(source, target, ref roads, roadParent);
                }
                else if (source.connectRoadIdx[coDirection] < 0)
                {
                    source.connectRoadIdx[coDirection] = target.connectRoadIdx[opDirection];
                }
                else if (target.connectRoadIdx[opDirection] < 0)
                {
                    target.connectRoadIdx[opDirection] = source.connectRoadIdx[coDirection];
                }
            }
            else
            {
                junctionGenerator.createNullJunction(source.coordIndex + directions[coDirection], source, (AbsDirection)coDirection, ref roadEnds);
                createRoads(source, roadEnds[roadEnds.Count - 1], ref roads, roadParent);
            }
        }