challenge-editor/scene/auction.gd
2024-10-10 18:09:32 +08:00

294 lines
8.7 KiB
GDScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

extends Control
const AUCTION_CARD = preload("res://scene/auction_card.tscn")
var auction_card_arr:Array=[null,null,null]
@export var texture_arr:Array[Texture2D]=[]
@export var scale_parameter:float=0.5
var now_index:int=0:
set(val):
now_index=val
if now_index!=now_auction_item_ind:
%out_price_btn.disable(true)
else:
%out_price_btn.disable(false)
update_out_price()
pass
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
func init_texture():
for i in %card_add_pos.get_children():
i.queue_free()
now_index=0
auction_card_arr[1]=AUCTION_CARD.instantiate()
%card_add_pos.add_child(auction_card_arr[1])
auction_card_arr[1].set_item_texture(texture_arr[0])
auction_card_arr[1].global_position=%center_mask.global_position
auction_card_arr[1].size=%center_mask.size
auction_card_arr[1].modulate.a=1
if texture_arr.size()>1:
auction_card_arr[2]=AUCTION_CARD.instantiate()
%card_add_pos.add_child(auction_card_arr[2])
auction_card_arr[2].set_item_texture(texture_arr[0])
auction_card_arr[2].global_position=%right_mark.global_position-%center_mask.size*scale_parameter/2
auction_card_arr[2].size=%center_mask.size*scale_parameter
auction_card_arr[2].set_item_texture(texture_arr[1])
auction_card_arr[2].modulate.a=1
pass
func left():
if now_index>0:
now_index-=1
var before_center_card:AuctionCard=auction_card_arr[1]
before_center_card.move_size_to(%right_mark.global_position-%center_mask.size*scale_parameter/2,%center_mask.size*scale_parameter,false)
var before_left=auction_card_arr[0]
if before_left is AuctionCard:
before_left.move_size_to(%center_mask.global_position,%center_mask.size,false)
if auction_card_arr[2]!=null:
var rightcard=auction_card_arr[2] as AuctionCard
rightcard.move_size_to(%right_out_mark.global_position-%center_mask.size*scale_parameter/2,%center_mask.size*scale_parameter,true)
auction_card_arr.pop_back()
if now_index>0:
var new_card:AuctionCard=AUCTION_CARD.instantiate()
%card_add_pos.add_child(new_card)
new_card.global_position=%left_out_mark.global_position-%center_mask.size*scale_parameter/2
new_card.size=%center_mask.size*scale_parameter
new_card.move_size_to(%left_mark.global_position-%center_mask.size*scale_parameter/2,%center_mask.size*scale_parameter,false)
new_card.set_item_texture(texture_arr[now_index-1])
auction_card_arr.push_front(new_card)
else:
auction_card_arr.push_front(null)
func right():
if now_index<texture_arr.size()-1:
now_index+=1
var before_center_card:AuctionCard=auction_card_arr[1]
before_center_card.move_size_to(%left_mark.global_position-%center_mask.size*scale_parameter/2,%center_mask.size*scale_parameter,false)
var before_right=auction_card_arr[2]
if before_right is AuctionCard:
before_right.move_size_to(%center_mask.global_position,%center_mask.size,false)
if auction_card_arr[0]!=null:
var leftcard=auction_card_arr[0] as AuctionCard
leftcard.move_size_to(%left_out_mark.global_position-%center_mask.size*scale_parameter/2,%center_mask.size*scale_parameter,true)
auction_card_arr.pop_front()
if now_index<texture_arr.size()-1:
var new_card:AuctionCard=AUCTION_CARD.instantiate()
%card_add_pos.add_child(new_card)
new_card.global_position=%right_out_mark.global_position-%center_mask.size*scale_parameter/2
new_card.size=%center_mask.size*scale_parameter
new_card.move_size_to(%right_mark.global_position-%center_mask.size*scale_parameter/2,%center_mask.size*scale_parameter,false)
new_card.set_item_texture(texture_arr[now_index+1])
auction_card_arr.push_back(new_card)
else:
auction_card_arr.push_back(null)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if timer.is_stopped():
%time.hide()
else:
%time.show()
%time.text=str(int(timer.time_left))
pass
#决策
#已知这个人有钱x当前竞拍物品标准价值y人物需要程度z0-1当前竞拍价格a最小加价b
func auction_bid_decision(x:int, y:int, z:float, a:int, b:int,t=0.7):
var p_max:int=int(y*(1+z))
if a+b>=p_max ||a+b>x:
return null
var r=randf()
if r>t:
if randf()>0.5:
return null
else:
var res:int= randi_range(a+b,p_max)
if res>x:
return null
else:
return res
else:
return a+b
func _on_test_pressed() -> void:
set_npc_arr_and_item_arr([Database.get_npc_data("npc_01"),Database.get_npc_data("npc_02")],[Database.get_item_data("item_01"),Database.get_item_data("item_02")])
pass # Replace with function body.
#本场参与拍卖的NPC字典数组
var npc_arr:Array=[]
#本场物品数组
var item_arr:Array=[]
#随机需求数组
var npc_need_item_arr:Array=[]
var now_auction_item_ind:int=0:
set(val):
now_auction_item_ind=val
if now_index!=now_auction_item_ind:
%out_price_btn.disable(true)
else:
%out_price_btn.disable(false)
#报价历史数组
#[[[npc序列,价格],[npc序列玩家为-1,价格]....],...]
var auction_price_arr:Array=[[]]
#最小加价
var min_price_add:int=10
func start_turn():
var res_arr=auction_price_arr[now_auction_item_ind]
var now_item=item_arr[now_auction_item_ind]
for i in npc_arr.size():
var now_price:int=0
if res_arr.size()==0:
now_price=now_item["price"]/2
else:
now_price=res_arr[res_arr.size()-1][1]
var npc_data=npc_arr[i]
var out_price=auction_bid_decision(npc_data["gold"],now_item["price"],npc_need_item_arr[i][now_auction_item_ind],now_price,min_price_add)
if out_price!=null:
res_arr.append([i,out_price])
timer.start(30)
update_out_price()
const AUCTION_LABEL = preload("res://scene/auction_label.tscn")
func update_out_price():
for i in %label_add_pos.get_children():
i.queue_free()
var res_arr
if now_index<auction_price_arr.size():
res_arr=auction_price_arr[now_index]
else:
res_arr=[]
for i in res_arr:
var new_label=AUCTION_LABEL.instantiate()
var str=""
if i[0]>=0:
str+=npc_arr[i[0]]["name"]
else:
str+=""
new_label.add_theme_color_override("font_color",Color.GREEN)
if i[1]<0:
str+="拍下了此物品"
else:
str+=(":出价"+str(i[1]))
pass
new_label.text=str
%label_add_pos.add_child(new_label)
pass
func set_npc_arr_and_item_arr(npc_arr:Array,item_arr:Array):
npc_need_item_arr.clear()
auction_price_arr=[[]]
self.npc_arr=npc_arr
self.item_arr=item_arr
#填写随机需求
for i in npc_arr.size():
npc_need_item_arr.append([])
for j in item_arr:
npc_need_item_arr[i].append(randf_range(0.5,1))
now_auction_item_ind=0
start_turn()
timer.start()
texture_arr.clear()
for i in item_arr:
texture_arr.append(Database.get_texture(i["texture"]))
init_texture()
pass
@onready var timer: Timer = $Timer
func _on_timer_timeout() -> void:
timer.stop()
var res_arr=auction_price_arr[now_auction_item_ind]
if res_arr.size()!=0:
var left_arr=res_arr[res_arr.size()-1]
var left_ind:int=left_arr[0]
var left_price:int=left_arr[1]
if left_ind<0:
Global.add_item_to_bag(item_arr[now_auction_item_ind])
Global.change_currency_game(1,-left_price)
pass
else:
add_item_to_npc(item_arr[now_auction_item_ind],npc_arr[left_ind])
res_arr.append([left_ind,-1])
pass
if now_index==now_auction_item_ind:
right()
now_auction_item_ind+=1
if now_auction_item_ind<item_arr.size():
auction_price_arr.append([])
start_turn()
else:
_on_close_pressed()
pass
#拍卖结束
pass # Replace with function body.
#向NPC背包中添加item
func add_item_to_npc(item_data:Dictionary,npc_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)
#钱 npc_data["gold"]
func _on_out_price_btn_pressed() -> void:
var price=%price_edit.value
var res_arr=auction_price_arr[now_auction_item_ind]
var now_item=item_arr[now_auction_item_ind]
var now_price:int=0
if res_arr.size()==0:
now_price=now_item["price"]/2
else:
now_price=res_arr[res_arr.size()-1][1]
if price-now_price>=min_price_add and price<=Global.get_currency_game(1):
res_arr.append([-1,price])
timer.stop()
update_out_price()
start_turn()
timer.start(30)
pass # Replace with function body.
func get_now_item():
if now_index<item_arr.size():
return item_arr[now_index]
else:
return null
func _on_jump_pressed() -> void:
_on_timer_timeout()
pass # Replace with function body.
signal close
func _on_close_pressed() -> void:
self.hide()
close.emit()
timer.stop()
pass # Replace with function body.
func _on_bag_pressed() -> void:
%warehouse.show()
%warehouse.fresh()
timer.paused=true
pass # Replace with function body.
func _on_warehouse_close() -> void:
timer.paused=false
pass # Replace with function body.