From f7bede5002d87d7baeff8a311029431fbe430357 Mon Sep 17 00:00:00 2001 From: Clifton Palmer Date: Thu, 21 Nov 2024 13:04:57 +0200 Subject: [PATCH 1/1] Player attributes now accessable --- player/player.gd | 73 +++++++++++++++++++++++++++++++----------------- project.godot | 25 +++++++++++++++++ test/test.gd | 6 ++++ test/test.tscn | 24 ++++++++-------- 4 files changed, 91 insertions(+), 37 deletions(-) create mode 100644 test/test.gd diff --git a/player/player.gd b/player/player.gd index f6b6c56..eae307e 100644 --- a/player/player.gd +++ b/player/player.gd @@ -1,41 +1,73 @@ -extends CharacterBody3D +class_name Player extends CharacterBody3D -@onready var _skin: KnightSkin = $skin +# class variables +@export_category("Movement") +@export var _walk_speed := 5.0 +@export var _jump_speed := 9.0 +@export var _fall_speed := 2.0 +@export var _locked_z := true + +@export_category("Camera") +@export var _camera_distance := 5.0 +@export var _dof_blur_far_distance := 5.0 +@export var _camera_pivot_rotate_y := 0.0 -const SPEED = 5.0 -const JUMP_VELOCITY = 4.5 * 3 -const FALL_SPEED = 2.0 +var _last_movement_direction := Vector3.RIGHT +@onready var _skin: KnightSkin = $skin +@onready var _camera_pivot: Node3D = $"camera-pivot" +@onready var _spring_arm: SpringArm3D = $"camera-pivot/SpringArm3D" +@onready var _camera: Camera3D = $"camera-pivot/SpringArm3D/Camera3D" +@onready var _camera_attr: CameraAttributes = _camera.attributes -var _last_movement_direction := Vector3.FORWARD +# inherited functions +func _ready() -> void: + _spring_arm.spring_length = _camera_distance + _camera_attr.dof_blur_far_distance = _dof_blur_far_distance + _camera_pivot.rotate_y(_camera_pivot_rotate_y) func _input(event: InputEvent) -> void: - if event.is_action_released("ui_accept"): - velocity.y = 0 + if event.is_action_released("player-jump"): + if velocity.y > 0: + velocity.y = 0 elif event.is_action_pressed("player-attack"): _skin.attack() +func _physics_process(delta: float) -> void: + # Add the gravity. + if not is_on_floor(): + velocity += get_gravity() * _fall_speed * delta + + _process_player_input(delta) + _process_player_skin(delta) + + move_and_slide() + + +# my functions func _process_player_input(_delta: float) -> void: + # Handle jump. + if Input.is_action_just_pressed("player-jump") and is_on_floor(): + velocity.y = _jump_speed + # 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("ui_left", "ui_right", "ui_up", "ui_down") + var input_dir := Input.get_vector("player-left", "player-right", "player-up", "player-down") 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 + velocity.x = direction.x * _walk_speed + if not _locked_z: + velocity.z = direction.z * _walk_speed _last_movement_direction = direction else: velocity.x = 0 velocity.z = 0 - # Handle jump. - if Input.is_action_just_pressed("ui_accept") and is_on_floor(): - velocity.y = JUMP_VELOCITY - func _process_player_skin(_delta: float) -> void: _skin.global_rotation.y = Vector3.BACK.signed_angle_to(_last_movement_direction, Vector3.UP) @@ -44,14 +76,3 @@ func _process_player_skin(_delta: float) -> void: _skin.move(velocity.length()) else: _skin.fall() - - -func _physics_process(delta: float) -> void: - # Add the gravity. - if not is_on_floor(): - velocity += get_gravity() * FALL_SPEED * delta - - _process_player_input(delta) - _process_player_skin(delta) - - move_and_slide() diff --git a/project.godot b/project.godot index 2b6d068..9b85a64 100644 --- a/project.godot +++ b/project.godot @@ -24,6 +24,31 @@ window/size/viewport_height=1944 player-attack={ "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":75,"key_label":0,"unicode":107,"location":0,"echo":false,"script":null) +] +} +player-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":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null) ] } +player-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":70,"key_label":0,"unicode":102,"location":0,"echo":false,"script":null) +] +} +player-up={ +"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":69,"key_label":0,"unicode":101,"location":0,"echo":false,"script":null) +] +} +player-down={ +"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":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null) +] +} +player-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,"location":0,"echo":false,"script":null) +] +} diff --git a/test/test.gd b/test/test.gd new file mode 100644 index 0000000..b8cdee7 --- /dev/null +++ b/test/test.gd @@ -0,0 +1,6 @@ +extends Node3D + + +func _unhandled_input(event: InputEvent) -> void: + if event.is_action_pressed("ui_cancel"): + get_tree().quit() diff --git a/test/test.tscn b/test/test.tscn index 4e181aa..b5d8aa8 100644 --- a/test/test.tscn +++ b/test/test.tscn @@ -1,5 +1,6 @@ -[gd_scene load_steps=9 format=3 uid="uid://bvydwnbie3pv4"] +[gd_scene load_steps=10 format=3 uid="uid://bvydwnbie3pv4"] +[ext_resource type="Script" path="res://test/test.gd" id="1_gmol0"] [ext_resource type="PackedScene" uid="uid://chtr62feiltax" path="res://player/player.tscn" id="1_ytdba"] [ext_resource type="PackedScene" uid="uid://dbthgkok84ru0" path="res://test/block.tscn" id="2_8u8xb"] @@ -29,6 +30,7 @@ albedo_color = Color(0.149763, 0.203291, 0.262937, 1) data = PackedVector3Array(20, 0, 4.49, -20, 0, 4.49, 20, 0, -4.49, -20, 0, 4.49, -20, 0, -4.49, 20, 0, -4.49) [node name="Test" type="Node3D"] +script = ExtResource("1_gmol0") [node name="WorldEnvironment" type="WorldEnvironment" parent="."] environment = SubResource("Environment_7wa85") @@ -41,32 +43,32 @@ shadow_enabled = true [node name="Player" parent="." instance=ExtResource("1_ytdba")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.806743, 1.19209e-07, 0.147649) -[node name="ground" type="Node3D" parent="."] +[node name="map" type="Node3D" parent="."] -[node name="plane" type="MeshInstance3D" parent="ground"] +[node name="ground" type="MeshInstance3D" parent="map"] mesh = SubResource("PlaneMesh_dplfc") skeleton = NodePath("../..") surface_material_override/0 = SubResource("StandardMaterial3D_87x6o") -[node name="StaticBody3D" type="StaticBody3D" parent="ground/plane"] +[node name="StaticBody3D" type="StaticBody3D" parent="map/ground"] -[node name="CollisionShape3D" type="CollisionShape3D" parent="ground/plane/StaticBody3D"] +[node name="CollisionShape3D" type="CollisionShape3D" parent="map/ground/StaticBody3D"] shape = SubResource("ConcavePolygonShape3D_73kmu") -[node name="Block" parent="ground" instance=ExtResource("2_8u8xb")] +[node name="Block" parent="map" instance=ExtResource("2_8u8xb")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 3.30582, 0, 0.134318) -[node name="Block2" parent="ground" instance=ExtResource("2_8u8xb")] +[node name="Block2" parent="map" instance=ExtResource("2_8u8xb")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 5.94243, 1.91616, 0.268636) -[node name="Block3" parent="ground" instance=ExtResource("2_8u8xb")] +[node name="Block3" parent="map" instance=ExtResource("2_8u8xb")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.315662, -4.76837e-07, -3.10105) -[node name="Block4" parent="ground" instance=ExtResource("2_8u8xb")] +[node name="Block4" parent="map" instance=ExtResource("2_8u8xb")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.899326, 0, -2.69083) -[node name="Block5" parent="ground" instance=ExtResource("2_8u8xb")] +[node name="Block5" parent="map" instance=ExtResource("2_8u8xb")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.8697, 0, 3.31299) -[node name="Block6" parent="ground" instance=ExtResource("2_8u8xb")] +[node name="Block6" parent="map" instance=ExtResource("2_8u8xb")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.79621, 4.76837e-07, 3.0765) -- 2.47.2