Files
Bomber-Thorsten/player.gd

102 lines
3.6 KiB
GDScript3
Raw Normal View History

2024-12-02 13:45:17 +01:00
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 = 500 # 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
2024-12-04 08:48:48 +01:00
var input_id = -1 # Default to keyboard (-1). Set to controller ID for multiplayer.
2024-12-03 21:01:00 +01:00
2024-12-02 13:45:17 +01:00
var charging = false
var current_power = 0.0
var bomb = null # Global bomb reference
2024-12-03 21:01:00 +01:00
var aim_direction = Vector2(1, -1) # Default aiming direction
2024-12-02 13:45:17 +01:00
2024-12-04 08:48:48 +01:00
@onready var label = $Label
2024-12-02 13:45:17 +01:00
@onready var line_indicator = $Line2D # Reference to the Line2D node
2024-12-03 21:01:00 +01:00
# Dead zone for joystick input
var dead_zone = 0.2
2024-12-02 13:45:17 +01:00
func _ready() -> void:
2024-12-04 08:48:48 +01:00
label.text = str(input_id)
2024-12-02 13:45:17 +01:00
# 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()
2024-12-03 21:01:00 +01:00
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)):
2024-12-02 13:45:17 +01:00
charging = true
current_power += charge_rate * delta
current_power = min(current_power, max_power) # Cap power at max_power
2024-12-03 21:01:00 +01:00
2024-12-02 13:45:17 +01:00
# 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
2024-12-03 21:01:00 +01:00
elif is_throwing_input_pressed() and is_instance_valid(bomb):
bomb.trigger_explosion()
elif not is_throwing_input_pressed() and charging:
2024-12-02 13:45:17 +01:00
# Throw bomb when the action is released
charging = false
2024-12-03 21:01:00 +01:00
throw_bomb(current_power)
2024-12-02 13:45:17 +01:00
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)
2024-12-03 21:01:00 +01:00
func update_aim_direction():
2024-12-04 08:48:48 +01:00
if input_id == -1: # Keyboard/Mouse
2024-12-03 21:01:00 +01:00
aim_direction = (get_global_mouse_position() - global_position).normalized()
else: # Controller
2024-12-04 08:48:48 +01:00
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)
2024-12-03 21:01:00 +01:00
# 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:
2024-12-04 08:48:48 +01:00
if input_id == -1: # Keyboard/Mouse
2024-12-03 21:01:00 +01:00
return Input.is_action_pressed("throw_bomb")
else: # Controller (use A button for throwing)
2024-12-04 08:48:48 +01:00
return Input.is_joy_button_pressed(input_id, JOY_BUTTON_A)
2024-12-03 21:01:00 +01:00
2024-12-02 13:45:17 +01:00
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)
2024-12-03 21:01:00 +01:00
2024-12-04 08:48:48 +01:00
func set_input_id(id: int) -> void:
input_id = id