grid #9
							
								
								
									
										6
									
								
								half-earth/Half-earth.csproj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								half-earth/Half-earth.csproj
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										19
									
								
								half-earth/Half-earth.sln
									
									
									
									
									
										Normal 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
 | 
			
		||||
							
								
								
									
										8
									
								
								half-earth/materials/tile.tres
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								half-earth/materials/tile.tres
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,8 @@
 | 
			
		||||
[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/c = null
 | 
			
		||||
							
								
								
									
										11
									
								
								half-earth/nodes/game.tscn
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								half-earth/nodes/game.tscn
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
			
		||||
[gd_scene load_steps=3 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]
 | 
			
		||||
 | 
			
		||||
[node name="Root" type="Node2D"]
 | 
			
		||||
 | 
			
		||||
[node name="Grid" parent="." instance=ExtResource( 1 )]
 | 
			
		||||
 | 
			
		||||
[node name="Cursor" parent="." instance=ExtResource( 2 )]
 | 
			
		||||
Grid = NodePath("../Grid")
 | 
			
		||||
							
								
								
									
										10
									
								
								half-earth/nodes/grid.tscn
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								half-earth/nodes/grid.tscn
									
									
									
									
									
										Normal 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
 | 
			
		||||
							
								
								
									
										8
									
								
								half-earth/nodes/tile.tscn
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								half-earth/nodes/tile.tscn
									
									
									
									
									
										Normal 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 )
 | 
			
		||||
							
								
								
									
										93
									
								
								half-earth/scripts/WorldGrid.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								half-earth/scripts/WorldGrid.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,93 @@
 | 
			
		||||
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 struct Tile
 | 
			
		||||
    {
 | 
			
		||||
        public float data;
 | 
			
		||||
        public ShaderMaterial material;
 | 
			
		||||
    }
 | 
			
		||||
    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("c", tile.data);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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.data = (float)(y * size + x) / 100f;
 | 
			
		||||
                
 | 
			
		||||
                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;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										7
									
								
								half-earth/shaders/tile_shader.gdshader
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								half-earth/shaders/tile_shader.gdshader
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
			
		||||
shader_type canvas_item;
 | 
			
		||||
 | 
			
		||||
uniform float c;
 | 
			
		||||
 | 
			
		||||
void fragment() {
 | 
			
		||||
	COLOR = vec4(c, 0.0, 0.0, 1.0);
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user