Compare commits

...

6 Commits

Author SHA1 Message Date
Cat Flynn db3e3e7253 add tick clock and tick counter UI #4 2022-12-06 20:11:42 +00:00
Cat Flynn 0905515c11 extract struct to file 2022-12-06 19:51:12 +00:00
Cat Flynn 8d122b650f add cursor bounds check #3 2022-12-06 19:50:56 +00:00
Cat Flynn 4ddb3cebf6 toggle tiles with cursor #3 2022-12-06 19:15:38 +00:00
Cat Flynn 318aa1486f grid with gradient #3 2022-12-06 19:15:07 +00:00
Cat Flynn 02e2928d7e move default resources #3 2022-12-06 19:14:27 +00:00
20 changed files with 351 additions and 5 deletions

View File

@ -0,0 +1,6 @@
<Project Sdk="Godot.NET.Sdk/3.3.0">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<RootNamespace>Halfearth</RootNamespace>
</PropertyGroup>
</Project>

19
half-earth/Half-earth.sln Normal file
View File

@ -0,0 +1,19 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Half-earth", "Half-earth.csproj", "{C894A75C-991B-4000-9BD9-9B0B186304C4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
ExportDebug|Any CPU = ExportDebug|Any CPU
ExportRelease|Any CPU = ExportRelease|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C894A75C-991B-4000-9BD9-9B0B186304C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C894A75C-991B-4000-9BD9-9B0B186304C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C894A75C-991B-4000-9BD9-9B0B186304C4}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU
{C894A75C-991B-4000-9BD9-9B0B186304C4}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU
{C894A75C-991B-4000-9BD9-9B0B186304C4}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU
{C894A75C-991B-4000-9BD9-9B0B186304C4}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,9 @@
[gd_resource type="ShaderMaterial" load_steps=2 format=2]
[ext_resource path="res://shaders/tile_shader.gdshader" type="Shader" id=1]
[resource]
shader = ExtResource( 1 )
shader_param/baseColor = Color( 0.992157, 1, 0, 1 )
shader_param/highlightColor = Color( 1, 0, 0.984314, 1 )
shader_param/isHighlighted = 0

View File

@ -0,0 +1,10 @@
[gd_resource type="ShaderMaterial" load_steps=2 format=2]
[ext_resource path="res://shaders/tile_shader.gdshader" type="Shader" id=1]
[resource]
resource_local_to_scene = true
shader = ExtResource( 1 )
shader_param/baseColor = Color( 0.0823529, 0, 1, 1 )
shader_param/highlightColor = Color( 1, 0, 0, 1 )
shader_param/isHighlighted = null

View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://scripts/Clock.cs" type="Script" id=1]
[node name="Clock" type="Node"]
script = ExtResource( 1 )

View File

@ -0,0 +1,22 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://scripts/DebugUI.cs" type="Script" id=1]
[node name="Debug UI" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 1 )
TicksLabelPath = NodePath("Ticks")
[node name="Ticks" type="Label" parent="."]
anchor_left = 1.0
anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
margin_right = -10.0
margin_bottom = -10.0
grow_horizontal = 0
grow_vertical = 0
text = "ticks: 0"
align = 2
valign = 2

View File

@ -0,0 +1,19 @@
[gd_scene load_steps=5 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]
[ext_resource path="res://nodes/debug_ui.tscn" type="PackedScene" id=3]
[ext_resource path="res://nodes/clock.tscn" type="PackedScene" id=4]
[node name="Root" type="Node"]
[node name="Grid" parent="." instance=ExtResource( 1 )]
[node name="Cursor" parent="." instance=ExtResource( 2 )]
Grid = NodePath("../Grid")
[node name="Debug UI" parent="." instance=ExtResource( 3 )]
[node name="Clock" parent="." instance=ExtResource( 4 )]
[connection signal="OnTick" from="Clock" to="Debug UI" method="_on_Clock_OnTick"]

View File

@ -0,0 +1,10 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://scripts/WorldGrid.cs" type="Script" id=1]
[ext_resource path="res://nodes/tile.tscn" type="PackedScene" id=2]
[node name="Grid" type="Node2D"]
script = ExtResource( 1 )
TileScene = ExtResource( 2 )
Size = 10
CellSize = 64

View File

