Refactor rhythm action architecture
This commit is contained in:
65
scenes/components/input_component.gd
Normal file
65
scenes/components/input_component.gd
Normal file
@@ -0,0 +1,65 @@
|
||||
class_name InputComponent
|
||||
extends Node
|
||||
|
||||
const InputIntentScript := preload("res://scenes/components/input_intent.gd")
|
||||
|
||||
signal intent_created(intent)
|
||||
signal combo_pressed(symbol: StringName, rhythm_action: StringName)
|
||||
signal combo_released(symbol: StringName)
|
||||
|
||||
const COMBO_ACTIONS: Dictionary = {
|
||||
&"combo_w": [&"W", &"w"],
|
||||
&"combo_a": [&"A", &"a"],
|
||||
&"combo_d": [&"D", &"d"],
|
||||
&"combo_s": [&"S", &"s"],
|
||||
&"combo_space": [&"SP", &"space"],
|
||||
}
|
||||
|
||||
const COMBO_ACTION_ORDER: Array[StringName] = [
|
||||
&"combo_w",
|
||||
&"combo_a",
|
||||
&"combo_d",
|
||||
&"combo_s",
|
||||
&"combo_space",
|
||||
]
|
||||
|
||||
var _suppressed_movement: Dictionary = {
|
||||
&"move_left": false,
|
||||
&"move_right": false,
|
||||
}
|
||||
|
||||
|
||||
func handle_input_event(event: InputEvent) -> bool:
|
||||
var key_event := event as InputEventKey
|
||||
if key_event != null and key_event.echo:
|
||||
return false
|
||||
for action_name: StringName in COMBO_ACTION_ORDER:
|
||||
if event.is_action_pressed(action_name, false, true):
|
||||
var data: Array = COMBO_ACTIONS[action_name]
|
||||
var intent: RefCounted = InputIntentScript.create(data[0], data[1], &"pressed", float(Time.get_ticks_msec()))
|
||||
intent_created.emit(intent)
|
||||
combo_pressed.emit(data[0], data[1])
|
||||
return true
|
||||
if event.is_action_released(action_name, true):
|
||||
var data: Array = COMBO_ACTIONS[action_name]
|
||||
var intent: RefCounted = InputIntentScript.create(data[0], data[1], &"released", float(Time.get_ticks_msec()))
|
||||
intent_created.emit(intent)
|
||||
combo_released.emit(data[0])
|
||||
return true
|
||||
return false
|
||||
|
||||
|
||||
func set_direction_suppressed(symbol: StringName, suppressed: bool) -> void:
|
||||
if symbol == &"A":
|
||||
_suppressed_movement[&"move_left"] = suppressed
|
||||
elif symbol == &"D":
|
||||
_suppressed_movement[&"move_right"] = suppressed
|
||||
|
||||
|
||||
func get_horizontal_axis() -> float:
|
||||
var axis := 0.0
|
||||
if Input.is_action_pressed(&"move_left") and not bool(_suppressed_movement.get(&"move_left", false)):
|
||||
axis -= 1.0
|
||||
if Input.is_action_pressed(&"move_right") and not bool(_suppressed_movement.get(&"move_right", false)):
|
||||
axis += 1.0
|
||||
return axis
|
||||
Reference in New Issue
Block a user