Controller Support added
This commit is contained in:
4
bomb.gd
4
bomb.gd
@@ -37,10 +37,6 @@ func _process(delta: float) -> void:
|
||||
if is_off_screen() and not has_exploded:
|
||||
trigger_explosion()
|
||||
|
||||
# Check for spacebar press to explode, only if the bomb hasn't exploded
|
||||
if Input.is_action_just_pressed("throw_bomb") and not has_exploded:
|
||||
trigger_explosion()
|
||||
|
||||
# This function is used to set the velocity of the bomb when it’s thrown
|
||||
func set_velocity(new_velocity: Vector2) -> void:
|
||||
# Apply an impulse based on the new velocity
|
||||
|
||||
50
player.gd
50
player.gd
@@ -12,14 +12,19 @@ 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 charging = false
|
||||
var current_power = 0.0
|
||||
var direction = Vector2(2, -2) # Default direction, can be modified for aiming
|
||||
var bomb = null # Global bomb reference
|
||||
var aim_direction = Vector2(1, -1) # Default aiming direction
|
||||
|
||||
@onready var velocity_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:
|
||||
# Set the initial points for the Line2D to indicate the starting vector
|
||||
line_indicator.clear_points()
|
||||
@@ -29,53 +34,68 @@ func _ready() -> void:
|
||||
func _process(delta: float) -> void:
|
||||
if position.y > 2000:
|
||||
get_tree().reload_current_scene()
|
||||
# Start charging when "throw_bomb" is pressed
|
||||
if Input.is_action_pressed("throw_bomb") and (!bomb or !is_instance_valid(bomb)):
|
||||
|
||||
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
|
||||
|
||||
|
||||
# Update velocity on the label
|
||||
velocity_label.text = "Power: " + str(current_power)
|
||||
|
||||
# Calculate the direction based on the input (for example, using the mouse position or joystick)
|
||||
var aim_direction = (get_global_mouse_position() - global_position).normalized()
|
||||
|
||||
# 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(1, end_point) # Set the ending point of the vector
|
||||
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 Input.is_action_just_released("throw_bomb") and charging:
|
||||
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
|
||||
throw_bomb(current_power)
|
||||
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 player_index == -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)
|
||||
|
||||
# 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
|
||||
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)
|
||||
|
||||
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
|
||||
|
||||
# Calculate the direction based on the mouse position
|
||||
var aim_direction = (get_global_mouse_position() - global_position).normalized()
|
||||
|
||||
# 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)
|
||||
|
||||
|
||||
# Clear velocity display after throwing (optional)
|
||||
velocity_label.text = "Power: 0"
|
||||
|
||||
@@ -20,7 +20,6 @@ config/icon="res://icon.svg"
|
||||
throw_bomb={
|
||||
"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,"location":0,"echo":false,"script":null)
|
||||
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":0,"pressure":0.0,"pressed":false,"script":null)
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user