Improved highlighting on water

WorldChunkRefactoring
Martin Felis 2022-10-09 19:36:08 +02:00
parent c4770b395a
commit fd2ef11b05
2 changed files with 49 additions and 39 deletions

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=13 format=2]
[gd_scene load_steps=15 format=2]
[ext_resource path="res://entities/PlayerEntity.gd" type="Script" id=1]
[ext_resource path="res://scenes/HexGrid3DTest.gd" type="Script" id=2]
@ -16,7 +16,18 @@ radial_segments = 16
radius = 0.25
height = 0.5
[sub_resource type="HeightMapShape" id=6]
[sub_resource type="CylinderMesh" id=7]
top_radius = 0.5
bottom_radius = 0.5
height = 0.1
radial_segments = 6
rings = 1
[sub_resource type="SpatialMaterial" id=8]
render_priority = 1
flags_transparent = true
albedo_color = Color( 1, 1, 1, 0.407843 )
roughness = 0.0
[sub_resource type="PlaneMesh" id=1]
subdivide_width = 24
@ -110,6 +121,8 @@ shader_param/uv_offset_amplitude = 1.2
shader_param/texture_scale = Vector2( 8, 8 )
shader_param/texturemap = ExtResource( 5 )
[sub_resource type="HeightMapShape" id=6]
[node name="HexGrid3DTest" type="Spatial"]
script = ExtResource( 2 )
@ -189,32 +202,25 @@ generate_flag = true
[node name="Tiles" type="Spatial" parent="World"]
[node name="Highlight" type="MeshInstance" parent="World"]
transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0, 0.2, 0 )
cast_shadow = 0
mesh = SubResource( 7 )
material/0 = SubResource( 8 )
[node name="Water" type="MeshInstance" parent="World"]
transform = Transform( 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, -0.1, 0 )
cast_shadow = 0
mesh = SubResource( 1 )
skeleton = NodePath("../..")
material/0 = SubResource( 3 )
[node name="ClckArea" type="Area" parent="World"]
[node name="CollisionShape" type="CollisionShape" parent="World/ClckArea"]
transform = Transform( 50, 0, 0, 0, 1, 0, 0, 0, 50, 0, 0, 0 )
shape = SubResource( 6 )
[node name="Water" type="Spatial" parent="."]
[node name="Water" type="MeshInstance" parent="Water"]
transform = Transform( 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, -0.1, 0 )
mesh = SubResource( 1 )
skeleton = NodePath("../..")
material/0 = SubResource( 3 )
[node name="Water2" type="MeshInstance" parent="Water"]
transform = Transform( -10, 0, 0, 0, 10, 0, 0, 0, 10, -20, -0.1, -0.01 )
mesh = SubResource( 1 )
skeleton = NodePath("../..")
material/0 = SubResource( 3 )
[node name="Water3" type="MeshInstance" parent="Water"]
transform = Transform( -10, 0, 0, 0, 10, 0, 0, 0, 10, 20, -0.1, -0.01 )
mesh = SubResource( 1 )
skeleton = NodePath("../..")
material/0 = SubResource( 3 )
[connection signal="input_event" from="World/ClckArea" to="World" method="_on_ClckArea_input_event"]
[connection signal="mouse_entered" from="World/ClckArea" to="World" method="_on_ClckArea_mouse_entered"]
[connection signal="mouse_exited" from="World/ClckArea" to="World" method="_on_ClckArea_mouse_exited"]

View File

