diff options
author | Hop311 <hop3114@gmail.com> | 2023-03-30 23:50:50 +0200 |
---|---|---|
committer | Hop311 <hop3114@gmail.com> | 2023-03-30 23:50:50 +0200 |
commit | 4e66822327a1d964a6324bd28e068c10a7183c65 (patch) | |
tree | f92754105aa33e05173dbfd6781c0ce5a2f98805 | |
parent | 3384b21177a160f7192a2e4877eea3b29880bf4e (diff) |
First go at terrain map implementation
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | game/common/map/terrain/terrain.png | bin | 0 -> 27542091 bytes | |||
-rw-r--r-- | game/common/map/terrain/terrain.png.import | 34 | ||||
-rw-r--r-- | game/project.godot | 39 | ||||
-rw-r--r-- | game/src/GameSession/GameSession.tscn | 11 | ||||
-rw-r--r-- | game/src/GameSession/MapControlPanel.tscn | 9 | ||||
-rw-r--r-- | game/src/GameSession/MapViewport.gd | 59 | ||||
-rw-r--r-- | game/src/GameSession/MapViewport.tscn | 31 | ||||
-rw-r--r-- | game/src/GameSession/TerrainMap.gdshader | 10 |
9 files changed, 192 insertions, 2 deletions
@@ -70,3 +70,4 @@ bin/* .DS_Store *.translation +!game/common/map/*.obj
\ No newline at end of file diff --git a/game/common/map/terrain/terrain.png b/game/common/map/terrain/terrain.png Binary files differnew file mode 100644 index 0000000..6d36dee --- /dev/null +++ b/game/common/map/terrain/terrain.png diff --git a/game/common/map/terrain/terrain.png.import b/game/common/map/terrain/terrain.png.import new file mode 100644 index 0000000..56ba5dc --- /dev/null +++ b/game/common/map/terrain/terrain.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://cmw0pvjthnn8c" +path="res://.godot/imported/terrain.png-c443b8a709ca7acc4b3bb3df1d3095a9.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://common/map/terrain/terrain.png" +dest_files=["res://.godot/imported/terrain.png-c443b8a709ca7acc4b3bb3df1d3095a9.ctex"] + +[params] + +compress/mode=3 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=2 +compress/channel_pack=0 +mipmaps/generate=true +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=0 diff --git a/game/project.godot b/game/project.godot index 363bbc1..f3f93d6 100644 --- a/game/project.godot +++ b/game/project.godot @@ -44,6 +44,45 @@ enabled=PackedStringArray("res://addons/keychain/plugin.cfg", "res://addons/open theme/custom="res://theme/default_theme.tres" +[input] + +map_north={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194320,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":87,"physical_keycode":0,"key_label":0,"unicode":119,"echo":false,"script":null) +] +} +map_east={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194321,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":68,"physical_keycode":0,"key_label":0,"unicode":100,"echo":false,"script":null) +] +} +map_south={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194322,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":83,"physical_keycode":0,"key_label":0,"unicode":115,"echo":false,"script":null) +] +} +map_west={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194319,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":65,"physical_keycode":0,"key_label":0,"unicode":97,"echo":false,"script":null) +] +} +map_zoomin={ +"deadzone": 0.5, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":8,"position":Vector2(174, 17),"global_position":Vector2(180, 80),"factor":1.0,"button_index":4,"pressed":true,"double_click":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":81,"physical_keycode":0,"key_label":0,"unicode":113,"echo":false,"script":null) +] +} +map_zoomout={ +"deadzone": 0.5, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":16,"position":Vector2(325, 24),"global_position":Vector2(331, 87),"factor":1.0,"button_index":5,"pressed":true,"double_click":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":69,"physical_keycode":0,"key_label":0,"unicode":101,"echo":false,"script":null) +] +} + [internationalization] locale/translation_remaps={} diff --git a/game/src/GameSession/GameSession.tscn b/game/src/GameSession/GameSession.tscn index f984daf..a707b04 100644 --- a/game/src/GameSession/GameSession.tscn +++ b/game/src/GameSession/GameSession.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=4 format=3 uid="uid://bgnupcshe1m7r"] +[gd_scene load_steps=5 format=3 uid="uid://bgnupcshe1m7r"] [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"] [node name="GameSession" type="Control" node_paths=PackedStringArray("_game_session_menu")] editor_description = "SS-102" @@ -15,6 +16,14 @@ 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="GameSessionMenu" parent="." instance=ExtResource("3_bvmqh")] visible = false layout_mode = 1 diff --git a/game/src/GameSession/MapControlPanel.tscn b/game/src/GameSession/MapControlPanel.tscn index 71d43e7..2a0c971 100644 --- a/game/src/GameSession/MapControlPanel.tscn +++ b/game/src/GameSession/MapControlPanel.tscn @@ -1,7 +1,13 @@ -[gd_scene load_steps=2 format=3 uid="uid://g524p8lr574w"] +[gd_scene load_steps=4 format=3 uid="uid://g524p8lr574w"] [ext_resource type="Script" path="res://src/GameSession/MapControlPanel.gd" id="1_ign64"] +[sub_resource type="InputEventAction" id="InputEventAction_5nck3"] +action = &"ui_cancel" + +[sub_resource type="Shortcut" id="Shortcut_fc1tk"] +events = [SubResource("InputEventAction_5nck3")] + [node name="PanelContainer" type="PanelContainer"] editor_description = "SS-103" script = ExtResource("1_ign64") @@ -27,6 +33,7 @@ layout_mode = 2 [node name="GameSessionMenuButton" type="Button" parent="HBoxContainer/AuxiliaryPanel"] editor_description = "UI-9" layout_mode = 2 +shortcut = SubResource("Shortcut_fc1tk") text = "ESC" [connection signal="pressed" from="HBoxContainer/AuxiliaryPanel/GameSessionMenuButton" to="." method="_on_game_session_menu_button_pressed"] diff --git a/game/src/GameSession/MapViewport.gd b/game/src/GameSession/MapViewport.gd new file mode 100644 index 0000000..fa57163 --- /dev/null +++ b/game/src/GameSession/MapViewport.gd @@ -0,0 +1,59 @@ +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 new file mode 100644 index 0000000..8a34b54 --- /dev/null +++ b/game/src/GameSession/MapViewport.tscn @@ -0,0 +1,31 @@ +[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) diff --git a/game/src/GameSession/TerrainMap.gdshader b/game/src/GameSession/TerrainMap.gdshader new file mode 100644 index 0000000..522e0f3 --- /dev/null +++ b/game/src/GameSession/TerrainMap.gdshader @@ -0,0 +1,10 @@ +shader_type spatial; + +render_mode unshaded; + +uniform sampler2D tex: source_color, repeat_enable; + +void fragment() { + vec2 new_uv = vec2(UV.x * 2.0 - 0.5, UV.y); + ALBEDO = texture(tex, new_uv).rgb; +} |