@@ -6,10 +6,11 @@ extends CharacterBody2D
66@onready var area2d = $ Area2D # Reference to the Area2D node
77
88var tile_size = Vector2 (16 , 16 )
9- var on_platform = false
9+ var on_platform = null
1010var previous_position = Vector2 .ZERO # Initialize with a default value
1111signal position_changed
1212signal on_platform_sig
13+ signal out_platform_sig
1314signal push_result (result : bool )
1415
1516func _ready ():
@@ -27,27 +28,37 @@ func _process(delta):
2728 previous_position = position
2829
2930func _on_interact ():
30- var direction = (position - player .position ).normalized ()
31- var new_position = position + direction * tile_size
32- position = new_position
33- emit_signal ("position_changed" )
31+ if _on_can_interact ():
32+ var direction = (position - player .position ).normalized ()
33+ direction .x = round (direction .x )
34+ direction .y = round (direction .y )
35+ var new_position = position + direction * tile_size
36+ position = new_position
37+ emit_signal ("position_changed" )
3438
3539func _on_can_interact () -> bool :
3640 var direction_to_box = (position - player .position ).normalized ()
3741 var player_facing_direction = player .facing .normalized ()
38- var is_next_to_box = abs (player .position .x - position .x ) <= tile_size .x and abs (player .position .y - position .y ) <= tile_size .y
42+ var is_next_to_box = abs (player .position .x - position .x ) <= (tile_size .x * 1.5 ) and abs (player .position .y - position .y ) <= (tile_size .y * 1.5 )
43+ if ! is_next_to_box :
44+ print (abs (player .position .x - position .x ))
45+ print (abs (player .position .y - position .y ))
46+
3947 var is_facing_box = direction_to_box .dot (player_facing_direction ) > 0.9
4048 var can_interact = is_next_to_box and is_facing_box
4149 emit_signal ("push_result" , can_interact ) # Emit result when checking if the box can be pushed
4250 return can_interact
4351
4452func _on_area_entered (area ):
4553 if area .is_in_group ("platforms" ):
46- on_platform = true
54+ on_platform = area
4755 print ("Box is on the platform!" )
4856 emit_signal ("on_platform_sig" , area )
49-
57+
5058func _on_area_exited (area ):
5159 if area .is_in_group ("platforms" ):
52- on_platform = false
53- print ("Box left the platform!" )
60+ print ("Box has left the platform!" )
61+ emit_signal ("out_platform_sig" , on_platform )
62+ on_platform = null
63+
64+
0 commit comments