khanat-client/scenes/items/pickup_item.gd
2021-01-21 22:03:44 +01:00

77 lines
2.2 KiB
GDScript

tool
extends Spatial
export( String ) var item_filename = "" setget set_item_filename
func set_item_filename( value ):
item_filename = value
if is_ready:
self.update_model()
var item = null
var is_ready = false
func _ready():
self.item = Datas.PickupItem.new( self.item_filename, self.global_transform )
self.update_model()
self.is_ready = true
func update_model():
for node in self.get_children():
self.remove_child( node )
node.queue_free()
if self.item:
if self.item.get_data( "model" ):
var item_node = load( self.item.get_data( "model" ) )
if item_node:
item_node = item_node.instance()
self.add_child( item_node )
func set_focus( p_focus = true ):
var _item
if p_focus:
for _item in self.get_children():
if _item.has_node( "focus" ):
_item.get_node( "focus" ).show()
else:
for _item in self.get_children():
if _item.has_node( "focus" ):
_item.get_node( "focus" ).hide()
func interact( p_object ):
if self.item.get_data( "pickable" ):
if p_object is Datas.Creature:
var new_inventory = p_object.get_data( "inventory" )
var number = self.item.get_data( "default_num_given" )
var stack = self.item.get_data( "stack" )
if number and stack and number > stack:
var lasting_number = number
while lasting_number > stack:
var inventory_item = Datas.InventoryItem.new( self.item_filename, stack, self.item )
inventory_item.set_data( "name", self.item_filename )
inventory_item.set_data( "number", stack )
new_inventory.push_back( inventory_item )
lasting_number -= stack
if lasting_number > 0:
var inventory_item = Datas.InventoryItem.new( self.item_filename, lasting_number, self.item )
inventory_item.set_data( "name", self.item_filename )
inventory_item.set_data( "number", lasting_number )
new_inventory.push_back( inventory_item )
else:
var inventory_item = Datas.InventoryItem.new( self.item_filename, number, self.item )
inventory_item.set_data( "name", self.item_filename )
new_inventory.push_back( inventory_item )
p_object.set_data( "inventory", new_inventory )
self.get_parent().remove_child( self )
self.queue_free()
return true
return false