Add joystick input

This commit is contained in:
karl 2021-06-24 13:34:25 +02:00
parent 0f90dd61f2
commit 15b8a20493
4 changed files with 67 additions and 17 deletions

40
DeadzoneInput.gd Normal file
View File

@ -0,0 +1,40 @@
extends Object
class_name DeadzoneInput
static func get_input(type, outer_deadzone, inner_deadzone, min_length = 0.0, normalized = true):
var input = Vector2(Input.get_action_strength(type + "_up") -
Input.get_action_strength(type + "_down"),
Input.get_action_strength(type + "_right") -
Input.get_action_strength(type + "_left"))
# Remove signs to reduce the number of cases
var signs = Vector2(sign(input.x), sign(input.y))
input = Vector2(abs(input.x), abs(input.y))
if input.length() < min_length:
return Vector2.ZERO
# Deazones for each axis
if input.x > outer_deadzone:
input.x = 1.0
elif input.x < inner_deadzone:
input.x = 0.0
else:
input.x = inverse_lerp(inner_deadzone, outer_deadzone, input.x)
if input.y > outer_deadzone:
input.y = 1.0
elif input.y < inner_deadzone:
input.y = 0.0
else:
input.y = inverse_lerp(inner_deadzone, outer_deadzone, input.y)
# Re-apply signs
input *= signs
# Limit at length 1
if normalized and input.length() > 1.0:
input /= input.length()
return input

View File

@ -111,15 +111,11 @@ func _physics_process(delta):
var move_velocity := Vector3.ZERO
var move_acceleration := Vector3.ZERO
# Movement and rotation
if Input.is_action_pressed("move_up"):
move_acceleration.z -= move_accel
if Input.is_action_pressed("move_down"):
move_acceleration.z += move_accel
if Input.is_action_pressed("move_left"):
rotate(transform.basis.y, delta * rotate_speed)
if Input.is_action_pressed("move_right"):
rotate(transform.basis.y, -delta * rotate_speed)
# Apply input
var input = DeadzoneInput.get_input("move", 0.65, 0.2, 0.0, false)
print(input)
move_acceleration.z += input.x * -move_accel
rotate(transform.basis.y, delta * rotate_speed * -input.y)
# Make movement local
move_acceleration = transform.basis * move_acceleration
@ -127,8 +123,6 @@ func _physics_process(delta):
# Get acceleration caused by gravity
var gravity_acceleration = get_gravity_acceleration()
# FIXME: Consider setting the gravity_acceleration to 0 if on ground
# Apply both acceleration types
apply_acceleration((move_acceleration + gravity_acceleration) * delta)

View File

@ -15,7 +15,7 @@
[sub_resource type="CubeMesh" id=1]
size = Vector3( 10, 0.5, 10 )
[sub_resource type="ConcavePolygonShape" id=12]
[sub_resource type="ConcavePolygonShape" id=2]
data = PoolVector3Array( -5, 0.25, 5, 5, 0.25, 5, -5, -0.25, 5, 5, 0.25, 5, 5, -0.25, 5, -5, -0.25, 5, 5, 0.25, -5, -5, 0.25, -5, 5, -0.25, -5, -5, 0.25, -5, -5, -0.25, -5, 5, -0.25, -5, 5, 0.25, 5, 5, 0.25, -5, 5, -0.25, 5, 5, 0.25, -5, 5, -0.25, -5, 5, -0.25, 5, -5, 0.25, -5, -5, 0.25, 5, -5, -0.25, -5, -5, 0.25, 5, -5, -0.25, 5, -5, -0.25, -5, 5, 0.25, 5, -5, 0.25, 5, 5, 0.25, -5, -5, 0.25, 5, -5, 0.25, -5, 5, 0.25, -5, -5, -0.25, 5, 5, -0.25, 5, -5, -0.25, -5, 5, -0.25, 5, 5, -0.25, -5, -5, -0.25, -5 )
[sub_resource type="PhysicsMaterial" id=3]
@ -118,7 +118,7 @@ mesh = SubResource( 1 )
material/0 = null
[node name="CollisionShape" type="CollisionShape" parent="MovingPlatformPivot/MovingPlatform"]
shape = SubResource( 12 )
shape = SubResource( 2 )
[node name="Planets" type="Spatial" parent="."]
script = ExtResource( 2 )

View File

@ -9,6 +9,11 @@
config_version=4
_global_script_classes=[ {
"base": "Object",
"class": "DeadzoneInput",
"language": "GDScript",
"path": "res://DeadzoneInput.gd"
}, {
"base": "KinematicBody",
"class": "MovingPlatform",
"language": "GDScript",
@ -25,6 +30,7 @@ _global_script_classes=[ {
"path": "res://Planets.gd"
} ]
_global_script_class_icons={
"DeadzoneInput": "",
"MovingPlatform": "",
"Planet": "",
"SolarSystem": ""
@ -63,28 +69,38 @@ ui_down={
]
}
move_up={
"deadzone": 0.5,
"deadzone": 0.0,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null)
]
}
move_down={
"deadzone": 0.5,
"deadzone": 0.0,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":1.0,"script":null)
]
}
move_left={
"deadzone": 0.5,
"deadzone": 0.0,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":-1.0,"script":null)
]
}
move_right={
"deadzone": 0.5,
"deadzone": 0.0,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":1.0,"script":null)
]
}
jump={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null)
]
}
movement_modifier={
"deadzone": 0.5,
"events": [ Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":6,"pressure":0.0,"pressed":false,"script":null)
]
}