Skip to content

Commit 0a93c2a

Browse files
committed
Added the ability to grab interactive nodes
This commit changes the code so it is possible now to grab things from the scenary.
1 parent 780bb12 commit 0a93c2a

File tree

5 files changed

+75
-11
lines changed

5 files changed

+75
-11
lines changed

house.tscn

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,18 @@ mesh = SubResource( 19 )
362362
material/0 = null
363363

364364
[node name="Talk Bubble" type="RichTextLabel" parent="Cole"]
365-
margin_left = 1.0
366-
margin_top = 135.0
367-
margin_right = 200.0
368-
margin_bottom = 165.0
365+
margin_left = 40.0
366+
margin_top = 40.0
367+
margin_right = 239.0
368+
margin_bottom = 70.0
369369
scroll_active = false
370370
__meta__ = {
371371
"_edit_use_anchors_": false
372372
}
373373

374+
[node name="talk_bubble_timer" type="Timer" parent="Cole/Talk Bubble"]
375+
one_shot = true
376+
374377
[node name="House" type="Spatial" parent="."]
375378

376379
[node name="Interactive" type="Spatial" parent="House"]

scripts/Cup.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
extends 'Interactive.gd'
2+
3+
func _ready():
4+
takeable = true

scripts/Interactive.gd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@ extends Spatial
55
onready var written_text
66
onready var position = self.transform.origin
77
onready var description = "It is just a " + name.to_lower()
8-
onready var take_position
8+
onready var takeable = false
9+
10+
onready var collision = $CollisionShape
11+
12+
func take():
13+
print("I am " + self.name.to_lower() + " and somebody took me")
14+
visible = false
15+
collision.disabled = true

scripts/Level.gd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const TAKE = 'take'
2424

2525
const ACTIONS = [READ, WALK, LOOK, TAKE]
2626
const properties_needed = {READ: "written_text", WALK: "position",
27-
LOOK: "description", TAKE: "take_position"}
27+
LOOK: "description", TAKE: "takeable"}
2828
const action_label = {READ: "Read", WALK: "Walk to",
2929
LOOK: "Look at", TAKE: "Take"}
3030

@@ -102,6 +102,9 @@ func _process(delta):
102102
mouse_position = viewport.get_mouse_position()
103103
obj_under_mouse = get_object_under_mouse(mouse_position)
104104

105+
# Move Cole's bubble to above his head
106+
$Cole.talk_bubble.rect_position = camera.unproject_position($Cole.transform.origin) + Vector2(-10, -230)
107+
105108
if Input.is_action_just_released("ui_weel_up"):
106109
change_action(1)
107110

scripts/player.gd

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,26 @@ onready var fsm_animation = $AnimationTree.get("parameters/playback")
88
onready var sprite = $cole_sprite
99
onready var navigation = $Navigation
1010
onready var talk_bubble = $"Talk Bubble"
11+
onready var talk_bubble_timer = get_node("Talk Bubble/talk_bubble_timer")
1112

1213
# Properties of our player
1314
var current_velocity = Vector3(0, 0, 0) # so we know its current speed
15+
var current_destination # current position to which I am walking
16+
var object_to_take
1417
var speed = 5
18+
var MINIMUM_DISTANCE = 0.5
1519

1620
# Variables for pathfinding -> The path our player has to follow
1721
var path_idx = 0
1822
var path = []
1923

24+
# Inventory
25+
var inventory = []
26+
27+
28+
func _ready():
29+
talk_bubble_timer.connect("timeout", self, "quiet")
30+
2031

2132
func key_input_velocity():
2233
# Function to control Cole using the keyboard
@@ -37,7 +48,9 @@ func key_input_velocity():
3748
keyboard_input = true
3849

3950
if keyboard_input:
51+
# Clear whatever we did with the mouse
4052
path = []
53+
object_to_take = null
4154

4255
# Modify current velocity
4356
current_velocity = direction.normalized() * speed
@@ -60,10 +73,11 @@ func animate_player():
6073

6174
func walk_to(object):
6275
# Walk to an object
63-
var destination = object.position
76+
current_destination = object.position
77+
object_to_take = null
6478

6579
var begin = navigation.get_closest_point(self.transform.origin)
66-
var end = navigation.get_closest_point(destination)
80+
var end = navigation.get_closest_point(current_destination)
6781

6882
path = navigation.get_simple_path(begin, end, true)
6983
path_idx = 0
@@ -73,8 +87,35 @@ func walk_to(object):
7387

7488

7589
func look(object):
76-
talk_bubble.rect_position = Vector2(40, 40)
77-
talk_bubble.text = object.description
90+
say(object.description)
91+
92+
93+
func quiet():
94+
talk_bubble.visible = false
95+
96+
97+
func say(text):
98+
talk_bubble.text = text
99+
talk_bubble.visible = true
100+
talk_bubble_timer.start()
101+
102+
103+
func check_if_should_take_something():
104+
if object_to_take:
105+
# Add object
106+
inventory.append(object_to_take)
107+
108+
say("I took the " + str(object_to_take.name).to_lower())
109+
110+
# Tell the object you took it
111+
object_to_take.take()
112+
113+
114+
func take(object):
115+
if object.position != current_destination:
116+
# First of all, walk to the object
117+
walk_to(object)
118+
object_to_take = object
78119

79120

80121
func path_input_velocity():
@@ -83,12 +124,18 @@ func path_input_velocity():
83124
# There is still path to walk
84125
var move_vec = (path[path_idx] - transform.origin)
85126

86-
if move_vec.length() < 1:
127+
if move_vec.length() < MINIMUM_DISTANCE:
87128
# too short to walk
88129
path_idx += 1
89130

90131
if path_idx < path.size():
132+
# So the animation doesn't stutter
91133
move_vec = (path[path_idx] - transform.origin)
134+
else:
135+
# We arrived
136+
move_vec = Vector3(0, 0, 0)
137+
current_destination = null
138+
check_if_should_take_something()
92139

93140
current_velocity = move_vec.normalized() * speed
94141

0 commit comments

Comments
 (0)