Demo: Init input via code to insure that demo works even when project.godot is not imported

This commit is contained in:
Serhii Snitsaruk 2024-02-15 22:34:32 +01:00
parent 5909735add
commit 91f3148493
3 changed files with 49 additions and 36 deletions

View File

@ -27,6 +27,7 @@ var attack_pressed: bool = false
func _ready() -> void:
super._ready()
can_dodge = true
_init_input_events()
_init_state_machine()
death.connect(func(): remove_from_group(&"player"))
@ -69,6 +70,28 @@ func _init_state_machine() -> void:
hsm.set_guard(_can_dodge)
func _init_input_events() -> void:
# Note: Ensures that input events are present even if project.godot wasn't imported.
_add_action(&"move_left", KEY_A)
_add_action(&"move_right", KEY_D)
_add_action(&"move_up", KEY_W)
_add_action(&"move_down", KEY_S)
_add_action(&"dodge", KEY_SPACE)
_add_action(&"attack", KEY_ENTER, KEY_F)
func _add_action(p_action: StringName, p_key: Key, p_alt: Key = KEY_NONE) -> void:
if not InputMap.has_action(p_action):
InputMap.add_action(p_action)
var event := InputEventKey.new()
event.keycode = p_key
InputMap.action_add_event(p_action, event)
if p_alt != KEY_NONE:
var alt := InputEventKey.new()
alt.keycode = p_alt
InputMap.action_add_event(p_action, alt)
func set_victorious() -> void:
idle_state.idle_animation = &"dance"

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=14 format=3 uid="uid://clyhgbpfpmtw7"]
[gd_scene load_steps=15 format=3 uid="uid://clyhgbpfpmtw7"]
[ext_resource type="Script" path="res://demo/scenes/game.gd" id="1_qw71e"]
[ext_resource type="Theme" uid="uid://boqtjf88xcpu4" path="res://demo/assets/ui.theme" id="2_r21q3"]
@ -25,6 +25,12 @@ font_color = Color(1, 1, 0.239216, 1)
outline_size = 20
outline_color = Color(0.211521, 0.23888, 0.290166, 1)
[sub_resource type="LabelSettings" id="LabelSettings_grcwx"]
font = ExtResource("4_w0vii")
font_size = 37
outline_size = 12
outline_color = Color(0.317525, 0.344884, 0.388373, 1)
[node name="Game" type="Node2D"]
y_sort_enabled = true
script = ExtResource("1_qw71e")
@ -116,6 +122,25 @@ label_settings = SubResource("LabelSettings_628x3")
horizontal_alignment = 2
vertical_alignment = 1
[node name="Keys" type="Label" parent="UI Layer/Control"]
layout_mode = 1
anchors_preset = 3
anchor_left = 1.0
anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = -336.0
offset_top = -208.0
offset_right = -18.0
offset_bottom = -25.0
grow_horizontal = 0
grow_vertical = 0
text = "Move WASD
Dodge SPACE
Attack ENTER / F
"
label_settings = SubResource("LabelSettings_grcwx")
[node name="Arena" parent="." instance=ExtResource("9_k3uu1")]
metadata/_edit_lock_ = true

View File

@ -24,41 +24,6 @@ window/size/window_width_override=1280
window/size/window_height_override=720
window/stretch/mode="canvas_items"
[input]
move_left={
"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":0,"physical_keycode":65,"key_label":0,"unicode":97,"echo":false,"script":null)
]
}
move_right={
"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":0,"physical_keycode":68,"key_label":0,"unicode":100,"echo":false,"script":null)
]
}
move_up={
"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":0,"physical_keycode":87,"key_label":0,"unicode":119,"echo":false,"script":null)
]
}
move_down={
"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":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null)
]
}
attack={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null)
, 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":4194309,"key_label":0,"unicode":0,"echo":false,"script":null)
, 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":70,"key_label":0,"unicode":102,"echo":false,"script":null)
]
}
dodge={
"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":0,"physical_keycode":32,"key_label":0,"unicode":32,"echo":false,"script":null)
]
}
[layer_names]
2d_physics/layer_1="Obstacle"