From aff0c7c791ed17b4fbfe1618aba0d4551e1595fb Mon Sep 17 00:00:00 2001 From: Rob Kelly Date: Sat, 20 Jul 2024 13:50:12 -0600 Subject: [PATCH] Basic player camera control & environment --- project.godot | 21 ++++++++++++++++++++- src/maps/arena_map/arena_map.tscn | 29 +++++++++++++++++++++++++---- src/player/player.gd | 31 +++++++++++++++++++++++++++++++ src/player/player.tscn | 24 ++++++++++++++++++++---- src/player/third_person_camera.gd | 26 ++++++++++++++++++++++++++ 5 files changed, 122 insertions(+), 9 deletions(-) create mode 100644 src/player/player.gd create mode 100644 src/player/third_person_camera.gd diff --git a/project.godot b/project.godot index 3e30bd7..549a078 100644 --- a/project.godot +++ b/project.godot @@ -11,12 +11,13 @@ config_version=5 [application] config/name="Sunstation" +run/main_scene="res://src/maps/arena_map/arena_map.tscn" config/features=PackedStringArray("4.2", "Forward Plus") run/max_fps=60 -config/icon="res://icon.svg" [debug] +settings/stdout/print_fps=true gdscript/warnings/untyped_declaration=2 gdscript/warnings/unsafe_property_access=2 gdscript/warnings/unsafe_method_access=2 @@ -25,6 +26,8 @@ gdscript/warnings/unsafe_call_argument=2 [display] +window/size/viewport_width=1900 +window/size/viewport_height=768 window/stretch/mode="canvas_items" [dotnet] @@ -65,3 +68,19 @@ right={ , Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":1.0,"script":null) ] } +jump={ +"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":32,"key_label":0,"unicode":32,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":0,"pressure":0.0,"pressed":true,"script":null) +] +} +boost={ +"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":4194325,"key_label":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":2,"pressure":0.0,"pressed":true,"script":null) +] +} + +[rendering] + +anti_aliasing/quality/msaa_3d=3 diff --git a/src/maps/arena_map/arena_map.tscn b/src/maps/arena_map/arena_map.tscn index 9093cdf..2011dd5 100644 --- a/src/maps/arena_map/arena_map.tscn +++ b/src/maps/arena_map/arena_map.tscn @@ -1,12 +1,25 @@ -[gd_scene load_steps=6 format=3 uid="uid://drbiyuustse8b"] +[gd_scene load_steps=9 format=3 uid="uid://drbiyuustse8b"] [ext_resource type="PackedScene" uid="uid://bqp5mwyat3asv" path="res://src/player/player.tscn" id="1_tgee4"] +[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_e4ffr"] +sky_top_color = Color(1, 1, 1, 1) +sky_horizon_color = Color(0, 0, 0, 1) +ground_bottom_color = Color(0, 0, 0, 1) +ground_horizon_color = Color(0, 0, 0, 1) + +[sub_resource type="Sky" id="Sky_7lp20"] +sky_material = SubResource("ProceduralSkyMaterial_e4ffr") + +[sub_resource type="Environment" id="Environment_0cwcw"] +background_mode = 2 +sky = SubResource("Sky_7lp20") + [sub_resource type="CylinderMesh" id="CylinderMesh_uarav"] top_radius = 50.0 bottom_radius = 50.0 -[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_s8fg7"] +[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_pxre0"] points = PackedVector3Array(0, -1, -50, -4.90407, -1, -49.7651, 0, 1, -50, 4.89428, -1, -49.7651, -4.90407, 1, -49.7651, -9.7592, -1, -49.0407, 4.89428, 1, -49.7651, 9.74941, -1, -49.0407, -9.7592, 1, -49.0407, -14.5164, -1, -47.8563, 9.74941, 1, -49.0407, 14.5067, -1, -47.8563, -14.5164, 1, -47.8563, -19.1366, -1, -46.202, 14.5067, 1, -47.8563, 19.1269, -1, -46.202, -19.1366, 1, -46.202, -23.5709, -1, -44.0975, 19.1269, 1, -46.202, 23.5611, -1, -44.0975, -23.5709, 1, -44.0975, -27.78, -1, -41.5818, 23.5611, 1, -44.0975, 27.7702, -1, -41.5818, -27.78, 1, -41.5818, -31.7247, -1, -38.6551, 27.7702, 1, -41.5818, 31.715, -1, -38.6551, -31.7247, 1, -38.6551, -35.3563, -1, -35.3563, 31.715, 1, -38.6551, 35.3465, -1, -35.3563, -35.3563, 1, -35.3563, -38.6551, -1, -31.7247, 35.3465, 1, -35.3563, 38.6453, -1, -31.7247, -38.6551, 1, -31.7247, -41.5818, -1, -27.78, 38.6453, 1, -31.7247, 41.572, -1, -27.78, -41.5818, 1, -27.78, -44.0975, -1, -23.5709, 41.572, 1, -27.78, 44.0877, -1, -23.5709, -44.0975, 1, -23.5709, -46.202, -1, -19.1366, 44.0877, 1, -23.5709, 46.1923, -1, -19.1366, -46.202, 1, -19.1366, -47.8563, -1, -14.5164, 46.1923, 1, -19.1366, 47.8465, -1, -14.5164, -47.8563, 1, -14.5164, -49.0407, -1, -9.7592, 47.8465, 1, -14.5164, 49.0309, -1, -9.7592, -49.0407, 1, -9.7592, -49.7651, -1, -4.90407, 49.0309, 1, -9.7592, 49.7553, -1, -4.90407, -49.7651, 1, -4.90407, -50, -1, 0, 49.7553, 1, -4.90407, 50, -1, 0, -50, 1, 0, -49.7651, -1, 4.89428, 50, 1, 0, 49.7553, -1, 4.89428, -49.7651, 1, 4.89428, -49.0407, -1, 9.74941, 49.7553, 1, 4.89428, 49.0309, -1, 9.74941, -49.0407, 1, 9.74941, -47.8563, -1, 14.5067, 49.0309, 1, 9.74941, 47.8465, -1, 14.5067, -47.8563, 1, 14.5067, -46.202, -1, 19.1269, 47.8465, 1, 14.5067, 46.1923, -1, 19.1269, -46.202, 1, 19.1269, -44.0975, -1, 23.5611, 46.1923, 1, 19.1269, 44.0877, -1, 23.5611, -44.0975, 1, 23.5611, -41.5818, -1, 27.7702, 44.0877, 1, 23.5611, 41.572, -1, 27.7702, -41.5818, 1, 27.7702, -38.6551, -1, 31.715, 41.572, 1, 27.7702, 38.6453, -1, 31.715, -38.6551, 1, 31.715, -35.3563, -1, 35.3465, 38.6453, 1, 31.715, 35.3465, -1, 35.3465, -35.3563, 1, 35.3465, -31.7247, -1, 38.6453, 35.3465, 1, 35.3465, 31.715, -1, 38.6453, -31.7247, 1, 38.6453, -27.78, -1, 41.572, 31.715, 1, 38.6453, 27.7702, -1, 41.572, -27.78, 1, 41.572, -23.5709, -1, 44.0877, 27.7702, 1, 41.572, 23.5611, -1, 44.0877, -23.5709, 1, 44.0877, -19.1366, -1, 46.1923, 23.5611, 1, 44.0877, 19.1269, -1, 46.1923, -19.1366, 1, 46.1923, -14.5164, -1, 47.8465, 19.1269, 1, 46.1923, 14.5067, -1, 47.8465, -14.5164, 1, 47.8465, -9.7592, -1, 49.0309, 14.5067, 1, 47.8465, 9.74941, -1, 49.0309, -9.7592, 1, 49.0309, -4.90407, -1, 49.7553, 9.74941, 1, 49.0309, 4.89428, -1, 49.7553, -4.90407, 1, 49.7553, 0, -1, 50, 4.89428, 1, 49.7553, 0, 1, 50) [sub_resource type="CylinderMesh" id="CylinderMesh_pfp3p"] @@ -17,7 +30,15 @@ points = PackedVector3Array(-0.5, -1, 0, -0.497651, -1, -0.0490407, -0.497651, - [node name="ArenaMap" type="Node3D"] [node name="Player" parent="." instance=ExtResource("1_tgee4")] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0) + +[node name="Environment" type="Node3D" parent="."] + +[node name="WorldEnvironment" type="WorldEnvironment" parent="Environment"] +environment = SubResource("Environment_0cwcw") + +[node name="DirectionalLight3D" type="DirectionalLight3D" parent="Environment"] +transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 50, 0) [node name="WorldGeometry" type="Node3D" parent="."] @@ -31,7 +52,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0) [node name="CollisionShape3D" type="CollisionShape3D" parent="WorldGeometry/Floor/StaticBody3D"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) -shape = SubResource("ConvexPolygonShape3D_s8fg7") +shape = SubResource("ConvexPolygonShape3D_pxre0") [node name="Pillar1" type="MeshInstance3D" parent="WorldGeometry"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 10, 1, 10) diff --git a/src/player/player.gd b/src/player/player.gd new file mode 100644 index 0000000..0bbb9b7 --- /dev/null +++ b/src/player/player.gd @@ -0,0 +1,31 @@ +extends CharacterBody3D + + +const SPEED = 5.0 +const JUMP_VELOCITY = 4.5 + +# Get the gravity from the project settings to be synced with RigidBody nodes. +var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity") + + +func _physics_process(delta: float) -> void: + # Add the gravity. + if not is_on_floor(): + velocity.y -= gravity * delta + + # Handle jump. + if Input.is_action_just_pressed("jump") and is_on_floor(): + velocity.y = JUMP_VELOCITY + + # Get the input direction and handle the movement/deceleration. + # As good practice, you should replace UI actions with custom gameplay actions. + var input_dir := Input.get_vector("left", "right", "forward", "backward") + var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() + if direction: + velocity.x = direction.x * SPEED + velocity.z = direction.z * SPEED + else: + velocity.x = move_toward(velocity.x, 0, SPEED) + velocity.z = move_toward(velocity.z, 0, SPEED) + + move_and_slide() diff --git a/src/player/player.tscn b/src/player/player.tscn index 6773a56..e078da5 100644 --- a/src/player/player.tscn +++ b/src/player/player.tscn @@ -1,16 +1,20 @@ -[gd_scene load_steps=5 format=3 uid="uid://bqp5mwyat3asv"] +[gd_scene load_steps=8 format=3 uid="uid://bqp5mwyat3asv"] +[ext_resource type="Script" path="res://src/player/player.gd" id="1_h16ke"] [ext_resource type="Texture2D" uid="uid://b0yefajw1cqo" path="res://assets/textures/character_placeholder.png" id="1_iys0p"] +[ext_resource type="Script" path="res://src/player/third_person_camera.gd" id="3_gichr"] [sub_resource type="CapsuleMesh" id="CapsuleMesh_8sedy"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_dd4i1"] albedo_texture = ExtResource("1_iys0p") -[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_igj5k"] -points = PackedVector3Array(-0.125207, -0.532801, -0.480507, 0.0227831, 0.47607, 0.498884, 0.169713, 0.559144, 0.464172, 0.231051, -0.803591, 0.320455, 0.40741, 0.651043, -0.243523, -0.482789, 0.594843, 0.0822132, -0.362868, -0.682312, 0.289697, 0.469044, -0.654529, -0.0662713, -0.127444, 0.842701, -0.338103, -0.393435, -0.683942, -0.244717, 0.438255, 0.623309, 0.200849, 0.0841477, 0.977454, 0.114795, -0.0682023, -0.976458, -0.12927, 0.20055, -0.563129, -0.451454, -0.185527, 0.595453, -0.453475, -0.273363, 0.592268, 0.407754, -0.00693649, -0.476823, 0.49966, 0.375821, -0.588614, 0.316955, 0.111579, 0.563059, -0.481177, -0.41725, 0.527866, -0.270497, -0.484546, -0.596972, -0.0665097, -0.279747, 0.908561, 0.0533361, -0.250197, -0.880712, 0.205319, 0.263647, -0.902771, -0.127394, 0.293368, 0.871526, -0.157196, 0.373412, -0.526319, -0.328246, 0.499663, 0.476641, -0.00688856, 0.0531056, 0.875001, 0.324703, -0.154543, -0.590854, 0.465879, -0.0972799, -0.782358, -0.398188, -0.387649, -0.498171, 0.31565, -0.30068, -0.587995, -0.388901) +[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_otw5j"] + +[sub_resource type="SphereShape3D" id="SphereShape3D_jv2hv"] [node name="Player" type="CharacterBody3D"] +script = ExtResource("1_h16ke") [node name="Mesh" type="Node3D" parent="."] @@ -20,4 +24,16 @@ skeleton = NodePath("../..") surface_material_override/0 = SubResource("StandardMaterial3D_dd4i1") [node name="CollisionShape3D" type="CollisionShape3D" parent="."] -shape = SubResource("ConvexPolygonShape3D_igj5k") +shape = SubResource("CapsuleShape3D_otw5j") + +[node name="CameraRoot" type="Node3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 0.866025, -0.5, 0, 0.5, 0.866025, 0, 0.5, 0) +script = ExtResource("3_gichr") + +[node name="SpringArm3D" type="SpringArm3D" parent="CameraRoot"] +transform = Transform3D(-1, 0, 8.74228e-08, 0, 1, 0, -8.74228e-08, 0, -1, 0, 0, 0) +shape = SubResource("SphereShape3D_jv2hv") +spring_length = 3.0 + +[node name="Camera3D" type="Camera3D" parent="CameraRoot/SpringArm3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 3) diff --git a/src/player/third_person_camera.gd b/src/player/third_person_camera.gd new file mode 100644 index 0000000..7885b23 --- /dev/null +++ b/src/player/third_person_camera.gd @@ -0,0 +1,26 @@ +extends Node3D + + +@export var yaw_sensitivity: float = 0.4 +@export var yaw_acceleration: float = 20.0 + +@export var pitch_sensitivity: float = 0.4 +@export var pitch_acceleration: float = 20.0 +@export_range(-90.0, 90.0) var pitch_min: float = -70.0 +@export_range(-90.0, 90.0) var pitch_max: float = 75.0 +@export var invert_pitch: bool = false + +@onready var _target: Vector2 = Vector2(rotation_degrees.x, rotation_degrees.y) + +func _ready() -> void: + Input.mouse_mode = Input.MOUSE_MODE_CAPTURED + +func _unhandled_input(event: InputEvent) -> void: + if event is InputEventMouseMotion: + var motion: Vector2 = (event as InputEventMouseMotion).relative + _target.y = _target.y - motion.x * yaw_sensitivity + _target.x = clampf(_target.x + motion.y * pitch_sensitivity * (-1 if invert_pitch else 1), pitch_min, pitch_max) + +func _physics_process(delta: float) -> void: + rotation_degrees.y = lerpf(rotation_degrees.y, _target.y, delta * yaw_acceleration) + rotation_degrees.x = lerpf(rotation_degrees.x, _target.x, delta * pitch_acceleration)