继续开始

This commit is contained in:
2025-06-28 14:39:37 +08:00
parent 12fbe9debf
commit 3ccd7e93ed
923 changed files with 28424 additions and 2362 deletions

View File

@@ -21,6 +21,7 @@ extends Panel
@onready var one_column_plant_btn: Button = $Grid/OneColumnPlantBtn #列种植
@onready var nine_square_plant_btn: Button = $Grid/NineSquarePlantBtn #九宫格种植
@onready var cross_plant_btn: Button = $Grid/CrossPlantBtn #十字法种植
@onready var random_plant_btn: Button = $Grid/RandomPlantBtn #随机种植
# 引用主游戏和其他面板
@onready var main_game = get_node("/root/main")
@@ -36,6 +37,9 @@ const BASE_COST = 500 # 基础费用
const COST_RATE = 0.2 # 种植成本比例20%
const PLANT_INTERVAL = 0.25 # 种植间隔时间
# 种植状态变量
var is_planting = false
var selected_crop_name = ""
@@ -57,6 +61,7 @@ func _ready():
one_column_plant_btn.pressed.connect(_on_one_column_plant_pressed)
nine_square_plant_btn.pressed.connect(_on_nine_square_plant_pressed)
cross_plant_btn.pressed.connect(_on_cross_plant_pressed)
random_plant_btn.pressed.connect(_on_random_plant_pressed)
# 设置按钮提示文本
_setup_button_tooltips()
@@ -68,6 +73,8 @@ func _process(delta):
plant_timer = 0.0
_process_plant_queue()
#=================================一键种植模式=========================================
# 全屏种植按钮处理
func _on_full_screen_plant_pressed():
_request_crop_selection("全屏种植", "选择要种植的作物进行全屏种植")
@@ -88,111 +95,10 @@ func _on_nine_square_plant_pressed():
func _on_cross_plant_pressed():
_start_lot_selection_mode("十字法种植")
# 开始地块选择模式
func _start_lot_selection_mode(plant_type: String):
is_waiting_for_lot_selection = true
pending_plant_type = plant_type
# 隐藏一键种植面板
self.hide()
# 显示提示信息
var tip_message = ""
match plant_type:
"行种植":
tip_message = "请点击一个地块来确定要种植的行"
"列种植":
tip_message = "请点击一个地块来确定要种植的列"
"九宫格种植":
tip_message = "请点击一个地块来确定九宫格种植的中心位置"
"十字法种植":
tip_message = "请点击一个地块来确定十字法种植的中心位置"
_:
tip_message = "请点击一个地块"
Toast.show(tip_message + "按ESC键取消", Color.CYAN)
print("进入地块选择模式:%s" % plant_type)
# 随机种植按钮处理
func _on_random_plant_pressed():
_prepare_random_plant()
# 处理地块选择从MainGame调用
func on_lot_selected(lot_index: int):
if not is_waiting_for_lot_selection:
return false # 不是等待地块选择状态返回false让MainGame正常处理
# 退出地块选择模式
is_waiting_for_lot_selection = false
# 设置选择的地块索引
main_game.selected_lot_index = lot_index
# 开始作物选择
_request_crop_selection(pending_plant_type, "选择要种植的作物进行" + pending_plant_type)
# 清空待处理的种植类型
pending_plant_type = ""
return true # 返回true表示已处理了地块选择
# 请求作物选择
func _request_crop_selection(plant_type: String, tip_message: String):
# 检查背包是否有种子
if main_game.player_bag.size() == 0:
Toast.show("背包中没有种子,请先去商店购买", Color.RED)
return
var has_seeds = false
for item in main_game.player_bag:
if item["count"] > 0:
has_seeds = true
break
if not has_seeds:
Toast.show("背包中没有可用的种子", Color.RED)
return
Toast.show(tip_message, Color.CYAN)
self.hide()
# 设置背包面板的种植模式回调
player_bag_panel.set_planting_mode(plant_type, self)
player_bag_panel.show()
# 背包选择作物回调函数
func on_crop_selected(crop_name: String, plant_type: String):
selected_crop_name = crop_name
# 检查背包中的作物数量
selected_crop_count = _get_crop_count_in_bag(crop_name)
if selected_crop_count <= 0:
Toast.show("背包中没有 " + crop_name + " 种子", Color.RED)
return
print("开始准备一键种植:")
print(" 选择作物:%s" % crop_name)
print(" 种植模式:%s" % plant_type)
print(" 背包数量:%d" % selected_crop_count)
# 根据种植类型生成种植队列
match plant_type:
"全屏种植":
_prepare_full_screen_plant()
"行种植":
_prepare_row_plant()
"列种植":
_prepare_column_plant()
"九宫格种植":
_prepare_nine_square_plant()
"十字法种植":
_prepare_cross_plant()
_:
Toast.show("未知的种植模式:" + plant_type, Color.RED)
print("错误:未知的种植模式:%s" % plant_type)
# 获取背包中指定作物的数量
func _get_crop_count_in_bag(crop_name: String) -> int:
for item in main_game.player_bag:
if item["name"] == crop_name:
return item["count"]
return 0
# 准备全屏种植
func _prepare_full_screen_plant():
@@ -277,6 +183,210 @@ func _prepare_cross_plant():
_start_planting("十字法种植")
# 准备随机种植
func _prepare_random_plant():
# 检查背包是否有种子
if main_game.player_bag.size() == 0:
Toast.show("背包中没有种子,请先去商店购买", Color.RED)
return
# 获取背包中所有的种子类型和数量
var available_seeds = []
for item in main_game.player_bag:
if item["count"] > 0:
# 根据种子数量添加多个相同种子到列表中
for i in range(item["count"]):
available_seeds.append(item["name"])
if available_seeds.size() == 0:
Toast.show("背包中没有可用的种子", Color.RED)
return
# 打乱种子列表
available_seeds.shuffle()
# 获取所有可种植的地块
var available_lots = []
for i in range(len(main_game.farm_lots)):
if _can_plant_at_index(i):
available_lots.append(i)
if available_lots.size() == 0:
Toast.show("没有可种植的地块", Color.YELLOW)
return
# 根据种子数量和可种植地块数量确定实际种植数量
var plant_count = min(available_seeds.size(), available_lots.size())
# 准备种植队列,每个元素包含地块索引和对应的种子名称
plant_queue.clear()
var random_plant_data = [] # 存储地块索引和对应种子的映射
for i in range(plant_count):
var lot_index = available_lots[i]
var seed_name = available_seeds[i]
plant_queue.append(lot_index)
random_plant_data.append({"lot_index": lot_index, "seed_name": seed_name})
# 存储随机种植数据到全局变量,供种植过程使用
if not has_meta("random_plant_data"):
set_meta("random_plant_data", random_plant_data)
else:
set_meta("random_plant_data", random_plant_data)
# 计算总费用(基于所有要种植的种子)
var total_crop_cost = 0
for data in random_plant_data:
var crop_data = main_game.can_planted_crop.get(data["seed_name"], {})
var crop_price = crop_data.get("花费", 0)
total_crop_cost += crop_price
var service_fee = int(total_crop_cost * COST_RATE) + BASE_COST
print("随机种植费用计算:")
var seed_names = []
for data in random_plant_data:
seed_names.append(data["seed_name"])
print(" 将要种植的种子:%s" % str(seed_names))
print(" 种植数量:%d 个地块" % plant_count)
print(" 作物总成本:%d" % total_crop_cost)
print(" 服务费率:%.1f%%" % (COST_RATE * 100))
print(" 基础费用:%d" % BASE_COST)
print(" 总服务费:%d" % service_fee)
print(" 玩家当前金钱:%d" % main_game.money)
# 检查金钱是否足够支付服务费
if main_game.money < service_fee:
Toast.show("金钱不足!随机种植需要服务费 %d 元(当前:%d 元)" % [service_fee, main_game.money], Color.RED)
return
# 扣除服务费
main_game.money -= service_fee
main_game._update_ui()
# 开始种植
is_planting = true
current_plant_index = 0
plant_timer = 0.0
# 更新按钮状态为种植中
_update_buttons_planting_state(true)
# 计算不同种子的数量
var unique_seeds = {}
for data in random_plant_data:
unique_seeds[data["seed_name"]] = true
var unique_seed_count = unique_seeds.size()
Toast.show("开始随机种植,预计种植 %d 个地块,使用 %d 种不同种子,服务费 %d" % [plant_count, unique_seed_count, service_fee], Color.GREEN)
#=================================一键种植模式=========================================
# 开始地块选择模式
func _start_lot_selection_mode(plant_type: String):
is_waiting_for_lot_selection = true
pending_plant_type = plant_type
# 隐藏一键种植面板
self.hide()
# 显示提示信息
var tip_message = ""
match plant_type:
"行种植":
tip_message = "请点击一个地块来确定要种植的行"
"列种植":
tip_message = "请点击一个地块来确定要种植的列"
"九宫格种植":
tip_message = "请点击一个地块来确定九宫格种植的中心位置"
"十字法种植":
tip_message = "请点击一个地块来确定十字法种植的中心位置"
_:
tip_message = "请点击一个地块"
Toast.show(tip_message + "按ESC键取消", Color.CYAN)
print("进入地块选择模式:%s" % plant_type)
# 处理地块选择从MainGame调用
func on_lot_selected(lot_index: int):
if not is_waiting_for_lot_selection:
return false # 不是等待地块选择状态返回false让MainGame正常处理
# 退出地块选择模式
is_waiting_for_lot_selection = false
# 设置选择的地块索引
main_game.selected_lot_index = lot_index
# 开始作物选择
_request_crop_selection(pending_plant_type, "选择要种植的作物进行" + pending_plant_type)
# 清空待处理的种植类型
pending_plant_type = ""
return true # 返回true表示已处理了地块选择
# 请求作物选择
func _request_crop_selection(plant_type: String, tip_message: String):
# 检查背包是否有种子
if main_game.player_bag.size() == 0:
Toast.show("背包中没有种子,请先去商店购买", Color.RED)
return
var has_seeds = false
for item in main_game.player_bag:
if item["count"] > 0:
has_seeds = true
break
if not has_seeds:
Toast.show("背包中没有可用的种子", Color.RED)
return
Toast.show(tip_message, Color.CYAN)
self.hide()
# 设置背包面板的种植模式回调
player_bag_panel.set_planting_mode(plant_type, self)
player_bag_panel.show()
# 背包选择作物回调函数
func on_crop_selected(crop_name: String, plant_type: String):
selected_crop_name = crop_name
# 检查背包中的作物数量
selected_crop_count = _get_crop_count_in_bag(crop_name)
if selected_crop_count <= 0:
Toast.show("背包中没有 " + crop_name + " 种子", Color.RED)
return
# 根据种植类型生成种植队列
match plant_type:
"全屏种植":
_prepare_full_screen_plant()
"行种植":
_prepare_row_plant()
"列种植":
_prepare_column_plant()
"九宫格种植":
_prepare_nine_square_plant()
"十字法种植":
_prepare_cross_plant()
_:
Toast.show("未知的种植模式:" + plant_type, Color.RED)
print("错误:未知的种植模式:%s" % plant_type)
# 获取背包中指定作物的数量
func _get_crop_count_in_bag(crop_name: String) -> int:
for item in main_game.player_bag:
if item["name"] == crop_name:
return item["count"]
return 0
# 开始种植
func _start_planting(plant_type: String):
if plant_queue.size() == 0:
@@ -322,7 +432,6 @@ func _start_planting(plant_type: String):
_update_buttons_planting_state(true)
Toast.show("开始%s,预计种植 %d 个地块,服务费 %d" % [plant_type, plant_queue.size(), total_cost], Color.GREEN)
print("开始%s,种植队列: %s" % [plant_type, str(plant_queue)])
# 处理种植队列
func _process_plant_queue():
@@ -333,36 +442,56 @@ func _process_plant_queue():
var lot_index = plant_queue[current_plant_index]
# 检查是否还有种子和该地块是否仍可种植
if _get_crop_count_in_bag(selected_crop_name) > 0 and _can_plant_at_index(lot_index):
# 执行种植
_plant_at_index(lot_index)
# 检查是否是随机种植模式
if has_meta("random_plant_data"):
# 随机种植模式:每个地块种植不同的种子
var random_plant_data = get_meta("random_plant_data")
if current_plant_index < random_plant_data.size():
var plant_data = random_plant_data[current_plant_index]
var seed_name = plant_data["seed_name"]
# 检查是否还有该种子和该地块是否仍可种植
if _get_crop_count_in_bag(seed_name) > 0 and _can_plant_at_index(lot_index):
# 执行随机种植
_plant_at_index_with_seed(lot_index, seed_name)
else:
# 普通种植模式:所有地块种植相同的种子
# 检查是否还有种子和该地块是否仍可种植
if _get_crop_count_in_bag(selected_crop_name) > 0 and _can_plant_at_index(lot_index):
# 执行种植
_plant_at_index(lot_index)
current_plant_index += 1
# 完成种植
func _finish_planting():
is_planting = false
var planted_count = current_plant_index
var success_count = min(planted_count, selected_crop_count)
# 恢复按钮状态
_update_buttons_planting_state(false)
Toast.show("一键种植完成!成功种植 %d 个地块" % success_count, Color.GREEN)
print("一键种植完成,成功种植了 %d 个地块" % success_count)
# 清空队列
plant_queue.clear()
current_plant_index = 0
# 清理随机种植的元数据
if has_meta("random_plant_data"):
remove_meta("random_plant_data")
# 在指定索引处种植
func _plant_at_index(lot_index: int):
if network_manager and network_manager.sendPlantCrop(lot_index, selected_crop_name):
print("发送种植请求:地块 %d,作物 %s" % [lot_index, selected_crop_name])
pass
else:
print("发送种植请求失败:地块 %d" % lot_index)
# 在指定索引处种植指定种子(用于随机种植)
func _plant_at_index_with_seed(lot_index: int, seed_name: String):
if network_manager and network_manager.sendPlantCrop(lot_index, seed_name):
pass
else:
print("发送种植请求失败:地块 %d,种子 %s" % [lot_index, seed_name])
# 检查指定索引的地块是否可以种植
func _can_plant_at_index(index: int) -> bool:
if index < 0 or index >= len(main_game.farm_lots):
@@ -403,6 +532,7 @@ func _setup_button_tooltips():
one_column_plant_btn.tooltip_text = "在选定地块所在的列中从上到下依次种植\n点击此按钮后,再点击农场中的任意地块确定列位置\n费用种植总成本的20% + 500元基础费"
nine_square_plant_btn.tooltip_text = "以选定地块为中心的3x3九宫格范围内种植\n点击此按钮后,再点击农场中的任意地块确定中心位置\n费用种植总成本的20% + 500元基础费"
cross_plant_btn.tooltip_text = "以选定地块为中心的十字形(上下左右+中心)种植\n点击此按钮后,再点击农场中的任意地块确定中心位置\n费用种植总成本的20% + 500元基础费"
random_plant_btn.tooltip_text = "获取背包中所有种子并打乱后随机种植到所有空地块\n自动使用背包中的各种种子,每个地块种植不同种子\n费用种植总成本的20% + 500元基础费"
# 更新按钮状态
func _update_buttons_planting_state(planting: bool):
@@ -413,12 +543,14 @@ func _update_buttons_planting_state(planting: bool):
one_column_plant_btn.disabled = true
nine_square_plant_btn.disabled = true
cross_plant_btn.disabled = true
random_plant_btn.disabled = true
full_screen_plant_btn.text = "种植中..."
one_row_plant_btn.text = "种植中..."
one_column_plant_btn.text = "种植中..."
nine_square_plant_btn.text = "种植中..."
cross_plant_btn.text = "种植中..."
random_plant_btn.text = "种植中..."
else:
# 种植完成,恢复按钮状态
full_screen_plant_btn.disabled = false
@@ -426,12 +558,14 @@ func _update_buttons_planting_state(planting: bool):
one_column_plant_btn.disabled = false
nine_square_plant_btn.disabled = false
cross_plant_btn.disabled = false
random_plant_btn.disabled = false
full_screen_plant_btn.text = "全屏种植"
one_row_plant_btn.text = "行种植"
one_column_plant_btn.text = "列种植"
nine_square_plant_btn.text = "九宫格种植"
cross_plant_btn.text = "十字法种植"
nine_square_plant_btn.text = "九宫格\n种植"
cross_plant_btn.text = "十字法\n种植"
random_plant_btn.text = "随机\n种植"
# 取消地块选择模式
func cancel_lot_selection():
@@ -439,7 +573,6 @@ func cancel_lot_selection():
is_waiting_for_lot_selection = false
pending_plant_type = ""
Toast.show("已取消地块选择", Color.YELLOW)
print("用户取消了地块选择")
# 重新显示一键种植面板
self.show()
@@ -448,10 +581,9 @@ func stop_planting():
if is_planting:
is_planting = false
Toast.show("一键种植已停止", Color.YELLOW)
print("用户停止了一键种植")
_finish_planting()
#这个不用管
#关闭一键种植面板
func _on_quit_button_pressed() -> void:
# 如果正在种植,先停止种植
if is_planting:
@@ -459,6 +591,6 @@ func _on_quit_button_pressed() -> void:
# 如果正在等待地块选择,取消选择
elif is_waiting_for_lot_selection:
cancel_lot_selection()
return # cancel_lot_selection已经重新显示了面板不需要hide
return
self.hide()
pass