using Godot; using System; public partial class World : Node2D { public enum GeneratorAlgorithm { RANDOM, FAST_NOISE_LITE } Vector2I darkGras = new Vector2I(0, 0); Vector2I brightGras = new Vector2I(1, 0); Vector2I flower = new Vector2I(2, 0); TileMapLayer tileMapLayer; Vector2 screenSize; float worldWidth; float worldHeight; [Export] GeneratorAlgorithm algorithm = GeneratorAlgorithm.RANDOM; public override void _Ready() { screenSize = GetViewportRect().Size; worldWidth = screenSize.X; worldHeight = screenSize.Y; tileMapLayer = GetNode("TileMapLayer"); GD.Randomize(); GenerateWorld(); } private void GenerateWorld() { switch (algorithm) { case GeneratorAlgorithm.RANDOM: GenerateWorldRandom(); break; case GeneratorAlgorithm.FAST_NOISE_LITE: GenerateWorldSimplexNoise(); break; default: break; } } private void GenerateWorldRandom() { for (int y = 0; y < worldHeight; y++) { for (int x = 0; x < worldWidth; x++) { float rnd = GD.Randf(); if (rnd < .7f) tileMapLayer.SetCell(new Vector2I(x, y), 0, darkGras); else if (rnd < .9f) tileMapLayer.SetCell(new Vector2I(x, y), 0, brightGras); else tileMapLayer.SetCell(new Vector2I(x, y), 0, flower); } } } private void GenerateWorldSimplexNoise() { var noise = new FastNoiseLite(); noise.Seed = GD.RandRange(int.MinValue, int.MaxValue); noise.FractalOctaves = 2; for (int y = 0; y < worldHeight; y++) { for (int x = 0; x < worldWidth; x++) { var rnd = noise.GetNoise2D(x, y); if (rnd < .3f) tileMapLayer.SetCell(new Vector2I(x, y), 0, darkGras); else if (rnd < .6f) tileMapLayer.SetCell(new Vector2I(x, y), 0, brightGras); else tileMapLayer.SetCell(new Vector2I(x, y), 0, flower); } } } }