make the orientation of obstacle align with four side of cell on gridmap

This commit is contained in:
2025-03-13 17:32:41 +08:00
parent 58142e9d67
commit d64b685e6e
6 changed files with 377 additions and 195 deletions
+75 -38
View File
@@ -68,7 +68,7 @@ enum ActionState {
# Obstacle
# Add these properties to track the current obstacle direction
var current_obstacle_direction = EnhancedGridMap.Direction.NORTH # Default to NORTH
var current_obstacle_orientation = ObstacleOrientation.NORTH # Default to NORTH
var current_obstacle_item = 12 # Starting with first obstacle item (12)
enum ObstacleDirection {
@@ -76,6 +76,13 @@ enum ObstacleDirection {
HORIZONTAL
}
enum ObstacleOrientation {
NORTH = 0,
EAST = 1,
SOUTH = 2,
WEST = 3
}
var current_action_state = ActionState.NONE
@onready var action_menu = $ActionMenu
@@ -223,46 +230,92 @@ func set_action_state(new_state):
local_player_character.highlight_valid_obstacle_cells()
# Update the place_obstacle function for floor 3
#func place_obstacle(grid_position: Vector2i):
#if not local_player_character or local_player_character.action_points < 1:
#return false
#
#var floor_index = 3 # Always place on floor 3
#var direction = EnhancedGridMap.Direction.BLOCKED_NORTH
#
#match current_obstacle_direction:
#ObstacleDirection.VERTICAL:
#direction = EnhancedGridMap.Direction.BLOCKED_NORTH # Block movement along east-west axis
#ObstacleDirection.HORIZONTAL:
#direction = EnhancedGridMap.Direction.BLOCKED_EAST # Block movement along north-south axis
#
#var success = $EnhancedGridMap.place_obstacle(
#Vector3i(grid_position.x, floor_index, grid_position.y),
#current_obstacle_item,
#direction
#)
#
#if success:
#local_player_character.action_points -= 1
#local_player_character.clear_highlights()
#
## Don't exit the obstacle placement mode to allow multiple placements
#local_player_character.highlight_valid_obstacle_cells()
#
## Exit obstacle placement mode and return to default state
#set_action_state(ActionState.NONE)
#
## Sync the obstacle with other clients
#if is_multiplayer_authority():
#rpc("sync_place_obstacle", grid_position.x, grid_position.y, floor_index, current_obstacle_item, direction)
#
#return true
#return false
func place_obstacle(grid_position: Vector2i):
if not local_player_character or local_player_character.action_points < 1:
return false
var floor_index = 3 # Always place on floor 3
var direction = EnhancedGridMap.Direction.BLOCKED_NORTH
match current_obstacle_direction:
ObstacleDirection.VERTICAL:
direction = EnhancedGridMap.Direction.BLOCKED_NORTH # Block movement along east-west axis
ObstacleDirection.HORIZONTAL:
direction = EnhancedGridMap.Direction.BLOCKED_EAST # Block movement along north-south axis
var floor_index = 3 # Always place on floor 3
var success = $EnhancedGridMap.place_obstacle(
Vector3i(grid_position.x, floor_index, grid_position.y),
current_obstacle_item,
direction
current_obstacle_orientation
)
if success:
local_player_character.action_points -= 1
local_player_character.clear_highlights()
# Don't exit the obstacle placement mode to allow multiple placements
local_player_character.highlight_valid_obstacle_cells()
# Clear all highlights after placing obstacle
local_player_character.clear_highlights()
# Exit obstacle placement mode and return to default state
set_action_state(ActionState.NONE)
# Sync the obstacle with other clients
if is_multiplayer_authority():
rpc("sync_place_obstacle", grid_position.x, grid_position.y, floor_index, current_obstacle_item, direction)
rpc("sync_place_obstacle", grid_position.x, grid_position.y, floor_index, current_obstacle_item, current_obstacle_orientation)
return true
return false
# Updated function to cycle through the four orientations
func cycle_obstacle_orientation():
var orientations = [ObstacleOrientation.NORTH, ObstacleOrientation.EAST, ObstacleOrientation.SOUTH, ObstacleOrientation.WEST]
var current_index = orientations.find(current_obstacle_orientation)
current_index = (current_index + 1) % orientations.size()
current_obstacle_orientation = orientations[current_index]
var direction_names = ["North", "East", "South", "West"]
return "Direction: " + direction_names[current_index]
# Function to cycle through obstacle types
func cycle_obstacle_type():
var obstacle_types = [12, 13, 14, 15]
var current_index = obstacle_types.find(current_obstacle_item)
current_index = (current_index + 1) % obstacle_types.size()
current_obstacle_item = obstacle_types[current_index]
return "Type: " + str(current_index + 1)
# Update the RPC for obstacle synchronization
@rpc("any_peer", "call_local")
func sync_place_obstacle(x: int, y: int, floor_index: int, item_index: int, direction: int):
$EnhancedGridMap.place_obstacle(Vector3i(x, floor_index, y), item_index, direction)
func sync_place_obstacle(x: int, y: int, floor_index: int, item_index: int, orientation: int):
$EnhancedGridMap.place_obstacle(Vector3i(x, floor_index, y), item_index, orientation)
func update_button_states():
if not local_player_character or local_player_character.is_in_group("Bots"):
@@ -997,13 +1050,13 @@ func setup_obstacle_ui():
obstacle_button.pressed.connect(func(): set_action_state(ActionState.PLACING_OBSTACLE))
$ActionMenu/ActionButtonContainer.add_child(obstacle_button)
# Create the direction cycle button
var direction_button = Button.new()
direction_button.text = "Direction: North"
direction_button.pressed.connect(func():
direction_button.text = cycle_obstacle_direction()
# Create the rotation/orientation cycle button
var orientation_button = Button.new()
orientation_button.text = "Direction: North"
orientation_button.pressed.connect(func():
orientation_button.text = cycle_obstacle_orientation()
)
$ActionMenu/ActionButtonContainer.add_child(direction_button)
$ActionMenu/ActionButtonContainer.add_child(orientation_button)
# Create the type cycle button
var type_button = Button.new()
@@ -1013,22 +1066,6 @@ func setup_obstacle_ui():
)
$ActionMenu/ActionButtonContainer.add_child(type_button)
# Add a function to cycle through obstacle types
func cycle_obstacle_type():
var obstacle_types = [12, 13, 14, 15]
var current_index = obstacle_types.find(current_obstacle_item)
current_index = (current_index + 1) % obstacle_types.size()
current_obstacle_item = obstacle_types[current_index]
return "Type: " + str(current_index + 1)
func cycle_obstacle_direction():
var directions = [EnhancedGridMap.Direction.NORTH, EnhancedGridMap.Direction.EAST, EnhancedGridMap.Direction.SOUTH, EnhancedGridMap.Direction.WEST]
var current_index = directions.find(current_obstacle_direction)
current_index = (current_index + 1) % directions.size()
current_obstacle_direction = directions[current_index]
var direction_names = ["North", "East", "South", "West"]
return "Direction: " + direction_names[current_index]
func update_board_slot(board_ui: Node, slot_idx: int, value: int):
var slot_node = board_ui.get_node_or_null("Slot%d" % (slot_idx + 1))
+1 -2
View File
@@ -39,13 +39,12 @@ turn_based_mode = false
mesh_library = ExtResource("1_110wo")
cell_size = Vector3(1, 1, 1)
data = {
"cells": PackedInt32Array(0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 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, 10, 3, 0, 10, 4, 0, 10, 5, 0, 10, 6, 0, 10, 7, 0, 10, 8, 0, 10, 9, 0, 10, 10, 0, 10, 11, 0, 11, 0, 0, 11, 1, 0, 11, 2, 0, 11, 3, 0, 11, 4, 0, 11, 5, 0, 11, 6, 0, 11, 7, 0, 11, 8, 0, 11, 9, 0, 11, 10, 0, 11, 11, 0, 12, 0, 0, 12, 1, 0, 12, 2, 0, 12, 3, 0, 12, 4, 0, 12, 5, 0, 12, 6, 0, 12, 7, 0, 12, 8, 0, 12, 9, 0, 12, 10, 0, 12, 11, 0, 13, 0, 0, 13, 1, 0, 13, 2, 0, 13, 3, 0, 13, 4, 0, 13, 5, 0, 13, 6, 0, 13, 7, 0, 13, 8, 0, 13, 9, 0, 13, 10, 0, 13, 11, 0, 65537, 1, 9, 65537, 2, 9, 65537, 3, 10, 65537, 4, 7, 65537, 5, 10, 65537, 6, 7, 65537, 7, 7, 65537, 8, 8, 65537, 9, 7, 65537, 10, 10, 65537, 11, 8, 65538, 1, 8, 65538, 2, 8, 65538, 3, 10, 65538, 4, 8, 65538, 5, 10, 65538, 6, 8, 65538, 7, 9, 65538, 8, 9, 65538, 9, 7, 65538, 10, 7, 65538, 11, 9, 65539, 0, 8, 65539, 1, 9, 65539, 2, 7, 65539, 3, 10, 65539, 4, 9, 65539, 5, 7, 65539, 6, 8, 65539, 7, 9, 65539, 8, 7, 65539, 9, 7, 65539, 10, 7, 65539, 11, 9, 65540, 0, 9, 65540, 1, 9, 65540, 2, 10, 65540, 3, 8, 65540, 4, 9, 65540, 5, 10, 65540, 6, 7, 65540, 7, 10, 65540, 8, 9, 65540, 9, 10, 65540, 10, 9, 65540, 11, 10, 65541, 0, 8, 65541, 1, 8, 65541, 2, 8, 65541, 3, 10, 65541, 4, 7, 65541, 5, 9, 65541, 6, 8, 65541, 7, 10, 65541, 8, 9, 65541, 9, 8, 65541, 10, 10, 65541, 11, 9, 65542, 0, 7, 65542, 1, 8, 65542, 2, 9, 65542, 3, 10, 65542, 4, 9, 65542, 5, 10, 65542, 6, 10, 65542, 7, 7, 65542, 8, 7, 65542, 9, 10, 65542, 10, 7, 65542, 11, 7, 65543, 0, 9, 65543, 1, 8, 65543, 2, 10, 65543, 3, 7, 65543, 4, 7, 65543, 5, 10, 65543, 6, 9, 65543, 7, 8, 65543, 8, 9, 65543, 9, 7, 65543, 10, 7, 65543, 11, 9, 65544, 0, 7, 65544, 1, 7, 65544, 2, 10, 65544, 3, 10, 65544, 4, 7, 65544, 5, 7, 65544, 6, 9, 65544, 7, 10, 65544, 8, 7, 65544, 9, 7, 65544, 10, 8, 65544, 11, 9, 65545, 0, 9, 65545, 1, 10, 65545, 2, 9, 65545, 3, 9, 65545, 4, 9, 65545, 5, 9, 65545, 6, 9, 65545, 7, 9, 65545, 8, 10, 65545, 9, 7, 65545, 10, 7, 65545, 11, 9, 65546, 0, 8, 65546, 1, 8, 65546, 2, 7, 65546, 3, 10, 65546, 4, 7, 65546, 5, 9, 65546, 6, 9, 65546, 7, 9, 65546, 8, 7, 65546, 9, 9, 65546, 10, 10, 65546, 11, 10, 65547, 0, 10, 65547, 1, 8, 65547, 2, 7, 65547, 3, 8, 65547, 4, 10, 65547, 5, 7, 65547, 6, 10, 65547, 7, 7, 65547, 8, 10, 65547, 9, 8, 65547, 10, 7, 65547, 11, 8, 65548, 0, 7, 65548, 1, 9, 65548, 2, 10, 65548, 3, 9, 65548, 4, 7, 65548, 5, 9, 65548, 6, 8, 65548, 7, 10, 65548, 8, 10, 65548, 9, 8, 65548, 10, 9, 65548, 11, 9, 65538, 0, 10, 65537, 0, 7)
"cells": PackedInt32Array(0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 10, 0, 0, 11, 0, 1, 0, 0, 1, 1, 0, 1, 2, 0, 1, 3, 0, 1, 4, 0, 1, 5, 0, 1, 6, 0, 1, 7, 0, 1, 8, 0,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0, 11, 3, 0, 11, 4, 0, 11, 5, 0, 11, 6, 0, 11, 7, 0, 11, 8, 0, 11, 9, 0, 11, 10, 0, 11, 11, 0, 12, 0, 0, 12, 1, 0, 12, 2, 0, 12, 3, 0, 12, 4, 0, 12, 5, 0, 12, 6, 0, 12, 7, 0, 12, 8, 0, 12, 9, 0, 12, 10, 0, 12, 11, 0, 13, 0, 0, 13, 1, 0, 13, 2, 0, 13, 3, 0, 13, 4, 0, 13, 5, 0, 13, 6, 0, 13, 7, 0, 13, 8, 0, 13, 9, 0, 13, 10, 0, 13, 11, 0, 65537, 1, 9, 65537, 2, 9, 65537, 3, 10, 65537, 4, 7, 65537, 5, 10, 65537, 6, 7, 65537, 7, 7, 65537, 8, 8, 65537, 9, 7, 65537, 10, 10, 65537, 11, 8, 65538, 1, 8, 65538, 2, 8, 65538, 3, 10, 65538, 4, 8, 65538, 5, 10, 65538, 6, 8, 65538, 7, 9, 65538, 8, 9, 65538, 9, 7, 65538, 10, 7, 65538, 11, 9, 65539, 0, 8, 65539, 1, 9, 65539, 2, 7, 65539, 3, 10, 65539, 4, 9, 65539, 5, 7, 65539, 6, 8, 65539, 7, 9, 65539, 8, 7, 65539, 9, 7, 65539, 10, 7, 65539, 11, 9, 65540, 0, 9, 65540, 1, 9, 65540, 2, 10, 65540, 3, 8, 65540, 4, 9, 65540, 5, 10, 65540, 6, 7, 65540, 7, 10, 65540, 8, 9, 65540, 9, 10, 65540, 10, 9, 65540, 11, 10, 65541, 0, 8, 65541, 1, 8, 65541, 2, 8, 65541, 3, 10, 65541, 4, 7, 65541, 5, 9, 65541, 6, 8, 65541, 7, 10, 65541, 8, 9, 65541, 9, 8, 65541, 10, 10, 65541, 11, 9, 65542, 0, 7, 65542, 1, 8, 65542, 2, 9, 65542, 3, 10, 65542, 4, 9, 65542, 5, 10, 65542, 6, 10, 65542, 7, 7, 65542, 8, 7, 65542, 9, 10, 65542, 10, 7, 65542, 11, 7, 65543, 0, 9, 65543, 1, 8, 65543, 2, 10, 65543, 3, 7, 65543, 4, 7, 65543, 5, 10, 65543, 6, 9, 65543, 7, 8, 65543, 8, 9, 65543, 9, 7, 65543, 10, 7, 65543, 11, 9, 65544, 0, 7, 65544, 1, 7, 65544, 2, 10, 65544, 3, 10, 65544, 4, 7, 65544, 5, 7, 65544, 6, 9, 65544, 7, 10, 65544, 8, 7, 65544, 9, 7, 65544, 10, 8, 65544, 11, 9, 65545, 0, 9, 65545, 1, 10, 65545, 2, 9, 65545, 3, 9, 65545, 4, 9, 65545, 5, 9, 65545, 6, 9, 65545, 7, 9, 65545, 8, 10, 65545, 9, 7, 65545, 10, 7, 65545, 11, 9, 65546, 0, 8, 65546, 1, 8, 65546, 2, 7, 65546, 3, 10, 65546, 4, 7, 65546, 5, 9, 65546, 6, 9, 65546, 7, 9, 65546, 8, 7, 65546, 9, 9, 65546, 10, 10, 65546, 11, 10, 65547, 0, 10, 65547, 1, 8, 65547, 2, 7, 65547, 3, 8, 65547, 4, 10, 65547, 5, 7, 65547, 6, 10, 65547, 7, 7, 65547, 8, 10, 65547, 9, 8, 65547, 10, 7, 65547, 11, 8, 65548, 0, 7, 65548, 1, 9, 65548, 2, 10, 65548, 3, 9, 65548, 4, 7, 65548, 5, 9, 65548, 6, 8, 65548, 7, 10, 65548, 8, 10, 65548, 9, 8, 65548, 10, 9, 65548, 11, 9, 65538, 0, 10, 65537, 0, 7, 131075, 8, 524300, 131075, 7, 524300)
}
script = ExtResource("2_hbe1v")
columns = 14
rows = 12
obstacle_items = Array[int]([12])
obstacle_directions = Array[int]([11, 10, 12, 13])
metadata/_editor_floor_ = Vector3(0, 2, 0)
[node name="Camera3D" type="Camera3D" parent="."]
+22 -6
View File
@@ -851,9 +851,9 @@ func highlight_movement_range():
var cells_to_highlight = []
# Only highlight cells within movement range that aren't blocked by obstacles
for x in range(enhanced_gridmap.columns):
for z in range(enhanced_gridmap.rows):
# For each position within movement range
for x in range(max(0, current_position.x - movement_range), min(enhanced_gridmap.columns, current_position.x + movement_range + 1)):
for z in range(max(0, current_position.y - movement_range), min(enhanced_gridmap.rows, current_position.y + movement_range + 1)):
var test_pos = Vector2i(x, z)
# Skip the current position
@@ -864,10 +864,26 @@ func highlight_movement_range():
if is_within_movement_range(test_pos):
var cell_item = enhanced_gridmap.get_cell_item(Vector3i(x, 0, z))
# Check if cell is walkable and not occupied
# Basic walkability check
if cell_item != -1 and not (cell_item in enhanced_gridmap.non_walkable_items) and not is_position_occupied(test_pos):
# Most importantly, check if movement is blocked by obstacles
if not enhanced_gridmap.is_blocked_by_obstacle(current_position, test_pos, 3):
# Check if there's a direct path without obstacles
var path_blocked = false
# For orthogonal movement
if test_pos.x == current_position.x or test_pos.y == current_position.y:
path_blocked = enhanced_gridmap.is_blocked_by_obstacle(current_position, test_pos, 3)
else:
# For diagonal movement, check if both orthogonal paths are blocked
var mid1 = Vector2i(test_pos.x, current_position.y)
var mid2 = Vector2i(current_position.x, test_pos.y)
var path1_blocked = enhanced_gridmap.is_blocked_by_obstacle(current_position, mid1, 3)
var path2_blocked = enhanced_gridmap.is_blocked_by_obstacle(current_position, mid2, 3)
# Only completely block if both paths are blocked
path_blocked = path1_blocked and path2_blocked
if not path_blocked:
cells_to_highlight.append(test_pos)
highlight_cells_if_authorized(cells_to_highlight)