Added item holder that can hold pickable item instances

This commit is contained in:
akshay committed 2022-08-14 04:46:50 -04:00
1 parent 8097a28cf8
commit 138519ea83
10 files changed
+122 -9

No files matched your search

+24
View File
@@ -5,6 +5,8 @@ const SPEED = 3.0
export var inventory = { "ore" : 0 }
var item_in_hand
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
@@ -67,3 +69,25 @@ func try_trigger_interact(area):
print("player interacted with ", owner)
return true
func drop_item_in_hand():
print("player dropped ", item_in_hand.item_name)
var item_dropped = item_in_hand
$ItemSpawn.remove_child(item_in_hand)
item_in_hand = null
return item_dropped
func pick_up_item(item):
var item_dropped = null
if item_in_hand != null:
item_dropped = drop_item_in_hand()
item_in_hand = item
$ItemSpawn.add_child(item_in_hand)
print("player picked up ", item_in_hand.item_name)
return item_dropped
func get_item_in_hand():
var item_to_return = null
if item_in_hand != null:
item_to_return = drop_item_in_hand()
return item_to_return
+8
View File
@@ -0,0 +1,8 @@
extends Spatial
export var item_name : String = ""
# Called when the node enters the scene tree for the first time.
func _ready():
assert(item_name != "")
$ItemLabel.text = "Item: %s" % item_name
+38
View File
@@ -0,0 +1,38 @@
extends Spatial
export var start_with_item : String = ""
var item_in_hold
# Called when the node enters the scene tree for the first time.
func _ready():
if start_with_item != "":
var item_instance = preload("res://scenes/item.tscn").instance()
item_instance.item_name = start_with_item
add_item(item_instance)
func remove_item():
assert(item_in_hold != null)
var removed_item = item_in_hold
$ItemSpawn.remove_child(item_in_hold)
item_in_hold = null
return removed_item
func add_item(item):
assert(item_in_hold == null)
$ItemSpawn.add_child(item)
item_in_hold = item
func on_player_interact(player) -> bool:
var player_item = player.get_item_in_hand()
if item_in_hold == null:
if player_item == null:
return false
add_item(player_item)
else:
var item_pickup = remove_item()
player.pick_up_item(item_pickup)
$AudioStreamPlayer.play()
if player_item != null:
add_item(player_item)
return true