@ -0,0 +1,10 @@
[gd_scene load_steps=4 format=2]
[ext_resource path="res://textures/icon.png" type="Texture" id=1]
[ext_resource path="res://scripts/GridCursor.cs" type="Script" id=2]
[ext_resource path="res://materials/cursor.tres" type="Material" id=3]
[node name="Grid Cursor" type="Sprite"]
material = ExtResource( 3 )
texture = ExtResource( 1 )
script = ExtResource( 2 )

View File

@ -0,0 +1,8 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://materials/tile.tres" type="Material" id=1]
[ext_resource path="res://textures/icon.png" type="Texture" id=2]
[node name="Tile" type="Sprite"]
material = ExtResource( 1 )
texture = ExtResource( 2 )

View File

@ -11,7 +11,8 @@ config_version=4
[application]
config/name="Half-earth"
config/icon="res://icon.png"
run/main_scene="res://nodes/game.tscn"
config/icon="res://textures/icon.png"
[gui]
@ -27,4 +28,5 @@ common/enable_pause_aware_picking=true
[rendering]
environment/default_environment="res://default_env.tres"
gles3/shaders/shader_compilation_mode=1
environment/default_environment="res://resources/default_env.tres"

View File

@ -0,0 +1,31 @@
using Godot;
using System;
public class Clock : Node
{
[Signal]
delegate void OnTick(int ticks);
private int _ticks = 0;
public override void _Input(InputEvent @event)
{
base._Input(@event);
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
{
switch ((KeyList)keyEvent.Scancode)
{
case KeyList.Space:
Tick();
break;
}
}
}
private void Tick()
{
_ticks++;
EmitSignal(nameof(OnTick), _ticks);
}
}

View File

@ -0,0 +1,22 @@
using Godot;
using System;
public class DebugUI : Control
{
[Export]
public NodePath TicksLabelPath { private get; set; }
private Label _ticksLabel;
public override void _Ready()
{
_ticksLabel = GetNode<Label>(TicksLabelPath);
SetTicksText(0);
}
public void _on_Clock_OnTick(int ticks) => SetTicksText(ticks);
private void SetTicksText(int ticks)
{
_ticksLabel.Text = $"ticks: {ticks}";
}
}

View File

@ -0,0 +1,51 @@
using Godot;
using System;
public class GridCursor : Sprite
{
[Export]
public NodePath Grid { get; set; }
private WorldGrid _grid;
private ShaderMaterial _material;
private const string IS_HIGHLIGHTED = "isHighlighted";
public override void _Ready()
{
_grid = GetNode<WorldGrid>(Grid);
_material = (ShaderMaterial)Material;
}
public override void _Input(InputEvent @event)
{
base._Input(@event);
if (@event is InputEventMouseMotion mouseMoveEvent)
{
var pos = mouseMoveEvent.Position;
_grid.GetGridPos(pos, out var x, out var y);
this.Visible = _grid.IsInBounds(x, y);
var position = new Vector2(x + .5f, y + .5f) * _grid.CellSize;
this.Position = position;
}
else if (@event is InputEventMouseButton mouseButtonEvent)
{
switch ((ButtonList)mouseButtonEvent.ButtonIndex)
{
case ButtonList.Left:
if (mouseButtonEvent.Pressed)
{
var pos = mouseButtonEvent.Position;
_grid.GetGridPos(pos, out var x, out var y);
_grid.ToggleTileHighlight(x, y);
_material.SetShaderParam(IS_HIGHLIGHTED, 1);
}
else
{
_material.SetShaderParam(IS_HIGHLIGHTED, 0);
}
break;
}
}
}
}

View File

@ -0,0 +1,8 @@
using Godot;
public struct Tile
{
public bool isHighlighted;
public float value;
public ShaderMaterial material;
}

View File

@ -0,0 +1,104 @@
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; }
private Tile[,] _tiles;
// 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("isHighlighted", tile.isHighlighted ? 1 : 0);
}
}
}
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];
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;
}
}
}
}

View File

@ -0,0 +1,9 @@
shader_type canvas_item;
uniform vec4 baseColor : hint_color;
uniform vec4 highlightColor : hint_color;
uniform int isHighlighted;
void fragment() {
COLOR = mix(baseColor, highlightColor, float(isHighlighted));
}

View File

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@ -2,15 +2,15 @@
importer="texture"
type="StreamTexture"
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
path="res://.import/icon.png-f931f6b997c470ed41f337ac62349254.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://icon.png"
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
source_file="res://textures/icon.png"
dest_files=[ "res://.import/icon.png-f931f6b997c470ed41f337ac62349254.stex" ]
[params]