From 60ddfc88fd6dc259792532fadf9cc4407f90e95f Mon Sep 17 00:00:00 2001 From: Hop311 Date: Fri, 31 Mar 2023 13:53:21 +0100 Subject: Removed SubViewport and made more variables export --- game/src/GameSession/GameSession.tscn | 10 ++---- game/src/GameSession/MapView.gd | 59 +++++++++++++++++++++++++++++++++++ game/src/GameSession/MapView.tscn | 26 +++++++++++++++ game/src/GameSession/MapViewport.gd | 59 ----------------------------------- game/src/GameSession/MapViewport.tscn | 31 ------------------ 5 files changed, 87 insertions(+), 98 deletions(-) create mode 100644 game/src/GameSession/MapView.gd create mode 100644 game/src/GameSession/MapView.tscn delete mode 100644 game/src/GameSession/MapViewport.gd delete mode 100644 game/src/GameSession/MapViewport.tscn (limited to 'game/src') diff --git a/game/src/GameSession/GameSession.tscn b/game/src/GameSession/GameSession.tscn index a707b04..fb9bb3d 100644 --- a/game/src/GameSession/GameSession.tscn +++ b/game/src/GameSession/GameSession.tscn @@ -3,7 +3,7 @@ [ext_resource type="Script" path="res://src/GameSession/GameSession.gd" id="1_eklvp"] [ext_resource type="PackedScene" uid="uid://g524p8lr574w" path="res://src/GameSession/MapControlPanel.tscn" id="3_afh6d"] [ext_resource type="PackedScene" uid="uid://dvdynl6eir40o" path="res://src/GameSession/GameSessionMenu.tscn" id="3_bvmqh"] -[ext_resource type="PackedScene" uid="uid://dkehmdnuxih2r" path="res://src/GameSession/MapViewport.tscn" id="4_xkg5j"] +[ext_resource type="PackedScene" uid="uid://dkehmdnuxih2r" path="res://src/GameSession/MapView.tscn" id="4_xkg5j"] [node name="GameSession" type="Control" node_paths=PackedStringArray("_game_session_menu")] editor_description = "SS-102" @@ -16,13 +16,7 @@ grow_vertical = 2 script = ExtResource("1_eklvp") _game_session_menu = NodePath("GameSessionMenu") -[node name="MapViewport" parent="." instance=ExtResource("4_xkg5j")] -layout_mode = 1 -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -grow_horizontal = 2 -grow_vertical = 2 +[node name="MapView" parent="." instance=ExtResource("4_xkg5j")] [node name="GameSessionMenu" parent="." instance=ExtResource("3_bvmqh")] visible = false diff --git a/game/src/GameSession/MapView.gd b/game/src/GameSession/MapView.gd new file mode 100644 index 0000000..4d06066 --- /dev/null +++ b/game/src/GameSession/MapView.gd @@ -0,0 +1,59 @@ +extends Node3D + +const _action_north : StringName = &"map_north" +const _action_east : StringName = &"map_east" +const _action_south : StringName = &"map_south" +const _action_west : StringName = &"map_west" +const _action_zoomin : StringName = &"map_zoomin" +const _action_zoomout : StringName = &"map_zoomout" + +@export var _move_speed : float = 1.0 + +@export var _zoom_target_min : float = 0.2 +@export var _zoom_target_max : float = 5.0 +@export var _zoom_target_step : float = 0.1 +@export var _zoom_epsilon : float = _zoom_target_step * 0.1 +@export var _zoom_speed : float = 5.0 +@export var _zoom_target : float = 1.0: + get: return _zoom_target + set(v): _zoom_target = clamp(v, _zoom_target_min, _zoom_target_max) + +@export var _camera : Camera3D +@export var _map_mesh : MeshInstance3D + +func _input(event : InputEvent): + if event.is_action_pressed(_action_zoomin, true): + _zoom_target -= _zoom_target_step + elif event.is_action_pressed(_action_zoomout, true): + _zoom_target += _zoom_target_step + +func _physics_process(delta): + # Process movement + var height : float = _camera.transform.origin.y + var move := Vector3( + float(Input.is_action_pressed(_action_east)) - float(Input.is_action_pressed(_action_west)), + 0.0, + float(Input.is_action_pressed(_action_south)) - float(Input.is_action_pressed(_action_north)), + ) + move *= _move_speed * height * delta + _camera.global_translate(move) + + # Keep within map bounds + var bounds := _map_mesh.get_aabb() * _map_mesh.transform + var width := bounds.end.x - bounds.position.x + var left := bounds.position.x + 0.25 * width + var longitude := fposmod(_camera.transform.origin.x - left, width * 0.5) + _camera.transform.origin.x = left + longitude + _camera.transform.origin.z = clamp(_camera.transform.origin.z, bounds.position.z, bounds.end.z) + + # Process zooming + var zoom : float = _zoom_target - height + height += zoom * _zoom_speed * delta + var new_zoom : float = _zoom_target - height + if abs(new_zoom) < _zoom_epsilon or sign(zoom) != sign(new_zoom): + height = _zoom_target + _camera.transform.origin.y = height + + # Orient based on height + var dir := Vector3(0, -1, -exp(-height * 2.0 + 0.5)) + _camera.look_at(_camera.transform.origin + dir) diff --git a/game/src/GameSession/MapView.tscn b/game/src/GameSession/MapView.tscn new file mode 100644 index 0000000..4c34035 --- /dev/null +++ b/game/src/GameSession/MapView.tscn @@ -0,0 +1,26 @@ +[gd_scene load_steps=6 format=3 uid="uid://dkehmdnuxih2r"] + +[ext_resource type="Script" path="res://src/GameSession/MapView.gd" id="1_exccw"] +[ext_resource type="Shader" path="res://src/GameSession/TerrainMap.gdshader" id="1_upocn"] +[ext_resource type="Texture2D" uid="uid://cmw0pvjthnn8c" path="res://common/map/terrain/terrain.png" id="3_l8pnf"] + +[sub_resource type="ShaderMaterial" id="ShaderMaterial_tayeg"] +render_priority = 0 +shader = ExtResource("1_upocn") +shader_parameter/tex = ExtResource("3_l8pnf") + +[sub_resource type="PlaneMesh" id="PlaneMesh_skc48"] +size = Vector2(10.8, 2) + +[node name="MapView" type="Node3D" node_paths=PackedStringArray("_camera", "_map_mesh")] +script = ExtResource("1_exccw") +_camera = NodePath("MapCamera") +_map_mesh = NodePath("MapMeshInstance") + +[node name="MapCamera" type="Camera3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 0.707107, 0.707107, 0, -0.707107, 0.707107, 0, 1, 1) + +[node name="MapMeshInstance" type="MeshInstance3D" parent="."] +transform = Transform3D(5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0) +material_override = SubResource("ShaderMaterial_tayeg") +mesh = SubResource("PlaneMesh_skc48") diff --git a/game/src/GameSession/MapViewport.gd b/game/src/GameSession/MapViewport.gd deleted file mode 100644 index fa57163..0000000 --- a/game/src/GameSession/MapViewport.gd +++ /dev/null @@ -1,59 +0,0 @@ -extends SubViewportContainer - -const _action_north : StringName = &"map_north" -const _action_east : StringName = &"map_east" -const _action_south : StringName = &"map_south" -const _action_west : StringName = &"map_west" -const _action_zoomin : StringName = &"map_zoomin" -const _action_zoomout : StringName = &"map_zoomout" - -const _move_speed : float = 1.0 - -const _zoom_target_min : float = 0.2 -const _zoom_target_max : float = 5.0 -const _zoom_target_step : float = 0.1 -const _zoom_epsilon : float = _zoom_target_step * 0.1 -const _zoom_speed : float = 5.0 -var _zoom_target : float = 1.0: - get: return _zoom_target - set(v): _zoom_target = clamp(v, _zoom_target_min, _zoom_target_max) - -@export var _camera : Camera3D -@export var _map_mesh : MeshInstance3D - -func _input(event : InputEvent): - if event.is_action_pressed(_action_zoomin, true): - _zoom_target -= _zoom_target_step - elif event.is_action_pressed(_action_zoomout, true): - _zoom_target += _zoom_target_step - -func _physics_process(delta): - # Process movement - var height : float = _camera.transform.origin.y - var move := Vector3( - float(Input.is_action_pressed(_action_east)) - float(Input.is_action_pressed(_action_west)), - 0.0, - float(Input.is_action_pressed(_action_south)) - float(Input.is_action_pressed(_action_north)), - ) - move *= _move_speed * height * delta - _camera.global_translate(move) - - # Keep within map bounds - var bounds := _map_mesh.get_aabb() * _map_mesh.transform - var width := bounds.end.x - bounds.position.x - var left := bounds.position.x + 0.25 * width - var longitude := fposmod(_camera.transform.origin.x - left, width * 0.5) - _camera.transform.origin.x = left + longitude - _camera.transform.origin.z = clamp(_camera.transform.origin.z, bounds.position.z, bounds.end.z) - - # Process zooming - var zoom : float = _zoom_target - height - height += zoom * _zoom_speed * delta - var new_zoom : float = _zoom_target - height - if abs(new_zoom) < _zoom_epsilon or sign(zoom) != sign(new_zoom): - height = _zoom_target - _camera.transform.origin.y = height - - # Orient based on height - var dir := Vector3(0, -1, -exp(-height * 2.0 + 0.5)) - _camera.look_at(_camera.transform.origin + dir) diff --git a/game/src/GameSession/MapViewport.tscn b/game/src/GameSession/MapViewport.tscn deleted file mode 100644 index 8a34b54..0000000 --- a/game/src/GameSession/MapViewport.tscn +++ /dev/null @@ -1,31 +0,0 @@ -[gd_scene load_steps=6 format=3 uid="uid://dkehmdnuxih2r"] - -[ext_resource type="Script" path="res://src/GameSession/MapViewport.gd" id="1_exccw"] -[ext_resource type="Shader" path="res://src/GameSession/TerrainMap.gdshader" id="1_upocn"] -[ext_resource type="Texture2D" uid="uid://cmw0pvjthnn8c" path="res://common/map/terrain/terrain.png" id="3_l8pnf"] - -[sub_resource type="ShaderMaterial" id="ShaderMaterial_tayeg"] -render_priority = 0 -shader = ExtResource("1_upocn") -shader_parameter/tex = ExtResource("3_l8pnf") - -[sub_resource type="PlaneMesh" id="PlaneMesh_skc48"] -size = Vector2(10.8, 2) - -[node name="MapViewportContainer" type="SubViewportContainer" node_paths=PackedStringArray("_camera", "_map_mesh")] -stretch = true -script = ExtResource("1_exccw") -_camera = NodePath("MapViewport/Camera3D") -_map_mesh = NodePath("MapViewport/MeshInstance3D") - -[node name="MapViewport" type="SubViewport" parent="."] -handle_input_locally = false -render_target_update_mode = 4 - -[node name="MeshInstance3D" type="MeshInstance3D" parent="MapViewport"] -transform = Transform3D(5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0) -material_override = SubResource("ShaderMaterial_tayeg") -mesh = SubResource("PlaneMesh_skc48") - -[node name="Camera3D" type="Camera3D" parent="MapViewport"] -transform = Transform3D(1, 0, 0, 0, 0.707107, 0.707107, 0, -0.707107, 0.707107, 0, 1, 1) -- cgit v1.2.3-56-ga3b1