feat: implement pause menu with loadout management and rebindable input settings
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
extends Node
|
||||
|
||||
const SAVE_PATH = "user://settings.json"
|
||||
|
||||
var default_bindings = {
|
||||
"jump": {"type": "key", "code": KEY_SPACE},
|
||||
"crouch": {"type": "key", "code": KEY_CTRL},
|
||||
"dash": {"type": "key", "code": KEY_SHIFT},
|
||||
"fire": {"type": "mouse", "code": MOUSE_BUTTON_LEFT},
|
||||
"fire_alt": {"type": "mouse", "code": MOUSE_BUTTON_RIGHT},
|
||||
"reload": {"type": "key", "code": KEY_R},
|
||||
"interact": {"type": "key", "code": KEY_E},
|
||||
"move_forward": {"type": "key", "code": KEY_W},
|
||||
"move_back": {"type": "key", "code": KEY_S},
|
||||
"move_left": {"type": "key", "code": KEY_A},
|
||||
"move_right": {"type": "key", "code": KEY_D},
|
||||
"weapon_next": {"type": "mouse", "code": MOUSE_BUTTON_WHEEL_UP},
|
||||
"weapon_prev": {"type": "mouse", "code": MOUSE_BUTTON_WHEEL_DOWN},
|
||||
"weapon_1": {"type": "key", "code": KEY_1},
|
||||
"weapon_2": {"type": "key", "code": KEY_2},
|
||||
"weapon_3": {"type": "key", "code": KEY_3},
|
||||
}
|
||||
|
||||
var current_bindings = {}
|
||||
|
||||
func _ready() -> void:
|
||||
# Initialize defaults
|
||||
current_bindings = default_bindings.duplicate(true)
|
||||
|
||||
# Load overrides
|
||||
_load_settings()
|
||||
|
||||
# Apply to InputMap
|
||||
_apply_all_bindings()
|
||||
|
||||
func _apply_all_bindings() -> void:
|
||||
for action in current_bindings:
|
||||
if not InputMap.has_action(action):
|
||||
InputMap.add_action(action)
|
||||
InputMap.action_erase_events(action)
|
||||
|
||||
var binding = current_bindings[action]
|
||||
var ev: InputEvent
|
||||
if binding["type"] == "key":
|
||||
ev = InputEventKey.new()
|
||||
ev.keycode = binding["code"]
|
||||
elif binding["type"] == "mouse":
|
||||
ev = InputEventMouseButton.new()
|
||||
ev.button_index = binding["code"]
|
||||
|
||||
if ev:
|
||||
InputMap.action_add_event(action, ev)
|
||||
|
||||
func rebind_action(action: String, event: InputEvent) -> void:
|
||||
if not current_bindings.has(action):
|
||||
return
|
||||
|
||||
if event is InputEventKey:
|
||||
var code = event.physical_keycode if event.physical_keycode != 0 else event.keycode
|
||||
current_bindings[action] = {"type": "key", "code": code}
|
||||
elif event is InputEventMouseButton:
|
||||
current_bindings[action] = {"type": "mouse", "code": event.button_index}
|
||||
else:
|
||||
return
|
||||
|
||||
_apply_all_bindings()
|
||||
_save_settings()
|
||||
|
||||
func reset_to_defaults() -> void:
|
||||
current_bindings = default_bindings.duplicate(true)
|
||||
_apply_all_bindings()
|
||||
_save_settings()
|
||||
|
||||
func _save_settings() -> void:
|
||||
var save_data = {
|
||||
"bindings": current_bindings
|
||||
}
|
||||
var json_string = JSON.stringify(save_data)
|
||||
var file = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
|
||||
if file:
|
||||
file.store_string(json_string)
|
||||
file.close()
|
||||
|
||||
func _load_settings() -> void:
|
||||
if FileAccess.file_exists(SAVE_PATH):
|
||||
var file = FileAccess.open(SAVE_PATH, FileAccess.READ)
|
||||
if file:
|
||||
var json_string = file.get_as_text()
|
||||
file.close()
|
||||
|
||||
var json = JSON.new()
|
||||
var error = json.parse(json_string)
|
||||
if error == OK:
|
||||
var data = json.get_data()
|
||||
if data is Dictionary and data.has("bindings"):
|
||||
var loaded_bindings = data["bindings"]
|
||||
for action in loaded_bindings:
|
||||
if current_bindings.has(action):
|
||||
current_bindings[action] = loaded_bindings[action]
|
||||
@@ -0,0 +1 @@
|
||||
uid://b6fe1eyas7wxv
|
||||
Reference in New Issue
Block a user