Separating features into separate scripts

This commit is contained in:
hz
2025-11-07 15:04:29 -06:00
parent 3da1f8a864
commit b997dae2af
2 changed files with 52 additions and 232 deletions
+24 -11
View File
@@ -1,15 +1,18 @@
[gd_scene load_steps=9 format=4 uid="uid://qd6xvgwrd00g"]
[gd_scene load_steps=12 format=4 uid="uid://qd6xvgwrd00g"]
[ext_resource type="Script" uid="uid://3goclr4srt7k" path="res://scenes/Grid/grid_plane_manager.gd" id="1_72cxc"]
[ext_resource type="Script" uid="uid://5p4mcxw7wann" path="res://scenes/Grid/grid_camera_controller.gd" id="2_o2iv9"]
[ext_resource type="Script" uid="uid://ehef5rrel4d6" path="res://scenes/Grid/grid_highlight_controller.gd" id="3_n2b7o"]
[ext_resource type="Script" uid="uid://oy8cl8dscrh2" path="res://scenes/Grid/day_night_controller.gd" id="4_u8bvi"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ground"]
albedo_color = Color(0.211765, 0.364706, 0.211765, 1)
[sub_resource type="PlaneMesh" id="PlaneMesh_ground"]
size = Vector2(10, 10)
size = Vector2(146.06345, 116.8072)
[sub_resource type="BoxShape3D" id="BoxShape3D_ground"]
size = Vector3(10, 0.4, 10)
size = Vector3(146.06345, 0.4, 116.8072)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_grid"]
no_depth_test = true
@@ -17,7 +20,7 @@ shading_mode = 0
vertex_color_use_as_albedo = true
albedo_color = Color(0.811765, 0.917647, 1, 0.74902)
[sub_resource type="ArrayMesh" id="ArrayMesh_q3k88"]
[sub_resource type="ArrayMesh" id="ArrayMesh_pxg5e"]
_surfaces = [{
"aabb": AABB(-5, 0.02, -5, 10, 1e-05, 10),
"attribute_data": PackedByteArray("M7L/pTOy/6Uzsv+lM7L/pTOy/6Uzsv+lM7L/pTOy/6Uzsv+lM7L/pTOy/6Uzsv+lM7L/pTOy/6Uzsv+lM7L/pTOy/6Uzsv+lM7L/pTOy/6Uzsv+lM7L/pTOy/6Uzsv+lM7L/pTOy/6Uzsv+lM7L/pTOy/6Uzsv+lM7L/pTOy/6Uzsv+lM7L/pTOy/6Uzsv+lM7L/pTOy/6Uzsv+lM7L/pTOy/6Uzsv+lM7L/pTOy/6U="),
@@ -34,7 +37,7 @@ no_depth_test = true
shading_mode = 0
albedo_color = Color(0.976471, 0.843137, 0.333333, 0.6)
[sub_resource type="PlaneMesh" id="PlaneMesh_8dkrf"]
[sub_resource type="PlaneMesh" id="PlaneMesh_dce6m"]
size = Vector2(1, 1)
[node name="GridField" type="Node3D"]
@@ -43,7 +46,7 @@ columns = 10
rows = 10
cell_size = 1.0
ground_thickness = 0.4
ground_padding = null
ground_padding = 20.0
show_grid_lines = true
grid_line_color = Color(0.2, 0.7, 1, 0.65)
@@ -63,22 +66,32 @@ shape = SubResource("BoxShape3D_ground")
unique_name_in_owner = true
material_override = SubResource("StandardMaterial3D_grid")
cast_shadow = 0
mesh = SubResource("ArrayMesh_q3k88")
mesh = SubResource("ArrayMesh_pxg5e")
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
unique_name_in_owner = true
transform = Transform3D(0.707107, -0.5, 0.5, 0, 0.707107, 0.707107, -0.707107, -0.5, 0.5, 0, 5, 0)
shadow_enabled = true
transform = Transform3D(-4.37114e-08, -1.0000001, -4.3711392e-08, 0, -4.3711392e-08, 1.0000001, -1.0000002, 4.3711392e-08, 1.9106857e-15, 5.165678e-06, 60, 9.1084837e-07)
light_color = Color(0.12, 0.18, 0.35, 1)
light_energy = 0.05
script = ExtResource("4_u8bvi")
grid_manager_path = NodePath("..")
[node name="Camera3D" type="Camera3D" parent="."]
unique_name_in_owner = true
transform = Transform3D(0.70710677, -0.40824828, 0.57735026, 0, 0.8164966, 0.57735026, -0.70710677, -0.40824828, 0.57735026, 6, 6, 6)
transform = Transform3D(0.7071067, -0.40824828, 0.5773502, 0, 0.8164966, 0.5773502, -0.7071067, -0.40824828, 0.5773502, 6, 6, 6)
current = true
fov = 60.0
script = ExtResource("2_o2iv9")
grid_manager_path = NodePath("..")
ground_mesh_path = NodePath("../GroundMesh")
ground_collision_path = NodePath("../GroundBody/GroundCollision")
[node name="HoverHighlight" type="MeshInstance3D" parent="."]
unique_name_in_owner = true
visible = false
material_override = SubResource("StandardMaterial3D_highlight")
cast_shadow = 0
mesh = SubResource("PlaneMesh_8dkrf")
mesh = SubResource("PlaneMesh_dce6m")
script = ExtResource("3_n2b7o")
grid_manager_path = NodePath("..")
camera_path = NodePath("../Camera3D")
+28 -221
View File
@@ -67,63 +67,10 @@ var _ground_padding: float = 20.0
if is_equal_approx(value, _ground_padding):
return
_ground_padding = value
_update_ground_assets()
emit_signal("grid_layout_changed", _cells)
get:
return _ground_padding
var _day_length_hours: float = 12.0
@export_range(1.0, 48.0, 0.5) var day_length_hours: float = 12.0:
set(value):
value = max(value, 1.0)
if is_equal_approx(value, _day_length_hours):
return
_day_length_hours = value
_time_of_day_hours = clamp(_time_of_day_hours, 0.0, _day_length_hours)
_apply_sun_lighting()
get:
return _day_length_hours
var _seconds_per_game_hour: float = 60.0
@export_range(1.0, 600.0, 1.0) var real_seconds_per_game_hour: float = 60.0:
set(value):
value = clamp(value, 1.0, 600.0)
if is_equal_approx(value, _seconds_per_game_hour):
return
_seconds_per_game_hour = value
get:
return _seconds_per_game_hour
@export_range(0.0, 48.0, 0.25) var start_time_of_day_hours: float = 6.0
var _time_of_day_hours: float = 0.0
var _zoom_factor: float = 1.0
var _zoom_min_internal: float = 0.5
var _zoom_max_internal: float = 3.0
var _zoom_step_internal: float = 0.1
@export_range(0.1, 10.0, 0.1) var zoom_min: float = 0.5:
set(value):
_zoom_min_internal = clamp(value, 0.1, min(_zoom_max_internal, 10.0))
if _zoom_factor < _zoom_min_internal:
_zoom_factor = _zoom_min_internal
_update_camera_view()
_update_ground_assets()
get:
return _zoom_min_internal
@export_range(0.1, 10.0, 0.1) var zoom_max: float = 3.0:
set(value):
_zoom_max_internal = max(value, _zoom_min_internal)
if _zoom_factor > _zoom_max_internal:
_zoom_factor = _zoom_max_internal
_update_camera_view()
_update_ground_assets()
get:
return _zoom_max_internal
@export_range(0.01, 1.0, 0.01) var zoom_step: float = 0.1:
set(value):
_zoom_step_internal = clamp(value, 0.01, 1.0)
get:
return _zoom_step_internal
var _show_grid_lines: bool = true
@export var show_grid_lines: bool:
set(value):
@@ -148,31 +95,31 @@ var _grid_line_color: Color = Color(0.2, 0.7, 1.0, 0.65)
const _LINE_HEIGHT_OFFSET := 0.02
const _SQRT3 := 1.7320508075688772
@onready var _ground_mesh: MeshInstance3D = %GroundMesh
@onready var _ground_collision: CollisionShape3D = %GroundCollision
@onready var _grid_lines: MeshInstance3D = %GridLines
@onready var _camera: Camera3D = %Camera3D
@onready var _hover_highlight: MeshInstance3D = %HoverHighlight
@onready var _sun_light: DirectionalLight3D = %DirectionalLight3D
var _grid_size: Vector2 = Vector2.ZERO
var _cells: Dictionary = {}
var _queued_rebuild: bool = false
var _hovered_cell: Vector2i = Vector2i(-1, -1)
var _hex_center_offset_local: Vector3 = Vector3.ZERO
func _ready() -> void:
set_process(true)
_zoom_factor = clamp(_zoom_factor, _zoom_min_internal, _zoom_max_internal)
_time_of_day_hours = clamp(start_time_of_day_hours, 0.0, _day_length_hours)
_queue_rebuild()
func get_cells() -> Dictionary:
return _cells.duplicate()
func get_grid_size() -> Vector2:
return _grid_size
func get_grid_type() -> GridType:
return _grid_type
func get_cell_center(index: Vector2i) -> Vector3:
return _cells.get(index, Vector3.ZERO)
func has_cell(index: Vector2i) -> bool:
return _cells.has(index)
func cell_to_world(index: Vector2i) -> Vector3:
return get_cell_center(index)
@@ -186,6 +133,22 @@ func for_each_cell(callback: Callable) -> void:
for index in _cells.keys():
callback.call(index, _cells[index])
func get_cell_size_value() -> float:
return _cell_size
func get_ground_thickness() -> float:
return _ground_thickness
func get_ground_padding() -> float:
return _ground_padding
func make_highlight_mesh() -> Mesh:
if _grid_type == GridType.HEX:
return _build_hex_highlight_mesh()
var plane := PlaneMesh.new()
plane.size = Vector2(_cell_size, _cell_size)
return plane
func _queue_rebuild() -> void:
if _queued_rebuild:
return
@@ -205,23 +168,8 @@ func _rebuild_grid() -> void:
_:
_generate_square_grid()
_update_grid_lines()
_update_highlight_mesh()
_update_camera_view()
_update_ground_assets()
_apply_sun_lighting()
emit_signal("grid_layout_changed", _cells)
func _update_ground_assets() -> void:
if not is_node_ready():
return
var required_size := _compute_required_ground_size()
if _ground_mesh and _ground_mesh.mesh is PlaneMesh:
var plane_mesh := _ground_mesh.mesh as PlaneMesh
plane_mesh.size = required_size
if _ground_collision and _ground_collision.shape is BoxShape3D:
var box := _ground_collision.shape as BoxShape3D
box.size = Vector3(required_size.x, _ground_thickness, required_size.y)
func _generate_square_grid() -> void:
_hex_center_offset_local = Vector3.ZERO
_cells.clear()
@@ -338,14 +286,8 @@ func _compute_hex_corners(center: Vector3, radius: float) -> Array[Vector3]:
return corners
func _update_highlight_mesh() -> void:
if not _hover_highlight:
return
if _grid_type == GridType.HEX:
_hover_highlight.mesh = _build_hex_highlight_mesh()
else:
var plane := PlaneMesh.new()
plane.size = Vector2(_cell_size, _cell_size)
_hover_highlight.mesh = plane
# Deprecated internally; kept for compatibility.
pass
func _build_hex_highlight_mesh() -> ArrayMesh:
var st := SurfaceTool.new()
@@ -411,138 +353,3 @@ func _cube_round(cube: Vector3) -> Vector3:
else:
rz = -rx - ry
return Vector3(rx, ry, rz)
func _update_camera_view() -> void:
if not _camera:
return
var extent: float = max(_grid_size.x, _grid_size.y)
var half_extent: float = extent * 0.5
var distance: float = max(half_extent * 1.1, 6.0) * _zoom_factor
var height: float = max(half_extent * 0.8, 6.0) * _zoom_factor
var offset: Vector3 = Vector3(distance, height, distance)
var target: Vector3 = global_transform.origin
var offset_global: Vector3 = global_transform.basis * offset
var desired_position: Vector3 = target + offset_global
_camera.look_at_from_position(desired_position, target, Vector3.UP)
func _compute_required_ground_size() -> Vector2:
var base_half := Vector2(_grid_size.x * 0.5, _grid_size.y * 0.5)
var half_extents := base_half
if is_instance_valid(_camera) and not Engine.is_editor_hint():
var viewport := get_viewport()
if viewport:
var view_size: Vector2 = viewport.get_visible_rect().size
var corners := [
Vector2.ZERO,
Vector2(view_size.x, 0),
Vector2(0, view_size.y),
view_size
]
var plane_normal: Vector3 = global_transform.basis.y.normalized()
var plane_point: Vector3 = global_transform.origin
for corner in corners:
var origin := _camera.project_ray_origin(corner)
var direction := _camera.project_ray_normal(corner)
var denominator := plane_normal.dot(direction)
if abs(denominator) < 0.0001:
continue
var t := plane_normal.dot(plane_point - origin) / denominator
if t <= 0.0:
continue
var world_point := origin + direction * t
var local_point := to_local(world_point)
half_extents.x = max(half_extents.x, abs(local_point.x))
half_extents.y = max(half_extents.y, abs(local_point.z))
var padding_vec := Vector2.ONE * _ground_padding
return (half_extents + padding_vec) * 2.0
func _update_day_cycle(delta: float) -> void:
if _day_length_hours <= 0.0 or _seconds_per_game_hour <= 0.0:
return
_time_of_day_hours = fposmod(_time_of_day_hours + delta / _seconds_per_game_hour, _day_length_hours)
_apply_sun_lighting()
func _apply_sun_lighting() -> void:
if not is_instance_valid(_sun_light):
return
if _day_length_hours <= 0.0:
return
var cycle: float = 0.0
if _day_length_hours > 0.0:
cycle = fposmod(_time_of_day_hours / _day_length_hours, 1.0)
var sun_angle: float = cycle * TAU
var rotation_axis: Vector3 = Vector3(1.0, 0.0, -1.0).normalized()
if rotation_axis.length() == 0.0:
rotation_axis = Vector3.RIGHT
var direction: Vector3 = Vector3.UP.rotated(rotation_axis, sun_angle)
direction = direction.rotated(Vector3.UP, deg_to_rad(35.0)).normalized()
var target: Vector3 = global_transform.origin
var sun_distance: float = 60.0
var sun_position: Vector3 = target - direction * sun_distance
_sun_light.look_at_from_position(sun_position, target, Vector3.UP)
var daylight: float = clamp(direction.y * 0.5 + 0.5, 0.0, 1.0)
var dawn_color: Color = Color(0.95, 0.58, 0.32)
var night_color: Color = Color(0.12, 0.18, 0.35)
var day_color: Color = Color(1.0, 0.96, 0.88)
var warm_mix: Color = dawn_color.lerp(day_color, clamp((daylight - 0.25) / 0.5, 0.0, 1.0))
_sun_light.light_color = night_color.lerp(warm_mix, daylight)
_sun_light.light_energy = lerp(0.05, 1.25, daylight)
_sun_light.shadow_enabled = daylight > 0.1
func _process(_delta: float) -> void:
if Engine.is_editor_hint():
return
var mouse_position: Vector2 = get_viewport().get_mouse_position()
_update_hovered_cell(mouse_position)
_update_day_cycle(_delta)
func _unhandled_input(event: InputEvent) -> void:
if Engine.is_editor_hint():
return
if event is InputEventMouseButton and event.pressed:
var button_event := event as InputEventMouseButton
if button_event.button_index == MOUSE_BUTTON_WHEEL_UP:
_adjust_zoom(-_zoom_step_internal)
elif button_event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
_adjust_zoom(_zoom_step_internal)
func _adjust_zoom(delta: float) -> void:
var previous: float = _zoom_factor
_zoom_factor = clamp(_zoom_factor + delta, _zoom_min_internal, _zoom_max_internal)
if is_equal_approx(previous, _zoom_factor):
return
_update_camera_view()
_update_ground_assets()
func _update_hovered_cell(mouse_position: Vector2) -> void:
if not _camera or not _hover_highlight:
return
var ray_origin: Vector3 = _camera.project_ray_origin(mouse_position)
var ray_direction: Vector3 = _camera.project_ray_normal(mouse_position)
var plane_normal: Vector3 = global_transform.basis.y.normalized()
var plane_point: Vector3 = global_transform.origin
var denominator: float = plane_normal.dot(ray_direction)
if abs(denominator) < 0.0001:
_clear_hover()
return
var t: float = plane_normal.dot(plane_point - ray_origin) / denominator
if t <= 0.0:
_clear_hover()
return
var world_point: Vector3 = ray_origin + ray_direction * t
var cell_index: Vector2i = world_to_cell(world_point, false)
if cell_index.x < 0 or cell_index.y < 0 or not _cells.has(cell_index):
_clear_hover()
return
if cell_index == _hovered_cell:
return
_hovered_cell = cell_index
var local_center: Vector3 = cell_to_world(cell_index)
var target_position: Vector3 = to_global(local_center)
_hover_highlight.global_position = target_position + plane_normal * 0.02
_hover_highlight.visible = true
func _clear_hover() -> void:
if _hover_highlight:
_hover_highlight.visible = false
_hovered_cell = Vector2i(-1, -1)