grid #9
							
								
								
									
										9
									
								
								half-earth/materials/cursor.tres
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								half-earth/materials/cursor.tres
									
									
									
									
									
										Normal 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
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										10
									
								
								half-earth/nodes/grid_cursor.tscn
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								half-earth/nodes/grid_cursor.tscn
									
									
									
									
									
										Normal 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 )
 | 
			
		||||
							
								
								
									
										51
									
								
								half-earth/scripts/GridCursor.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								half-earth/scripts/GridCursor.cs
									
									
									
									
									
										Normal 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;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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);
 | 
			
		||||
 | 
			
		||||
@ -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));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user