diff options
author | Daniel Weipert <code@drogueronin.de> | 2021-02-14 14:53:17 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2021-02-14 14:53:17 +0100 |
commit | 7115379ccf5714816eeb91fc16d3a63e019c8082 (patch) | |
tree | f28c7595ca3f1324bcffadd40027df6c46f7d321 | |
parent | 332bab8de321d0358b30d5bf159cbda512c14852 (diff) |
Adds user configurable key bindings
-rw-r--r-- | Characters/Character.gd | 18 | ||||
-rw-r--r-- | Enum.gd | 6 | ||||
-rw-r--r-- | Global.gd | 6 | ||||
-rw-r--r-- | Levels/Level.gd | 5 | ||||
-rw-r--r-- | Network/Lobby.tscn | 7 | ||||
-rw-r--r-- | UI/Menu/KeyBindings.gd | 71 | ||||
-rw-r--r-- | UI/Menu/KeyBindings.tscn | 123 | ||||
-rw-r--r-- | UI/Menu/Menu.gd | 15 | ||||
-rw-r--r-- | UI/Menu/Menu.tscn | 41 | ||||
-rw-r--r-- | UI/Menu/MenuButton.gd | 6 | ||||
-rw-r--r-- | UI/Menu/MenuButton.tscn | 15 | ||||
-rw-r--r-- | project.godot | 34 |
12 files changed, 332 insertions, 15 deletions
diff --git a/Characters/Character.gd b/Characters/Character.gd index a60046b..175094d 100644 --- a/Characters/Character.gd +++ b/Characters/Character.gd @@ -14,7 +14,7 @@ var jumpHeightModifier = 0 func _physics_process(_delta): - if Input.is_action_pressed("ui_left"): + if Input.is_action_pressed("DIRECTION_LEFT"): velocity.x = -SPEED direction = Enum.DIRECTION.LEFT $Sprite.flip_h = true @@ -23,7 +23,7 @@ func _physics_process(_delta): $Sprite.play("run") else: $Sprite.play("walk") - elif Input.is_action_pressed("ui_right"): + elif Input.is_action_pressed("DIRECTION_RIGHT"): velocity.x = SPEED direction = Enum.DIRECTION.RIGHT $Sprite.flip_h = false @@ -62,7 +62,7 @@ func _physics_process(_delta): # jump - if Input.is_action_just_pressed("ui_up"): + if Input.is_action_just_pressed("JUMP"): if is_on_floor(): canDoubleJump = true velocity.y = JUMPFORCE @@ -70,11 +70,11 @@ func _physics_process(_delta): elif not is_on_floor() and not is_on_wall() and canDoubleJump: canDoubleJump = false velocity.y = JUMPFORCE - if Input.is_action_pressed("ui_up"): + if Input.is_action_pressed("JUMP"): jumpHeightModifier += 10 if jumpHeightModifier > 0: jumpHeightModifier = 0 - if Input.is_action_just_released("ui_up"): + if Input.is_action_just_released("JUMP"): velocity.y -= jumpHeightModifier @@ -82,14 +82,14 @@ func _physics_process(_delta): velocity.y *= 0.8 $Sprite.play("wall") - if Input.is_action_just_pressed("ui_up"): + if Input.is_action_just_pressed("JUMP"): velocity.y = JUMPFORCE * 0.75 if direction == Enum.DIRECTION.LEFT: - Input.action_release("ui_left") + Input.action_release("DIRECTION_LEFT") velocity.x = 2000 $Sprite.flip_h = false elif direction == Enum.DIRECTION.RIGHT: - Input.action_release("ui_right") + Input.action_release("DIRECTION_RIGHT") velocity.x = -2000 $Sprite.flip_h = true @@ -99,7 +99,7 @@ func _physics_process(_delta): func is_running(): - return Input.is_action_pressed("ui_accept") and is_on_floor() + return Input.is_action_pressed("RUN") and is_on_floor() func check_flag(): @@ -9,3 +9,9 @@ const DIRECTION = { enum PLAYER { FIRST, SECOND, THIRD, } + +enum ACTIONS { + DIRECTION_LEFT, DIRECTION_RIGHT, + JUMP, + RUN, +} @@ -169,3 +169,9 @@ func check_win_timer(): if has_won: emit_signal("game_won", Network.player) + + +func open_menu(): + var Menu = load("res://UI/Menu/Menu.tscn").instance() + get_tree().paused = true + get_tree().current_scene.add_child(Menu) diff --git a/Levels/Level.gd b/Levels/Level.gd index 6777854..2e623e5 100644 --- a/Levels/Level.gd +++ b/Levels/Level.gd @@ -25,6 +25,11 @@ func _process(delta): self.LabelTimer.text = "%.2fs" % self.timer +func _input(event): + if event is InputEventKey and event.scancode == KEY_ESCAPE: + Global.open_menu() + + func set_player(character: KinematicBody2D): character.position.x = $Character.position.x character.position.y = $Character.position.y diff --git a/Network/Lobby.tscn b/Network/Lobby.tscn index 7e8c52a..cbe5be9 100644 --- a/Network/Lobby.tscn +++ b/Network/Lobby.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=3 format=2] [ext_resource path="res://Network/Lobby.gd" type="Script" id=1] +[ext_resource path="res://UI/Menu/MenuButton.tscn" type="PackedScene" id=2] [node name="Lobby" type="Control"] anchor_right = 1.0 @@ -118,6 +119,10 @@ text = "<" visible = true margin_right = 83.0 margin_bottom = 58.0 + +[node name="MenuButton" parent="." instance=ExtResource( 2 )] +margin_left = 976.0 +margin_right = 0.0 [connection signal="pressed" from="Connect/Host" to="." method="_on_Host_pressed"] [connection signal="pressed" from="Connect/Join" to="." method="_on_Join_pressed"] [connection signal="pressed" from="Players/Start" to="." method="_on_Start_pressed"] diff --git a/UI/Menu/KeyBindings.gd b/UI/Menu/KeyBindings.gd new file mode 100644 index 0000000..4650be9 --- /dev/null +++ b/UI/Menu/KeyBindings.gd @@ -0,0 +1,71 @@ +extends Control +# @see https://www.gotut.net/godot-key-bindings-tutorial/ + + +var input_action = null + + +func _ready(): + self.set_keys() + + +func _input(event): + if self.is_input_event(event) and input_action != null: + self.change_key(self.input_action, event) + + +func is_input_event(event): + # TODO: read up on proper controller input handling + return event is InputEventKey or event is InputEventJoypadButton or event is InputEventJoypadMotion + + +func set_keys(): + for action in Enum.ACTIONS: + var input = get_node("Panel/VBoxContainer/" + str(action) + "/Input") + input.set_pressed(false) + + var action_list = InputMap.get_action_list(action) + if action_list.empty(): + input.set_text("SET BUTTON") + else: + var text = "" + for idx in range(action_list.size()): + var btn = action_list[idx] + var btn_text = btn.as_text() + + if btn is InputEventJoypadButton: + btn_text = "BtnIdx: " + str(btn.button_index) + elif btn is InputEventJoypadMotion: + btn_text = "JoyAxis: " + str(btn.axis_value) + + if idx == 0: + text += btn_text + else: + text += ", " + btn_text + + input.set_text(text) + + +func change_key(action, key): + if key is InputEventJoypadMotion: + if key.get_axis_value() > 0: + key.set_axis_value(1) + else: + key.set_axis_value(-1) + + InputMap.action_add_event(action, key) + self.input_action = null + self.set_keys() + + +func clear_keys(action): + InputMap.action_erase_events(action) + self.set_keys() + + +func _on_Clear_pressed(action): + self.clear_keys(action) + + +func _on_Input_pressed(action): + self.input_action = action diff --git a/UI/Menu/KeyBindings.tscn b/UI/Menu/KeyBindings.tscn new file mode 100644 index 0000000..948b44b --- /dev/null +++ b/UI/Menu/KeyBindings.tscn @@ -0,0 +1,123 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://UI/Menu/KeyBindings.gd" type="Script" id=1] + +[node name="KeyBindings" type="Control"] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_left = 64.0 +margin_top = 64.0 +margin_right = -64.0 +margin_bottom = -88.0 +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Panel" type="Panel" parent="."] +margin_right = 896.0 +margin_bottom = 448.0 + +[node name="VBoxContainer" type="VBoxContainer" parent="Panel"] +margin_left = 256.0 +margin_top = 24.0 +margin_right = 640.0 +margin_bottom = 424.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="DIRECTION_LEFT" type="HBoxContainer" parent="Panel/VBoxContainer"] +margin_right = 384.0 +margin_bottom = 20.0 + +[node name="Label" type="Label" parent="Panel/VBoxContainer/DIRECTION_LEFT"] +margin_top = 3.0 +margin_right = 24.0 +margin_bottom = 17.0 +text = "Left" + +[node name="Input" type="Button" parent="Panel/VBoxContainer/DIRECTION_LEFT"] +margin_left = 28.0 +margin_right = 40.0 +margin_bottom = 20.0 + +[node name="Clear" type="Button" parent="Panel/VBoxContainer/DIRECTION_LEFT"] +margin_left = 44.0 +margin_right = 88.0 +margin_bottom = 20.0 +text = "Clear" + +[node name="DIRECTION_RIGHT" type="HBoxContainer" parent="Panel/VBoxContainer"] +margin_top = 24.0 +margin_right = 384.0 +margin_bottom = 44.0 + +[node name="Label" type="Label" parent="Panel/VBoxContainer/DIRECTION_RIGHT"] +margin_top = 3.0 +margin_right = 32.0 +margin_bottom = 17.0 +text = "Right" + +[node name="Input" type="Button" parent="Panel/VBoxContainer/DIRECTION_RIGHT"] +margin_left = 36.0 +margin_right = 48.0 +margin_bottom = 20.0 + +[node name="Clear" type="Button" parent="Panel/VBoxContainer/DIRECTION_RIGHT"] +margin_left = 52.0 +margin_right = 96.0 +margin_bottom = 20.0 +text = "Clear" + +[node name="JUMP" type="HBoxContainer" parent="Panel/VBoxContainer"] +margin_top = 48.0 +margin_right = 384.0 +margin_bottom = 68.0 + +[node name="Label" type="Label" parent="Panel/VBoxContainer/JUMP"] +margin_top = 3.0 +margin_right = 32.0 +margin_bottom = 17.0 +text = "Jump" + +[node name="Input" type="Button" parent="Panel/VBoxContainer/JUMP"] +margin_left = 36.0 +margin_right = 48.0 +margin_bottom = 20.0 + +[node name="Clear" type="Button" parent="Panel/VBoxContainer/JUMP"] +margin_left = 52.0 +margin_right = 96.0 +margin_bottom = 20.0 +text = "Clear" + +[node name="RUN" type="HBoxContainer" parent="Panel/VBoxContainer"] +margin_top = 72.0 +margin_right = 384.0 +margin_bottom = 92.0 + +[node name="Label" type="Label" parent="Panel/VBoxContainer/RUN"] +margin_top = 3.0 +margin_right = 24.0 +margin_bottom = 17.0 +text = "Run" + +[node name="Input" type="Button" parent="Panel/VBoxContainer/RUN"] +margin_left = 28.0 +margin_right = 40.0 +margin_bottom = 20.0 + +[node name="Clear" type="Button" parent="Panel/VBoxContainer/RUN"] +margin_left = 44.0 +margin_right = 88.0 +margin_bottom = 20.0 +text = "Clear" +[connection signal="pressed" from="Panel/VBoxContainer/DIRECTION_LEFT/Input" to="." method="_on_Input_pressed" binds= [ "DIRECTION_LEFT" ]] +[connection signal="pressed" from="Panel/VBoxContainer/DIRECTION_LEFT/Clear" to="." method="_on_Clear_pressed" binds= [ "DIRECTION_LEFT" ]] +[connection signal="pressed" from="Panel/VBoxContainer/DIRECTION_RIGHT/Input" to="." method="_on_Input_pressed" binds= [ "DIRECTION_RIGHT" ]] +[connection signal="pressed" from="Panel/VBoxContainer/DIRECTION_RIGHT/Clear" to="." method="_on_Clear_pressed" binds= [ "DIRECTION_RIGHT" ]] +[connection signal="pressed" from="Panel/VBoxContainer/JUMP/Input" to="." method="_on_Input_pressed" binds= [ "JUMP" ]] +[connection signal="pressed" from="Panel/VBoxContainer/JUMP/Clear" to="." method="_on_Clear_pressed" binds= [ "JUMP" ]] +[connection signal="pressed" from="Panel/VBoxContainer/RUN/Input" to="." method="_on_Input_pressed" binds= [ "RUN" ]] +[connection signal="pressed" from="Panel/VBoxContainer/RUN/Clear" to="." method="_on_Clear_pressed" binds= [ "RUN" ]] diff --git a/UI/Menu/Menu.gd b/UI/Menu/Menu.gd new file mode 100644 index 0000000..400f669 --- /dev/null +++ b/UI/Menu/Menu.gd @@ -0,0 +1,15 @@ +extends CanvasLayer +# needs to be a CanvasLayer as parent so it's drawn on top + + +func _ready(): + self.pause_mode = PAUSE_MODE_PROCESS + + +func close(): + get_tree().paused = false + self.queue_free() + + +func _on_Close_pressed(): + self.close() diff --git a/UI/Menu/Menu.tscn b/UI/Menu/Menu.tscn new file mode 100644 index 0000000..69c14c2 --- /dev/null +++ b/UI/Menu/Menu.tscn @@ -0,0 +1,41 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://UI/Menu/Menu.gd" type="Script" id=1] +[ext_resource path="res://UI/Menu/KeyBindings.tscn" type="PackedScene" id=2] + +[sub_resource type="StyleBoxFlat" id=3] +bg_color = Color( 0, 0, 0, 0.882353 ) + +[sub_resource type="StyleBoxFlat" id=2] +bg_color = Color( 1, 1, 1, 0 ) +border_width_left = 1 +border_width_top = 1 +border_width_right = 1 +border_width_bottom = 1 + +[node name="Menu" type="CanvasLayer"] +pause_mode = 2 +script = ExtResource( 1 ) + +[node name="Container" type="Panel" parent="."] +margin_right = 1024.0 +margin_bottom = 600.0 +custom_styles/panel = SubResource( 3 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Close" type="Button" parent="Container"] +margin_left = 976.0 +margin_right = 1023.0 +margin_bottom = 20.0 +custom_styles/normal = SubResource( 2 ) +text = "Close" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="KeyBindings" parent="Container" instance=ExtResource( 2 )] +margin_right = 24.0 +margin_bottom = 24.0 +[connection signal="pressed" from="Container/Close" to="." method="_on_Close_pressed"] diff --git a/UI/Menu/MenuButton.gd b/UI/Menu/MenuButton.gd new file mode 100644 index 0000000..e116acb --- /dev/null +++ b/UI/Menu/MenuButton.gd @@ -0,0 +1,6 @@ +extends Button + + +# freeze current scene and overlay Menu.tscn +func _on_MenuButton_pressed(): + Global.open_menu() diff --git a/UI/Menu/MenuButton.tscn b/UI/Menu/MenuButton.tscn new file mode 100644 index 0000000..b5626fb --- /dev/null +++ b/UI/Menu/MenuButton.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://UI/Menu/MenuButton.gd" type="Script" id=1] + +[node name="MenuButton" type="Button"] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_right = -976.0 +margin_bottom = -580.0 +text = "Menu" +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} +[connection signal="pressed" from="." to="." method="_on_MenuButton_pressed"] diff --git a/project.godot b/project.godot index f3b7238..3f9fe89 100644 --- a/project.godot +++ b/project.godot @@ -32,7 +32,6 @@ ui_accept={ "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":16777221,"unicode":0,"echo":false,"script":null) , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777222,"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) -, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":67,"unicode":0,"echo":false,"script":null) , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777237,"unicode":0,"echo":false,"script":null) ] } @@ -40,14 +39,12 @@ ui_left={ "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":16777231,"unicode":0,"echo":false,"script":null) , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":14,"pressure":0.0,"pressed":false,"script":null) -, 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) ] } ui_right={ "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":16777233,"unicode":0,"echo":false,"script":null) , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":15,"pressure":0.0,"pressed":false,"script":null) -, 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) ] } ui_up={ @@ -55,14 +52,41 @@ ui_up={ "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":16777232,"unicode":0,"echo":false,"script":null) , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null) , 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(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) ] } ui_down={ "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":16777234,"unicode":0,"echo":false,"script":null) , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null) -, 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) + ] +} +DIRECTION_LEFT={ +"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":16777231,"unicode":0,"echo":false,"script":null) +, 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(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":14,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":-1.0,"script":null) + ] +} +DIRECTION_RIGHT={ +"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":16777233,"unicode":0,"echo":false,"script":null) +, 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(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":15,"pressure":0.0,"pressed":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":16777232,"unicode":0,"echo":false,"script":null) +, 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(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null) + ] +} +RUN={ +"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":67,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":7,"pressure":0.0,"pressed":false,"script":null) ] } |