164 lines
4.0 KiB
C#
164 lines
4.0 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public class WorldGrid : Node2D
|
|
{
|
|
[Export]
|
|
private PackedScene TileScene { get; set; }
|
|
|
|
[Export]
|
|
public int Size { get; set; }
|
|
|
|
[Export]
|
|
public int CellSize { get; set; }
|
|
|
|
[Export]
|
|
public float DiffusionCoefficient { get; set; } = 0.01f;
|
|
|
|
private Tile[,] _tiles;
|
|
private float[,] _nextValues = null;
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
public override void _Ready()
|
|
{
|
|
GenerateGrid(Size);
|
|
}
|
|
|
|
public override void _Process(float delta)
|
|
{
|
|
base._Process(delta);
|
|
|
|
for (int x = 0; x < Size; x++)
|
|
{
|
|
for (int y = 0; y < Size; y++)
|
|
{
|
|
var tile = _tiles[x, y];
|
|
var material = tile.material;
|
|
material.SetShaderParam("t", tile.value);
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool IsInBounds(int x, int y)
|
|
{
|
|
return x >= 0 && x < Size && y >= 0 && y < Size;
|
|
}
|
|
|
|
public void ToggleTileHighlight(int x, int y)
|
|
{
|
|
if (!IsInBounds(x, y))
|
|
return;
|
|
|
|
_tiles[x, y].isHighlighted ^= true;
|
|
}
|
|
|
|
public void SetTileValue(int x, int y, float value)
|
|
{
|
|
_tiles[x, y].value = value;
|
|
}
|
|
|
|
public void GetGridPos(Vector2 position, out int x, out int y)
|
|
{
|
|
x = Mathf.FloorToInt(position.x / CellSize);
|
|
y = Mathf.FloorToInt(position.y / CellSize);
|
|
}
|
|
|
|
private void Clear()
|
|
{
|
|
_tiles = null;
|
|
|
|
int children = this.GetChildCount();
|
|
GD.Print(children);
|
|
for (int i = 0; i < children; i++)
|
|
{
|
|
var child = this.GetChild(i);
|
|
child.QueueFree();
|
|
}
|
|
}
|
|
|
|
private void GenerateGrid(int size)
|
|
{
|
|
this.Clear();
|
|
|
|
_tiles = new Tile[size, size];
|
|
_nextValues = new float[size, size];
|
|
|
|
for (int x = 0; x < size; x++)
|
|
{
|
|
for (int y = 0; y < size; y++)
|
|
{
|
|
var tile = new Tile();
|
|
tile.isHighlighted = false;
|
|
tile.value = 0.0f;
|
|
|
|
var node = TileScene.Instance<Node2D>();
|
|
this.AddChild(node);
|
|
var position = new Vector2
|
|
{
|
|
x = (x + .5f) * CellSize,
|
|
y = (y + .5f) * CellSize
|
|
};
|
|
node.Position = position;
|
|
var canvasItem = (CanvasItem)node;
|
|
tile.material = (ShaderMaterial)canvasItem.Material;
|
|
|
|
_tiles[x, y] = tile;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void _on_Clock_OnTick(int ticks)
|
|
{
|
|
Diffuse();
|
|
}
|
|
|
|
private float TransferHeat(float t0, float alpha, float nx, float ny, float px, float py)
|
|
{
|
|
float d2tdx2 = nx - 2 * t0 + px;
|
|
float d2tdy2 = ny - 2 * t0 + py;
|
|
return t0 + alpha * (d2tdx2 + d2tdy2);
|
|
}
|
|
|
|
private void GetNeighbourTemperatures(int x, int y, out float nx, out float ny, out float px, out float py)
|
|
{
|
|
// default value
|
|
var t = _tiles[x, y].value;
|
|
|
|
nx = x > 0 ? _tiles[x - 1, y].value : t;
|
|
px = x < Size - 1 ? _tiles[x + 1, y].value : t;
|
|
|
|
ny = y > 0 ? _tiles[x, y - 1].value : t;
|
|
py = y < Size - 1 ? _tiles[x, y + 1].value : t;
|
|
}
|
|
|
|
private void Diffuse()
|
|
{
|
|
for (int x = 0; x < Size; x++)
|
|
{
|
|
for (int y = 0; y < Size; y++)
|
|
{
|
|
float t = _tiles[x, y].value;
|
|
var D = DiffusionCoefficient;
|
|
|
|
GetNeighbourTemperatures(
|
|
x, y,
|
|
out var nx,
|
|
out var ny,
|
|
out var px,
|
|
out var py);
|
|
|
|
// current value
|
|
_nextValues[x, y] = TransferHeat(t, D, nx, ny, px, py);
|
|
}
|
|
}
|
|
|
|
for (int x = 0; x < Size; x++)
|
|
{
|
|
for (int y = 0; y < Size; y++)
|
|
{
|
|
_tiles[x, y].value = _nextValues[x, y];
|
|
}
|
|
}
|
|
}
|
|
}
|