diff --git a/Scenes/Main.tscn b/Scenes/Main.tscn index 890c256..36d7a66 100644 --- a/Scenes/Main.tscn +++ b/Scenes/Main.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=4 format=3 uid="uid://i5jawm0cwlat"] +[gd_scene load_steps=5 format=3 uid="uid://i5jawm0cwlat"] [ext_resource type="Script" path="res://Scripts/Main.gd" id="1_2yjjx"] [ext_resource type="PackedScene" uid="uid://deff7lt34nj0h" path="res://Spaces/Dungeon.tscn" id="2_4gs22"] +[ext_resource type="PackedScene" uid="uid://b83k3b5x1wmyl" path="res://Scenes/Player.tscn" id="3_eq7hn"] [sub_resource type="Environment" id="Environment_pn2uj"] @@ -11,8 +12,7 @@ script = ExtResource("1_2yjjx") [node name="Dungeon" parent="." instance=ExtResource("2_4gs22")] visible = false -[node name="Camera" type="Camera3D" parent="."] -transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, 0) +[node name="Player" parent="." instance=ExtResource("3_eq7hn")] [node name="WorldEnvironment" type="WorldEnvironment" parent="."] environment = SubResource("Environment_pn2uj") diff --git a/Scenes/Player.tscn b/Scenes/Player.tscn new file mode 100644 index 0000000..edc1979 --- /dev/null +++ b/Scenes/Player.tscn @@ -0,0 +1,11 @@ +[gd_scene load_steps=2 format=3 uid="uid://b83k3b5x1wmyl"] + +[ext_resource type="Script" path="res://Scripts/Player.gd" id="1_m1g7d"] + +[node name="Player" type="CharacterBody3D" node_paths=PackedStringArray("camera")] +script = ExtResource("1_m1g7d") +camera = NodePath("Pivot/Camera3D") + +[node name="Pivot" type="Node3D" parent="."] + +[node name="Camera3D" type="Camera3D" parent="Pivot"] diff --git a/Scripts/Main.gd b/Scripts/Main.gd index 57f250a..5e3860d 100644 --- a/Scripts/Main.gd +++ b/Scripts/Main.gd @@ -3,9 +3,9 @@ extends Node3D func _ready(): add_child($Dungeon.generate_geometry()) - $Camera.position = $Dungeon.get_spawn_point() - $Camera.position.y += 0.5 + $Player.position = $Dungeon.get_spawn_point() + $Player.position.y += 0.5 func _process(delta): - $Camera.rotate_y(0.01) + pass diff --git a/Scripts/Player.gd b/Scripts/Player.gd new file mode 100644 index 0000000..baa35fe --- /dev/null +++ b/Scripts/Player.gd @@ -0,0 +1,64 @@ +extends CharacterBody3D + + +@export var camera: Camera3D + +const VIEW_BOB_FREQUENCY = 8 +const VIEW_BOB_AMPLITUDE = 0.03 + +var speed := 2 +var turn_speed_sec := 0.1 +var turn_degrees := 90 +var view_bob_time := 0.0 # increased while velocity is not zero + + +func _get_input() -> Vector3: + var input_dir := Vector3() + + if Input.is_action_pressed("move_forward"): + input_dir -= global_transform.basis.z + if Input.is_action_pressed("move_backward"): + input_dir += global_transform.basis.z + + return input_dir.normalized() + + +func _process_view_bobbing(delta: float) -> void: + view_bob_time += delta * velocity.length() + camera.position.y = sin(view_bob_time * VIEW_BOB_FREQUENCY) * VIEW_BOB_AMPLITUDE + camera.position.x = cos(view_bob_time * (VIEW_BOB_FREQUENCY / 2)) * VIEW_BOB_AMPLITUDE + + +func _ready() -> void: + pass + + +func _unhandled_input(event: InputEvent) -> void: + if event.is_action_pressed("turn_left"): + create_tween().tween_property( + self, + "rotation_degrees:y", + rotation_degrees.y + turn_degrees, + turn_speed_sec + ) + if event.is_action_pressed("turn_right"): + create_tween().tween_property( + self, + "rotation_degrees:y", + rotation_degrees.y - turn_degrees, + turn_speed_sec + ) + + +func _process(delta: float) -> void: + _process_view_bobbing(delta) + + +func _physics_process(delta: float) -> void: + # TODO + #velocity.y += gravity * delta + var final_velocity = _get_input() * speed + + velocity.x = final_velocity.x + velocity.z = final_velocity.z + move_and_slide() diff --git a/project.godot b/project.godot index ef7dd37..f4486e2 100644 --- a/project.godot +++ b/project.godot @@ -19,6 +19,33 @@ config/icon="res://icon.svg" enabled=PackedStringArray() +[input] + +move_forward={ +"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":0,"physical_keycode":4194320,"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":0,"physical_keycode":87,"key_label":0,"unicode":119,"echo":false,"script":null) +] +} +move_backward={ +"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":0,"physical_keycode":4194322,"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":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null) +] +} +turn_left={ +"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":0,"physical_keycode":4194319,"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":0,"physical_keycode":65,"key_label":0,"unicode":97,"echo":false,"script":null) +] +} +turn_right={ +"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":0,"physical_keycode":4194321,"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":0,"physical_keycode":68,"key_label":0,"unicode":100,"echo":false,"script":null) +] +} + [rendering] renderer/rendering_method="gl_compatibility"