Example #1
0
    public void GeneratePoints(Map.Area area, List <Vector2> areaPoints)
    {
        // Make at least one point be a station or a planet
        Map.Area.POIData data = new Map.Area.POIData();

        Vector2 pos = areaPoints[Random.Range(0, areaPoints.Count)];

        Station station = (Station)PickRandom(stations.Cast <PointOfInterest>().ToArray());

        data.pointOfInterest = station;
        data.position        = pos;

        area.pointsOfInterest.Add(data);

        areaPoints.Remove(pos);

        // Set others randomly
        foreach (Vector2 v in areaPoints)
        {
            Map.Area.POIData d = new Map.Area.POIData();

            AsteroidField field = (AsteroidField)PickRandom(asteroidFields.Cast <PointOfInterest>().ToArray());
            d.pointOfInterest = field;
            d.position        = v;

            area.pointsOfInterest.Add(d);
        }
    }
Example #2
0
    public Map GenerateMap(int seed)
    {
        Map map = new Map();

        map.seed = seed;
        Random.InitState(seed);

        map.areaList  = new Map.Area[mapLength];
        map.mapLength = mapLength;
        map.mapHeight = mapHeight;

        // Generate lengths for each area here, so that we can generate the point list
        float[] areaLengths = new float[mapLength];
        for (int i = 0; i < mapLength; i++)
        {
            areaLengths[i] = Random.Range(areaMinMaxLength.x, areaMinMaxLength.y);
        }
        map.totalMapLength = areaLengths.Sum();

        List <Vector2> allPoints = new PoissonDiscSampler
                                   (
            map.totalMapLength,
            mapHeight,
            distanceBetweenPoints
                                   ).Samples().Where
                                   (
            p =>
            p.y <= mapHeight - emptyDistanceFromTopAndBottom &&
            p.y >= emptyDistanceFromTopAndBottom
                                   ).ToList();

        float xDistance = 0f;

        for (int x = 0; x < mapLength; x++)
        {
            Vector2        areaSize   = new Vector2(areaLengths[x], mapHeight);
            List <Vector2> areaPoints = allPoints.FindAll
                                        (
                p =>
                p.x > xDistance &&
                p.x < xDistance + areaSize.x
                                        );

            Map.Area area = new Map.Area();
            area.areaSize         = areaSize;
            area.xOffsetFromStart = xDistance + areaSize.x / 2f;
            GeneratePoints(area, areaPoints);

            map.areaList[x] = area;
            xDistance      += areaSize.x;
        }

        return(map);
    }
