This commit is contained in:
2024-11-04 17:25:09 +08:00
parent c87de7de5d
commit 471d425b23
30 changed files with 2319 additions and 584 deletions
+254 -175
View File
@@ -10,18 +10,15 @@ var player_scene = preload("res://scenes/player.tscn")
var current_turn_index = 0
@export var players = []
var game_started = false
var max_message_input_char = 51
var max_players = 4
var bots = []
@export var turn_based_mode: bool = true # Toggle between turn-based and realtime modes
var bot_move_timer: float = 0.0 # Timer for bot movement in realtime mode
const BOT_MOVE_INTERVAL: float = 2.0 # Time between bot movements in realtime mode
@export var turn_based_mode: bool = true
var bot_move_timer: float = 0.0
const BOT_MOVE_INTERVAL: float = 2.0
# Bot movement state tracking
var moving_bots = {} # Dictionary to track which bots are currently moving
var moving_bots = {}
# Action State Management
enum ActionState {
NONE,
MOVING,
@@ -33,7 +30,6 @@ enum ActionState {
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
@@ -42,6 +38,14 @@ var current_action_state = ActionState.NONE
@onready var arrange_button = $ActionMenu/ActionButtonContainer/ArrangeButton
@onready var playerboard_ui = $PlayerboardUI
const item_tex = [
preload("res://assets/textures/player_board_and_blue_print/tile_null.tres"),
preload("res://assets/textures/player_board_and_blue_print/tile_heart.tres"),
preload("res://assets/textures/player_board_and_blue_print/tile_diamond.tres"),
preload("res://assets/textures/player_board_and_blue_print/tile_star.tres"),
preload("res://assets/textures/player_board_and_blue_print/tile_coin.tres")
]
func _ready():
multiplayer_peer.peer_connected.connect(_on_peer_connected)
multiplayer_peer.peer_disconnected.connect(_on_peer_disconnected)
@@ -49,87 +53,132 @@ func _ready():
setup_playerboard_ui()
func _process(delta):
if multiplayer.is_server():
if game_started:
if turn_based_mode:
rpc("sync_turn_index", current_turn_index)
else:
# Handle realtime bot movement
bot_move_timer += delta
if bot_move_timer >= BOT_MOVE_INTERVAL:
bot_move_timer = 0.0
for bot_id in bots:
if not moving_bots.get(bot_id, false): # Only move if bot isn't already moving
move_bot(bot_id)
if multiplayer.is_server() and game_started:
if turn_based_mode:
rpc("sync_turn_index", current_turn_index)
else:
bot_move_timer += delta
if bot_move_timer >= BOT_MOVE_INTERVAL:
bot_move_timer = 0.0
var current_bots = bots.duplicate()
for bot_id in current_bots:
if not moving_bots.get(bot_id, false):
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))
put_button.pressed.connect(func():
if local_player_character:
local_player_character.handle_put_action()
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))
arrange_button.pressed.connect(func():
if local_player_character and local_player_character.action_points >= 2:
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)
var highlight_rect = TextureRect.new()
var hr_tex = load("res://assets/models/pboard/HighlightRect.tres")
var select_rect = TextureRect.new()
var sr_tex = load("res://assets/models/pboard/SelectRect.tres")
var adjacent_rect = TextureRect.new()
var ar_tex = load("res://assets/models/pboard/AdjacentRect.tres")
slot.custom_minimum_size = Vector2(36, 36)
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)
slot.texture = item_tex[0]
playerboard_ui.add_child(slot, true)
highlight_rect.texture = hr_tex
highlight_rect.size = Vector2(36, 36)
select_rect.texture = sr_tex
select_rect.size = Vector2(36, 36)
adjacent_rect.texture = ar_tex
adjacent_rect.size = Vector2(36, 36)
slot.add_child(highlight_rect)
slot.add_child(select_rect)
slot.add_child(adjacent_rect)
slot.get_child(0).hide()
slot.get_child(1).hide()
slot.get_child(2).hide()
func set_action_state(new_state):
if not local_player_character:
if not local_player_character or not local_player_character.is_multiplayer_authority() or current_action_state == new_state or local_player_character.action_points <= 0:
return
current_action_state = new_state
# Clear previous highlights
current_action_state = new_state
local_player_character.clear_highlights()
local_player_character.clear_playerboard_highlights()
match new_state:
ActionState.MOVING:
local_player_character.highlight_movement_range()
ActionState.GRABBING:
local_player_character.highlight_adjacent_cells()
if local_player_character.has_item_at_current_position():
local_player_character.highlighted_cells.append(local_player_character.current_position)
local_player_character.enhanced_gridmap.set_cell_item(
Vector3i(local_player_character.current_position.x, 0, local_player_character.current_position.y),
local_player_character.enhanced_gridmap.hover_item
)
ActionState.PUTTING:
local_player_character.highlight_empty_adjacent_cells()
local_player_character.highlight_occupied_playerboard_slots()
ActionState.RANDOMIZING:
local_player_character.highlight_random_valid_cells()
ActionState.ARRANGING:
show_arrangement_ui()
local_player_character.highlight_occupied_playerboard_slots()
func update_button_states():
if not local_player_character:
if not local_player_character or local_player_character.is_in_group("Bots"):
move_button.visible = false
grab_button.visible = false
put_button.visible = false
randomize_button.visible = false
arrange_button.visible = false
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
move_button.visible = true
grab_button.visible = true
put_button.visible = true
randomize_button.visible = true
arrange_button.visible = true
move_button.disabled = local_player_character.is_player_moving or local_player_character.has_performed_action
grab_button.disabled = not local_player_character.has_item_at_current_position()
put_button.disabled = not (local_player_character.has_items_in_playerboard() and not local_player_character.has_item_at_current_position())
randomize_button.disabled = local_player_character.has_performed_action
arrange_button.disabled = not local_player_character.has_items_in_playerboard()
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)
if not local_player_character:
return
match current_action_state:
ActionState.ARRANGING:
local_player_character.arrange_playerboard_item(slot_index)
ActionState.GRABBING:
local_player_character.handle_playerboard_slot_selected(slot_index)
ActionState.PUTTING:
local_player_character.handle_put_slot_selected(slot_index)
func update_playerboard_ui():
if not local_player_character:
@@ -138,14 +187,20 @@ func update_playerboard_ui():
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)
slot.texture = item_tex[0]
match item:
7: slot.texture = item_tex[1]
8: slot.texture = item_tex[2]
9: slot.texture = item_tex[3]
10: slot.texture = item_tex[4]
func update_playerboard_highlights(highlighted_slots: Array):
for i in range(playerboard_ui.get_child_count()):
var slot = playerboard_ui.get_child(i)
if slot.get_child_count() > 1:
slot.get_child(1).visible = highlighted_slots.has(i)
func show_arrangement_ui():
if playerboard_ui:
@@ -162,7 +217,6 @@ func _on_host_pressed():
add_player_character(1)
players.append(1)
# Add bots to fill remaining slots
for i in range(2, max_players + 1):
add_bot(i)
@@ -194,8 +248,15 @@ func add_player_character(peer_id):
connected_peer_ids.append(peer_id)
var player_character = player_scene.instantiate()
player_character.set_multiplayer_authority(peer_id)
if multiplayer.is_server() and bots.size() > 0:
var bot_to_replace = get_node_or_null(str(bots[0]))
if bot_to_replace:
player_character.current_position = bot_to_replace.current_position
add_child(player_character)
player_character.add_to_group("Players", true)
if peer_id == multiplayer.get_unique_id():
local_player_character = player_character
update_button_states()
@@ -209,7 +270,7 @@ func add_bot(bot_id):
@rpc("call_local")
func create_bot(bot_id):
var bot_character = player_scene.instantiate()
bot_character.set_multiplayer_authority(1) # Set server as authority for bots
bot_character.set_multiplayer_authority(1)
bot_character.name = str(bot_id)
add_child(bot_character)
bot_character.add_to_group("Players", true)
@@ -224,17 +285,17 @@ func replace_bot_with_player(player_id):
var bot_id = bots.pop_front()
players.erase(bot_id)
players.append(player_id)
moving_bots.erase(bot_id) # Remove bot from moving tracking
moving_bots.erase(bot_id)
rpc("remove_bot", bot_id)
rpc("sync_players", players)
@rpc("call_local")
func remove_bot(bot_id):
var bot_node = get_node(str(bot_id))
var bot_node = get_node_or_null(str(bot_id))
if bot_node:
bot_node.queue_free()
func get_next_available_bot_id():
func get_next_available_bot_id() -> int:
for i in range(2, max_players + 1):
if not i in players:
return i
@@ -256,7 +317,6 @@ func sync_game_state(current_players, current_bots, is_game_started, is_turn_bas
game_started = is_game_started
turn_based_mode = is_turn_based
# Create bot characters for existing bots
for bot_id in bots:
if not has_node(str(bot_id)):
create_bot(bot_id)
@@ -288,9 +348,7 @@ func sync_players(new_players):
func next_turn():
if multiplayer.is_server() and turn_based_mode:
current_turn_index += 1
if current_turn_index >= players.size():
current_turn_index = 0
current_turn_index = (current_turn_index + 1) % players.size()
rpc("set_current_turn", players[current_turn_index])
if players[current_turn_index] in bots:
move_bot(players[current_turn_index])
@@ -311,124 +369,145 @@ func set_current_turn(player_id):
if not turn_based_mode:
return
var players = get_tree().get_nodes_in_group("Players")
for player in players:
player.is_my_turn = (player.name == str(player_id))
if player.is_my_turn:
for player in get_tree().get_nodes_in_group("Players"):
if player.name == str(player_id):
player.is_my_turn = true
player.action_points = 2
player.has_moved_this_turn = false
player.has_performed_action = false
player.start_turn()
else:
player.is_my_turn = false
func end_current_turn():
if multiplayer.is_server():
next_turn()
rpc("sync_turn_index", current_turn_index)
func _on_message_input_text_submitted(new_text):
if new_text.length() > max_message_input_char:
new_text = new_text.substr(0, max_message_input_char) + " ... "
local_player_character.rpc("display_message", new_text)
$MessageInput.text = ""
$MessageInput.release_focus()
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()
if not multiplayer.is_server() or moving_bots.get(bot_id, false):
return
var bot = get_node_or_null(str(bot_id))
if not is_instance_valid(bot) or bot.is_player_moving:
moving_bots.erase(bot_id)
return
if bot.action_points <= 0:
if turn_based_mode:
moving_bots[bot_id] = false
end_current_turn()
else:
bot.action_points = 2
bot.has_moved_this_turn = false
bot.has_performed_action = false
moving_bots[bot_id] = false
await get_tree().create_timer(0.5).timeout
move_bot(bot_id)
return
moving_bots[bot_id] = true
await get_tree().create_timer(0.5).timeout
if not is_instance_valid(bot):
moving_bots.erase(bot_id)
return
var best_move = evaluate_bot_move(bot)
execute_bot_move(bot, bot_id, best_move)
func bot_movement_completed(bot_id):
if multiplayer.is_server():
moving_bots[bot_id] = false
func execute_bot_move(bot, bot_id, move):
match move.action:
"arrange":
if bot.action_points >= 2:
bot.arrange_playerboard_item(bot.find_best_arrangement_slot())
bot.action_points -= 2
moving_bots[bot_id] = false
if bot.action_points > 0 or not turn_based_mode:
move_bot(bot_id)
elif turn_based_mode:
end_current_turn()
"grab", "put":
var success = bot.grab_item(move.position) if move.action == "grab" else bot.put_item(move.position)
if success:
bot.action_points -= 1
moving_bots[bot_id] = false
if bot.action_points > 0 or not turn_based_mode:
move_bot(bot_id)
elif turn_based_mode:
end_current_turn()
"move":
if is_instance_valid(bot) and bot.action_points >= 1:
var path = bot.enhanced_gridmap.find_path(Vector2(bot.current_position), Vector2(move.position))
if path.size() > 1:
path.pop_front()
var trimmed_path = path.slice(0, bot.movement_range)
bot.rotate_towards_target(move.position)
bot.move_bot_along_path(trimmed_path, bot_id)
bot.action_points -= 1
else:
moving_bots[bot_id] = false
if bot.action_points > 0 or not turn_based_mode:
move_bot(bot_id)
elif turn_based_mode:
end_current_turn()
else:
moving_bots[bot_id] = false
if bot.action_points > 0 or not turn_based_mode:
move_bot(bot_id)
elif turn_based_mode:
end_current_turn()
_:
moving_bots[bot_id] = false
if turn_based_mode:
end_current_turn()
else:
move_bot(bot_id)
func evaluate_bot_move(bot: Node) -> Dictionary:
var best_move = {
"action": "none",
"position": bot.current_position,
"value": -1
}
if not is_instance_valid(bot) or bot.action_points <= 0:
return { "action": "none", "position": Vector2i.ZERO, "value": -1 }
var moves = []
# If no action performed yet, prioritize grab/put
if not bot.has_performed_action:
# Check current position for grabbing
var current_cell = Vector3i(bot.current_position.x, 1, bot.current_position.y)
var current_item = bot.enhanced_gridmap.get_cell_item(current_cell)
if bot.action_points >= 2 and bot.check_playerboard_arrangement(bot):
moves.append({ "action": "arrange", "position": bot.current_position, "value": 20 })
if bot.action_points >= 1:
var put_position = bot.find_best_put_position(bot)
if put_position != Vector2i(-1, -1):
moves.append({ "action": "put", "position": put_position, "value": 18 })
if bot.action_points >= 1 and not bot.playerboard_is_full():
var grab_position = bot.find_best_grab_position()
if grab_position != Vector2i(-1, -1):
moves.append({ "action": "grab", "position": grab_position, "value": 15 })
if bot.action_points >= 1 and not bot.has_moved_this_turn:
var random_pos = bot.find_random_valid_position_in_range()
if random_pos != bot.current_position:
moves.append({ "action": "move", "position": random_pos, "value": 5 })
moves.sort_custom(func(a, b): return a.value > b.value)
return moves[0] if moves.size() > 0 else { "action": "none", "position": bot.current_position, "value": 0 }
func bot_movement_completed(bot_id):
if not multiplayer.is_server():
return
# Evaluate grabbing current item if it matches goals
if current_item != -1 and current_item in bot.goals and bot.playerboard.find(-1) != -1:
return {
"action": "grab",
"position": bot.current_position,
"value": 10
}
# Evaluate putting item from playerboard
for goal in bot.goals:
var item_index = bot.playerboard.find(goal)
if item_index != -1 and current_item == -1:
return {
"action": "put",
"position": bot.current_position,
"value": 15
}
moving_bots[bot_id] = false
var bot = get_node_or_null(str(bot_id))
if not is_instance_valid(bot):
moving_bots.erase(bot_id)
return
if bot.action_points > 0 or not turn_based_mode:
await get_tree().create_timer(0.5).timeout
if is_instance_valid(bot):
move_bot(bot_id)
else:
# Bot has performed action, look for movement
var neighbors = bot.enhanced_gridmap.get_neighbors(bot.current_position, 1)
for neighbor in neighbors:
if not neighbor.is_walkable:
continue
if not bot.is_within_movement_range(neighbor.position):
continue
var cell = Vector3i(neighbor.position.x, 1, neighbor.position.y)
var item = bot.enhanced_gridmap.get_cell_item(cell)
# Evaluate position for next turn
if item in bot.goals or item == -1:
var value = 8
if value > best_move.value:
best_move = {
"action": "move",
"position": neighbor.position,
"value": value
}
return best_move
end_current_turn()