From 5d39ec2b21c4f2e44623b13b48169401a87fc0dd Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sat, 3 Dec 2022 21:58:28 +0100 Subject: [PATCH] Ported HexTile3D to C#. --- export_presets.cfg | 14 ++-- project.godot | 2 +- scenes/AdaptiveWorldStream.tscn | 4 +- scenes/HexTile3D.cs | 109 ++++++++++++++++++++++++++++++++ scenes/HexTile3D.tscn | 6 +- scenes/StreamContainer.cs | 13 ++-- 6 files changed, 127 insertions(+), 21 deletions(-) create mode 100644 scenes/HexTile3D.cs diff --git a/export_presets.cfg b/export_presets.cfg index e3ed1dd..db1cb56 100644 --- a/export_presets.cfg +++ b/export_presets.cfg @@ -1,13 +1,13 @@ [preset.0] -name="GodotComponentTest" +name="GodotComponentTestMono" platform="Android" runnable=true custom_features="" export_filter="all_resources" include_filter="" exclude_filter="" -export_path="./GodotComponentTest.apk" +export_path="./GodotComponentTestMono.apk" script_export_mode=1 script_encryption_key="" @@ -15,8 +15,10 @@ script_encryption_key="" custom_template/debug="" custom_template/release="" -custom_template/use_custom_build=false -custom_template/export_format=0 +custom_build/use_custom_build=false +custom_build/export_format=0 +custom_build/min_sdk="" +custom_build/target_sdk="" architectures/armeabi-v7a=true architectures/arm64-v8a=true architectures/x86=false @@ -30,8 +32,6 @@ keystore/release_password="" one_click_deploy/clear_previous_install=true version/code=1 version/name="1.0" -version/min_sdk=19 -version/target_sdk=30 package/unique_name="org.godotengine.$genname" package/name="" package/signed=true @@ -41,7 +41,6 @@ package/exclude_from_recents=false launcher_icons/main_192x192="" launcher_icons/adaptive_foreground_432x432="" launcher_icons/adaptive_background_432x432="" -graphics/32_bits_framebuffer=true graphics/opengl_debug=false xr_features/xr_mode=0 xr_features/hand_tracking=0 @@ -130,6 +129,7 @@ permissions/location_hardware=false permissions/manage_accounts=false permissions/manage_app_tokens=false permissions/manage_documents=false +permissions/manage_external_storage=false permissions/master_clear=false permissions/media_content_control=false permissions/modify_audio_settings=false diff --git a/project.godot b/project.godot index 32fa79f..c6a9cdb 100644 --- a/project.godot +++ b/project.godot @@ -75,7 +75,7 @@ _global_script_class_icons={ [application] config/name="i3sc1 GodotComponentTest" -run/main_scene="res://scenes/HexGrid3DTest.tscn" +run/main_scene="res://scenes/AdaptiveWorldStream.tscn" config/icon="res://icon.png" [display] diff --git a/scenes/AdaptiveWorldStream.tscn b/scenes/AdaptiveWorldStream.tscn index 944b742..dda4c69 100644 --- a/scenes/AdaptiveWorldStream.tscn +++ b/scenes/AdaptiveWorldStream.tscn @@ -190,7 +190,7 @@ text = "0" [node name="StreamContainer" type="Spatial" parent="."] script = ExtResource( 4 ) -Dimensions = Vector2( 40, 30 ) +Dimensions = Vector2( 18, 10 ) [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 ) mesh = SubResource( 8 ) -[connection signal="world_generated" from="World" to="." method="_on_World_world_generated"] - [editable path="OPlayer"] diff --git a/scenes/HexTile3D.cs b/scenes/HexTile3D.cs new file mode 100644 index 0000000..eea0071 --- /dev/null +++ b/scenes/HexTile3D.cs @@ -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("Mesh"); + _area = GetNode("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("res://materials/SandTile.tres"); + _grassMaterial = GD.Load("res://materials/GrassTile.tres"); + _deepGrassMaterial = GD.Load("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) +// { +// +// } +} + diff --git a/scenes/HexTile3D.tscn b/scenes/HexTile3D.tscn index 4cd6d62..3e6b7ce 100644 --- a/scenes/HexTile3D.tscn +++ b/scenes/HexTile3D.tscn @@ -1,6 +1,6 @@ [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] [sub_resource type="CylinderMesh" id=6] @@ -29,7 +29,3 @@ monitorable = false [node name="CollisionShape" type="CollisionShape" parent="Mesh/Area"] transform = Transform( 1, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0 ) 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"] diff --git a/scenes/StreamContainer.cs b/scenes/StreamContainer.cs index 741963d..017d62b 100644 --- a/scenes/StreamContainer.cs +++ b/scenes/StreamContainer.cs @@ -1,3 +1,4 @@ +using System; using Godot; using System.Collections.Generic; using System.Linq; @@ -21,8 +22,9 @@ public class StreamContainer : Spatial private Rect2 _currentOffsetCoordRect; private Rect2 _oldOffsetCoordRect; private HexGrid _hexGrid; + private Random _tileTypeRandom; - private Dictionary _coordToTile = new Dictionary(); + private Dictionary _coordToTile = new Dictionary(); public List RemovedCoords = new List(); public List AddedCoords = new List(); @@ -36,15 +38,13 @@ public class StreamContainer : Spatial { _bounds = GetNode("Bounds"); _activeTiles = GetNode("ActiveTiles"); + _tileTypeRandom = new Random(); _hexGrid = new HexGrid(); Transform boundsTransform = Transform.Identity; boundsTransform = boundsTransform.Scaled(new Vector3(Dimensions.x, 1, Dimensions.y)); _bounds.Transform = boundsTransform; - - Spatial hexTile3d = (Spatial)_hexTileScene.Instance(); - AddChild(hexTile3d); } @@ -110,6 +110,7 @@ public class StreamContainer : Spatial Spatial tile3d = _coordToTile[coord]; _activeTiles.RemoveChild(tile3d); tile3d.QueueFree(); + _coordToTile.Remove(coord); } } } @@ -121,7 +122,7 @@ public class StreamContainer : Spatial { if (!_coordToTile.Keys.Contains(offsetCoords)) { - Spatial tile3d = (Spatial)_hexTileScene.Instance(); + HexTile3D tile3d = (HexTile3D)_hexTileScene.Instance(); HexCell cell = new HexCell(); cell.OffsetCoords = offsetCoords; 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); tile3d.Transform = tile3dTransform; _activeTiles.AddChild(tile3d); + + tile3d.Type = HexTile3D.ValidTileTypes[_tileTypeRandom.Next(HexTile3D.ValidTileTypes.Length)]; _coordToTile[offsetCoords] = tile3d; }