Ported HexTile3D to C#.

WorldChunkRefactoring
Martin Felis 2022-12-03 21:58:28 +01:00
parent 38b5b7f5fb
commit 5d39ec2b21
6 changed files with 127 additions and 21 deletions

View File

@ -1,13 +1,13 @@
[preset.0] [preset.0]
name="GodotComponentTest" name="GodotComponentTestMono"
platform="Android" platform="Android"
runnable=true runnable=true
custom_features="" custom_features=""
export_filter="all_resources" export_filter="all_resources"
include_filter="" include_filter=""
exclude_filter="" exclude_filter=""
export_path="./GodotComponentTest.apk" export_path="./GodotComponentTestMono.apk"
script_export_mode=1 script_export_mode=1
script_encryption_key="" script_encryption_key=""
@ -15,8 +15,10 @@ script_encryption_key=""
custom_template/debug="" custom_template/debug=""
custom_template/release="" custom_template/release=""
custom_template/use_custom_build=false custom_build/use_custom_build=false
custom_template/export_format=0 custom_build/export_format=0
custom_build/min_sdk=""
custom_build/target_sdk=""
architectures/armeabi-v7a=true architectures/armeabi-v7a=true
architectures/arm64-v8a=true architectures/arm64-v8a=true
architectures/x86=false architectures/x86=false
@ -30,8 +32,6 @@ keystore/release_password=""
one_click_deploy/clear_previous_install=true one_click_deploy/clear_previous_install=true
version/code=1 version/code=1
version/name="1.0" version/name="1.0"
version/min_sdk=19
version/target_sdk=30
package/unique_name="org.godotengine.$genname" package/unique_name="org.godotengine.$genname"
package/name="" package/name=""
package/signed=true package/signed=true
@ -41,7 +41,6 @@ package/exclude_from_recents=false
launcher_icons/main_192x192="" launcher_icons/main_192x192=""
launcher_icons/adaptive_foreground_432x432="" launcher_icons/adaptive_foreground_432x432=""
launcher_icons/adaptive_background_432x432="" launcher_icons/adaptive_background_432x432=""
graphics/32_bits_framebuffer=true
graphics/opengl_debug=false graphics/opengl_debug=false
xr_features/xr_mode=0 xr_features/xr_mode=0
xr_features/hand_tracking=0 xr_features/hand_tracking=0
@ -130,6 +129,7 @@ permissions/location_hardware=false
permissions/manage_accounts=false permissions/manage_accounts=false
permissions/manage_app_tokens=false permissions/manage_app_tokens=false
permissions/manage_documents=false permissions/manage_documents=false
permissions/manage_external_storage=false
permissions/master_clear=false permissions/master_clear=false
permissions/media_content_control=false permissions/media_content_control=false
permissions/modify_audio_settings=false permissions/modify_audio_settings=false

View File

@ -75,7 +75,7 @@ _global_script_class_icons={
[application] [application]
config/name="i3sc1 GodotComponentTest" config/name="i3sc1 GodotComponentTest"
run/main_scene="res://scenes/HexGrid3DTest.tscn" run/main_scene="res://scenes/AdaptiveWorldStream.tscn"
config/icon="res://icon.png" config/icon="res://icon.png"
[display] [display]

View File

@ -190,7 +190,7 @@ text = "0"
[node name="StreamContainer" type="Spatial" parent="."] [node name="StreamContainer" type="Spatial" parent="."]
script = ExtResource( 4 ) script = ExtResource( 4 )
Dimensions = Vector2( 40, 30 ) Dimensions = Vector2( 18, 10 )
[node name="ActiveTiles" type="Spatial" parent="StreamContainer"] [node name="ActiveTiles" type="Spatial" parent="StreamContainer"]
@ -224,6 +224,4 @@ script = ExtResource( 6 )
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 ) transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 )
mesh = SubResource( 8 ) mesh = SubResource( 8 )
[connection signal="world_generated" from="World" to="." method="_on_World_world_generated"]
[editable path="OPlayer"] [editable path="OPlayer"]

109
scenes/HexTile3D.cs Normal file
View File

