private Boolean checkOverlap(Vector3 newPosition, CartographicIsland newIsland, float minDistance)
        {
            int cc = 0;

            foreach (GraphVertex existingVertex in graph.Vertices)
            {
                Vector3 existingPos    = existingVertex.getPosition();
                float   distance       = Vector3.Distance(existingPos, newPosition);
                float   existingRadius = existingVertex.getIsland().getRadius();
                float   newRadius      = newIsland.getRadius();
                if (distance <= (minDistance + (existingRadius + newRadius)))
                {
                    return(false);
                }
            }
            return(true);
        }
示例#2
0
        private IslandGO constructIslandGO(CartographicIsland island, Vector3 pos)
        {
            int rngSeed = island.getName().GetHashCode() + 200;

            RNG = new System.Random(rngSeed);
            GameObject islandGO          = new GameObject(island.getName());
            IslandGO   islandGOComponent = islandGO.AddComponent <IslandGO>();

            islandGOComponent.setIslandStructure(island);
            island.setIslandGO(islandGO);

            #region create countries
            List <List <TnetMesh> > tmeshList   = island.getPackageMeshes();
            List <List <VFace> >    islandCells = island.getPackageCells();
            List <Package>          packageList = island.getPackages();

            float maximumBuildingBoundSize = 0;
            int   counter = 0;
            foreach (List <TnetMesh> tmesh in tmeshList)
            {
                Package pckg = packageList[counter];

                GameObject region = new GameObject(pckg.getName());

                Region regionComponent = region.AddComponent <Region>();
                regionComponent.setParentIsland(islandGOComponent);
                region.transform.SetParent(islandGO.transform);
                islandGOComponent.addRegion(regionComponent);

                #region RegionArea
                GameObject regionArea = new GameObject("Region area");
                regionArea.transform.SetParent(region.transform);
                MeshFilter   mFilter = regionArea.AddComponent <MeshFilter>();
                MeshRenderer mRender = regionArea.AddComponent <MeshRenderer>();
                mRender.sharedMaterial = combinedHoloMaterial;

                regionComponent.setRegionArea(regionArea);
                regionComponent.setPackage(pckg);
                #endregion

                List <VFace>      packageCells      = islandCells[counter];
                CombineInstance[] combineCellMeshes = new CombineInstance[tmesh.Count];
                int cc = 0;
                #region Combine package cell meshes
                foreach (TnetMesh tm in tmesh)
                {
                    Mesh m = Helperfunctions.convertTriangleNETMesh(tm);
                    combineCellMeshes[cc].mesh = m;
                    cc++;
                }
                mFilter.mesh = new Mesh();
                mFilter.mesh.CombineMeshes(combineCellMeshes, true, false);

                float rndU = (float)RNG.NextDouble();
                float rndV = (float)RNG.NextDouble() * 0.4f;

                Vector2 rndUV = new Vector2(rndU, rndV);
                setUVsToSingularCoord(rndUV, mFilter);
                #endregion

                cc = 0;
                #region Create CUs
                foreach (CompilationUnit cu in pckg.getCompilationUnits())
                {
                    float x           = (float)packageCells[cc].generator.X;
                    float y           = (float)packageCells[cc].generator.Z;
                    float z           = (float)packageCells[cc].generator.Y;
                    int   heightLevel = Helperfunctions.mapLOCtoLevel(cu.getLoc());

                    GameObject building;
                    if (cu.implementsServiceComponent())
                    {
                        building = GameObject.Instantiate(SIPrefabs[heightLevel], region.transform);
                        building.AddComponent <ServiceLayerGO>();
                    }
                    else if (cu.declaresService())
                    {
                        building = GameObject.Instantiate(SDPrefabs[heightLevel], region.transform);
                        building.AddComponent <ServiceLayerGO>();
                    }
                    else
                    {
                        building = GameObject.Instantiate(CUPrefabs[heightLevel], region.transform);
                    }

                    building.name = cu.getName();
                    Vector3 randomRotation = new Vector3(0f, UnityEngine.Random.Range(-180, 180), 0f);
                    building.transform.localEulerAngles = randomRotation;
                    Building buildingComponent = building.AddComponent <Building>();
                    buildingComponent.setCU(cu);
                    cu.setGameObject(building);
                    building.transform.position   = new Vector3(x, y, z);
                    building.transform.localScale = new Vector3(GlobalVar.cuScale, GlobalVar.cuScale, GlobalVar.cuScale);
                    regionComponent.addBuilding(buildingComponent);
                    //////////////////////////
                    #region BuildingCollider
                    building.layer = LayerMask.NameToLayer("InteractionSystemLayer");
                    CapsuleCollider capsuleCol = building.AddComponent <CapsuleCollider>();
                    capsuleCol.isTrigger = true;
                    #endregion
                    float currentBuildingExtent = capsuleCol.bounds.size.magnitude;
                    if (currentBuildingExtent > maximumBuildingBoundSize)
                    {
                        maximumBuildingBoundSize = currentBuildingExtent;
                    }
                    //////////////////////////
                    cc++;
                }
                #endregion

                counter++;
            }


            #endregion

            #region Combine CU meshes

            /*
             * GameObject combinedCUs = new GameObject("Combined CUs");
             * MeshFilter mFilterCU = combinedCUs.AddComponent<MeshFilter>();
             * MeshRenderer mRenderCU = combinedCUs.AddComponent<MeshRenderer>();
             * mRenderCU.material = defaultMaterial;
             * mFilterCU.mesh = new Mesh();
             * mFilterCU.mesh.CombineMeshes(combineCuMeshes, true, true);
             * combinedCUs.transform.SetParent(islandGO.transform);
             */
            #endregion

            #region create coastline
            GameObject coastline = new GameObject("Coastline");
            islandGOComponent.setCoast(coastline);
            coastline.transform.SetParent(islandGO.transform);
            MeshFilter   coastMFilter = coastline.AddComponent <MeshFilter>();
            MeshRenderer coastMRender = coastline.AddComponent <MeshRenderer>();
            coastMRender.sharedMaterial = combinedHoloMaterial;
            List <TnetMesh>   tmeshCoastList       = island.getCoastlineMeshes();
            CombineInstance[] combineCoastInstance = new CombineInstance[tmeshCoastList.Count];
            counter = 0;
            foreach (TnetMesh tmesh in tmeshCoastList)
            {
                Mesh m = Helperfunctions.convertTriangleNETMesh(tmesh);
                combineCoastInstance[counter].mesh = m;
                counter++;
            }
            coastMFilter.mesh = new Mesh();
            coastMFilter.mesh.CombineMeshes(combineCoastInstance, true, false);

            setUVsToSingularCoord(new Vector2(0f, 0.7f), coastMFilter);

            #endregion

            #region init docks

            //get graph vertex associated with the island
            GraphVertex vert = island.getDependencyVertex();
            if (vert != null)
            {
                //Relative dock position
                Vector3 dockDirection = new Vector3(UnityEngine.Random.value, 0, UnityEngine.Random.value);
                dockDirection.Normalize();
                dockDirection *= island.getRadius();

                //Import Dock
                Vector3 dockPosition = island.getWeightedCenter() + dockDirection;
                dockPosition.y -= Mathf.Abs(GlobalVar.islandHeightProfile[GlobalVar.islandHeightProfile.Length - 1]) * GlobalVar.islandAboveOcean;
                GameObject importD = Instantiate(importDockPrefab, dockPosition, Quaternion.identity);
                importD.layer = LayerMask.NameToLayer("InteractionSystemLayer");
                importD.name  = island.getName() + " import dock";
                importD.transform.localScale = new Vector3(1, 1, 1);
                importD.transform.SetParent(islandGO.transform);
                islandGOComponent.setImportDock(importD);
                //setUVsToSingularCoord(new Vector2(0.7f, 0.1f), importD.GetComponent<MeshFilter>());

                //Export Dock
                GameObject exportD = Instantiate(exportDockPrefab, dockPosition, Quaternion.identity);
                exportD.layer = LayerMask.NameToLayer("InteractionSystemLayer");
                exportD.name  = island.getName() + " export dock";
                exportD.transform.localScale = new Vector3(1, 1, 1);
                exportD.transform.SetParent(islandGO.transform);
                islandGOComponent.setExportDock(exportD);
                //setUVsToSingularCoord(new Vector2(0.1f, 0.1f), exportD.GetComponent<MeshFilter>());
            }
            #endregion

            islandGO.transform.position = pos;
            islandGO.transform.SetParent(VisualizationContainer.transform);

            #region rise Islands above ocean
            float   newIslandHeight = Mathf.Abs(GlobalVar.islandHeightProfile[GlobalVar.islandHeightProfile.Length - 1]) * GlobalVar.islandAboveOcean;
            Vector3 newIslandPos    = islandGO.transform.localPosition;
            newIslandPos.y = newIslandHeight;
            islandGO.transform.localPosition = newIslandPos;
            #endregion

            #region Create colliders

            #region CountryCollider
            List <Region> regions = islandGOComponent.getRegions();
            foreach (Region region in regions)
            {
                GameObject countryGO = region.gameObject;
                countryGO.layer = LayerMask.NameToLayer("InteractionSystemLayer");
                MeshCollider cColliderCountry = countryGO.AddComponent <MeshCollider>();
                MeshFilter   mFilter          = region.getRegionArea().GetComponent <MeshFilter>();

                cColliderCountry.sharedMesh = mFilter.sharedMesh;
                cColliderCountry.convex     = true;
                cColliderCountry.isTrigger  = true;
            }
            #endregion

            #region IslandCollider
            islandGO.layer = LayerMask.NameToLayer("InteractionSystemLayer");
            CapsuleCollider cColliderIsland = islandGO.AddComponent <CapsuleCollider>();
            float           b = island.getRadius();
            cColliderIsland.radius = b;
            float newColliderHeight = islandGOComponent.getCoast().GetComponent <MeshFilter>().sharedMesh.bounds.size.y;
            cColliderIsland.height = newColliderHeight;
            Vector3 newCenter = island.getWeightedCenter();
            newCenter.y               = -islandGOComponent.getCoast().GetComponent <MeshFilter>().sharedMesh.bounds.size.y + (newColliderHeight * 0.5f);
            cColliderIsland.center    = newCenter;
            cColliderIsland.isTrigger = true;
            #endregion

            #endregion

            return(islandGOComponent);
        }