challenge-editor/scene/trade.gd
2024-10-09 20:34:18 +08:00

156 lines
4.4 KiB
GDScript

extends Control
const TRADE_CARD = preload("res://scene/trade_card.tscn")
var npc_data:Dictionary
var npc_id:String
#当前选择的对方背包里的物品的序列
var now_select_other_item_ind:int=0
#当前选择的自己背包的物品的序列
var now_select_self_item_ind:int=0
var favor:int=0:
set(val):
favor=val
%sold_value.text=str(clamp((200-favor),50,200))+"%"
%back_value.text=str(clamp((50+favor),50,150))+"%"
#NPC拥有金钱
var other_gold:int=0:
set(val):
%other_gold.text=str(val)
other_gold=val
func set_data(npc_id:String):
npc_data=Global.get_now_game_npc_data(npc_id)
favor=Global.get_now_game_npc_favor(npc_id)
%trade_name.text=npc_data["name"]
fresh()
pass
func fresh():
%self_item_panel.hide()
%other_item_panel.hide()
other_gold=npc_data["gold"]
%self_gold.text=str(Global.get_currency_game(1))
for i in %other_bag.get_children():
i.queue_free()
for i in %self_bag.get_children():
i.queue_free()
var sold_item:Array=npc_data["sold_item"]
for i in sold_item.size():
var new_card=TRADE_CARD.instantiate()
%other_bag.add_child(new_card)
var i_data=sold_item[i]
new_card.discount=clamp((2-float(favor)/100),0.5,2)
new_card.set_item(i_data)
new_card.click.connect(other_bag_card_click.bind([i,i_data]))
var self_item=Global.get_all_item_game_data()
for i in self_item.size():
var new_card=TRADE_CARD.instantiate()
%self_bag.add_child(new_card)
new_card.discount=clamp(0.5+float(favor)/100,0.5,1.5)
new_card.set_item(self_item[i])
new_card.click.connect(self_bag_card_click.bind([i,self_item[i]]))
func other_bag_card_click(arr:Array):
var ind:int=arr[0]
var data=arr[1]
now_select_other_item_ind=ind
var item_data=npc_data["sold_item"][ind]
%other_item_panel.show()
%self_item_panel.hide()
%other_item_icon.texture=Database.get_texture(item_data["texture"])
%other_item_introduction.text=item_data["introduction"]
if int(item_data["price"]*clamp((2-float(favor)/100),0.5,2))>Global.get_currency_game(1):
%other_item_accept_btn.disable(true)
else:
%other_item_accept_btn.disable(false)
func self_bag_card_click(arr:Array):
var ind:int=arr[0]
var data=arr[1]
now_select_self_item_ind=ind
var item_data=data
%self_item_panel.show()
%other_item_panel.hide()
%self_item_icon.texture=Database.get_texture(item_data["texture"])
%self_item_introduction.text=item_data["introduction"]
if int(item_data["price"]*clamp(0.5+float(favor)/100,0.5,1.5))>npc_data["gold"]:
%self_item_accept_btn.disable(true)
else:
%self_item_accept_btn.disable(false)
func _on_other_item_accept_btn_pressed() -> void:
var item_data=npc_data["sold_item"][now_select_other_item_ind].duplicate(true)
dec_item_from_npc(now_select_other_item_ind)
if item_data.has("num"):
item_data["num"]=1
var price=item_data["price"]
price=int(price*clamp((2-float(favor)/100),0.5,2)) #计算价格
Global.change_currency_game(1,-price)
npc_data["gold"]-=price
Global.add_item_to_bag(item_data)
fresh()
pass # Replace with function body.
func _on_other_item_refuse_btn_pressed() -> void:
%other_item_panel.hide()
pass # Replace with function body.
func _on_self_item_refuse_btn_pressed() -> void:
%self_item_panel.hide()
pass # Replace with function body.
#向NPC背包中添加item
func add_item_to_npc(item_data:Dictionary):
if item_data["allow_merge"]:
for i in npc_data["sold_item"]:
if i["id"]==item_data["id"]:
if i.has("num"):
i["num"]+=item_data["num"]
else:
i["num"]=2
return
npc_data["sold_item"].append(item_data)
else:
npc_data["sold_item"].append(item_data)
func dec_item_from_npc(ind:int):
if npc_data["sold_item"][ind].has("num"):
if npc_data["sold_item"][ind]["num"]<=1:
npc_data["sold_item"].pop_at(ind)
else:
npc_data["sold_item"][ind]["num"]-=1
pass
else:
npc_data["sold_item"].pop_at(ind)
pass
func _on_self_item_accept_btn_pressed() -> void:
var item_data=Global.get_all_item_game_data()[now_select_self_item_ind].duplicate(true)
if item_data["allow_merge"]:
item_data["num"]=1
else:
item_data.erase("num")
var price=item_data["price"]
price=int(price*clamp(0.5+float(favor)/100,0.5,1.5))
Global.change_currency_game(1,price)
npc_data["gold"]-=price
Global.decrease_item_num_index(now_select_self_item_ind)
add_item_to_npc(item_data)
fresh()
pass # Replace with function body.
func _on_back_pressed() -> void:
%self_item_panel.hide()
%other_item_panel.hide()
self.hide()
pass # Replace with function body.