82 lines
3.2 KiB
GDScript3
82 lines
3.2 KiB
GDScript3
|
|
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
|
||
|
|
|
||
|
|
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
|
||
|
|
|
||
|
|
@onready var velocity_label = $Label
|
||
|
|
@onready var line_indicator = $Line2D # Reference to the Line2D node
|
||
|
|
|
||
|
|
func _ready() -> void:
|
||
|
|
# 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()
|
||
|
|
# Start charging when "throw_bomb" is pressed
|
||
|
|
if Input.is_action_pressed("throw_bomb") 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:
|
||
|
|
# Throw bomb when the action is released
|
||
|
|
throw_bomb(current_power)
|
||
|
|
charging = false
|
||
|
|
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 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"
|