Add the ActionState. Random

This commit is contained in:
2025-03-04 17:27:11 +08:00
parent 837d522408
commit 9c162c29f8
2 changed files with 109 additions and 119 deletions
+65 -64
View File
@@ -223,7 +223,7 @@ func update_button_states():
arrange_button.visible = true arrange_button.visible = true
# Only keep randomize button's disable condition # Only keep randomize button's disable condition
randomize_button.disabled = local_player_character.has_performed_action #randomize_button.disabled = local_player_character.has_performed_action
# Remove disabled conditions for other buttons: # Remove disabled conditions for other buttons:
move_button.disabled = false move_button.disabled = false
@@ -244,27 +244,6 @@ func _on_playerboard_slot_clicked(event, slot_index):
ActionState.PUTTING: ActionState.PUTTING:
local_player_character.handle_put_slot_selected(slot_index) local_player_character.handle_put_slot_selected(slot_index)
#func update_goals_ui():
#if not local_player_character:
#return
#
#for i in range(9): # 9 slots in the goals UI
#var slot = $PlayergoalsUI.get_child(i)
#var goal_value = local_player_character.goals[i]
#
## Hide all tile textures first
#slot.get_node("TileHeart").hide()
#slot.get_node("TileDiamond").hide()
#slot.get_node("TileStar").hide()
#slot.get_node("TileCoin").hide()
#
## Show the appropriate texture based on goal value
#match goal_value:
#7: slot.get_node("TileHeart").show()
#8: slot.get_node("TileDiamond").show()
#9: slot.get_node("TileStar").show()
#10: slot.get_node("TileCoin").show()
func update_playerboard_ui(): func update_playerboard_ui():
if not local_player_character: if not local_player_character:
return return
@@ -349,48 +328,6 @@ func _on_join_pressed():
await get_tree().create_timer(2.0).timeout await get_tree().create_timer(2.0).timeout
rpc_id(1, "request_full_player_sync", multiplayer.get_unique_id()) rpc_id(1, "request_full_player_sync", multiplayer.get_unique_id())
#func _on_peer_connected(new_peer_id):
#if multiplayer.is_server():
## Increase delay to ensure scene is ready
#await get_tree().create_timer(1.5).timeout
#
## First sync game state
#rpc_id(new_peer_id, "sync_game_state", players, bots, game_started, turn_based_mode)
#rpc_id(new_peer_id, "sync_preset_goals", preset_goals)
#
## Wait a bit for the client to process state
#await get_tree().create_timer(0.5).timeout
#
## Then sync all existing players in order
#var sorted_players = players.duplicate()
#sorted_players.sort()
#for peer_id in sorted_players:
#if peer_id != new_peer_id:
## First ensure player exists
#var player = get_node_or_null(str(peer_id))
#if player:
## Sync player's full state
#var player_data = {
#"position": player.current_position,
#"goals": player.goals,
#"playerboard": player.playerboard
#}
#rpc_id(new_peer_id, "sync_existing_player", peer_id, player_data)
#await get_tree().create_timer(0.1).timeout # Small delay between players
#
## Finally add the new player
#await get_tree().create_timer(0.5).timeout
#add_player_character(new_peer_id)
#rpc("add_newly_connected_player_character", new_peer_id)
#
## Replace bot if needed
#if bots.size() > 0:
#replace_bot_with_player(new_peer_id)
#
## Final sync of all goals
#await get_tree().create_timer(0.5).timeout
#rpc("force_update_all_goals")
func _on_peer_connected(new_peer_id): func _on_peer_connected(new_peer_id):
if multiplayer.is_server(): if multiplayer.is_server():
# Create a more robust state sync process # Create a more robust state sync process
@@ -1118,3 +1055,67 @@ func initialize_random_goals(_size:int, min_value:int, max_value:int, null_count
goals.append(val if not val in SPECIAL_VALUES else SPECIAL_VALUES[val]) goals.append(val if not val in SPECIAL_VALUES else SPECIAL_VALUES[val])
return goals return goals
func randomize_item_at_position(grid_position: Vector2i):
if not multiplayer.is_server() and multiplayer.get_unique_id() == multiplayer.get_remote_sender_id():
# Request server to randomize item
rpc_id(1, "request_randomize_item", grid_position)
return
var enhanced_gridmap = $EnhancedGridMap
if enhanced_gridmap:
var cell = Vector3i(grid_position.x, 1, grid_position.y)
var current_item = enhanced_gridmap.get_cell_item(cell)
# Only randomize if there's already an item
if current_item != -1:
# Generate a random item value (7-10 based on the game's item system)
var rng = RandomNumberGenerator.new()
rng.randomize()
var new_item = rng.randi_range(7, 10)
# Make sure it's different from the current item
while new_item == current_item:
new_item = rng.randi_range(7, 10)
# Update the item and sync to all clients
sync_grid_item(cell.x, cell.y, cell.z, new_item)
rpc("sync_grid_item", cell.x, cell.y, cell.z, new_item)
# Consume action points for the player who performed the action
var player
if multiplayer.get_remote_sender_id() != 0:
player = get_node_or_null(str(multiplayer.get_remote_sender_id()))
else:
player = get_node_or_null(str(multiplayer.get_unique_id()))
if player and player.has_method("consume_action_points"):
player.has_performed_action = true
player.consume_action_points(1)
player.clear_highlights()
set_action_state(ActionState.NONE)
return true
return false
@rpc("any_peer")
func request_randomize_item(grid_position: Vector2i):
if multiplayer.is_server():
# Verify request came from a valid authority
var sender_id = multiplayer.get_remote_sender_id()
var player = get_node_or_null(str(sender_id))
if player and player.is_multiplayer_authority() and player.action_points > 0:
randomize_item_at_position(grid_position)
@rpc("any_peer", "call_local", "reliable")
func sync_grid_item(x: int, y: int, z: int, item: int):
var enhanced_gridmap = $EnhancedGridMap
if enhanced_gridmap:
var cell = Vector3i(x, y, z)
# Log the change for debugging
print("Main: Setting grid item at ", cell, " to ", item, " (called by ", multiplayer.get_remote_sender_id(), ")")
# Make sure we set the cell reliably
enhanced_gridmap.set_cell_item(cell, item)
+43 -54
View File
@@ -523,44 +523,6 @@ func grab_item(grid_position: Vector2i = current_position) -> bool:
return false return false
# func put_item(grid_position: Vector2i = current_position) -> bool:
# if not enhanced_gridmap or action_points <= 0 or selected_playerboard_slot == -1:
# return false
# var cell = Vector3i(grid_position.x, 1, grid_position.y)
# if enhanced_gridmap.get_cell_item(cell) != -1:
# return false
# # Check if position is adjacent or current position
# if grid_position != current_position:
# var is_adjacent = false
# var neighbors = enhanced_gridmap.get_neighbors(current_position, 0)
# for neighbor in neighbors:
# if neighbor.position == grid_position:
# is_adjacent = true
# break
# if not is_adjacent:
# return false
# var item = playerboard[selected_playerboard_slot]
# if is_multiplayer_authority():
# rpc("sync_grid_item", cell.x, cell.y, cell.z, item)
# playerboard[selected_playerboard_slot] = -1
# rpc("sync_playerboard", playerboard)
# has_performed_action = true
# consume_action_points(1)
# if not is_bot == true:
# clear_highlights()
# clear_playerboard_highlights()
# selected_playerboard_slot = -1
# var main = get_tree().get_root().get_node_or_null("Main")
# if main:
# main.set_action_state(main.ActionState.NONE)
# _after_action_completed()
# return true
func put_item(grid_position: Vector2i = current_position) -> bool: func put_item(grid_position: Vector2i = current_position) -> bool:
if not enhanced_gridmap or action_points <= 0 or selected_playerboard_slot == -1: if not enhanced_gridmap or action_points <= 0 or selected_playerboard_slot == -1:
return false return false
@@ -915,28 +877,51 @@ func highlight_empty_adjacent_cells():
func sync_action_points(points: int): func sync_action_points(points: int):
action_points = points action_points = points
#func highlight_random_valid_cells():
#if is_bot == true or is_in_group("Bots") or not is_multiplayer_authority():
#return
#
#var valid_cells = []
#for x in range(enhanced_gridmap.columns):
#for z in range(enhanced_gridmap.rows):
#var cell_pos = Vector2i(x, z)
#var cell_item = enhanced_gridmap.get_cell_item(Vector3i(x, 0, z))
#if cell_item != -1 and not (cell_item in enhanced_gridmap.non_walkable_items):
#valid_cells.append(cell_pos)
#
#var rng = RandomNumberGenerator.new()
#rng.randomize()
#for _i in range(min(5, valid_cells.size())):
#var index = rng.randi() % valid_cells.size()
#var cell = valid_cells[index]
#highlighted_cells.append(cell)
#enhanced_gridmap.set_cell_item(Vector3i(cell.x, 0, cell.y), enhanced_gridmap.hover_item)
#valid_cells.remove_at(index)
func highlight_random_valid_cells(): func highlight_random_valid_cells():
if is_bot == true or is_in_group("Bots") or not is_multiplayer_authority(): if is_bot == true or is_in_group("Bots") or not is_multiplayer_authority():
return return
var valid_cells = [] clear_highlights()
for x in range(enhanced_gridmap.columns):
for z in range(enhanced_gridmap.rows):
var cell_pos = Vector2i(x, z)
var cell_item = enhanced_gridmap.get_cell_item(Vector3i(x, 0, z))
if cell_item != -1 and not (cell_item in enhanced_gridmap.non_walkable_items):
valid_cells.append(cell_pos)
var rng = RandomNumberGenerator.new() # First check the current position
rng.randomize() var current_cell = Vector3i(current_position.x, 1, current_position.y)
for _i in range(min(5, valid_cells.size())): var current_item = enhanced_gridmap.get_cell_item(current_cell)
var index = rng.randi() % valid_cells.size() if current_item != -1:
var cell = valid_cells[index] highlighted_cells.append(current_position)
highlighted_cells.append(cell) enhanced_gridmap.set_cell_item(Vector3i(current_position.x, 0, current_position.y),
enhanced_gridmap.set_cell_item(Vector3i(cell.x, 0, cell.y), enhanced_gridmap.hover_item) enhanced_gridmap.hover_item)
valid_cells.remove_at(index)
# Then check all adjacent cells for items
var neighbors = enhanced_gridmap.get_neighbors(current_position, 0)
for neighbor in neighbors:
if neighbor.is_walkable:
var cell_pos = neighbor.position
var cell = Vector3i(cell_pos.x, 1, cell_pos.y)
if enhanced_gridmap.get_cell_item(cell) != -1: # Only highlight cells with items
highlighted_cells.append(cell_pos)
enhanced_gridmap.set_cell_item(Vector3i(cell_pos.x, 0, cell_pos.y),
enhanced_gridmap.hover_item)
func highlight_occupied_playerboard_slots(): func highlight_occupied_playerboard_slots():
if is_bot == true or is_in_group("Bots") or not is_multiplayer_authority(): if is_bot == true or is_in_group("Bots") or not is_multiplayer_authority():
@@ -1119,6 +1104,10 @@ func sync_playerboard(new_playerboard: Array):
_after_action_completed() _after_action_completed()
func _after_action_completed(): func _after_action_completed():
# Clear the highlights after placing the tiles. ( Quickfix for Clientside )
clear_highlights()
if multiplayer.get_unique_id() == get_multiplayer_authority(): if multiplayer.get_unique_id() == get_multiplayer_authority():
var main = get_tree().get_root().get_node_or_null("Main") var main = get_tree().get_root().get_node_or_null("Main")
if main: if main: