Skip to content

Commit 7071b24

Browse files
committed
can_push
1 parent fbf7d15 commit 7071b24

File tree

9 files changed

+73
-20
lines changed

9 files changed

+73
-20
lines changed

Code/TileMap.gd

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ var Dic = {}
55

66
# Called when the node enters the scene tree for the first time.
77
func _ready():
8-
#for x in GridSize:
9-
#for y in GridSize:
10-
#set_cell(0, Vector2(x,y), 0, Vector2i(0,0),0)
11-
pass # Replace with function body.
12-
8+
pass
139

1410
# Called every frame. 'delta' is the elapsed time since the previous frame.
1511
func _process(_delta):

Code/box.gd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ var on_platform = false
1010
var previous_position = Vector2.ZERO # Initialize with a default value
1111
signal position_changed
1212
signal on_platform_sig
13+
signal push_result(result: bool)
1314

1415
func _ready():
16+
add_to_group("box")
1517
interaction_area.interact = Callable(self, "_on_interact")
18+
interaction_area.can_interact = Callable(self, "_on_can_interact")
1619
previous_position = position
1720
area2d.connect("area_entered", Callable(self, "_on_area_entered"))
1821
area2d.connect("area_exited", Callable(self, "_on_area_exited"))
@@ -28,6 +31,15 @@ func _on_interact():
2831
var new_position = position + direction * tile_size
2932
position = new_position
3033
emit_signal("position_changed")
34+
35+
func _on_can_interact() -> bool:
36+
var direction_to_box = (position - player.position).normalized()
37+
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
39+
var is_facing_box = direction_to_box.dot(player_facing_direction) > 0.9
40+
var can_interact = is_next_to_box and is_facing_box
41+
emit_signal("push_result", can_interact) # Emit result when checking if the box can be pushed
42+
return can_interact
3143

3244
func _on_area_entered(area):
3345
if area.is_in_group("platforms"):

Code/characters/player_robot.gd

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extends CharacterBody2D
33
signal done
44
signal picked_up
55
signal push
6+
signal can_push
67

78
var targetPos = Vector2.ZERO
89
var facing = Vector2(0, 1) # Default direction facing down
@@ -11,10 +12,14 @@ var waiting = true
1112
var abort = false
1213
var commands = []
1314
var inventory = []
15+
var removal_distance = 32
16+
var can_push_result = false
17+
var result_received = false
1418

1519
@onready var animation_tree = $AnimationTree
1620
@onready var state_machine = animation_tree.get("parameters/playback")
1721
@onready var end = get_tree().get_first_node_in_group("end")
22+
@onready var interaction_manager = get_tree().get_first_node_in_group("interaction_manager") # Ensure to point to the InteractionManager
1823

1924
func _ready():
2025
targetPos = self.position
@@ -25,6 +30,8 @@ func _ready():
2530
commands.append(line)
2631
print(line)
2732
file.close()
33+
34+
interaction_manager.connect("can_push_result", Callable(self, "_on_can_push_result"))
2835

2936
func _process(_delta):
3037
if abort:
@@ -49,6 +56,8 @@ func _process(_delta):
4956
_on_node_rotate_clockwise()
5057
'rcc':
5158
_on_node_rotate_c_clockwise()
59+
'cp':
60+
_on_check_if_can_push()
5261
animation_tree.set("parameters/Walk/blend_position", facing)
5362
else:
5463
state_machine.travel("End") # Idle state if no commands are left
@@ -97,7 +106,10 @@ func _on_node_rotate_c_clockwise():
97106
print("Rotated counter-clockwise. New facing:", facing)
98107
animation_tree.set("parameters/Walk/blend_position", facing)
99108

100-
var removal_distance = 32
109+
func _on_check_if_can_push() -> bool:
110+
var can_push_result = await interaction_manager.call("check_can_push")
111+
print("Final push result: ", can_push_result)
112+
return can_push_result
101113

102114
func _on_pick_up():
103115
emit_signal("picked_up")

Code/commands.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
l
22
l
33
l
4-
p
5-
d
6-
p
4+
rc
5+
cp

Code/commands.txt.bak

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
u
1+
l
2+
l
3+
l
4+
p
5+
d
6+
p
Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
extends Node2D
22

33
@onready var player = get_tree().get_first_node_in_group("player")
4+
@onready var box = get_tree().get_first_node_in_group("player")
45

56
var active_areas = []
67
var can_interact = true
78

8-
# Assuming player has a direction variable to determine its facing direction
9-
# Directions can be "up", "down", "left", "right"
10-
var player_facing_direction: String
11-
129
func _ready():
13-
player.connect("picked_up", Callable(self, "_on_interact"))
10+
player.connect("picked_up", Callable(self, "_on_push"))
11+
player.connect("can_push", Callable(self, "_on_can_push"))
12+
13+
box.connect("push_result", Callable(self, "_on_push_result"))
1414

1515
func register_area(area: InteractionArea):
1616
active_areas.push_back(area)
@@ -20,9 +20,30 @@ func unregister_area(area: InteractionArea):
2020
if index != -1:
2121
active_areas.remove_at(index)
2222

23-
func _on_interact():
23+
func _on_push():
2424
if can_interact:
2525
if active_areas.size() > 0:
2626
can_interact = false
2727
await active_areas[0].interact.call()
2828
can_interact = true
29+
30+
func _on_can_push():
31+
if can_interact:
32+
if active_areas.size() > 0:
33+
can_interact = false
34+
var can_push = await active_areas[0].can_interact.call()
35+
can_interact = true
36+
emit_signal("can_push_result", can_push) # Emit the result to the player
37+
return can_push
38+
39+
func _on_push_result(result: bool):
40+
if result:
41+
print("The box can be pushed!")
42+
else:
43+
print("The box cannot be pushed!")
44+
45+
func check_can_push() -> bool:
46+
if active_areas.size() > 0:
47+
var can_push = await active_areas[0].can_interact.call()
48+
return can_push
49+
return false

Code/interaction/interaction area/interaction_area.gd

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ class_name InteractionArea
33

44
@export var action_name: String = "interact"
55

6+
@onready var interaction_manager = get_tree().get_first_node_in_group("interaction_manager") # Ensure to point to the InteractionManager
7+
8+
69
var interact: Callable = func():
710
pass
811

12+
var can_interact: Callable = func():
13+
pass
914

1015
func _on_body_entered(body):
1116
if body.is_in_group("player"):
12-
InteractionManager.register_area(self)
17+
interaction_manager.register_area(self)
1318

1419

1520
func _on_body_exited(body):
1621
if body.is_in_group("player"):
17-
InteractionManager.unregister_area(self)
22+
interaction_manager.unregister_area(self)

Code/interaction/interaction area/interaction_area.tscn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
[node name="InteractionArea" type="Area2D"]
66
collision_layer = 0
77
script = ExtResource("1_ffku4")
8-
action_name = null
98

109
[connection signal="body_entered" from="." to="." method="_on_body_entered"]
1110
[connection signal="body_exited" from="." to="." method="_on_body_exited"]

Code/levels/level_0.tscn

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=13 format=3 uid="uid://dgm4e3dj1l320"]
1+
[gd_scene load_steps=14 format=3 uid="uid://dgm4e3dj1l320"]
22

33
[ext_resource type="Texture2D" uid="uid://os7624ctmhq3" path="res://art/robot_atlas.png" id="1_qi4vs"]
44
[ext_resource type="Texture2D" uid="uid://dwsmec5b5rj0y" path="res://art/tiles/water.png" id="2_1g22o"]
@@ -8,6 +8,7 @@
88
[ext_resource type="PackedScene" uid="uid://cchfvbtuste6s" path="res://characters/player_robot.tscn" id="6_hjdgv"]
99
[ext_resource type="PackedScene" uid="uid://deb7q6h0om8hc" path="res://End.tscn" id="7_xxre3"]
1010
[ext_resource type="PackedScene" uid="uid://5q5c8pt7mew2" path="res://GameManager.tscn" id="8_nfk0q"]
11+
[ext_resource type="PackedScene" uid="uid://c7m1f03wf08sa" path="res://interaction/InteractionManager.tscn" id="9_h8w3f"]
1112

1213
[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_0ydj6"]
1314
texture = ExtResource("1_qi4vs")
@@ -361,3 +362,6 @@ z_index = 0
361362
position = Vector2(56, 104)
362363

363364
[node name="GameManager" parent="." instance=ExtResource("8_nfk0q")]
365+
366+
[node name="InteractionManager" parent="." groups=["interaction_manager"] instance=ExtResource("9_h8w3f")]
367+
position = Vector2(104, 56)

0 commit comments

Comments
 (0)