Quellcode durchsuchen

:gamepad: Isolate input control

DricomDragon vor 1 Jahr
Ursprung
Commit
cfc5c6f5d4

+ 18 - 11
first3d/component/entity/walker/walker.gd

@@ -11,22 +11,29 @@ extends CharacterBody3D
 var target_velocity = Vector3.ZERO
 
 
-func _physics_process(delta: float) -> void:
-	var direction = Vector3.ZERO
+func trigger_jump() -> void:
+	if is_on_floor():
+		target_velocity.y = jump_impulse
 
-	var ground_velocity: Vector2 = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
 
-	# Ground Velocity
-	target_velocity.x = ground_velocity.x * speed
-	target_velocity.z = ground_velocity.y * speed
+func trigger_direction(dir: Vector2) -> void:
+	target_velocity.x = dir.x * speed
+	target_velocity.z = dir.y * speed
 
-	# Vertical Velocity
-	if is_on_floor():
-		if Input.is_action_just_pressed("jump"):
-			target_velocity.y = jump_impulse
-	else:
+
+func _physics_process(delta: float) -> void:
+	# Gravity
+	if not is_on_floor():
 		target_velocity.y = target_velocity.y - (fall_acceleration * delta)
 
 	# Moving the Character
 	velocity = target_velocity
 	move_and_slide()
+
+
+func _on_dir_changed(dir: Vector2) -> void:
+	trigger_direction(dir)
+
+
+func _on_main_action() -> void:
+	trigger_jump()

+ 9 - 2
first3d/component/entity/walker/walker.tscn

@@ -1,7 +1,8 @@
-[gd_scene load_steps=4 format=3 uid="uid://b10k58capm04o"]
+[gd_scene load_steps=5 format=3 uid="uid://b10k58capm04o"]
 
 [ext_resource type="Script" path="res://component/entity/walker/walker.gd" id="1_xg6by"]
 [ext_resource type="PackedScene" uid="uid://bt8on6t7lqtrt" path="res://component/entity/walker/BasicPlayer.glb" id="2_p2xjj"]
+[ext_resource type="Script" path="res://flow/control/local_input/local_input.gd" id="3_ya20m"]
 
 [sub_resource type="BoxShape3D" id="BoxShape3D_6q2sk"]
 size = Vector3(0.56, 0.8, 0.4)
@@ -11,8 +12,14 @@ collision_layer = 2
 collision_mask = 5
 script = ExtResource("1_xg6by")
 
-[node name="BasicPlayer" parent="." instance=ExtResource("2_p2xjj")]
+[node name="BasicPlayerModel" parent="." instance=ExtResource("2_p2xjj")]
 
 [node name="Shape" type="CollisionShape3D" parent="."]
 transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.4, 0)
 shape = SubResource("BoxShape3D_6q2sk")
+
+[node name="Control" type="Node" parent="."]
+script = ExtResource("3_ya20m")
+
+[connection signal="dir_changed" from="Control" to="." method="_on_dir_changed"]
+[connection signal="main_action" from="Control" to="." method="_on_main_action"]

+ 13 - 0
first3d/flow/control/local_input/local_input.gd

@@ -0,0 +1,13 @@
+class_name LocalInput
+extends Node
+# Sends commands from a local device
+
+signal dir_changed(new_dir: Vector2)
+signal main_action
+
+func _unhandled_input(event: InputEvent) -> void:
+	if event.is_action_pressed("jump"):
+		main_action.emit()
+	elif event is InputEventJoypadMotion:
+		var dir: Vector2 = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
+		dir_changed.emit(dir)