87 lines
2 KiB
GDScript
87 lines
2 KiB
GDScript
extends CharacterBody2D
|
|
|
|
@export var speed = 340
|
|
@export var gravity = 50
|
|
var jump_count = 1
|
|
@export var jump_strength = 100
|
|
var is_touching_floor : bool = true
|
|
var jump_buffer_timer : float
|
|
var coyote_timer : float = 0.2
|
|
var input_direction = 0
|
|
@export var max_link_distance: float = 200.0
|
|
var data_link: Line2D
|
|
var target_scale
|
|
|
|
func _ready():
|
|
data_link = $data_link
|
|
|
|
func get_input(delta):
|
|
var left = Input.is_action_pressed("player_left")
|
|
var right = Input.is_action_pressed("player_right")
|
|
|
|
if left and right:
|
|
input_direction = 0
|
|
elif left:
|
|
input_direction = -1
|
|
elif right:
|
|
input_direction = 1
|
|
else:
|
|
input_direction = 0
|
|
|
|
velocity.x = input_direction * speed
|
|
|
|
if is_on_floor():
|
|
is_touching_floor = true
|
|
jump_count = 1
|
|
coyote_timer = 0.2
|
|
if jump_buffer_timer > 0:
|
|
Jump()
|
|
|
|
if not is_on_floor():
|
|
if jump_buffer_timer > 0:
|
|
jump_buffer_timer -= delta
|
|
if coyote_timer > 0.0:
|
|
coyote_timer -= delta
|
|
else:
|
|
jump_count = 0
|
|
|
|
if Input.is_action_just_pressed("player_jump"):
|
|
if jump_count > 0:
|
|
Jump()
|
|
else:
|
|
jump_buffer_timer = 0.2
|
|
elif Input.is_action_pressed("player_jump"):
|
|
velocity.y += -1 * 25
|
|
|
|
func _physics_process(delta):
|
|
get_input(delta)
|
|
velocity.y += gravity
|
|
move_and_slide()
|
|
update_data_link()
|
|
|
|
func Jump():
|
|
velocity.y = -1 * jump_strength
|
|
jump_count = 0
|
|
is_touching_floor = false
|
|
|
|
func _on_hurtbox_body_entered(body: Node2D) -> void:
|
|
get_tree().reload_current_scene()
|
|
|
|
func update_data_link():
|
|
var nearest_block = find_nearest_block()
|
|
if nearest_block:
|
|
data_link.visible = true
|
|
data_link.set_point_position(0, Vector2.ZERO)
|
|
data_link.set_point_position(1, get_transform().affine_inverse() * nearest_block.position)
|
|
else:
|
|
data_link.visible = false
|
|
|
|
func find_nearest_block() -> Node2D:
|
|
var closest_distance = max_link_distance
|
|
var closest_block: Node2D = null
|
|
for block in get_tree().get_nodes_in_group("scalable_blocks"):
|
|
var distance = global_position.distance_to(block.global_position)
|
|
if distance <= closest_distance:
|
|
closest_distance = distance
|
|
closest_block = block
|
|
return closest_block
|