public void GenerateAndDisplayMap() { noiseMap = NoiseMap.GenerateNoiseMap(worldWidth, worldHeight, scale, octaves, persistance, lacunarity, animationCurve); MapDisplay mapDisplay = FindObjectOfType <MapDisplay>(); MeshData meshData = MeshGenerator.GenerateMesh(noiseMap, meshMultiplier); Mesh mesh = meshData.CreateMesh(); MeshCollider meshCollider = meshObject.GetComponent <MeshCollider>(); if (meshCollider == null) { meshCollider = meshObject.AddComponent <MeshCollider>(); } meshCollider.sharedMesh = mesh; mapDisplay.DrawMesh(mesh, mapDisplay.CreateTexture(worldWidth, worldHeight)); textureData.ApplyToMaterial(meshObject.GetComponent <Renderer>().sharedMaterial, meshData.minHeight, meshData.maxHeight); }
void Generate() { Destroy(Buildings); Buildings = new GameObject(); Noise = NoiseMap.GenerateNoiseMap(resolution, seed, scale, octaves, persistance, lacunarity, offset, size); float halfSize = size / 2f; for (int z = 0; z <= resolution; z++) { for (int x = 0; x <= resolution; x++) { GameObject b = GameObject.CreatePrimitive(PrimitiveType.Cube); b.transform.position = new Vector3(x + x / (int)group.x * group.y, Noise[x, z] / 2, z + z / (int)group.x * group.y); b.transform.localScale = new Vector3(1f, Noise[x, z], 1f); b.transform.parent = Buildings.transform; b.GetComponent <Renderer>().material = buildingMat[Random.Range(0, buildingMat.Count)]; } } }
/// Temporary method @TODO: Clean this shit. public void TempMapGen() { this.groundNoiseMap = NoiseMap.GenerateNoiseMap(this.size, 11, NoiseMap.GroundWave(42)); foreach (Vector2Int position in this.rect) { this.Spawn( position, new Ground( position, Ground.GroundByHeight(this.groundNoiseMap[position.x + position.y * this.size.x]) ) ); if (this.grids[Layer.Ground].GetTilableAt(position).def.uid == "rocks") { this.Spawn( position, new Mountain(position, Defs.mountains["mountain"]) ); } if (this[position].fertility > 0f && !this[position].blockPlant) { foreach (TilableDef tilableDef in Defs.plants.Values) { if ( this[position].fertility >= tilableDef.plantDef.minFertility && Random.value <= tilableDef.plantDef.probability ) { this.Spawn( position, new Plant(position, tilableDef, true) ); break; } } } } this.UpdateConnectedMountains(); }
public void MapGeneration() { float[,] noiseMap = NoiseMap.GenerateNoiseMap(mapWidth, mapHeight, seed, noiseScale, octaves, persistance, lacunarity, offSet); Color[] colorMap = new Color[mapWidth * mapHeight]; for (int yy = 0; yy < mapHeight; yy++) { for (int xx = 0; xx < mapWidth; xx++) { float currentHeight = noiseMap[xx, yy]; for (int ii = 0; ii < regions.Length; ii++) { if (currentHeight <= regions[ii].height) { colorMap[yy * mapWidth + xx] = regions[ii].color; break; } } } } MapDisplay display = FindObjectOfType <MapDisplay>(); if (drawMode == DrawMode.NoiseMap) { display.DrawTexture(TextureGenerator.TextureFromHeightMap(noiseMap)); } else if (drawMode == DrawMode.colorMap) { display.DrawTexture(TextureGenerator.TextureFromHeightMap(noiseMap)); } else if (drawMode == DrawMode.Mesh) { display.DrawMesh(MapMeshGenerator.GenerateMapMesh(noiseMap, meshHeightMulti, meshHeightCurve), TextureGenerator.TextureFromColorMap(colorMap, mapWidth, mapHeight)); } }
static void Main(string[] args) { parameters = new Parameters(args); //print error codes if (parameters.ErrorCode != 0) { switch (parameters.ErrorCode) { case 1: Console.WriteLine("Error: Invalid program arguments."); break; case 2: Console.WriteLine("Error: Row count has to be positive."); break; case 3: Console.WriteLine("Error: Column count has to be positive."); break; case 4: Console.WriteLine("Error: level count has to be positive."); break; case 5: Console.WriteLine("Error: Output image would be too tall: reduce number of rows or tile size."); break; case 6: Console.WriteLine("Error: Output image would be too wide: reduce number of columns or tile size."); break; case 7: Console.WriteLine("Error: Too many subdivision levels: reduce level count or increase tile size."); break; case 8: Console.WriteLine("Error: Invalid palette id specified."); break; } return; } //if -h is given as a program argument if (parameters.DisplayHelp) { PrintHelp(); return; } //actual program starts here random = new Random(parameters.Seed); tileset = new Tileset(parameters.TileSize, parameters.DivisionLevels, Palette.PaletteList[parameters.PaletteID]); int canvasWidth = (parameters.ColumnCount) * parameters.TileSize; int canvasHeight = (parameters.RowCount) * parameters.TileSize; if (!parameters.Borderless) { canvasWidth += parameters.TileSize; canvasHeight += parameters.TileSize; } double[,] noise = null; //block matrix for the tiles Tile[,] tileMatrix; if (parameters.Perlin) { noise = NoiseMap.GenerateNoiseMap(random, canvasWidth / 10, canvasHeight / 10, 2d, 1d, 3); tileMatrix = CreatePerlinTileMatrix(noise); } else { tileMatrix = CreatePseudorandomTileMatrix(); } GenerateImage(canvasWidth, canvasHeight, tileMatrix); //debug if (parameters.Debug) { if (parameters.Perlin) { GenerateNoiseImage(noise); } tileset.GenerateDebugImage(); } }
public void GenerateLevel(int height, int width) { int seed = Random.Range(-100000, 100000); this.Width = width; this.Height = height; GameObject Blocks = new GameObject("Blocks"); GenerateBlockPrefab(Blocks); BlockList = new Block[Width, Height]; float[,] noiseMap = NoiseMap.GenerateNoiseMap(Width, Height, seed, 50, 2, .5f, 3, new Vector2(5, 8)); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { GameObject Block = Instantiate(Blocks, new Vector3(x, y, 0), Quaternion.identity); BlockList[x, y] = new Block(x, y, Block, BlockType.Empty); Block.name = "Block No." + x + "|" + y; int RandomOre = Random.Range(1, 100); if (y == 0 || y == Height - 1 || x == 0 || x == Width - 1) { BlockList[x, y]._Type = BlockType.Bedrock; } else if ((int)(noiseMap[x, y] * 100) > 5 && RandomOre > 5) { int Height = RandomOre; if (Height < 75) { BlockList[x, y]._Type = BlockType.Stone; } else if (Height > 75 && Height < 85) { BlockList[x, y]._Type = BlockType.Coal; } else if (Height > 85 && Height < 95) { BlockList[x, y]._Type = BlockType.Iron; } else if (Height < 95) { BlockList[x, y]._Type = BlockType.Gold; } } else { BlockList[x, y]._Type = BlockType.Empty; } } } GameObject PlayerObject = Player.Instance.gameObject; PlayerObject.transform.position = new Vector2(Random.Range(40, Width - 40), Random.Range(25, Height - 25)); for (int x = (int)PlayerObject.transform.position.x; x < (int)PlayerObject.transform.position.x + 5; x++) { for (int y = (int)PlayerObject.transform.position.y; y < (int)PlayerObject.transform.position.y + 5; y++) { BlockList[x - 2, y - 2].ChangeType(BlockType.Empty); } } for (int i = 0; i < LevelIndex + 1; i++) { SpawnBoss(); } for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { Block tmpBlock = BlockList[x, y]; if (tmpBlock._Type != BlockType.Empty && x != 0 && y != 0 && x != Width - 1 && y != Height - 1) { CheckForNeighbourBlocks(BlockList[x, y]); tmpBlock.ChangeType(tmpBlock._Type); } else if (tmpBlock._Type == BlockType.Empty) { tmpBlock.MyGameObject.GetComponent <BoxCollider2D>().enabled = false; tmpBlock.MyGameObject.GetComponent <SpriteRenderer>().sprite = GrassSprites[Random.Range(0, 3)]; } } } BossCount = LevelIndex + 1; StartCoroutine(LevelTimer(Player.Instance.TimeLimit)); }
/// Temporary method @TODO: Clean this shit. public void TempMapGen() { this.groundNoiseMap = NoiseMap.GenerateNoiseMap(this.size, 11, NoiseMap.GroundWave(42)); foreach (Vector2Int position in this.rect) { this.Spawn( position, new Ground( position, Ground.GroundByHeight(this.groundNoiseMap[position.x + position.y * this.size.x]) ) ); if (this.grids[Layer.Ground].GetTilableAt(position).def.uid == "rocks") { this.Spawn( position, new Mountain(position, Defs.mountains["mountain"]) ); } if (this[position].fertility > 0f && !this[position].blockPlant) { foreach (TilableDef tilableDef in Defs.plants.Values) { if ( this[position].fertility >= tilableDef.plantDef.minFertility && Random.value <= tilableDef.plantDef.probability ) { this.Spawn( position, new Plant(position, tilableDef, true) ); break; } } } } foreach (LayerGridBucket bucket in this.grids[Layer.Mountain].buckets) { bool changed = false; foreach (Tilable tilable in bucket.tilables) { if (tilable != null) { tilable.UpdateGraphics(); changed = true; } } if (changed) { bucket.rebuildMatrices = true; } } foreach (Vector2Int position in new RectI(new Vector2Int(10, 10), 5, 5)) { if (this[position].blockStackable == false) { this.Spawn(position, new Stackable( position, Defs.stackables["logs"], Random.Range(1, Defs.stackables["logs"].maxStack) )); } } }