local Multiplayer support

This commit is contained in:
2024-12-04 08:48:48 +01:00
parent 15be351c86
commit bb7dbb054a
13 changed files with 233 additions and 24 deletions

View File

@@ -12,20 +12,23 @@ extends RigidBody2D
@export var friction = 5
@export var max_speed = 500.0
@export var player_index = 0 # Default to keyboard (-1). Set to controller ID for multiplayer.
var input_id = -1 # Default to keyboard (-1). Set to controller ID for multiplayer.
var charging = false
var current_power = 0.0
var bomb = null # Global bomb reference
var aim_direction = Vector2(1, -1) # Default aiming direction
@onready var velocity_label = $Label
@onready var label = $Label
@onready var line_indicator = $Line2D # Reference to the Line2D node
# Dead zone for joystick input
var dead_zone = 0.2
func _ready() -> void:
label.text = str(input_id)
# Set the initial points for the Line2D to indicate the starting vector
line_indicator.clear_points()
line_indicator.add_point(Vector2.ZERO) # Starting point at the player
@@ -43,9 +46,6 @@ func _process(delta: float) -> void:
current_power += charge_rate * delta
current_power = min(current_power, max_power) # Cap power at max_power
# Update velocity on the label
velocity_label.text = "Power: " + str(current_power)
# Set the start position of the line a bit away from the player
var start_position = aim_direction * line_start_offset
# Update the Line2D to visualize the throw direction and power
@@ -67,21 +67,21 @@ func _process(delta: float) -> void:
line_indicator.set_point_position(1, Vector2.ZERO)
func update_aim_direction():
if player_index == -1: # Keyboard/Mouse
if input_id == -1: # Keyboard/Mouse
aim_direction = (get_global_mouse_position() - global_position).normalized()
else: # Controller
var joy_x = Input.get_joy_axis(player_index, JOY_AXIS_LEFT_X)
var joy_y = Input.get_joy_axis(player_index, JOY_AXIS_LEFT_Y)
var joy_x = Input.get_joy_axis(input_id, JOY_AXIS_LEFT_X)
var joy_y = Input.get_joy_axis(input_id, JOY_AXIS_LEFT_Y)
# Apply dead zone
if abs(joy_x) > dead_zone or abs(joy_y) > dead_zone:
aim_direction = Vector2(joy_x, joy_y).normalized()
func is_throwing_input_pressed() -> bool:
if player_index == -1: # Keyboard/Mouse
if input_id == -1: # Keyboard/Mouse
return Input.is_action_pressed("throw_bomb")
else: # Controller (use A button for throwing)
return Input.is_joy_button_pressed(player_index, JOY_BUTTON_A)
return Input.is_joy_button_pressed(input_id, JOY_BUTTON_A)
func throw_bomb(power: float):
# Check if there is no bomb or if the current bomb instance is invalid
@@ -97,5 +97,5 @@ func throw_bomb(power: float):
# Add the bomb to the scene
add_child(bomb)
# Clear velocity display after throwing (optional)
velocity_label.text = "Power: 0"
func set_input_id(id: int) -> void:
input_id = id