Minor Update

- Refactor the Player.gd

- Attempt to fix joining the available room, via Room ID
This commit is contained in:
2025-12-03 21:51:23 +08:00
parent a97a8e68f6
commit dce55c2fe6
5 changed files with 118 additions and 840 deletions
+20 -19
View File
@@ -4,6 +4,7 @@ extends Node
var player: Node3D
var enhanced_gridmap: Node
var highlighted_cells = []
func initialize(p_player: Node3D, p_gridmap: Node):
player = p_player
@@ -56,7 +57,7 @@ func after_action_completed():
if main:
# Add this condition for bots
if not TurnManager.turn_based_mode and (player.action_points <= 0 or player.is_bot):
player.action_points = 20 # For bots in non-turn-based mode, this will keep refreshing
player.action_points = 20 # For bots in non-turn-based mode, this will keep refreshing
player.has_performed_action = false
player.has_moved_this_turn = false
@@ -82,9 +83,9 @@ func highlight_cells_if_authorized(cells_to_highlight: Array):
clear_highlights()
for cell in cells_to_highlight:
player.highlighted_cells.append(cell)
highlighted_cells.append(cell)
enhanced_gridmap.set_cell_item(
Vector3i(cell.x, 0, cell.y),
Vector3i(cell.x, 0, cell.y),
enhanced_gridmap.hover_item
)
@@ -101,8 +102,8 @@ func highlight_empty_adjacent_cells():
# Highlight current position if empty
var current_cell = Vector3i(player.current_position.x, 1, player.current_position.y)
if enhanced_gridmap.get_cell_item(current_cell) == -1:
player.highlighted_cells.append(player.current_position)
enhanced_gridmap.set_cell_item(Vector3i(player.current_position.x, 0, player.current_position.y),
highlighted_cells.append(player.current_position)
enhanced_gridmap.set_cell_item(Vector3i(player.current_position.x, 0, player.current_position.y),
enhanced_gridmap.hover_item)
print("Highlighted current position: ", player.current_position)
@@ -112,9 +113,9 @@ func highlight_empty_adjacent_cells():
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: # Check if cell is empty
player.highlighted_cells.append(cell_pos)
enhanced_gridmap.set_cell_item(Vector3i(cell_pos.x, 0, cell_pos.y),
if enhanced_gridmap.get_cell_item(cell) == -1: # Check if cell is empty
highlighted_cells.append(cell_pos)
enhanced_gridmap.set_cell_item(Vector3i(cell_pos.x, 0, cell_pos.y),
enhanced_gridmap.hover_item)
print("Highlighted adjacent cell: ", cell_pos)
@@ -128,8 +129,8 @@ func highlight_random_valid_cells():
var current_cell = Vector3i(player.current_position.x, 1, player.current_position.y)
var current_item = enhanced_gridmap.get_cell_item(current_cell)
if current_item != -1:
player.highlighted_cells.append(player.current_position)
enhanced_gridmap.set_cell_item(Vector3i(player.current_position.x, 0, player.current_position.y),
highlighted_cells.append(player.current_position)
enhanced_gridmap.set_cell_item(Vector3i(player.current_position.x, 0, player.current_position.y),
enhanced_gridmap.hover_item)
# Then check all adjacent cells for items
@@ -138,9 +139,9 @@ func highlight_random_valid_cells():
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
player.highlighted_cells.append(cell_pos)
enhanced_gridmap.set_cell_item(Vector3i(cell_pos.x, 0, 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():
@@ -162,8 +163,8 @@ func highlight_occupied_playerboard_slots():
if player.playerboard[i] in player.goals:
var slot = main.ui_manager.playerboard_ui.get_child(i)
if slot.get_child_count() > 0:
slot.get_child(0).show() # Show highlight for matching items
player.highlighted_cells.append(i) # Add to highlighted cells for tracking
slot.get_child(0).show() # Show highlight for matching items
highlighted_cells.append(i) # Add to highlighted cells for tracking
# Update the UI to reflect changes
main.ui_manager.update_playerboard_ui()
@@ -180,7 +181,7 @@ func highlight_valid_obstacle_cells():
for x in range(enhanced_gridmap.columns):
for z in range(enhanced_gridmap.rows):
var pos = Vector2i(x, z)
var cell = Vector3i(x, 3, z) # Check floor 3 for occupancy
var cell = Vector3i(x, 3, z) # Check floor 3 for occupancy
var occupied_by_player = false
var occupied_by_obstacle = false
@@ -212,11 +213,11 @@ func clear_highlights():
var main = player.get_tree().get_root().get_node_or_null("Main")
var current_state = main.ui_manager.current_action_state if main else null
for cell in player.highlighted_cells:
for cell in highlighted_cells:
if cell is Vector2i:
enhanced_gridmap.set_cell_item(Vector3i(cell.x, 0, cell.y), enhanced_gridmap.normal_items[0])
player.highlighted_cells.clear()
highlighted_cells.clear()
if main and main.ui_manager.playerboard_ui:
for i in range(main.ui_manager.playerboard_ui.get_child_count()):
@@ -244,4 +245,4 @@ func clear_playerboard_highlights():
if slot.get_child_count() > 1: slot.get_child(1).hide()
if slot.get_child_count() > 2: slot.get_child(2).hide()
player.highlighted_cells.clear()
highlighted_cells.clear()
+21 -21
View File
@@ -43,7 +43,7 @@ func grab_item(grid_position: Vector2i) -> bool:
var target_slot = find_best_goal_slot_for_item(item)
if target_slot == -1:
print("Player: No valid slot found for item.")
return false # no space
return false # no space
if not player.is_multiplayer_authority():
return false
@@ -191,44 +191,44 @@ func auto_put_item() -> bool:
# Now scan playerboard
for i in range(player.playerboard.size()):
var item = player.playerboard[i]
if item == -1:
var current_item = player.playerboard[i]
if current_item == -1:
continue
# Case 1: Item is not in goals at all → definitely junk
if not item in player.goals:
if not current_item in player.goals:
put_slot = i
break
# Case 2: Item is in goals, but we already have enough in correct spots
var current_count = 0
for r in range(1, 4): # central rows 1-3 (5x5 board)
for c in range(1, 4): # central cols 1-3
for r in range(1, 4): # central rows 1-3 (5x5 board)
for c in range(1, 4): # central cols 1-3
var idx = r * 5 + c
if player.playerboard[idx] == item:
if player.playerboard[idx] == current_item:
current_count += 1
# If we already have all needed copies in central area, this is extra
if current_count >= goal_counts.get(item, 0):
if current_count >= goal_counts.get(current_item, 0):
put_slot = i
break
# If no junk found, fall back to any non-goal-matching tile outside center
if put_slot == -1:
for i in range(player.playerboard.size()):
var item = player.playerboard[i]
if item == -1:
var board_item = player.playerboard[i]
if board_item == -1:
continue
var row = i / 5
var col = i % 5
# If it's outside the central 3x3, it shouldn't be there
if row < 1 or row > 3 or col < 1 or col > 3:
if not item in player.goals or player.playerboard[i] != player.goals[(row - 1) * 3 + (col - 1)]:
if not board_item in player.goals or player.playerboard[i] != player.goals[(row - 1) * 3 + (col - 1)]:
put_slot = i
break
if put_slot == -1:
return false # Nothing suitable to put
return false # Nothing suitable to put
# Step 3: Perform the put
var target_pos = valid_put_positions[0]
@@ -256,7 +256,7 @@ func arrange_playerboard_item(slot_index: int):
if player.action_points < 2 or player.playerboard[slot_index] == -1:
return
var selected_item = player.playerboard[slot_index]
#var selected_item = player.playerboard[slot_index]
var adjacent_slots = get_adjacent_playerboard_slots(slot_index)
var main = player.get_tree().get_root().get_node_or_null("Main")
@@ -273,11 +273,11 @@ func arrange_playerboard_item(slot_index: int):
# Highlight valid adjacent slots
for adj_slot in adjacent_slots:
if player.playerboard[adj_slot] == -1: # Only highlight empty adjacent slots
if player.playerboard[adj_slot] == -1: # Only highlight empty adjacent slots
var adj_slot_ui = main.ui_manager.playerboard_ui.get_child(adj_slot)
if adj_slot_ui.get_child_count() > 2:
adj_slot_ui.get_child(2).show()
player.highlighted_cells.append(adj_slot)
player.action_manager.highlighted_cells.append(adj_slot)
# Connect to slot click signals
for i in range(player.playerboard.size()):
@@ -336,10 +336,10 @@ func find_best_goal_slot_for_item(item: int) -> int:
for i in range(3):
for j in range(3):
if goals_2d[i][j] == item:
var board_row = i + 1 # offset to center in 5x5
var board_row = i + 1 # offset to center in 5x5
var board_col = j + 1
var slot_index = board_row * 5 + board_col
if player.playerboard[slot_index] == -1: # only if empty
if player.playerboard[slot_index] == -1: # only if empty
return slot_index
# No ideal slot? Return any empty slot
@@ -377,7 +377,7 @@ func find_best_put_candidate() -> Dictionary:
# Is it already in the correct central position?
var is_in_correct_central_spot = false
if board_i in [1,2,3] and board_j in [1,2,3]:
if board_i in [1, 2, 3] and board_j in [1, 2, 3]:
var goal_i = board_i - 1
var goal_j = board_j - 1
if goals_2d[goal_i][goal_j] == item:
@@ -424,7 +424,7 @@ func find_best_put_candidate() -> Dictionary:
if not valid_cells.is_empty():
return {
"slot_index": cand.slot,
"grid_position": valid_cells[0] # pick first valid cell
"grid_position": valid_cells[0] # pick first valid cell
}
# Fallback: just put any candidate item
@@ -446,9 +446,9 @@ func get_adjacent_playerboard_slots(slot_index) -> Array:
return adjacent
func is_valid_arrangement_slot(from_slot: int, to_slot: int) -> bool:
var from_row = from_slot / 5
var from_row = int(from_slot / 5)
var from_col = from_slot % 5
var to_row = to_slot / 5
var to_row = int(to_slot / 5)
var to_col = to_slot % 5
var row_diff = abs(from_row - to_row)
+10 -4
View File
@@ -33,12 +33,17 @@ func _process(_delta):
if socket:
pass
func connect_to_nakama_async(email: String = "", password: String = "") -> void:
func connect_to_nakama_async(email: String = "", password: String = "") -> bool:
if is_connected_to_nakama():
print("Already connected to Nakama.")
emit_signal("connected_to_nakama")
return true
# 1. Authenticate
if email == "":
var device_id = OS.get_unique_id()
# For testing, append a random number to device ID to simulate unique users on one machine
# device_id = device_id + str(randi())
device_id = device_id + str(randi())
session = await client.authenticate_device_async(device_id)
else:
session = await client.authenticate_email_async(email, password)
@@ -46,7 +51,7 @@ func connect_to_nakama_async(email: String = "", password: String = "") -> void:
if session.is_exception():
printerr("Auth Error: ", session.get_exception().message)
emit_signal("connection_failed", session.get_exception().message)
return
return false
# 2. Connect Socket
socket = Nakama.create_socket_from(client)
@@ -55,7 +60,7 @@ func connect_to_nakama_async(email: String = "", password: String = "") -> void:
if socket_result.is_exception():
printerr("Socket Error: ", socket_result.get_exception().message)
emit_signal("connection_failed", socket_result.get_exception().message)
return
return false
# 3. Initialize Multiplayer Bridge
# This links Nakama's socket to Godot's High-Level Multiplayer API
@@ -71,6 +76,7 @@ func connect_to_nakama_async(email: String = "", password: String = "") -> void:
print("Connected to Nakama and Bridge Initialized.")
emit_signal("connected_to_nakama")
return true
# --- Match Management ---