Example #3
0
        private void CalcuateLayer(Map map, Area exportArea, float areaDiameter)
        {
            Dictionary <int, UnitInfo> unitInfos = new Dictionary <int, UnitInfo>();

            CalcuateAllPrefab(exportArea.gameObject, unitInfos);
            List <GameObject> unitObjs = new List <GameObject>();

            foreach (var unitInfo in unitInfos)
            {
                unitObjs.Add(unitInfo.Value.Unit);
            }

            if (unitObjs != null && unitObjs.Count > 0)
            {
                float totalDiameter = 0f;
                unitObjs.Sort((unit1, unit2) =>
                {
                    UnitInfo info1      = unitInfos[unit1.GetInstanceID()];
                    UnitInfo info2      = unitInfos[unit2.GetInstanceID()];
                    Bounds unit1AABB    = info1.AABB;
                    float unit1Diameter = Mathf.Max(unit1AABB.size.x, unit1AABB.size.y, unit1AABB.size.z);
                    Bounds unit2AABB    = info2.AABB;
                    float unit2Diameter = Mathf.Max(unit2AABB.size.x, unit2AABB.size.y, unit2AABB.size.z);
                    return(unit1Diameter.CompareTo(unit2Diameter));
                });
                for (int iUnit = 0; iUnit < unitObjs.Count; iUnit++)
                {
                    GameObject unit     = unitObjs[iUnit];
                    UnitInfo   unitInfo = unitInfos[unit.GetInstanceID()];
                    Bounds     unitAABB = unitInfo.AABB;
                    EditorUtility.DisplayProgressBar("analyze", unit.name, iUnit / unitObjs.Count);
                    float unitDiameter = Mathf.Max(unitAABB.size.x, unitAABB.size.y, unitAABB.size.z);
                    totalDiameter += unitDiameter;
                }

                m_EditorSplitAreaLayerSettings.Clear();

                float avergeDiameter = totalDiameter / unitObjs.Count;

                EditorSplitAreaLayerSetting layerInfo = new EditorSplitAreaLayerSetting();
                layerInfo.m_MaxAABBSize = areaDiameter;
                layerInfo.m_MinAABBSize = avergeDiameter;
                layerInfo.m_LayerName   = AreaLayer.BigLayer.ToString();
                layerInfo.m_Priority    = (int)AreaLayer.BigLayer;
                layerInfo.m_GridSize    = Mathf.Max(Mathf.CeilToInt(areaDiameter), 1);


                float avergeHalfDiameter = avergeDiameter / 3;
                float halfDiameter       = avergeDiameter - 2 * avergeHalfDiameter;
                EditorSplitAreaLayerSetting layerInfoMedium = new EditorSplitAreaLayerSetting();
                layerInfoMedium.m_MaxAABBSize = avergeDiameter;
                layerInfoMedium.m_MinAABBSize = halfDiameter;
                layerInfoMedium.m_LayerName   = AreaLayer.MediumLayer.ToString();
                layerInfoMedium.m_Priority    = (int)AreaLayer.MediumLayer;
                layerInfoMedium.m_GridSize    = Mathf.Max(Mathf.CeilToInt(avergeDiameter), 1);

                EditorSplitAreaLayerSetting layerInfoSmall = new EditorSplitAreaLayerSetting();
                layerInfoSmall.m_MaxAABBSize = halfDiameter;
                layerInfoSmall.m_MinAABBSize = 0;
                layerInfoSmall.m_LayerName   = AreaLayer.SmallLayer.ToString();
                layerInfoSmall.m_Priority    = (int)AreaLayer.SmallLayer;
                layerInfoSmall.m_GridSize    = Mathf.Max(Mathf.CeilToInt(halfDiameter), 1);


                float layer1MaxDiameter = -1f; //层1中的unit最大直径
                float layer2MaxDiameter = -1f; //层2中的unit最大直径
                float layer3MaxDiameter = -1f; //层3中的unit最大直径
                                               //TODO:计算27宫格偏移
                for (int iUnit = 0; iUnit < unitObjs.Count; iUnit++)
                {
                    GameObject unit = unitObjs[iUnit];

                    Bounds unitAABB = unitInfos[unit.GetInstanceID()].AABB;
                    EditorUtility.DisplayProgressBar("analyze", unit.name, iUnit / unitObjs.Count);
                    float unitDiameter = Mathf.Max(unitAABB.size.x, unitAABB.size.y, unitAABB.size.z);
                    if (unitDiameter > avergeDiameter && unitDiameter <= areaDiameter)//big layer
                    {
                        if (layer1MaxDiameter < unitDiameter)
                        {
                            layer1MaxDiameter = unitDiameter;
                        }
                    }
                    else if (unitDiameter > halfDiameter && unitDiameter <= avergeDiameter)//medium layer
                    {
                        if (layer2MaxDiameter < unitDiameter)
                        {
                            layer2MaxDiameter = unitDiameter;
                        }
                    }
                    else if (unitDiameter > 0 && unitDiameter <= halfDiameter)//small layer
                    {
                        if (layer3MaxDiameter < unitDiameter)
                        {
                            layer3MaxDiameter = unitDiameter;
                        }
                    }
                }


                float halfTanCameraFov = RendererUtility.CaculateHalfTanCameraFov(map.ExpectedFov);
                float toCameraDistance = RendererUtility.CacluateToCameraDistance(layer1MaxDiameter, m_EditorSplitAreaSetting.m_Rate, halfTanCameraFov);
                toCameraDistance = Mathf.Min(toCameraDistance, exportArea.GetDiameter());

                layerInfo.m_Offest       = Mathf.Clamp(Mathf.CeilToInt(toCameraDistance / layerInfo.m_GridSize), (int)GridOffest.Min, (int)GridOffest.Max);
                toCameraDistance         = RendererUtility.CacluateToCameraDistance(layer2MaxDiameter, m_EditorSplitAreaSetting.m_Rate, halfTanCameraFov);
                layerInfoMedium.m_Offest = Mathf.Clamp(Mathf.CeilToInt(toCameraDistance / layerInfoMedium.m_GridSize), (int)GridOffest.Min, (int)GridOffest.Max);
                toCameraDistance         = RendererUtility.CacluateToCameraDistance(layer3MaxDiameter, m_EditorSplitAreaSetting.m_Rate, halfTanCameraFov);
                layerInfoSmall.m_Offest  = Mathf.Clamp(Mathf.CeilToInt(toCameraDistance / layerInfoSmall.m_GridSize), (int)GridOffest.Min, (int)GridOffest.Max);
                m_EditorSplitAreaLayerSettings.Add(layerInfo);
                m_EditorSplitAreaLayerSettings.Add(layerInfoMedium);
                m_EditorSplitAreaLayerSettings.Add(layerInfoSmall);
            }
        }