示例#1
0
 void Awake()
 {
     m_tileMap = GameManager.Singleton.TileMap;
     m_world   = GameManager.Singleton.World;
     hexData   = new HexData();
     wind      = new Wind();
 }
示例#2
0
 public void CopyValues(HexData other)
 {
     age            = other.age;
     plateId        = other.plateId;
     oldPlateId     = other.oldPlateId;
     height         = other.height;
     temp           = other.temp;
     wetness        = other.wetness;
     empty          = other.empty;
     moved          = other.moved;
     formingMoutain = other.formingMoutain;
     isOcean        = other.isOcean;
     isCoast        = other.isCoast;
     isHotSpot      = other.isHotSpot;
     lastEmpty      = other.lastEmpty;
     lastMoved      = other.lastMoved;
 }
示例#3
0
        private void Smooth()
        {
            Dictionary <Hex, HexData> tempHeights = new Dictionary <Hex, HexData>(m_world.TileData.Count);

            foreach (var pair in m_world.TileData)
            {
                HexData hData = pair.Value.hexData;

                if (tempHeights.ContainsKey(pair.Key))
                {
                    hData.CopyValues(tempHeights[pair.Key]);
                }

                if (hData.isCoast && pair.Value.HeightRange(90, 110))
                {
                    hData.height = (m_rand.NextFloat() < 0.5f) ? 95f : 105f;
                }

                hData.empty = false;
            }
            ApplyTiles(tempHeights);
        }
示例#4
0
        public World CreateWorld()
        {
            m_world  = new World(settingsScriptableObject);
            m_layout = m_world.layout;
            m_width  = m_world.size.x;
            m_height = m_world.size.y;

            m_seed = 123;
            SimplexNoise.Noise.Seed = 209323094;
            m_rand = new Unity.Mathematics.Random(m_seed);

            m_state = GenState.PRE_GEN;
            StartCoroutine(GenerateRoutine());
            //if (File.Exists("test.txt"))
            //File.Delete("test.txt");
            //StreamWriter writer = File.AppendText("test.txt");

            for (int r = 0; r <= m_height; r++) // height
            {
                //writer.WriteLine();
                int r_offset = Mathf.FloorToInt(r / 2f);
                for (int q = -r_offset; q <= m_width - r_offset; q++) // width with offset
                {
                    Hex hex    = new Hex(q, r, -q - r);
                    var mapKey = hex.GetKey();
                    //writer.Write(mapKey.ToString() + "|");
                    Point pixel = m_layout.HexToPixel(hex);

                    if (!m_world.TileData.ContainsKey(mapKey))
                    {
                        GameObject gameobject = Instantiate(prefab, new Vector3((float)pixel.x, (float)pixel.y, 0), Quaternion.identity);
                        TileObject tObj       = gameobject.GetComponent <TileObject>();
                        m_world.TileData.Add(mapKey, tObj);
                        HexData hData = tObj.hexData;

                        float d = Noise.CalcPixel2D(q, r, .1f);

                        if (d < 55)
                        {
                            hData.isOcean = true;
                        }

                        tObj.hex = hex;

                        hData.age    = Random.Range(0, 50);
                        hData.height = d;

                        float distFromEquator = (float)m_world.Equator
                                                - Mathf.Abs((float)m_world.Equator - (float)r);
                        float tempFromDist = m_world.settings.poleTemp
                                             + (m_world.settings.equatorTemp - m_world.settings.poleTemp)
                                             * (distFromEquator / m_world.Equator);
                        hData.temp = tempFromDist + Random.Range(0, m_world.size.y * .25f + 1);

                        hData.wetness = Random.Range(0f, 255f);
                        Tile tile = tObj.FindCorrectTile();
                        tObj.SetTile(tile);
                    }
                }
            }
            m_world.windManager.Init();
            //writer.Close();
            m_state = GenState.GENERATE;
            Generate();
            return(m_world);
        }
