From 0a93c2a088add27044966e1b7e02d5ab7381ee09 Mon Sep 17 00:00:00 2001 From: gagdiez Date: Fri, 17 Apr 2020 23:54:46 +0200 Subject: [PATCH] Added the ability to grab interactive nodes This commit changes the code so it is possible now to grab things from the scenary. --- house.tscn | 11 +++++--- scripts/Cup.gd | 4 +++ scripts/Interactive.gd | 9 ++++++- scripts/Level.gd | 5 +++- scripts/player.gd | 57 ++++++++++++++++++++++++++++++++++++++---- 5 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 scripts/Cup.gd diff --git a/house.tscn b/house.tscn index 68a77b5..8692563 100644 --- a/house.tscn +++ b/house.tscn @@ -362,15 +362,18 @@ mesh = SubResource( 19 ) material/0 = null [node name="Talk Bubble" type="RichTextLabel" parent="Cole"] -margin_left = 1.0 -margin_top = 135.0 -margin_right = 200.0 -margin_bottom = 165.0 +margin_left = 40.0 +margin_top = 40.0 +margin_right = 239.0 +margin_bottom = 70.0 scroll_active = false __meta__ = { "_edit_use_anchors_": false } +[node name="talk_bubble_timer" type="Timer" parent="Cole/Talk Bubble"] +one_shot = true + [node name="House" type="Spatial" parent="."] [node name="Interactive" type="Spatial" parent="House"] diff --git a/scripts/Cup.gd b/scripts/Cup.gd new file mode 100644 index 0000000..2233bb6 --- /dev/null +++ b/scripts/Cup.gd @@ -0,0 +1,4 @@ +extends 'Interactive.gd' + +func _ready(): + takeable = true diff --git a/scripts/Interactive.gd b/scripts/Interactive.gd index b096795..bca89be 100644 --- a/scripts/Interactive.gd +++ b/scripts/Interactive.gd @@ -5,4 +5,11 @@ extends Spatial onready var written_text onready var position = self.transform.origin onready var description = "It is just a " + name.to_lower() -onready var take_position +onready var takeable = false + +onready var collision = $CollisionShape + +func take(): + print("I am " + self.name.to_lower() + " and somebody took me") + visible = false + collision.disabled = true diff --git a/scripts/Level.gd b/scripts/Level.gd index e7e6ec8..27fd71d 100644 --- a/scripts/Level.gd +++ b/scripts/Level.gd @@ -24,7 +24,7 @@ const TAKE = 'take' const ACTIONS = [READ, WALK, LOOK, TAKE] const properties_needed = {READ: "written_text", WALK: "position", - LOOK: "description", TAKE: "take_position"} + LOOK: "description", TAKE: "takeable"} const action_label = {READ: "Read", WALK: "Walk to", LOOK: "Look at", TAKE: "Take"} @@ -102,6 +102,9 @@ func _process(delta): mouse_position = viewport.get_mouse_position() obj_under_mouse = get_object_under_mouse(mouse_position) + # Move Cole's bubble to above his head + $Cole.talk_bubble.rect_position = camera.unproject_position($Cole.transform.origin) + Vector2(-10, -230) + if Input.is_action_just_released("ui_weel_up"): change_action(1) diff --git a/scripts/player.gd b/scripts/player.gd index 7bf3bae..b12a918 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -8,15 +8,26 @@ onready var fsm_animation = $AnimationTree.get("parameters/playback") onready var sprite = $cole_sprite onready var navigation = $Navigation onready var talk_bubble = $"Talk Bubble" +onready var talk_bubble_timer = get_node("Talk Bubble/talk_bubble_timer") # Properties of our player var current_velocity = Vector3(0, 0, 0) # so we know its current speed +var current_destination # current position to which I am walking +var object_to_take var speed = 5 +var MINIMUM_DISTANCE = 0.5 # Variables for pathfinding -> The path our player has to follow var path_idx = 0 var path = [] +# Inventory +var inventory = [] + + +func _ready(): + talk_bubble_timer.connect("timeout", self, "quiet") + func key_input_velocity(): # Function to control Cole using the keyboard @@ -37,7 +48,9 @@ func key_input_velocity(): keyboard_input = true if keyboard_input: + # Clear whatever we did with the mouse path = [] + object_to_take = null # Modify current velocity current_velocity = direction.normalized() * speed @@ -60,10 +73,11 @@ func animate_player(): func walk_to(object): # Walk to an object - var destination = object.position + current_destination = object.position + object_to_take = null var begin = navigation.get_closest_point(self.transform.origin) - var end = navigation.get_closest_point(destination) + var end = navigation.get_closest_point(current_destination) path = navigation.get_simple_path(begin, end, true) path_idx = 0 @@ -73,8 +87,35 @@ func walk_to(object): func look(object): - talk_bubble.rect_position = Vector2(40, 40) - talk_bubble.text = object.description + say(object.description) + + +func quiet(): + talk_bubble.visible = false + + +func say(text): + talk_bubble.text = text + talk_bubble.visible = true + talk_bubble_timer.start() + + +func check_if_should_take_something(): + if object_to_take: + # Add object + inventory.append(object_to_take) + + say("I took the " + str(object_to_take.name).to_lower()) + + # Tell the object you took it + object_to_take.take() + + +func take(object): + if object.position != current_destination: + # First of all, walk to the object + walk_to(object) + object_to_take = object func path_input_velocity(): @@ -83,12 +124,18 @@ func path_input_velocity(): # There is still path to walk var move_vec = (path[path_idx] - transform.origin) - if move_vec.length() < 1: + if move_vec.length() < MINIMUM_DISTANCE: # too short to walk path_idx += 1 if path_idx < path.size(): + # So the animation doesn't stutter move_vec = (path[path_idx] - transform.origin) + else: + # We arrived + move_vec = Vector3(0, 0, 0) + current_destination = null + check_if_should_take_something() current_velocity = move_vec.normalized() * speed