Skip to content

Made keyboard inputs physical and add controller support #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 84 additions & 79 deletions player/Controls.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class_name Controls
@export var max_pitch: float = 75
@export var zoom_step: float = .05
@export var sensitivity: float = 0.1
@export var controller_sensitivity: float = 3.0

@onready var _mobile_controls = $MobileControls

Expand All @@ -23,108 +24,112 @@ var _is_swimming_down: bool = false
var _is_surging: bool = false

func _ready():
# wait until the parent node is ready
await get_parent().ready

# OS.has_touchscreen_ui_hint() reports true for anything with touch support including HTML5
# which isn't exactly what we want when it comes down to enabling touchscreen controls
# so instead we check if the OS name is Android or iOS, and if so, we enable touch controls
var os_name := OS.get_name()
if os_name == "Android" || os_name == "iOS":
_is_touchscreen = true

# if touchscreen controls are disabled we capture the mouse in the game window
if !_is_touchscreen:
_is_capturing = true
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
# otherwise we just make the mobile controls node visible
_mobile_controls.visible = true
# wait until the parent node is ready
await get_parent().ready

# OS.has_touchscreen_ui_hint() reports true for anything with touch support including HTML5
# which isn't exactly what we want when it comes down to enabling touchscreen controls
# so instead we check if the OS name is Android or iOS, and if so, we enable touch controls
var os_name := OS.get_name()
if os_name == "Android" || os_name == "iOS":
_is_touchscreen = true

# if touchscreen controls are disabled we capture the mouse in the game window
if !_is_touchscreen:
_is_capturing = true
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
# otherwise we just make the mobile controls node visible
_mobile_controls.visible = true

func _process(delta):
# if the mouse cursor is being captured in the game window
if _is_capturing:
# determine the movement direction based checked the input strengths of the four movement directions
var dx := Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
var dy := Input.get_action_strength("move_forward") - Input.get_action_strength("move_backwards")

# and set the movement direction vector to the normalized vector so the player can't unintentionally
# move faster when moving diagonally
_move_vec = Vector2(dx, -dy).normalized()
elif _is_touchscreen:
# othwerise if we're checked a touchscreen device, get the movement direction, camera rotation and
# zoom scale values from the mobile controls node
_move_vec = _mobile_controls.get_movement_vector()
_cam_rot = _mobile_controls.get_camera_rotation()
_zoom_scale = _mobile_controls.get_zoom_scale()

# in both desktop and touch screen devices the jump flag can be determined via the jump action
# same goes for other actions
_is_jumping = Input.is_action_just_pressed("jump")
_is_sprinting = Input.is_action_pressed("sprint")
_is_dashing = Input.is_action_pressed("dash")
_is_crouching = Input.is_action_pressed("crouch")
_is_swimming_up = Input.is_action_pressed("swim_up")
_is_swimming_down = Input.is_action_pressed("swim_down")
_is_surging = Input.is_action_pressed("surge")
# if the mouse cursor is being captured in the game window
if _is_capturing:
# determine the movement direction based checked the input strengths of the four movement directions
var dx := Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
var dy := Input.get_action_strength("move_forward") - Input.get_action_strength("move_backwards")
# rotate the camera with controller
_cam_rot -= Input.get_vector("pan_left", "pan_right", "pan_up", "pan_down") * controller_sensitivity
_cam_rot.y = clamp(_cam_rot.y, min_pitch, max_pitch)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this interfere with the mouse controls? Can we still use the mouse without an issue even if a controller is plugged in or not?


# and set the movement direction vector to the normalized vector so the player can't unintentionally
# move faster when moving diagonally
_move_vec = Vector2(dx, -dy).normalized()
elif _is_touchscreen:
# othwerise if we're checked a touchscreen device, get the movement direction, camera rotation and
# zoom scale values from the mobile controls node
_move_vec = _mobile_controls.get_movement_vector()
_cam_rot = _mobile_controls.get_camera_rotation()
_zoom_scale = _mobile_controls.get_zoom_scale()

# in both desktop and touch screen devices the jump flag can be determined via the jump action
# same goes for other actions
_is_jumping = Input.is_action_just_pressed("jump")
_is_sprinting = Input.is_action_pressed("sprint")
_is_dashing = Input.is_action_pressed("dash")
_is_crouching = Input.is_action_pressed("crouch")
_is_swimming_up = Input.is_action_pressed("swim_up")
_is_swimming_down = Input.is_action_pressed("swim_down")
_is_surging = Input.is_action_pressed("surge")

func _input(event):
# checked non-touchscreen devices toggle the mouse cursor's capture mode when the ui_cancel action is
# pressed (e.g. the Esc key)
if Input.is_action_just_pressed("ui_cancel"):
_is_capturing = !_is_capturing

if _is_capturing:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