示例#5
0
        void Update()
        {
            if (paused)
            {
                return;
            }
            if (m_state == GenState.DONE)
            {
                return;
            }
            if (m_state != GenState.ITERATING)
            {
                m_state = GenState.ITERATING;
            }
            m_timer += Time.deltaTime;
            if (m_timer < 0.05f)
            {
                return;
            }
            m_timer = 0;
            if (m_iters > m_world.settings.numberOfIterations)
            {
                m_state = GenState.DONE;
                print("Done simulation!");
                StopCoroutine(GenerateRoutine());
                Smooth();
            }

            // Update to World
            if (m_world.worldTemp.changeType != WorldTempChangeType.ICE_AGE)
            {
                if (Random.value > 1f - m_world.settings.iceAgeChance)
                {
                    m_world.worldTemp.StartIceAge(new TemperatureEvent(-10, 3, 10, 6), 9);
                }
            }

            // Update to plates every 5 iterations.
            if (m_iters != 0 && m_iters % m_world.settings.itersForUpdate == 0)
            {
                for (int i = m_world.plates.Count - 1; i > -1; i--)
                {
                    Plate p = m_world.plates[i];
                    p.direction = (HexDirection)Random.Range(0, HexConstants.MAX_DIR);
                    p.TrySplit();
                    p.movementSpeed = 100f;
                }
                m_world.worldTemp.tempChange = Random.Range(-.3f, .3f);
            }

            // Hex loop
            Dictionary <Hex, HexData> tempData   = new Dictionary <Hex, HexData>();
            Dictionary <int, float>   tempPlates = new Dictionary <int, float>();

            for (int r = 0; r <= m_height; r++) // height
            {
                int r_offset = Mathf.FloorToInt(r / 2);
                for (int q = -r_offset; q <= m_width - r_offset; q++) // width with offset
                {
                    Hex hex = new Hex(q, r, -q - r);

                    if (!m_world.ContainsHex(hex))
                    {
                        continue;
                    }

                    m_world.TryGetHexData(hex, out TileObject obj);
                    HexData hData = obj.hexData;

                    Plate hPlate = m_world.GetPlateByID(hData.plateId);
                    if (!tempData.ContainsKey(hex))
                    {
                        tempData.Add(hex, new HexData(hData));
                    }
                    HexData tempHexData = tempData[hex];

                    tempHexData.temp += m_world.worldTemp.FinalTempChange;

                    Hex  dirHex      = hex.Neighbor((int)hPlate.direction);
                    bool dirInBounds = m_world.TryGetHexData(dirHex, out TileObject dirObj);

                    if (!dirInBounds)
                    {
                        if (!tempPlates.ContainsKey(hPlate.id))
                        {
                            tempPlates.Add(hPlate.id, hPlate.movementSpeed - -5f);
                        }
                        else
                        {
                            tempPlates[hPlate.id] += -5f;
                        }
                        continue;
                    }


                    dirHex = dirObj.hex; // Reassign hex incase world wrapping is on and we need to wrap.
                    HexData dirData  = dirObj.hexData;
                    Plate   dirPlate = m_world.GetPlateByID(dirData.plateId);
                    if (!tempData.ContainsKey(dirHex))
                    {
                        tempData.Add(dirHex, new HexData(dirData));
                    }
                    HexData tempDirData = tempData[dirHex];

                    SimulatedLoopHex loopHex = new SimulatedLoopHex()
                    {
                        hex         = hex,
                        obj         = obj,
                        tempData    = tempHexData,
                        plate       = hPlate,
                        dirHex      = dirHex,
                        dirObj      = dirObj,
                        tempDirData = tempDirData
                    };

                    m_world.windManager.SimulateWind(m_iters, loopHex);

                    bool platesDiff    = hPlate.id != dirPlate.id;
                    bool platesCollide = Mathf.Abs(hPlate.direction - dirPlate.direction) == 3;

                    bool heightGTE = hData.height >= dirData.height;

                    tempHexData.age++;

                    if (hPlate.movementSpeed > 0 && platesDiff)
                    {
                        const float HEIGHT_MOD = 20f;
                        float       spd        = 0f;

                        if (heightGTE)
                        {
                            tempHexData.height        += HEIGHT_MOD;
                            tempHexData.formingMoutain = true;
                            tempHexData.moved          = true;
                            spd += (platesCollide) ? -1f : -.25f;
                        }
                        else
                        {
                            //tempHexData.height -= HEIGHT_MOD;
                            tempHexData.formingMoutain = false;
                            tempHexData.moved          = false;
                            spd += (platesCollide) ? -10f : -2.5f;
                        }

                        tempHexData.empty = false;


                        if (!tempPlates.ContainsKey(hPlate.id))
                        {
                            tempPlates.Add(hPlate.id, hPlate.movementSpeed - spd);
                        }
                        else
                        {
                            tempPlates[hPlate.id] += spd;
                        }
                    }
                    else if (hPlate.movementSpeed <= 0)
                    {
                        tempHexData.empty = false;
                        tempHexData.moved = false;
                    }

                    if (tempHexData.moved)
                    {
                        float mod = 1f;
                        if (hData.isHotSpot) // Hot spots
                        {
                            mod += 20f + m_rand.NextFloat(0f, 10f);
                        }
                        if (hData.height < SEA_LVL - 55)
                        {
                            mod += 1;
                        }
                        if (hData.age < 5) // New created land gains more height
                        {
                            mod += 15;
                        }
                        if (hData.age < 50) // New created land gains more height
                        {
                            mod += 1f;
                        }
                        if (hData.height > HILL_LVL && hData.age > 100)
                        {
                            mod -= 2f;
                        }
                        if (hData.height > HILL_LVL + 55)
                        {
                            mod -= 3f;
                        }
                        if (dirData.height > HILL_LVL + 55 && hData.height < dirData.height - 25 && !dirData.isCoast && !dirData.isOcean)
                        {
                            mod += 3f;
                        }
                        if (hData.height < dirData.height - 35 && !dirData.isCoast && !dirData.isOcean)
                        {
                            mod += 2f;
                        }

                        tempDirData.height = hData.height + mod;

                        tempDirData.empty   = false;
                        tempDirData.plateId = hPlate.id;
                    }
                    tempHexData.oldPlateId = hPlate.id;
                }
            }

            foreach (var pair in tempPlates)
            {
                Plate p = m_world.GetPlateByID(pair.Key);
                p.movementSpeed = pair.Value;
            }

            ApplyTiles(tempData);

            IterationEvent?.Invoke(m_iters, m_world);

            if (step)
            {
                paused = true;
                step   = false;
            }
            m_iters++;
        }
示例#6
0
 public HexData(HexData other)
 {
     CopyValues(other);
 }