@ -0,0 +1,109 @@
using Godot;
public class HexTile3D : Spatial
{
public enum TileType
{
Sand,
Grass,
DeepGrass
}
static public TileType[] ValidTileTypes = { TileType.Sand, TileType.Grass, TileType.DeepGrass };
// scene nodes
private MeshInstance _mesh;
private Area _area;
// signals
[Signal]
delegate void TileSelected(HexTile3D tile3d);
// other member variables
private SpatialMaterial _sandMaterial;
private SpatialMaterial _grassMaterial;
private SpatialMaterial _deepGrassMaterial;
private SpatialMaterial _previousMaterial;
public HexCell Cell = new HexCell();
public bool IsMouseOver = false;
private TileType _type;
public TileType Type
{
get
{
return _type;
}
set
{
_type = value;
switch (_type)
{
case TileType.Sand: _mesh.SetSurfaceMaterial(0, _sandMaterial);
break;
case TileType.Grass: _mesh.SetSurfaceMaterial(0, _grassMaterial);
break;
case TileType.DeepGrass: _mesh.SetSurfaceMaterial(0, _deepGrassMaterial);
break;
default:
GD.Print("Invalid tile type: " + value.ToString());
break;
}
}
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_mesh = GetNode<MeshInstance>("Mesh");
_area = GetNode<Area>("Mesh/Area");
_area.Connect("input_event", this, nameof(OnAreaInputEvent));
_area.Connect("mouse_entered", this, nameof(OnAreaMouseEntered));
_area.Connect("mouse_exited", this, nameof(OnAreaMouseExited));
_sandMaterial = GD.Load<SpatialMaterial>("res://materials/SandTile.tres");
_grassMaterial = GD.Load<SpatialMaterial>("res://materials/GrassTile.tres");
_deepGrassMaterial = GD.Load<SpatialMaterial>("res://materials/DeepGrassTile.tres");
this.Type = TileType.Grass;
}
public void OnAreaInputEvent(Node camera, InputEvent inputEvent, Vector3 position, Vector3 normal, int shapeIndex)
{
if (IsMouseOver && inputEvent is InputEventMouseButton)
{
InputEventMouseButton mouseButtonEvent = (InputEventMouseButton)inputEvent;
if (mouseButtonEvent.ButtonIndex == 1 && mouseButtonEvent.Pressed)
{
EmitSignal("tile_selected", this);
}
}
}
public void OnAreaMouseEntered()
{
IsMouseOver = true;
_previousMaterial = (SpatialMaterial)_mesh.MaterialOverride;
SpatialMaterial clonedMaterial = (SpatialMaterial)_mesh.GetSurfaceMaterial(0).Duplicate();
clonedMaterial.AlbedoColor = new Color(1, 0, 0);
_mesh.MaterialOverride = clonedMaterial;
}
public void OnAreaMouseExited()
{
IsMouseOver = false;
_mesh.MaterialOverride = _previousMaterial;
}
// // Called every frame. 'delta' is the elapsed time since the previous frame.
// public override void _Process(float delta)
// {
//
// }
}

View File

@ -1,6 +1,6 @@
[gd_scene load_steps=5 format=2] [gd_scene load_steps=5 format=2]
[ext_resource path="res://scenes/HexTile3D.gd" type="Script" id=1] [ext_resource path="res://scenes/HexTile3D.cs" type="Script" id=1]
[ext_resource path="res://materials/DeepGrassTile.tres" type="Material" id=2] [ext_resource path="res://materials/DeepGrassTile.tres" type="Material" id=2]
[sub_resource type="CylinderMesh" id=6] [sub_resource type="CylinderMesh" id=6]
@ -29,7 +29,3 @@ monitorable = false
[node name="CollisionShape" type="CollisionShape" parent="Mesh/Area"] [node name="CollisionShape" type="CollisionShape" parent="Mesh/Area"]
transform = Transform( 1, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0 ) transform = Transform( 1, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0 )
shape = SubResource( 5 ) shape = SubResource( 5 )
[connection signal="input_event" from="Mesh/Area" to="." method="_on_Area_input_event"]
[connection signal="mouse_entered" from="Mesh/Area" to="." method="_on_Area_mouse_entered"]
[connection signal="mouse_exited" from="Mesh/Area" to="." method="_on_Area_mouse_exited"]

