Skip to content

Commit

Permalink
Add joystick support
Browse files Browse the repository at this point in the history
  • Loading branch information
alnikyur committed Jan 7, 2025
1 parent 106b874 commit c66fa0e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 8 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ config/features=PackedStringArray("4.2", "GL Compatibility")
window/size/viewport_width=1280
window/size/viewport_height=800

[input]

joy_fire={
"deadzone": 0.5,
"events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":0,"pressure":0.0,"pressed":true,"script":null)
]
}

[rendering]

textures/canvas_textures/default_texture_filter=0
Expand Down
13 changes: 12 additions & 1 deletion scenes/spaceship/CharacterBody2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func _on_node_added(node):

func _process(delta):
var input_direction = Vector2.ZERO

# Keyboard input support
if Input.is_action_pressed("ui_left"):
input_direction.x -= 1
$AnimatedSprite2D.play("spaceleft")
Expand All @@ -42,17 +44,26 @@ func _process(delta):
if Input.is_action_pressed("ui_down"):
input_direction.y += 1

# Joystick input support
var joystick_direction = Vector2(
Input.get_joy_axis(0, JOY_AXIS_LEFT_X),
Input.get_joy_axis(0, JOY_AXIS_LEFT_Y)
)

input_direction += joystick_direction
input_direction = input_direction.normalized()

velocity += input_direction * acceleration * delta
if velocity.length() > max_speed:
velocity = velocity.normalized() * max_speed
if input_direction == Vector2.ZERO:
velocity = velocity.move_toward(Vector2.ZERO, friction * delta)
move_and_slide()

if Input.is_action_just_pressed("ui_select"):
if Input.is_action_just_pressed("ui_select") or Input.is_action_just_pressed("joy_fire"):
fire()


func fire():
if current_fire_count > 0:
var num_lasers = 1 # Количество лазеров по умолчанию
Expand Down

0 comments on commit c66fa0e

Please sign in to comment.