using Godot; using System; using System.Linq; public class TileWorld : Spatial { // signals [Signal] delegate void WorldGenerated(); // public members public Image Heightmap; public Vector2 Size = new Vector2(100, 100); public float HeightScale = 10; // private members private HexGrid _hexGrid; private Random _tileTypeRandom; // Called when the node enters the scene tree for the first time. public override void _Ready() { _hexGrid = new HexGrid(); _tileTypeRandom = new Random(); } public void Generate() { // GenerateSimpleMap(); GenerateNoiseMap(); EmitSignal("WorldGenerated"); } private void GenerateSimpleMap() { Heightmap = new Image(); Heightmap.Create((int)Size.x, (int)Size.y, false, Image.Format.Rf); Heightmap.Lock(); foreach (int coord_x in Enumerable.Range(0, (int)Size.x)) { foreach (int coord_y in Enumerable.Range(0, (int)Size.y)) { SetHeightAtOffset(new Vector2(coord_x, coord_y), 0.5f); } } } private void GenerateNoiseMap() { Heightmap = new Image(); Heightmap.Create((int)Size.x, (int)Size.y, false, Image.Format.Rf); NoiseTexture noise_texture = new NoiseTexture(); OpenSimplexNoise noise_generator = new OpenSimplexNoise(); noise_generator.Seed = -1626828106; noise_generator.Octaves = 3; noise_generator.Period = 5; noise_generator.Persistence = 0.5f; noise_generator.Lacunarity = 2; Heightmap.CopyFrom(noise_generator.GetImage((int)Size.x, (int)Size.y, null)); } private void ApplyHeightMap() { } public bool IsOffsetCoordValid(Vector2 offset_coord) { return ((int)Math.Clamp(offset_coord.x, 0, Size.x) == (int)offset_coord.x && (int)Math.Clamp(offset_coord.y, 0, Size.y) == (int)offset_coord.y); } public HexTile3D.TileType GetTileTypeAtOffset(Vector2 offset_coord) { if (!IsOffsetCoordValid(offset_coord)) { return HexTile3D.TileType.Undefined; } return HexTile3D.ValidTileTypes[_tileTypeRandom.Next(HexTile3D.ValidTileTypes.Length)]; } public void SetHeightAtOffset(Vector2 offset_coord, float height) { if (!IsOffsetCoordValid(offset_coord)) { return; } Heightmap.SetPixel((int) offset_coord.x, (int) offset_coord.y, new Color(height / HeightScale, 0f, 0f)); } public float GetHeightAtOffset(Vector2 offset_coord) { if (!IsOffsetCoordValid(offset_coord)) { return -0.1f; } return Heightmap.GetPixel((int)offset_coord.x, (int)offset_coord.y).r * HeightScale - HeightScale * 0.5f ; } public Vector2 WorldToOffsetCoords(Vector3 world_coord) { return _hexGrid.GetHexAt(new Vector2(world_coord.x, world_coord.z)).OffsetCoords; } // // Called every frame. 'delta' is the elapsed time since the previous frame. // public override void _Process(float delta) // { // // } }