private void Generate(UnityEngine.Object[] objects, bool[] valids, GenState gen) { for (int i = 0; i < valids.Length; i++) { if (!valids[i]) { continue; } TextAsset atlas = objects[i] as TextAsset; if (gen.genOBJ) { Mesh[] meshes = TexturePackerImporter.GenerateMeshes(atlas); if (meshes == null) { return; } TexturePackerImporter.GenerateOBJs(atlas, meshes); } if (gen.genPrefab) { Mesh[] meshes = TexturePackerImporter.GenerateMeshes(atlas); if (meshes == null) { return; } Material mat = TexturePackerImporter.GenerateMaterial(atlas); if (mat == null) { return; } TexturePackerImporter.GeneratePrefabs(atlas, meshes, mat); } if (gen.genMesh) { TexturePackerImporter.GenerateMeshes(atlas); } if (gen.genMat) { TexturePackerImporter.GenerateMaterial(atlas); } } }
void OnGUI() { UnityEngine.Object[] objects = Selection.objects; bool[] valids = TexturePackerImporter.ValidateAssets(objects); int numValids = 0; EditorGUI.indentLevel++; for (int i = 0; i < valids.Length; i++) { if (valids[i]) { numValids++; EditorGUILayout.LabelField(objects[i].name); } } EditorGUI.indentLevel--; if (numValids == 0) { return; } GenState gen = new GenState(); gen.genMesh = Toggle("Generate Meshes", prefGenMesh); if (!gen.genMesh && EditorPrefs.GetBool(prefGenOBJ)) { EditorPrefs.SetBool(prefGenOBJ, false); } if (!gen.genMesh && EditorPrefs.GetBool(prefGenPrefab)) { EditorPrefs.SetBool(prefGenPrefab, false); } gen.genMat = Toggle("Generate Materials", prefGenMaterial); gen.genOBJ = Toggle("Generate OBJs", prefGenOBJ); gen.genPrefab = Toggle("Generate Prefabs", prefGenPrefab); if (gen.genOBJ && !gen.genMesh) { EditorPrefs.SetBool(prefGenMesh, true); } if (gen.genPrefab && !gen.genMesh) { EditorPrefs.SetBool(prefGenMesh, true); } if (gen.genPrefab && !gen.genMat) { EditorPrefs.SetBool(prefGenMaterial, true); } if (GUILayout.Button("Generate")) { Generate(objects, valids, gen); } }
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); }
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++; }
// Use this for initialization void Start() { player = findPlayer();//in case we grab the feet first lastPatternBaseEnd = new Vector3(player.transform.position.x, player.GetComponent<Collider2D>().bounds.min.y, 0); gameScreen = new Rect(player.transform.position.x - Settings.gameBackScreenX, player.transform.position.y - Settings.gameScreenY / 2, Settings.gameScreenX, Settings.gameScreenY); lastPatternPlatEnd = new Vector3(); if(levelSeed.Equals(0)) levelSeed = (int)((0.5f - Random.value) * int.MaxValue); Debug.Log(levelSeed); levelRandom = new System.Random(levelSeed); platformHeight = -1; platformLength = 0; platformEnding = false; baseLength = 0; genstate = GenState.ground; patterns = new PatternStyle[PatternStyleGO.Length]; for(int gi = 0; gi<PatternStyleGO.Length; gi++) { PatternStyle ps = PatternStyleGO[gi].GetComponent<PatternStyle>(); patterns[gi] = ps; Debug.Log(ps.ping()); } levelGenerateInitial(5); timer = 0; scoreSys = new ScoreSystem(); guiScoreText.text = "0"; }
private void levelGenerateSetGenState(bool patternTick, bool platTick) { //make gaps in ground if(genstate == GenState.ground && patternTick) { //start chance is base change, reduces with each check (platform length) if(levelRandom.Next(Settings.levelPlatformBaseStartChance + platformLength) == 0) { //reset platform height and length to starting active value platformLength = 0; genstate = GenState.platformStart; } else { //increase chances platformLength--; genstate = GenState.ground; } } //check/end length of current platform if(genstate == GenState.groundPlatform && platTick) { //roll for ending limit using same rules as starting one (see above) if(levelRandom.Next(Settings.levelPlatformBaseEndChance - platformLength) == 0) { //platformEnding = true; genstate = GenState.platformEnding; } else { platformLength++; } } if(genstate == GenState.platformStart && patternTick) { if(platformHeight != -1) { genstate = GenState.groundPlatform; } } if(platTick && (genstate == GenState.platformEnding && platformHeight <= Settings.levelPlatformMinHeight)) { if(platformHeight <= Settings.levelPlatformMinHeight) { platformHeight = -1; platformLength = 0; genstate = GenState.ground; } } }