Add delayed hook input detection

See InputContinuous, stores a hook input and generates input events for a few
frames, so the player can hook on a target even if they miss it by a small margin
This commit is contained in:
Nathan Lovato 2019-05-19 16:55:44 +09:00
parent 75b5a979a7
commit ae54ee9139
2 changed files with 55 additions and 6 deletions

View file

@ -23,11 +23,7 @@ var aim_mode: = false setget set_aim_mode
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("hook") and _can_hook():
cooldown.start()
arrow.hook_position = snap_detector.target.global_position if snap_detector.target else ray.get_collision_point()
if aim_mode:
self.aim_mode = false
emit_signal("hooked_onto_target", _get_hook_position())
_hook()
get_tree().set_input_as_handled()
if event.is_action_pressed("aim"):
@ -35,6 +31,14 @@ func _unhandled_input(event: InputEvent) -> void:
get_tree().set_input_as_handled()
func _hook() -> void:
cooldown.start()
arrow.hook_position = snap_detector.target.global_position if snap_detector.target else ray.get_collision_point()
if aim_mode:
self.aim_mode = false
emit_signal("hooked_onto_target", _get_hook_position())
func set_aim_mode(value:bool) -> void:
aim_mode = value
@ -73,6 +77,21 @@ func _get_hook_position() -> Vector2:
func _get_aim_direction() -> Vector2:
match Settings.controls:
Settings.GAMEPAD:
return ControlUtils.get_aim_joystick_direction()
return get_aim_joystick_direction()
Settings.KBD_MOUSE, _:
return (get_global_mouse_position() - global_position).normalized()
# FIXME
static func get_aim_joystick_direction() -> Vector2:
var use_right_stick: bool = ProjectSettings.get_setting('debug/testing/controls/use_right_stick')
# FIXME: axes should be 2 and 3, or RX and RY, is there a calibration issue with the gamepad?
if use_right_stick:
return Vector2(
Input.get_joy_axis(0, JOY_AXIS_3),
Input.get_joy_axis(0, JOY_AXIS_4)).normalized()
else:
return Vector2(
Input.get_joy_axis(0, JOY_AXIS_0),
Input.get_joy_axis(0, JOY_AXIS_1)).normalized()

View file

@ -0,0 +1,30 @@
"""
Generates input events continuously based on a timer
Allows the player to hook onto a target even if they pressed the hook key before the hook was in range
"""
extends Node
export var timer_duration: = 0.05
const ACTION_HOOK: = 'hook'
var _action_names: = []
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed(ACTION_HOOK) and not ACTION_HOOK in _action_names:
_action_names.append(ACTION_HOOK)
_remove_delayed(_action_names.size() - 1, timer_duration)
func _physics_process(delta: float) -> void:
for action_name in _action_names:
var event: = InputEventAction.new()
event.action = action_name
event.pressed = true
Input.parse_input_event(event)
# Coroutine
func _remove_delayed(action_id:int, delay:float) -> void:
yield(get_tree().create_timer(delay), "timeout")
_action_names.remove(action_id)