fixed the data_link length
This commit is contained in:
parent
0eb335e0bf
commit
55f4163ef0
11 changed files with 257 additions and 62 deletions
|
@ -4,17 +4,20 @@ extends CharacterBody2D
|
|||
@export var gravity = 50
|
||||
var jump_count = 1
|
||||
@export var jump_strength = 100
|
||||
@export var jump_strength_max = 300
|
||||
var is_touching_floor : bool = true
|
||||
var jump_buffer_timer : float
|
||||
var coyote_timer : float = 0.2 # 200 millisecond buffer
|
||||
var input_direction = 0 #To keep track of which direction we where moving in last frame
|
||||
var coyote_timer : float = 0.2
|
||||
var input_direction = 0
|
||||
@export var max_link_distance: float = 200.0
|
||||
var data_link: Line2D
|
||||
|
||||
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:
|
||||
|
@ -23,18 +26,16 @@ func get_input(delta):
|
|||
input_direction = 1
|
||||
else:
|
||||
input_direction = 0
|
||||
|
||||
# This line updates the player's velocity
|
||||
|
||||
velocity.x = input_direction * speed
|
||||
|
||||
|
||||
if is_on_floor():
|
||||
# reset the jump count
|
||||
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
|
||||
|
@ -42,43 +43,44 @@ func get_input(delta):
|
|||
coyote_timer -= delta
|
||||
else:
|
||||
jump_count = 0
|
||||
|
||||
if Input.is_action_just_pressed("player_jump"):
|
||||
|
||||
if Input.is_action_just_pressed("player_jump"):
|
||||
if jump_count > 0:
|
||||
Jump()
|
||||
else:
|
||||
jump_buffer_timer = 0.2 # set a timer to jump again once the player has reached the ground, provided the jump is is still being held down
|
||||
elif Input.is_action_pressed("player_jump"): #Fall less fast if we keep holding the button
|
||||
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()
|
||||
|
||||
if(get_slide_collision_count() > 0):
|
||||
for i in get_slide_collision_count():
|
||||
var box = get_slide_collision(i).get_collider() as Box
|
||||
if scale.x == 2:
|
||||
if box and velocity.y >= 0:
|
||||
box.velocity.x = velocity.x*0.8
|
||||
else:
|
||||
if box and velocity.y >= 0 and box.scale.x < 2:
|
||||
box.velocity.x = velocity.x*0.8
|
||||
# print(jump_count) # Uncomment for debugging
|
||||
update_data_link()
|
||||
|
||||
func Jump():
|
||||
if scale.x == 1:
|
||||
velocity.y = -1 * jump_strength
|
||||
elif scale.x == 2:
|
||||
velocity.y = -1 * jump_strength_max
|
||||
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 _on_area_2d_body_entered(body):
|
||||
print("Body entered")
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue