goals now visible on every players, need to fix the pattern of goals generator

This commit is contained in:
2025-02-04 17:34:56 +08:00
parent 1c67557135
commit c578c3e97e
12 changed files with 1914 additions and 79 deletions
+172 -24
View File
@@ -12,6 +12,7 @@ var current_turn_index = 0
var game_started = false
var max_players = 4
var bots = []
var preset_goals = []
@export var turn_based_mode: bool = true
#var bot_move_timer: float = 0.0
@@ -180,32 +181,33 @@ func _on_playerboard_slot_clicked(event, slot_index):
ActionState.PUTTING:
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_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():
if not local_player_character:
return
update_goals_ui() # Update goals UI whenever playerboard updates
#update_goals_ui() # Update goals UI whenever playerboard updates
update_all_players_goals() # Update all players' goals UI
for i in range(25):
var slot = playerboard_ui.get_child(i)
@@ -240,11 +242,30 @@ func _on_host_pressed():
add_player_character(1)
players.append(1)
# Generate goals for host first
var host_player = get_node_or_null("1")
if host_player:
host_player.append_random_goals()
rpc("sync_player_goals", 1, host_player.goals)
# Generate and store goals for future bots/players
var preset_goals = []
for i in range(2, max_players + 1):
var goals = initialize_random_goals(9, 7, 10, 1.0)
preset_goals.append(goals)
# Store preset goals in network state
rpc("sync_preset_goals", preset_goals)
for i in range(2, max_players + 1):
add_bot(i)
start_game()
@rpc("reliable")
func sync_preset_goals(goals_list: Array):
preset_goals = goals_list
func _on_join_pressed():
$NetworkInfo/NetworkSideDisplay.text = "Client"
$Menu.visible = false
@@ -268,6 +289,11 @@ func _on_peer_connected(new_peer_id):
add_player_character(new_peer_id)
rpc("add_newly_connected_player_character", new_peer_id)
replace_bot_with_player(new_peer_id)
# Sync all players' goals to the new peer
for player in get_tree().get_nodes_in_group("Players"):
var player_id = String(player.name).to_int() # Convert StringName to String first, then to int
rpc_id(new_peer_id, "sync_player_goals", player_id, player.goals)
func _on_peer_disconnected(peer_id):
if multiplayer.is_server():
@@ -293,7 +319,28 @@ func add_player_character(peer_id):
local_player_character = player_character
update_button_states()
update_playerboard_ui()
update_goals_ui()
#update_goals_ui()
# Sync this player's goals to everyone
if multiplayer.is_server():
rpc("sync_player_goals", peer_id, player_character.goals)
if multiplayer.is_server():
if peer_id > 1: # Not the host
# Assign preset goals
var goal_index = peer_id - 2
if goal_index < preset_goals.size():
player_character.goals = preset_goals[goal_index]
rpc("sync_player_goals", peer_id, player_character.goals)
if multiplayer.is_server():
# If replacing a bot, inherit its goals
var bot_to_replace = get_node_or_null(str(bots[0])) if bots.size() > 0 else null
if bot_to_replace:
player_character.goals = bot_to_replace.goals.duplicate()
else:
# Only generate new goals if not inheriting from a bot
player_character.append_random_goals()
func add_bot(bot_id):
rpc("create_bot", bot_id)
@@ -324,16 +371,41 @@ func create_bot(bot_id):
if multiplayer.is_server():
bots.append(bot_id)
players.append(bot_id)
# Assign preset goals
var goal_index = bot_id - 2
if goal_index < preset_goals.size():
bot_character.goals = preset_goals[goal_index]
# Sync bot status after a short delay to ensure node is ready
await get_tree().create_timer(0.1).timeout
bot_character.rpc("sync_bot_status", true)
# Sync bot's goals
rpc("sync_player_goals", bot_id, bot_character.goals)
# Only generate goals for new bots, not replacement bots
if not (players.size() > max_players):
bot_character.append_random_goals()
# Always sync the bot's goals
rpc("sync_player_goals", bot_id, bot_character.goals)
func replace_bot_with_player(player_id):
if multiplayer.is_server() and bots.size() > 0:
var bot_id = bots.pop_front()
var bot_id = bots[0]
var bot_node = get_node_or_null(str(bot_id))
if bot_node:
# Transfer bot's goals to new player
var goals = bot_node.goals
var player_node = get_node_or_null(str(player_id))
if player_node:
player_node.goals = goals
rpc("sync_player_goals", player_id, goals)
# Now remove the bot
bots.pop_front()
players.erase(bot_id)
players.append(player_id)
#moving_bots.erase(bot_id)
rpc("remove_bot", bot_id)
rpc("sync_players", players)
@@ -435,3 +507,79 @@ func end_current_turn():
if multiplayer.is_server():
next_turn()
rpc("sync_turn_index", current_turn_index)
func update_all_players_goals():
if not game_started:
return
var all_players = get_tree().get_nodes_in_group("Players")
# Fix: Convert name to string and handle potential scene-unique names properly
all_players.sort_custom(func(a, b):
var a_str = String(a.name).get_slice("@", 0) # Explicitly convert StringName to String
var b_str = String(b.name).get_slice("@", 0)
return int(a_str) < int(b_str)
)
for i in range(min(all_players.size(), 4)):
var player = all_players[i]
var goals_grid = $AllPlayerGoals.get_child(i) # Playergoals_1, _2, _3, or _4
for slot_idx in range(9):
var slot = goals_grid.get_child(slot_idx)
var goal_value = player.goals[slot_idx]
# 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()
@rpc("any_peer", "call_local")
func sync_player_goals(player_id: int, goals: Array):
var player_idx = players.find(player_id)
if player_idx >= 0 and player_idx < 4:
var goals_grid = $AllPlayerGoals.get_child(player_idx)
for slot_idx in range(9):
var slot = goals_grid.get_child(slot_idx)
var goal_value = goals[slot_idx]
# Hide all tiles
slot.get_node("TileHeart").hide()
slot.get_node("TileDiamond").hide()
slot.get_node("TileStar").hide()
slot.get_node("TileCoin").hide()
# Show appropriate tile
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()
# Add this function near the top with other helper functions
func initialize_random_goals(_size:int, min_value:int, max_value:int, null_count:float) -> Array:
var goals = []
var rng = RandomNumberGenerator.new()
rng.randomize()
var null_val = 0
var max_nulls = 3
const SPECIAL_VALUES = {1: 7, 2: 8, 3: 9, 4: 10}
for i in range(_size):
if null_val < max_nulls and rng.randf() < null_count:
goals.append(-1)
null_val += 1
else:
var val = rng.randi_range(min_value, max_value)
goals.append(val if not val in SPECIAL_VALUES else SPECIAL_VALUES[val])
return goals