# if the mouse cursor is being captured, update the camera rotation using the relative movement
# and the sensitivity we defined earlier. also clamp the vertical camera rotation to the pitch
# range we defined earlier so we don't end up in weird look angles
if _is_capturing && event is InputEventMouseMotion:
_cam_rot.x -= event.relative.x * sensitivity
_cam_rot.y -= event.relative.y * sensitivity
_cam_rot.y = clamp(_cam_rot.y, min_pitch, max_pitch)

# if the mouse cursor is being captured, increse or decrease the zoom when the corresponding
# action has just been pressed
if _is_capturing:
if Input.is_action_just_pressed("zoom_in"):
_zoom_scale = clamp(_zoom_scale - zoom_step, 0, 1)
if Input.is_action_just_pressed("zoom_out"):
_zoom_scale = clamp(_zoom_scale + zoom_step, 0, 1)
# checked non-touchscreen devices toggle the mouse cursor's capture mode when the ui_cancel action is
# pressed (e.g. the Esc key)
if Input.is_action_just_pressed("ui_cancel"):
_is_capturing = !_is_capturing

if _is_capturing:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

# if the mouse cursor is being captured, update the camera rotation using the relative movement
# and the sensitivity we defined earlier. also clamp the vertical camera rotation to the pitch
# range we defined earlier so we don't end up in weird look angles
if _is_capturing:
if event is InputEventMouseMotion:
_cam_rot.x -= event.relative.x * sensitivity
_cam_rot.y -= event.relative.y * sensitivity
_cam_rot.y = clamp(_cam_rot.y, min_pitch, max_pitch)

# if the mouse cursor is being captured, increse or decrease the zoom when the corresponding
# action has just been pressed
if _is_capturing:
if Input.is_action_just_pressed("zoom_in"):
_zoom_scale = clamp(_zoom_scale - zoom_step, 0, 1)
if Input.is_action_just_pressed("zoom_out"):
_zoom_scale = clamp(_zoom_scale + zoom_step, 0, 1)

func get_movement_vector():
return _move_vec
return _move_vec

func is_jumping():
return _is_jumping
return _is_jumping

func is_sprinting():
return _is_sprinting
return _is_sprinting

func is_dashing():
return _is_dashing
return _is_dashing

func is_crouching():
return _is_crouching
return _is_crouching

func is_swimming_up():
return _is_swimming_up
return _is_swimming_up

func is_swimming_down():
return _is_swimming_down
return _is_swimming_down

func is_surging():
return _is_surging
return _is_surging

func get_camera_rotation():
return _cam_rot
return _cam_rot

func get_zoom_scale():
return _zoom_scale
return _zoom_scale

func set_zoom_scale(zoom_scale):
_zoom_scale = zoom_scale
_mobile_controls.set_zoom_scale(zoom_scale)
_zoom_scale = zoom_scale
_mobile_controls.set_zoom_scale(zoom_scale)
53 changes: 42 additions & 11 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,32 @@ window/size/height=768

move_forward={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":87,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
"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":87,"key_label":0,"unicode":119,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":1,"axis_value":-1.0,"script":null)
]
}
move_backwards={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":83,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
"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,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":1,"axis_value":1.0,"script":null)
]
}
move_left={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":65,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
"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":65,"key_label":0,"unicode":97,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":-1.0,"script":null)
]
}
move_right={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":68,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
"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,"echo":false,"script":null)
, 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":32,"physical_keycode":0,"unicode":32,"echo":false,"script":null)
"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":3,"pressure":0.0,"pressed":true,"script":null)
]
}
zoom_in={
Expand All @@ -121,32 +126,58 @@ zoom_out={
}
sprint={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194325,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
"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":1,"pressure":0.0,"pressed":true,"script":null)
]
}
dash={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":81,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
"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":81,"key_label":0,"unicode":113,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":4,"axis_value":1.0,"script":null)
]
}
crouch={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194326,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
"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":4194326,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":7,"pressure":0.0,"pressed":true,"script":null)
]
}
swim_up={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":32,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
"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":3,"pressure":0.0,"pressed":true,"script":null)
]
}
swim_down={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194326,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
"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":4194326,"key_label":0,"unicode":0,"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)
]
}
surge={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":81,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
"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":81,"key_label":0,"unicode":113,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":4,"axis_value":1.0,"script":null)
]
}
pan_up={
"deadzone": 0.5,
"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":3,"axis_value":-1.0,"script":null)
]
}
pan_down={
"deadzone": 0.5,
"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":3,"axis_value":1.0,"script":null)
]
}
pan_left={
"deadzone": 0.5,
"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":2,"axis_value":-1.0,"script":null)
]
}
pan_right={
"deadzone": 0.5,
"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":2,"axis_value":1.0,"script":null)
]
}

Expand Down