extends RigidBody2D @export var speed = 400 @export var Bomb = load("res://bomb.tscn") @export var max_power = 1000 # Maximum throw power @export var charge_rate = 900 # Power increase per second @export var line_start_offset = 30 # Distance from the player where the line starts (in pixels) @export var gravity = 800.0 # Gravity force (pixels per second squared) @export var jump_force = -400.0 # Upward force applied during a jump @export var max_fall_speed = 1000.0 # Limit for how fast the player can fall @export var friction = 5 @export var max_speed = 500.0 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 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 line_indicator.add_point(Vector2.ZERO) # Ending point, will update dynamically func _process(delta: float) -> void: if position.y > 2000: get_tree().reload_current_scene() update_aim_direction() # Continuously update aiming direction # Start charging when "throw_bomb" is pressed (either button or key) if is_throwing_input_pressed() and (!bomb or !is_instance_valid(bomb)): charging = true current_power += charge_rate * delta current_power = min(current_power, max_power) # Cap power at max_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 var end_point = start_position + (aim_direction * current_power / 10) line_indicator.set_point_position(0, start_position) # Set the starting point of the vector line_indicator.set_point_position(1, end_point) # Set the ending point of the vector elif is_throwing_input_pressed() and is_instance_valid(bomb): bomb.trigger_explosion() elif not is_throwing_input_pressed() and charging: # Throw bomb when the action is released charging = false throw_bomb(current_power) current_power = 0.0 # Reset power # Reset Line2D to initial state after release line_indicator.set_point_position(0, Vector2.ZERO) line_indicator.set_point_position(1, Vector2.ZERO) func update_aim_direction(): if input_id == -1: # Keyboard/Mouse aim_direction = (get_global_mouse_position() - global_position).normalized() else: # Controller 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 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(input_id, JOY_BUTTON_A) func throw_bomb(power: float): # Check if there is no bomb or if the current bomb instance is invalid if !bomb or !is_instance_valid(bomb): # Instantiate the bomb bomb = Bomb.instantiate() # Use global bomb reference bomb.position = global_position # Set the velocity for the bomb: direction * power var launch_velocity = aim_direction * power * 2 bomb.set_velocity(launch_velocity) # Add the bomb to the scene add_child(bomb) func set_input_id(id: int) -> void: input_id = id