GodotComponentTest/scenes/TileWorld.cs

429 lines
14 KiB
C#

using Godot;
using System;
using System.Linq;
using System.Diagnostics;
using Godot.Collections;
using Namespace;
using Array = Godot.Collections.Array;
using Vector2 = Godot.Vector2;
using Vector3 = Godot.Vector3;
public class TileWorld : Spatial
{
private enum GenerationState
{
Heightmap,
Color,
Objects,
Done
}
// constants
private static readonly Color RockColor = new Color(0.5f, 0.5f, 0.4f, 1f);
private static readonly Color GrassColor = new Color(0, 0.4f, 0, 1f);
private GenerationState _currentGenerationState = GenerationState.Heightmap;
// signals
[Signal]
delegate void WorldGenerated();
// public members
public enum MapType
{
Noise,
Debug,
Flat,
}
[ExportFlagsEnum(typeof(MapType))] public MapType GenerationMapType = MapType.Debug;
[Export] public int Size = 64;
[Export] public bool DebugMap = false;
public float HeightScale = 2.0f;
public Image Heightmap;
public Image Colormap;
public int Seed = 0;
public Spatial Entities;
public HexGrid HexGrid;
// private members
private int _halfSize;
private Random _tileTypeRandom;
private Viewport _worldOffscreenViewport;
private TextureRect _worldOffscreenTextureRect;
private Viewport _heightmapOffscreenViewport;
private TextureRect _heightmapOffscreenTextureRect;
private Array<Spatial> _rockAssets = new Array<Spatial>();
private Array<Spatial> _grassAssets = new Array<Spatial>();
private Array<Spatial> _treeAssets = new Array<Spatial>();
private Spatial _environmentNode;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
HexGrid = new HexGrid();
_tileTypeRandom = new Random();
_worldOffscreenViewport = (Viewport)GetNode("WorldOffscreenViewport");
Debug.Assert(_worldOffscreenViewport != null);
_worldOffscreenViewport.Size = new Vector2(Size, Size);
_worldOffscreenTextureRect = (TextureRect)GetNode("WorldOffscreenViewport/TextureRect");
Debug.Assert(_worldOffscreenTextureRect != null);
_worldOffscreenTextureRect.SetSize(new Vector2(Size, Size));
_heightmapOffscreenViewport = (Viewport)GetNode("HeightmapOffscreenViewport");
Debug.Assert(_heightmapOffscreenViewport != null);
_heightmapOffscreenViewport.Size = new Vector2(Size, Size);
_heightmapOffscreenTextureRect = (TextureRect)GetNode("HeightmapOffscreenViewport/TextureRect");
Debug.Assert(_heightmapOffscreenTextureRect != null);
_heightmapOffscreenTextureRect.SetSize(new Vector2(Size, Size));
GetNode<Spatial>("Assets").Visible = false;
foreach (Spatial asset in GetNode<Node>("Assets/Rocks").GetChildren())
{
_rockAssets.Add(asset);
}
foreach (Spatial asset in GetNode<Node>("Assets/Grass").GetChildren())
{
_grassAssets.Add(asset);
}
foreach (Spatial asset in GetNode<Node>("Assets/Trees").GetChildren())
{
_treeAssets.Add(asset);
}
_environmentNode = GetNode<Spatial>("Environment");
Entities = GetNode<Spatial>("Entities");
Generate(Size);
}
public void Generate(int size)
{
GD.Print("Triggering generation for size: " + size);
Size = size;
_worldOffscreenViewport.Size = new Vector2(size, size);
_heightmapOffscreenViewport.Size = new Vector2(size, size);
_halfSize = Mathf.RoundToInt((float)size) / 2;
HexGrid.SetBounds(
HexGrid.GetHexAtOffset(new Vector2(-_halfSize, -_halfSize)),
HexGrid.GetHexAtOffset(new Vector2(_halfSize, _halfSize)));
HexGrid.Obstacles.Clear();
HexGrid.Barriers.Clear();
OnMapGenerationStart();
switch (GenerationMapType)
{
case MapType.Debug:
GenerateDebugMap();
break;
case MapType.Flat:
GenerateFlatMap();
break;
case MapType.Noise:
GenerateNoiseMap();
break;
}
}
private void GenerateDebugMap()
{
Colormap = new Image();
Colormap.Create(Size, Size, false, Image.Format.Rgba8);
Heightmap = new Image();
Heightmap.Create(Size, Size, false, Image.Format.Rf);
Heightmap.Lock();
Colormap.Lock();
foreach (int coord_x in Enumerable.Range(0, Size))
{
foreach (int coord_y in Enumerable.Range(0, Size))
{
Colormap.SetPixel(coord_x, coord_y,
new Color((float)Mathf.Min(coord_x, coord_y) / Size, (float)0, 0, 1));
Heightmap.SetPixel(coord_x, coord_y,
new Color((float)Mathf.Min(coord_x, coord_y) / Size, (float)0, 0, 1));
}
}
// Colormap.SetPixel(Size - 1, Size - 1, new Color(1, 1, 1, 1));
Colormap.Fill(Colors.ForestGreen);
Colormap.Unlock();
OnMapGenerationComplete();
}
private void GenerateFlatMap()
{
Colormap = new Image();
Colormap.Create(Size, Size, false, Image.Format.Rgba8);
Heightmap = new Image();
Heightmap.Create(Size, Size, false, Image.Format.Rf);
Heightmap.Lock();
Colormap.Lock();
foreach (int coord_x in Enumerable.Range(0, Size))
{
foreach (int coord_y in Enumerable.Range(0, Size))
{
Heightmap.SetPixel(coord_x, coord_y,
new Color(0, 0, 0, 1));
}
}
Colormap.Fill(Colors.ForestGreen);
Colormap.Unlock();
OnMapGenerationComplete();
}
private void GenerateNoiseMap()
{
Heightmap = new Image();
Heightmap.Create(Size, Size, false, Image.Format.Rgba8);
OpenSimplexNoise noiseGenerator = new OpenSimplexNoise();
noiseGenerator.Seed = Seed;
noiseGenerator.Octaves = 4;
noiseGenerator.Period = 20;
noiseGenerator.Persistence = 0.2f;
noiseGenerator.Lacunarity = 4;
ImageTexture heightmapTexture = new ImageTexture();
heightmapTexture.CreateFromImage(noiseGenerator.GetSeamlessImage((int)Size));
heightmapTexture.Flags = 0;
_heightmapOffscreenTextureRect.Texture = heightmapTexture;
Heightmap.CopyFrom(_heightmapOffscreenViewport.GetTexture().GetData());
OnHeightMapChanged();
}
private void OnMapGenerationStart()
{
foreach (Node child in _environmentNode.GetChildren())
{
child.QueueFree();
}
foreach (Node child in Entities.GetChildren())
{
child.QueueFree();
}
}
private void OnHeightMapChanged()
{
_currentGenerationState = GenerationState.Heightmap;
_heightmapOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Once;
}
private void OnMapGenerationComplete()
{
_currentGenerationState = GenerationState.Done;
EmitSignal("WorldGenerated");
}
Spatial SelectAsset(Vector2 offsetCoord, Array<Spatial> assets, Random randomGenerator, double probability)
{
if (randomGenerator.NextDouble() < 1.0 - probability)
{
return null;
}
int assetIndex = randomGenerator.Next(assets.Count);
Spatial assetInstance = (Spatial)assets[assetIndex].Duplicate();
Transform assetTransform = Transform.Identity;
assetTransform.origin = GetTileWorldCenterFromOffset(offsetCoord);
assetTransform.origin.y += 1.2f;
assetTransform.basis =
assetTransform.basis.Rotated(Vector3.Up, (float)(randomGenerator.NextDouble() * Mathf.Pi * 2));
assetInstance.Transform = assetTransform;
return assetInstance;
}
bool IsColorEqualApprox(Color colorA, Color colorB)
{
Vector3 colorDifference = new Vector3(colorA.r - colorB.r, colorA.g - colorB.g, colorA.b - colorB.b);
return colorDifference.LengthSquared() < 0.1 * 0.1;
}
bool IsColorWater(Color color)
{
return (color.r == 0 && color.g == 0 && color.b > 0.01);
}
private void PopulateEnvironment()
{
Random environmentRandom = new Random(Seed);
Colormap.Lock();
foreach (int coord_x in Enumerable.Range(0, Size))
{
foreach (int coord_y in Enumerable.Range(0, Size))
{
Vector2 offsetCoord = new Vector2(coord_x, coord_y);
Color colorValue = Colormap.GetPixel(coord_x, coord_y);
HexCell cell = HexCell.FromOffsetCoords(offsetCoord - Vector2.One * _halfSize);
if (IsColorEqualApprox(colorValue, RockColor))
{
Spatial rockAsset = SelectAsset(offsetCoord, _rockAssets, environmentRandom, 0.15);
if (rockAsset != null)
{
_environmentNode.AddChild(rockAsset);
HexGrid.AddObstacle(cell);
}
}
else if (IsColorEqualApprox(colorValue, GrassColor))
{
Spatial grassAsset = SelectAsset(offsetCoord, _grassAssets, environmentRandom, 0.35);
if (grassAsset != null)
{
_environmentNode.AddChild(grassAsset);
}
Spatial treeAsset = SelectAsset(offsetCoord, _treeAssets, environmentRandom, 0.10);
if (treeAsset != null)
{
Entities.AddChild(treeAsset);
HexGrid.AddObstacle(cell);
}
} else if (IsColorWater(colorValue))
{
HexGrid.AddObstacle(cell);
}
}
}
Colormap.Unlock();
}
public override void _Process(float delta)
{
if (_currentGenerationState == GenerationState.Heightmap)
{
_currentGenerationState = GenerationState.Color;
ImageTexture imageTexture = new ImageTexture();
imageTexture.CreateFromImage(Heightmap);
imageTexture.Flags = 0;
_worldOffscreenTextureRect.Texture = imageTexture;
_worldOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Once;
}
else if (_currentGenerationState == GenerationState.Color)
{
Colormap = new Image();
Colormap.Create(Size, Size, false, Image.Format.Rgb8);
Colormap.CopyFrom(_worldOffscreenViewport.GetTexture().GetData());
Heightmap.Lock();
_currentGenerationState = GenerationState.Objects;
PopulateEnvironment();
OnMapGenerationComplete();
}
}
public bool IsOffsetCoordValid(Vector2 offsetCoord)
{
return ((int)Math.Clamp(offsetCoord.x, -Size / 2, Size / 2 - 1) == (int)offsetCoord.x
&& (int)Math.Clamp(offsetCoord.y, -Size / 2, Size / 2 - 1) == (int)offsetCoord.y);
}
public HexTile3D.TileType GetTileTypeAtOffset(Vector2 offsetCoord)
{
if (!IsOffsetCoordValid(offsetCoord))
{
return HexTile3D.TileType.Undefined;
}
return HexTile3D.ValidTileTypes[_tileTypeRandom.Next(HexTile3D.ValidTileTypes.Length)];
}
public Vector2 OffsetToTextureCoord(Vector2 offsetCoord)
{
// Vector2 textureCoord = (offsetCoord - Vector2.One * (Mathf.Floor(Size / 2))) % (Vector2.One * Size);
Vector2 mapSize = Vector2.One * Size;
Vector2 textureCoord = (offsetCoord + mapSize / 2).PosMod(mapSize);
return textureCoord;
}
public void SetHeightAtOffset(Vector2 offsetCoord, float height)
{
Vector2 textureCoord = OffsetToTextureCoord(offsetCoord);
Heightmap.SetPixel((int)textureCoord.x, (int)textureCoord.y, new Color(height, 0f, 0f));
}
public float GetHeightAtOffset(Vector2 offsetCoord)
{
if (_currentGenerationState != GenerationState.Done)
{
return 0f;
}
Vector2 textureCoord = OffsetToTextureCoord(offsetCoord);
float heightmapHeight = Heightmap.GetPixel((int)textureCoord.x, (int)(textureCoord.y)).r;
if (heightmapHeight > 0.5)
{
heightmapHeight = 0.6f;
}
// heightmapHeight = Mathf.Floor(heightmapHeight);
// heightmapHeight = heightmapHeight * 10)
// heightmapHeight = Mathf.Clamp(heightmapHeight, -1f, 5);
return heightmapHeight * HeightScale;
}
public void SetTileColorAtOffset(Vector2 offsetCoord, Color color)
{
Vector2 textureCoord = OffsetToTextureCoord(offsetCoord);
Colormap.Lock();
Colormap.SetPixel((int)textureCoord.x, (int)textureCoord.y, color);
Colormap.Unlock();
EmitSignal("WorldGenerated");
}
public Vector2 WorldToOffsetCoords(Vector3 worldCoord)
{
return HexGrid.GetHexAt(new Vector2(worldCoord.x, worldCoord.z)).OffsetCoords + Vector2.One * Mathf.Round(Size / 2f);
}
public Vector3 GetTileWorldCenterFromOffset(Vector2 offsetCoord)
{
Vector2 tileCenter = HexGrid.GetHexCenterFromOffset(offsetCoord - Vector2.One * Mathf.Round(Size / 2f));
return new Vector3(tileCenter.x, GetHeightAtOffset(offsetCoord), tileCenter.y);
}
public Vector3 GetHexCenterFromOffset(Vector2 offsetCoord)
{
Vector2 tileCenter = HexGrid.GetHexCenterFromOffset(offsetCoord);
return new Vector3(tileCenter.x, GetHeightAtOffset(offsetCoord), tileCenter.y);
}
}