This commit is contained in:
2024-10-25 13:18:37 +08:00
parent b8a1569e33
commit c87de7de5d
4 changed files with 482 additions and 152 deletions
+164 -84
View File
@@ -21,9 +21,32 @@ const BOT_MOVE_INTERVAL: float = 2.0 # Time between bot movements in realtime m
# Bot movement state tracking
var moving_bots = {} # Dictionary to track which bots are currently moving
# Action State Management
enum ActionState {
NONE,
MOVING,
GRABBING,
PUTTING,
RANDOMIZING,
ARRANGING
}
var current_action_state = ActionState.NONE
# UI References
@onready var action_menu = $ActionMenu
@onready var move_button = $ActionMenu/ActionButtonContainer/MoveButton
@onready var grab_button = $ActionMenu/ActionButtonContainer/GrabButton
@onready var put_button = $ActionMenu/ActionButtonContainer/PutButton
@onready var randomize_button = $ActionMenu/ActionButtonContainer/RandomizeButton
@onready var arrange_button = $ActionMenu/ActionButtonContainer/ArrangeButton
@onready var playerboard_ui = $PlayerboardUI
func _ready():
multiplayer_peer.peer_connected.connect(_on_peer_connected)
multiplayer_peer.peer_disconnected.connect(_on_peer_disconnected)
setup_action_buttons()
setup_playerboard_ui()
func _process(delta):
if multiplayer.is_server():
@@ -39,6 +62,96 @@ func _process(delta):
if not moving_bots.get(bot_id, false): # Only move if bot isn't already moving
move_bot(bot_id)
func setup_action_buttons():
move_button.pressed.connect(func(): set_action_state(ActionState.MOVING))
grab_button.pressed.connect(func(): set_action_state(ActionState.GRABBING))
put_button.pressed.connect(func(): set_action_state(ActionState.PUTTING))
randomize_button.pressed.connect(func(): set_action_state(ActionState.RANDOMIZING))
arrange_button.pressed.connect(func(): set_action_state(ActionState.ARRANGING))
func setup_playerboard_ui():
# Clear existing children
for child in playerboard_ui.get_children():
child.queue_free()
# Setup grid
playerboard_ui.columns = 5
# Create slots
for i in range(25):
var slot = TextureRect.new()
slot.custom_minimum_size = Vector2(40, 40)
slot.gui_input.connect(func(event): _on_playerboard_slot_clicked(event, i))
# Add a colored rectangle as background
var style = StyleBoxFlat.new()
style.bg_color = Color(0.2, 0.2, 0.2, 1.0)
style.border_color = Color(0.4, 0.4, 0.4, 1.0)
slot.add_theme_stylebox_override("panel", style)
playerboard_ui.add_child(slot)
func set_action_state(new_state):
if not local_player_character:
return
current_action_state = new_state
# Clear previous highlights
local_player_character.clear_highlights()
match new_state:
ActionState.MOVING:
local_player_character.highlight_movement_range()
ActionState.GRABBING:
local_player_character.highlight_adjacent_cells()
ActionState.PUTTING:
local_player_character.highlight_empty_adjacent_cells()
ActionState.RANDOMIZING:
local_player_character.highlight_random_valid_cells()
ActionState.ARRANGING:
show_arrangement_ui()
func update_button_states():
if not local_player_character:
return
var can_move = not local_player_character.is_player_moving and not local_player_character.has_performed_action
var can_grab = local_player_character.has_item_at_current_position()
var can_put = local_player_character.has_items_in_playerboard() and not local_player_character.has_item_at_current_position()
var can_randomize = not local_player_character.has_performed_action
var can_arrange = local_player_character.has_items_in_playerboard()
move_button.disabled = not can_move
grab_button.disabled = not can_grab
put_button.disabled = not can_put
randomize_button.disabled = not can_randomize
arrange_button.disabled = not can_arrange
func _on_playerboard_slot_clicked(event, slot_index):
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
if current_action_state == ActionState.ARRANGING and local_player_character:
local_player_character.arrange_playerboard_item(slot_index)
func update_playerboard_ui():
if not local_player_character:
return
for i in range(25):
var slot = playerboard_ui.get_child(i)
var item = local_player_character.playerboard[i]
if item != -1:
# Update slot appearance based on item
slot.modulate = Color(1, 1, 1, 1)
# You can set different colors or textures based on item type
slot.self_modulate = Color(0.5 + (item * 0.1), 0.7, 0.3, 1.0)
else:
slot.modulate = Color(0.5, 0.5, 0.5, 0.5)
slot.self_modulate = Color(1, 1, 1, 1)
func show_arrangement_ui():
if playerboard_ui:
playerboard_ui.visible = true
update_playerboard_ui()
func _on_host_pressed():
$NetworkInfo/NetworkSideDisplay.text = "Server"
$Menu.visible = false
@@ -85,6 +198,8 @@ func add_player_character(peer_id):
player_character.add_to_group("Players", true)
if peer_id == multiplayer.get_unique_id():
local_player_character = player_character
update_button_states()
update_playerboard_ui()
func add_bot(bot_id):
if multiplayer.is_server():
@@ -214,9 +329,55 @@ func _on_message_input_text_submitted(new_text):
$MessageInput.text = ""
$MessageInput.release_focus()
# ---------
# Bot Logic
# ---------
func move_bot(bot_id):
if multiplayer.is_server():
if moving_bots.get(bot_id, false):
return
var bot = get_node(str(bot_id))
if bot and (turn_based_mode or not bot.is_player_moving):
moving_bots[bot_id] = true
var best_move = evaluate_bot_move(bot)
match best_move.action:
"grab":
if bot.grab_item():
bot.has_performed_action = true
moving_bots[bot_id] = false
# Don't end turn, allow movement after grab
move_bot(bot_id) # Try to move after grabbing
"put":
if bot.put_item():
bot.has_performed_action = true
moving_bots[bot_id] = false
# Don't end turn, allow movement after put
move_bot(bot_id) # Try to move after putting
"move":
if bot.has_performed_action:
var path = bot.enhanced_gridmap.find_path(Vector2(bot.current_position), Vector2(best_move.position))
if path.size() > 1:
path.pop_front()
var trimmed_path = path.slice(0, bot.movement_range)
bot.rotate_towards_target(best_move.position)
bot.move_bot_along_path(trimmed_path, bot_id)
else:
moving_bots[bot_id] = false
if turn_based_mode:
end_current_turn()
else:
moving_bots[bot_id] = false
if turn_based_mode:
end_current_turn()
_:
moving_bots[bot_id] = false
if turn_based_mode:
end_current_turn()
func bot_movement_completed(bot_id):
if multiplayer.is_server():
moving_bots[bot_id] = false
func evaluate_bot_move(bot: Node) -> Dictionary:
var best_move = {
"action": "none",
@@ -271,84 +432,3 @@ func evaluate_bot_move(bot: Node) -> Dictionary:
}
return best_move
#func move_bot(bot_id):
#if multiplayer.is_server():
#if moving_bots.get(bot_id, false):
#return # Skip if bot is already moving
#
#var bot = get_node(str(bot_id))
#if bot and (turn_based_mode or not bot.is_player_moving):
#moving_bots[bot_id] = true # Mark bot as moving
#var target_position = bot.find_random_valid_position_in_range() # Use the new function
#if target_position != bot.current_position:
#var path = bot.enhanced_gridmap.find_path(Vector2(bot.current_position), Vector2(target_position))
#
#if path.size() > 1:
#path.pop_front()
## Trim path to respect movement range
#var trimmed_path = path.slice(0, bot.movement_range)
#bot.move_bot_along_path(trimmed_path, bot_id)
#else:
#print("No valid path found for bot")
#moving_bots[bot_id] = false
#if turn_based_mode:
#end_current_turn()
#else:
#print("No new valid position found for bot")
#moving_bots[bot_id] = false
#if turn_based_mode:
#end_current_turn()
#else:
#print("Bot not found or is moving")
#if turn_based_mode:
#end_current_turn()
func move_bot(bot_id):
if multiplayer.is_server():
if moving_bots.get(bot_id, false):
return
var bot = get_node(str(bot_id))
if bot and (turn_based_mode or not bot.is_player_moving):
moving_bots[bot_id] = true
var best_move = evaluate_bot_move(bot)
match best_move.action:
"grab":
if bot.grab_item():
bot.has_performed_action = true
moving_bots[bot_id] = false
# Don't end turn, allow movement after grab
move_bot(bot_id) # Try to move after grabbing
"put":
if bot.put_item():
bot.has_performed_action = true
moving_bots[bot_id] = false
# Don't end turn, allow movement after put
move_bot(bot_id) # Try to move after putting
"move":
if bot.has_performed_action:
var path = bot.enhanced_gridmap.find_path(Vector2(bot.current_position), Vector2(best_move.position))
if path.size() > 1:
path.pop_front()
var trimmed_path = path.slice(0, bot.movement_range)
bot.rotate_towards_target(best_move.position)
bot.move_bot_along_path(trimmed_path, bot_id)
else:
moving_bots[bot_id] = false
if turn_based_mode:
end_current_turn()
else:
moving_bots[bot_id] = false
if turn_based_mode:
end_current_turn()
_:
moving_bots[bot_id] = false
if turn_based_mode:
end_current_turn()
# Called when a bot finishes moving
func bot_movement_completed(bot_id):
if multiplayer.is_server():
moving_bots[bot_id] = false