add cycleable heat overlay #6

This commit is contained in:
Cat Flynn 2022-12-10 17:24:14 +00:00 committed by ktyl
parent cf03021c2a
commit 95e201888a
10 changed files with 162 additions and 23 deletions

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=11 format=2]
[gd_scene load_steps=12 format=2]
[ext_resource path="res://nodes/grid.tscn" type="PackedScene" id=1]
[ext_resource path="res://nodes/grid_cursor.tscn" type="PackedScene" id=2]
@ -9,6 +9,7 @@
[ext_resource path="res://nodes/interaction_modes/build_mode.tscn" type="PackedScene" id=7]
[ext_resource path="res://resources/tiles/tiles.tres" type="Resource" id=8]
[ext_resource path="res://nodes/ui/mode_selection_ui.tscn" type="PackedScene" id=9]
[ext_resource path="res://nodes/overlays.tscn" type="PackedScene" id=10]
[sub_resource type="Curve" id=1]
_data = [ Vector2( 0, 0 ), 0.0, 5.0, 0, 0, Vector2( 0.5, 1 ), 0.0, 0.0, 0, 0, Vector2( 1, 0 ), -4.0, 0.0, 0, 0 ]
@ -18,21 +19,24 @@ _data = [ Vector2( 0, 0 ), 0.0, 5.0, 0, 0, Vector2( 0.5, 1 ), 0.0, 0.0, 0, 0, Ve
[node name="Clock" parent="." instance=ExtResource( 4 )]
_autoTick = true
[node name="Grid" parent="." instance=ExtResource( 1 )]
[node name="World Grid" parent="." instance=ExtResource( 1 )]
DiffusionCoefficient = 0.1
_tileGridResource = ExtResource( 8 )
[node name="Cursor" parent="." instance=ExtResource( 2 )]
Grid = NodePath("../Grid")
Grid = NodePath("../World Grid")
_pulseShape = SubResource( 1 )
[node name="Interaction Modes" parent="." instance=ExtResource( 5 )]
_buildModePath = NodePath("Build Mode")
[node name="Build Mode" parent="Interaction Modes" instance=ExtResource( 7 )]
_gridPath = NodePath("../../Grid")
_gridPath = NodePath("../../World Grid")
_cursorPath = NodePath("../../Cursor")
[node name="Overlays" parent="." instance=ExtResource( 10 )]
_worldGridPath = NodePath("../World Grid")
[node name="UI" type="Control" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
@ -46,7 +50,7 @@ _buildModePath = NodePath("../../Interaction Modes/Build Mode")
[node name="Mode Selection" parent="UI" instance=ExtResource( 9 )]
[connection signal="OnPauseChanged" from="Clock" to="UI/Debug" method="_on_Clock_OnPauseChanged"]
[connection signal="OnTick" from="Clock" to="Grid" method="_on_Clock_OnTick"]
[connection signal="OnTick" from="Clock" to="World Grid" method="_on_Clock_OnTick"]
[connection signal="OnTick" from="Clock" to="UI/Debug" method="_on_Clock_OnTick"]
[connection signal="OnInteractionModeChanged" from="Interaction Modes" to="Cursor" method="_on_Interaction_Mode_OnInteractionModeChanged"]
[connection signal="OnInteractionModeChanged" from="Interaction Modes" to="UI/Debug" method="_on_Interaction_Mode_OnInteractionModeChanged"]

View File

@ -0,0 +1,12 @@
[gd_scene load_steps=5 format=2]
[ext_resource path="res://scripts/overlays/Overlays.cs" type="Script" id=1]
[ext_resource path="res://resources/overlays/heat_overlay.tres" type="Resource" id=2]
[ext_resource path="res://resources/overlays/no_overlay.tres" type="Resource" id=3]
[ext_resource path="res://resources/tiles/tiles.tres" type="Resource" id=4]
[node name="Overlays" type="Node"]
script = ExtResource( 1 )
_cycleOverlayKey = 16777218
_overlayResources = [ ExtResource( 3 ), ExtResource( 2 ) ]
_tileGridResource = ExtResource( 4 )

View File

@ -0,0 +1,8 @@
[gd_resource type="Resource" load_steps=2 format=2]
[ext_resource path="res://scripts/overlays/HeatOverlay.cs" type="Script" id=1]
[resource]
script = ExtResource( 1 )
_coldColor = Color( 0, 0, 0, 1 )
_hotColor = Color( 1, 0.654902, 0, 1 )

View File

@ -0,0 +1,6 @@
[gd_resource type="Resource" load_steps=2 format=2]
[ext_resource path="res://scripts/overlays/NoOverlay.cs" type="Script" id=1]
[resource]
script = ExtResource( 1 )

View File

@ -8,4 +8,4 @@ Name = "Developed"
BuildLabel = "[D]eveloped"
Key = 68
Color = Color( 0.372549, 0.372549, 0.372549, 1 )
HeatGeneration = 0.5
HeatGeneration = 0.1

View File

@ -18,7 +18,7 @@ public class WorldGrid : Node2D
private float[] _nextValues = null;
private struct TileView
public struct TileView
{
public ShaderMaterial material;
}
@ -32,21 +32,6 @@ public class WorldGrid : Node2D
GenerateCanvasItems(_tileGrid);
}
public override void _Process(float delta)
{
base._Process(delta);
// TODO: this is a rendering concern that should be performed
// by the heat overlay
for (int i = 0; i < _tileGrid.Count; i++)
{
var tile = _tileGrid[i];
var view = _tileViews[i];
view.material.SetShaderParam("t", tile.temperature);
}
}
#region Positioning
public void GetGridPos(Vector2 position, out int x, out int y)
{
@ -116,13 +101,15 @@ public class WorldGrid : Node2D
}
}
public ShaderMaterial GetTileMaterial(int idx) => _tileViews[idx].material;
#region Simulation
public void _on_Clock_OnTick(int ticks)
{
GenerateHeat();
Diffuse();
}
#region Simulation
private float TransferHeat(float t0, float alpha, float nx, float ny, float px, float py)
{
float d2tdx2 = nx - 2 * t0 + px;

View File

@ -0,0 +1,22 @@
using Godot;
using System;
public class HeatOverlay : Overlay
{
[Export]
private Color _coldColor;
[Export]
private Color _hotColor;
public override void Initialise(Tile tile, ShaderMaterial material)
{
material.SetShaderParam("lowColor", _coldColor);
material.SetShaderParam("highColor", _hotColor);
}
public override void Process(Tile tile, ShaderMaterial material)
{
material.SetShaderParam("t", tile.temperature);
}
}

View File

@ -0,0 +1,17 @@
using Godot;
public class NoOverlay : Overlay
{
// in this view we want to draw tiles with their normal colour
public override void Initialise(Tile tile, ShaderMaterial material)
{
var type = tile.type;
material.SetShaderParam("lowColor", type.Color);
material.SetShaderParam("t", 0);
}
public override void Process(Tile tile, ShaderMaterial material)
{
}
}

View File

@ -0,0 +1,7 @@
using Godot;
public abstract class Overlay : Resource
{
public abstract void Initialise(Tile tile, ShaderMaterial material);
public abstract void Process(Tile tile, ShaderMaterial material);
}

View File

@ -0,0 +1,76 @@
using Godot;
using System;
public class Overlays : Node
{
[Export]
private KeyList _cycleOverlayKey;
[Export]
private Resource[] _overlayResources;
private Overlay[] _overlays;
private int _overlayIdx = 0;
private Overlay Current => _overlays[_overlayIdx];
[Export]
private Resource _tileGridResource;
private TileGrid _tileGrid;
[Export]
private NodePath _worldGridPath;
private WorldGrid _worldGrid;
public override void _Ready()
{
_tileGrid = (TileGrid)_tileGridResource;
_overlays = new Overlay[_overlayResources.Length];
for (int i = 0; i < _overlayResources.Length; i++)
{
_overlays[i] = (Overlay)_overlayResources[i];
}
_worldGrid = GetNode<WorldGrid>(_worldGridPath);
}
public override void _Input(InputEvent @event)
{
base._Input(@event);
if (!(@event is InputEventKey keyEvent))
return;
if (!(keyEvent.Pressed))
return;
if ((KeyList)keyEvent.Scancode != _cycleOverlayKey)
return;
CycleOverlay();
}
public override void _Process(float delta)
{
base._Process(delta);
for (int i = 0; i < _tileGrid.Count; i++)
{
var tile = _tileGrid[i];
var material = _worldGrid.GetTileMaterial(i);
Current.Process(tile, material);
}
}
private void CycleOverlay()
{
_overlayIdx++;
_overlayIdx %= _overlays.Length;
for (int i = 0; i < _tileGrid.Count; i++)
{
var tile = _tileGrid[i];
var material = _worldGrid.GetTileMaterial(i);
Current.Initialise(tile, material);
}
}
}