diff --git a/half-earth/nodes/game.tscn b/half-earth/nodes/game.tscn index c86e8f4..bde9c19 100644 --- a/half-earth/nodes/game.tscn +++ b/half-earth/nodes/game.tscn @@ -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"] diff --git a/half-earth/nodes/overlays.tscn b/half-earth/nodes/overlays.tscn new file mode 100644 index 0000000..1e6405b --- /dev/null +++ b/half-earth/nodes/overlays.tscn @@ -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 ) diff --git a/half-earth/resources/overlays/heat_overlay.tres b/half-earth/resources/overlays/heat_overlay.tres new file mode 100644 index 0000000..5f1c269 --- /dev/null +++ b/half-earth/resources/overlays/heat_overlay.tres @@ -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 ) diff --git a/half-earth/resources/overlays/no_overlay.tres b/half-earth/resources/overlays/no_overlay.tres new file mode 100644 index 0000000..d4a783f --- /dev/null +++ b/half-earth/resources/overlays/no_overlay.tres @@ -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 ) diff --git a/half-earth/resources/tiles/tile_types/developed.tres b/half-earth/resources/tiles/tile_types/developed.tres index 1efdccf..70a9358 100644 --- a/half-earth/resources/tiles/tile_types/developed.tres +++ b/half-earth/resources/tiles/tile_types/developed.tres @@ -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 diff --git a/half-earth/scripts/WorldGrid.cs b/half-earth/scripts/WorldGrid.cs index 6083638..6a7b01a 100644 --- a/half-earth/scripts/WorldGrid.cs +++ b/half-earth/scripts/WorldGrid.cs @@ -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; diff --git a/half-earth/scripts/overlays/HeatOverlay.cs b/half-earth/scripts/overlays/HeatOverlay.cs new file mode 100644 index 0000000..42d1ca4 --- /dev/null +++ b/half-earth/scripts/overlays/HeatOverlay.cs @@ -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); + } +} diff --git a/half-earth/scripts/overlays/NoOverlay.cs b/half-earth/scripts/overlays/NoOverlay.cs new file mode 100644 index 0000000..f92d2de --- /dev/null +++ b/half-earth/scripts/overlays/NoOverlay.cs @@ -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) + { + } +} \ No newline at end of file diff --git a/half-earth/scripts/overlays/Overlay.cs b/half-earth/scripts/overlays/Overlay.cs new file mode 100644 index 0000000..d395c09 --- /dev/null +++ b/half-earth/scripts/overlays/Overlay.cs @@ -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); +} \ No newline at end of file diff --git a/half-earth/scripts/overlays/Overlays.cs b/half-earth/scripts/overlays/Overlays.cs new file mode 100644 index 0000000..85c844d --- /dev/null +++ b/half-earth/scripts/overlays/Overlays.cs @@ -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(_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); + } + } +}