View File

@ -1,3 +1,4 @@
using System;
using Godot; using Godot;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -21,8 +22,9 @@ public class StreamContainer : Spatial
private Rect2 _currentOffsetCoordRect; private Rect2 _currentOffsetCoordRect;
private Rect2 _oldOffsetCoordRect; private Rect2 _oldOffsetCoordRect;
private HexGrid _hexGrid; private HexGrid _hexGrid;
private Random _tileTypeRandom;
private Dictionary<Vector2, Spatial> _coordToTile = new Dictionary<Vector2, Spatial>(); private Dictionary<Vector2, HexTile3D> _coordToTile = new Dictionary<Vector2, HexTile3D>();
public List<Vector2> RemovedCoords = new List<Vector2>(); public List<Vector2> RemovedCoords = new List<Vector2>();
public List<Vector2> AddedCoords = new List<Vector2>(); public List<Vector2> AddedCoords = new List<Vector2>();
@ -36,15 +38,13 @@ public class StreamContainer : Spatial
{ {
_bounds = GetNode<MeshInstance>("Bounds"); _bounds = GetNode<MeshInstance>("Bounds");
_activeTiles = GetNode<Spatial>("ActiveTiles"); _activeTiles = GetNode<Spatial>("ActiveTiles");
_tileTypeRandom = new Random();
_hexGrid = new HexGrid(); _hexGrid = new HexGrid();
Transform boundsTransform = Transform.Identity; Transform boundsTransform = Transform.Identity;
boundsTransform = boundsTransform.Scaled(new Vector3(Dimensions.x, 1, Dimensions.y)); boundsTransform = boundsTransform.Scaled(new Vector3(Dimensions.x, 1, Dimensions.y));
_bounds.Transform = boundsTransform; _bounds.Transform = boundsTransform;
Spatial hexTile3d = (Spatial)_hexTileScene.Instance();
AddChild(hexTile3d);
} }
@ -110,6 +110,7 @@ public class StreamContainer : Spatial
Spatial tile3d = _coordToTile[coord]; Spatial tile3d = _coordToTile[coord];
_activeTiles.RemoveChild(tile3d); _activeTiles.RemoveChild(tile3d);
tile3d.QueueFree(); tile3d.QueueFree();
_coordToTile.Remove(coord);
} }
} }
} }
@ -121,7 +122,7 @@ public class StreamContainer : Spatial
{ {
if (!_coordToTile.Keys.Contains(offsetCoords)) if (!_coordToTile.Keys.Contains(offsetCoords))
{ {
Spatial tile3d = (Spatial)_hexTileScene.Instance(); HexTile3D tile3d = (HexTile3D)_hexTileScene.Instance();
HexCell cell = new HexCell(); HexCell cell = new HexCell();
cell.OffsetCoords = offsetCoords; cell.OffsetCoords = offsetCoords;
Vector2 cellPlaneCoords = _hexGrid.GetHexCenter(cell); Vector2 cellPlaneCoords = _hexGrid.GetHexCenter(cell);
@ -129,6 +130,8 @@ public class StreamContainer : Spatial
tile3dTransform.origin = new Vector3(cellPlaneCoords.x, GD.Randf() * 0.1f, cellPlaneCoords.y); tile3dTransform.origin = new Vector3(cellPlaneCoords.x, GD.Randf() * 0.1f, cellPlaneCoords.y);
tile3d.Transform = tile3dTransform; tile3d.Transform = tile3dTransform;
_activeTiles.AddChild(tile3d); _activeTiles.AddChild(tile3d);
tile3d.Type = HexTile3D.ValidTileTypes[_tileTypeRandom.Next(HexTile3D.ValidTileTypes.Length)];
_coordToTile[offsetCoords] = tile3d; _coordToTile[offsetCoords] = tile3d;
} }