@ -4,6 +4,7 @@ var GameTile = load("res://utils/GameTile.gd")
var HexCell = preload("res://addons/gdhexgrid/HexCell.gd")
onready var HexGrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var IslandGenerator = preload("res://utils/IslandGenerator.gd").new()
onready var highlight = $Highlight
onready var tiles = $Tiles
var has_tiles_initialized = false
@ -11,7 +12,7 @@ var tile_map = {}
var active_tile = null
var island_count = 5
export var size = [50, 50]
export var size = Vector2(20, 20)
export var generate_flag: bool = false setget do_generate
func _ready():
@ -57,8 +58,8 @@ func generate():
for i in range (island_count):
generate_island(Vector2(
rand_range(-size[0] / 2, size[0] / 2),
rand_range(-size[1] / 2, size[1] / 2)
rand_range(-size.x / 2, size.x / 2),
rand_range(-size.y / 2, size.y / 2)
))
mark_sand_tiles()
@ -71,8 +72,8 @@ func generate():
func generate_base_tiles():
print ("generating world")
for i in range (-size[0] / 2, size[0] / 2):
for j in range (-size[1] / 2, size[1] / 2):
for i in range (-size.x / 2, size.x / 2):
for j in range (-size.y / 2, size.y / 2):
var tile = GameTile.new()
tile.type = GameTile.TileType.Water
tile.offset_coords = Vector2(i,j)
@ -116,8 +117,8 @@ func mark_sand_tiles():
func smooth_water_tiles():
# horizontal smoothing
for i in range ((-size[0] / 2) + 1, (size[0] / 2) - 1):
for j in range ((-size[1] / 2) + 1, (size[1] / 2) - 1):
for i in range ((-size.x / 2) + 1, (size.x / 2) - 1):
for j in range ((-size.y / 2) + 1, (size.y / 2) - 1):
var prev = tile_map[Vector2(i - 1,j)]
var current = tile_map[Vector2(i,j)]
var next = tile_map[Vector2(i + 1,j)]
@ -125,11 +126,11 @@ func smooth_water_tiles():
if current.game_tile.type != GameTile.TileType.Water:
continue
current.transform.origin[1] = (prev.transform.origin[1] + next.transform.origin[1]) * 0.5
current.transform.origin[1] = min(-0.2, (prev.transform.origin[1] + next.transform.origin[1]) * 0.5)
# vertical smoothings
for i in range ((-size[0] / 2) + 1, (size[0] / 2) - 1):
for j in range ((-size[1] / 2) + 1, (size[1] / 2) - 1):
for i in range ((-size.x / 2) + 1, (size.x / 2) - 1):
for j in range ((-size.y / 2) + 1, (size.y / 2) - 1):
var prev = tile_map[Vector2(i,j - 1)]
var current = tile_map[Vector2(i,j)]
var next = tile_map[Vector2(i,j + 1)]
@ -137,7 +138,7 @@ func smooth_water_tiles():
if current.game_tile.type != GameTile.TileType.Water:
continue
current.transform.origin[1] = (prev.transform.origin[1] + next.transform.origin[1]) * 0.5
current.transform.origin[1] = min(-0.2, (prev.transform.origin[1] + next.transform.origin[1]) * 0.5)
func do_generate(flag: bool):
@ -147,14 +148,17 @@ func do_generate(flag: bool):
func _on_ClckArea_input_event(_camera, _event, position, _normal, _shape_idx):
var offset_coords = HexGrid.get_hex_at(position).offset_coords
var hex_at_position = HexGrid.get_hex_at(position)
var offset_coords = hex_at_position.offset_coords
var hex_center = HexGrid.get_hex_center_from_offset(offset_coords)
highlight.transform.origin = Vector3 (hex_center.x, 0.0, hex_center.y)
var world_tile = null
if offset_coords in tile_map.keys():
world_tile = tile_map[offset_coords]
if active_tile != null and world_tile != active_tile:
active_tile._on_Area_mouse_exited()
pass
if active_tile == world_tile:
return
@ -163,12 +167,12 @@ func _on_ClckArea_input_event(_camera, _event, position, _normal, _shape_idx):
return
active_tile = world_tile
active_tile._on_Area_mouse_entered()
func _on_ClckArea_mouse_entered():
pass # Replace with function body.
highlight.visible = true
func _on_ClckArea_mouse_exited():
pass # Replace with function body.
highlight.visible = false