Skip to content

Commit

Permalink
Added the ability to grab interactive nodes
Browse files Browse the repository at this point in the history
This commit changes the code so it is possible now to grab things
from the scenary.
  • Loading branch information
gagdiez committed Apr 17, 2020
1 parent 780bb12 commit 0a93c2a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
11 changes: 7 additions & 4 deletions house.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 4 additions & 0 deletions scripts/Cup.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extends 'Interactive.gd'

func _ready():
takeable = true
9 changes: 8 additions & 1 deletion scripts/Interactive.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion scripts/Level.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down Expand Up @@ -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)

Expand Down
57 changes: 52 additions & 5 deletions scripts/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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():
Expand All @@ -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

Expand Down

0 comments on commit 0a93c2a

Please sign in to comment.