From 4ddb3cebf62da63b81e6e146af8f1d0838e4a271 Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Tue, 6 Dec 2022 01:28:25 +0000 Subject: [PATCH] toggle tiles with cursor #3 --- half-earth/materials/cursor.tres | 9 +++++ half-earth/materials/tile.tres | 4 +- half-earth/nodes/grid_cursor.tscn | 10 +++++ half-earth/scripts/GridCursor.cs | 51 +++++++++++++++++++++++++ half-earth/scripts/WorldGrid.cs | 23 +++++++---- half-earth/shaders/tile_shader.gdshader | 6 ++- 6 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 half-earth/materials/cursor.tres create mode 100644 half-earth/nodes/grid_cursor.tscn create mode 100644 half-earth/scripts/GridCursor.cs diff --git a/half-earth/materials/cursor.tres b/half-earth/materials/cursor.tres new file mode 100644 index 0000000..a3218a5 --- /dev/null +++ b/half-earth/materials/cursor.tres @@ -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 diff --git a/half-earth/materials/tile.tres b/half-earth/materials/tile.tres index 4bc4d4a..327c5eb 100644 --- a/half-earth/materials/tile.tres +++ b/half-earth/materials/tile.tres @@ -5,4 +5,6 @@ [resource] resource_local_to_scene = true shader = ExtResource( 1 ) -shader_param/c = null +shader_param/baseColor = Color( 0.0823529, 0, 1, 1 ) +shader_param/highlightColor = Color( 1, 0, 0, 1 ) +shader_param/isHighlighted = null diff --git a/half-earth/nodes/grid_cursor.tscn b/half-earth/nodes/grid_cursor.tscn new file mode 100644 index 0000000..668f1a2 --- /dev/null +++ b/half-earth/nodes/grid_cursor.tscn @@ -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 ) diff --git a/half-earth/scripts/GridCursor.cs b/half-earth/scripts/GridCursor.cs new file mode 100644 index 0000000..fd97c42 --- /dev/null +++ b/half-earth/scripts/GridCursor.cs @@ -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(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; + } + } + } +} diff --git a/half-earth/scripts/WorldGrid.cs b/half-earth/scripts/WorldGrid.cs index e03923b..5a06ef9 100644 --- a/half-earth/scripts/WorldGrid.cs +++ b/half-earth/scripts/WorldGrid.cs @@ -14,7 +14,7 @@ public class WorldGrid : Node2D private struct Tile { - public float data; + public bool isHighlighted; public ShaderMaterial material; } private Tile[,] _tiles; @@ -35,18 +35,25 @@ public class WorldGrid : Node2D { var tile = _tiles[x, y]; var material = tile.material; - material.SetShaderParam("c", tile.data); + 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) + { + _tiles[x, y].isHighlighted ^= true; + } + public void GetGridPos(Vector2 position, out int x, out int y) { - int GetCoord(float f) => - Mathf.Clamp(Mathf.FloorToInt(f / CellSize), 0, CellSize -1); - - x = GetCoord(position.x); - y = GetCoord(position.y); + x = Mathf.FloorToInt(position.x / CellSize); + y = Mathf.FloorToInt(position.y / CellSize); } private void Clear() @@ -73,7 +80,7 @@ public class WorldGrid : Node2D for (int y = 0; y < size; y++) { var tile = new Tile(); - tile.data = (float)(y * size + x) / 100f; + tile.isHighlighted = false; var node = TileScene.Instance(); this.AddChild(node); diff --git a/half-earth/shaders/tile_shader.gdshader b/half-earth/shaders/tile_shader.gdshader index e948f41..98f8fe0 100644 --- a/half-earth/shaders/tile_shader.gdshader +++ b/half-earth/shaders/tile_shader.gdshader @@ -1,7 +1,9 @@ shader_type canvas_item; -uniform float c; +uniform vec4 baseColor : hint_color; +uniform vec4 highlightColor : hint_color; +uniform int isHighlighted; void fragment() { - COLOR = vec4(c, 0.0, 0.0, 1.0); + COLOR = mix(baseColor, highlightColor, float(isHighlighted)); }