grid #9

Merged
ktyl merged 4 commits from grid into main 2022-12-07 01:06:04 +01:00
6 changed files with 92 additions and 11 deletions
Showing only changes of commit 4ddb3cebf6 - Show all commits

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

@ -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

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,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

@ -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<Node2D>();
this.AddChild(node);

View File

@ -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));
}