edit special power up
This commit is contained in:
@@ -5,10 +5,10 @@ extends GridMap
|
||||
signal mesh_library_changed
|
||||
signal grid_updated
|
||||
|
||||
@export var columns: int = 10 : set = set_columns
|
||||
@export var rows: int = 10 : set = set_rows
|
||||
@export var floors: int = 3 : set = set_floors
|
||||
@export var auto_generate: bool = false : set = set_auto_generate
|
||||
@export var columns: int = 10: set = set_columns
|
||||
@export var rows: int = 10: set = set_rows
|
||||
@export var floors: int = 3: set = set_floors
|
||||
@export var auto_generate: bool = false: set = set_auto_generate
|
||||
|
||||
@export var normal_items: Array[int] = [0]
|
||||
@export var non_walkable_items: Array[int] = [4]
|
||||
@@ -23,23 +23,12 @@ var grid_data: Array = [] # 3D array [floor][row][column]
|
||||
var astar_by_floor = {} # Dictionary of AStar2D instances per floor
|
||||
var path = []
|
||||
|
||||
# Update the obstacle items array to use your specified item indices
|
||||
@export var obstacle_items: Array[int] = [12, 13, 14, 15] # Obstacle items in mesh library
|
||||
@export var obstacle_directions: Dictionary = {} # Store direction for each placed obstacle: {Vector3i position: Direction}
|
||||
|
||||
# Dictionary to store obstacle information: {cell_pos: orientation}
|
||||
# orientation: 0=North, 1=East, 2=South, 3=West
|
||||
var obstacles = {}
|
||||
|
||||
# Direction and movement systems
|
||||
enum Direction {
|
||||
NORTHWEST, NORTH, NORTHEAST,
|
||||
WEST, CENTER, EAST,
|
||||
SOUTHWEST, SOUTH, SOUTHEAST,
|
||||
BLOCKED_NORTH = 10,
|
||||
BLOCKED_EAST = 11,
|
||||
BLOCKED_SOUTH = 12,
|
||||
BLOCKED_WEST = 13
|
||||
SOUTHWEST, SOUTH, SOUTHEAST
|
||||
}
|
||||
|
||||
var diagonal_movement: bool = false
|
||||
@@ -353,20 +342,15 @@ func get_neighbors(current_pos: Vector2i, floor_index: int) -> Array[NeighborInf
|
||||
var is_walkable = is_cell_walkable(neighbor_pos, floor_index)
|
||||
|
||||
# Check if movement to this neighbor is blocked by obstacles
|
||||
if not is_diagonal_direction(dir) and is_movement_blocked(current_pos, neighbor_pos, floor_index):
|
||||
is_walkable = false
|
||||
# if not is_diagonal_direction(dir) and is_movement_blocked(current_pos, neighbor_pos, floor_index):
|
||||
# is_walkable = false
|
||||
|
||||
if is_diagonal_direction(dir):
|
||||
# For diagonal movement, check if both orthogonal paths are blocked
|
||||
var mid1 = Vector2i(neighbor_pos.x, current_pos.y)
|
||||
var mid2 = Vector2i(current_pos.x, neighbor_pos.y)
|
||||
|
||||
var path1_blocked = is_movement_blocked(current_pos, mid1, floor_index)
|
||||
var path2_blocked = is_movement_blocked(current_pos, mid2, floor_index)
|
||||
|
||||
if path1_blocked and path2_blocked:
|
||||
is_walkable = false
|
||||
|
||||
|
||||
if is_walkable:
|
||||
neighbors.append(NeighborInfo.new(neighbor_pos, dir, is_walkable))
|
||||
|
||||
@@ -374,7 +358,7 @@ func get_neighbors(current_pos: Vector2i, floor_index: int) -> Array[NeighborInf
|
||||
|
||||
# Helper functions for neighbor checking
|
||||
func is_diagonal_direction(direction: Direction) -> bool:
|
||||
return direction in [Direction.NORTHWEST, Direction.NORTHEAST,
|
||||
return direction in [Direction.NORTHWEST, Direction.NORTHEAST,
|
||||
Direction.SOUTHWEST, Direction.SOUTHEAST]
|
||||
|
||||
func is_position_valid(pos: Vector2i) -> bool:
|
||||
@@ -414,7 +398,7 @@ func initialize_astar():
|
||||
var weight = 1.0 if not is_diagonal_direction(neighbor.direction) else 1.4142
|
||||
|
||||
# Check if movement is allowed by obstacles
|
||||
if not is_blocked_by_obstacle(current_pos, neighbor.position, 3):
|
||||
if true: # Obstacle check removed
|
||||
astar.connect_points(current_point_id, neighbor_id, true)
|
||||
astar.set_point_weight_scale(neighbor_id, weight)
|
||||
|
||||
@@ -488,124 +472,10 @@ func update_grid_data():
|
||||
grid_data.append(floor_data)
|
||||
emit_signal("grid_updated")
|
||||
|
||||
# Check the obstacle on a cell
|
||||
func has_obstacle_at(pos: Vector3i) -> bool:
|
||||
var item = get_cell_item(pos)
|
||||
return item in obstacle_items
|
||||
|
||||
# Get orientation ( rotation )
|
||||
# Orientation helper
|
||||
func get_cell_orientation(pos: Vector3i) -> int:
|
||||
return get_cell_item_orientation(pos)
|
||||
|
||||
# Get obstacle direction
|
||||
# Get the direction of an obstacle at a specific position
|
||||
func get_obstacle_direction(pos: Vector3i) -> Direction:
|
||||
if obstacle_directions.has(pos):
|
||||
return obstacle_directions[pos]
|
||||
return Direction.CENTER
|
||||
|
||||
func get_obstacle_orientation(pos: Vector3i) -> int:
|
||||
return get_cell_item_orientation(pos)
|
||||
|
||||
func is_movement_blocked(from_pos: Vector2i, to_pos: Vector2i, floor_index: int = 3) -> bool:
|
||||
# Must be adjacent cells for direct blocking check
|
||||
if abs(from_pos.x - to_pos.x) + abs(from_pos.y - to_pos.y) != 1:
|
||||
return false
|
||||
|
||||
# Get 3D positions for the cells
|
||||
var from_pos3d = Vector3i(from_pos.x, floor_index, from_pos.y)
|
||||
var to_pos3d = Vector3i(to_pos.x, floor_index, to_pos.y)
|
||||
|
||||
# Check if the starting cell has an obstacle
|
||||
if has_obstacle_at(from_pos3d):
|
||||
var orientation = get_obstacle_orientation(from_pos3d)
|
||||
|
||||
# Check if the obstacle is blocking the requested movement direction
|
||||
if from_pos.y > to_pos.y and orientation == 0: # Moving NORTH, obstacle faces NORTH
|
||||
return true
|
||||
elif from_pos.x < to_pos.x and orientation == 1: # Moving EAST, obstacle faces EAST
|
||||
return true
|
||||
elif from_pos.y < to_pos.y and orientation == 2: # Moving SOUTH, obstacle faces SOUTH
|
||||
return true
|
||||
elif from_pos.x > to_pos.x and orientation == 3: # Moving WEST, obstacle faces WEST
|
||||
return true
|
||||
|
||||
# Check if the destination cell has an obstacle blocking entry
|
||||
if has_obstacle_at(to_pos3d):
|
||||
var orientation = get_obstacle_orientation(to_pos3d)
|
||||
|
||||
# Check if the obstacle is blocking entry from the requested direction
|
||||
if to_pos.y < from_pos.y and orientation == 2: # Coming from SOUTH, obstacle faces SOUTH
|
||||
return true
|
||||
elif to_pos.x > from_pos.x and orientation == 3: # Coming from WEST, obstacle faces WEST
|
||||
return true
|
||||
elif to_pos.y > from_pos.y and orientation == 0: # Coming from NORTH, obstacle faces NORTH
|
||||
return true
|
||||
elif to_pos.x < from_pos.x and orientation == 1: # Coming from EAST, obstacle faces EAST
|
||||
return true
|
||||
|
||||
return false
|
||||
|
||||
# Function to check if a cell is blocked by any obstacles in its vicinity
|
||||
func is_cell_blocked_by_obstacles(pos: Vector2i, floor_index: int = 3) -> bool:
|
||||
var pos3d = Vector3i(pos.x, floor_index, pos.y)
|
||||
|
||||
# Check if this cell itself has an obstacle
|
||||
if has_obstacle_at(pos3d):
|
||||
return true
|
||||
|
||||
# Check all adjacent cells for obstacles that might block this cell
|
||||
var adjacent_positions = [
|
||||
Vector2i(pos.x, pos.y - 1), # North
|
||||
Vector2i(pos.x + 1, pos.y), # East
|
||||
Vector2i(pos.x, pos.y + 1), # South
|
||||
Vector2i(pos.x - 1, pos.y), # West
|
||||
]
|
||||
|
||||
for adj_pos in adjacent_positions:
|
||||
var adj_pos3d = Vector3i(adj_pos.x, floor_index, adj_pos.y)
|
||||
|
||||
# Check if position is valid
|
||||
if is_position_valid(adj_pos) and has_obstacle_at(adj_pos3d):
|
||||
var orientation = get_obstacle_orientation(adj_pos3d)
|
||||
|
||||
# Check if the obstacle is blocking this cell
|
||||
if adj_pos.y < pos.y and orientation == 0: # Obstacle to NORTH facing NORTH
|
||||
return true
|
||||
elif adj_pos.x > pos.x and orientation == 1: # Obstacle to EAST facing EAST
|
||||
return true
|
||||
elif adj_pos.y > pos.y and orientation == 2: # Obstacle to SOUTH facing SOUTH
|
||||
return true
|
||||
elif adj_pos.x < pos.x and orientation == 3: # Obstacle to WEST facing WEST
|
||||
return true
|
||||
|
||||
return false
|
||||
|
||||
# Function to get all cells blocked by an obstacle at a specific position
|
||||
func get_cells_blocked_by_obstacle(obstacle_pos: Vector2i, orientation: int, floor_index: int = 3) -> Array:
|
||||
var blocked_cells = []
|
||||
|
||||
# Determine which cells are blocked based on orientation
|
||||
match orientation:
|
||||
0: # NORTH - blocks the row above
|
||||
for x in range(max(0, obstacle_pos.x - 1), min(columns, obstacle_pos.x + 2)):
|
||||
blocked_cells.append(Vector2i(x, obstacle_pos.y - 1))
|
||||
|
||||
1: # EAST - blocks the column to the right
|
||||
for y in range(max(0, obstacle_pos.y - 1), min(rows, obstacle_pos.y + 2)):
|
||||
blocked_cells.append(Vector2i(obstacle_pos.x + 1, y))
|
||||
|
||||
2: # SOUTH - blocks the row below
|
||||
for x in range(max(0, obstacle_pos.x - 1), min(columns, obstacle_pos.x + 2)):
|
||||
blocked_cells.append(Vector2i(x, obstacle_pos.y + 1))
|
||||
|
||||
3: # WEST - blocks the column to the left
|
||||
for y in range(max(0, obstacle_pos.y - 1), min(rows, obstacle_pos.y + 2)):
|
||||
blocked_cells.append(Vector2i(obstacle_pos.x - 1, y))
|
||||
|
||||
# Filter out invalid positions
|
||||
return blocked_cells.filter(func(pos): return is_position_valid(pos))
|
||||
|
||||
# Cell rotation handling
|
||||
func get_cell_rotation(position: Vector3i) -> int:
|
||||
return get_cell_item_orientation(position)
|
||||
@@ -649,60 +519,3 @@ func _emit_grid_updated():
|
||||
func set_diagonal_movement(enable: bool):
|
||||
diagonal_movement = enable
|
||||
initialize_astar()
|
||||
|
||||
func is_blocked_by_obstacle(from_pos: Vector2i, to_pos: Vector2i, floor_index: int = 3) -> bool:
|
||||
# For direct orthogonal movement (up, down, left, right)
|
||||
if (from_pos.x == to_pos.x and abs(from_pos.y - to_pos.y) == 1) or (from_pos.y == to_pos.y and abs(from_pos.x - to_pos.x) == 1):
|
||||
return is_movement_blocked(from_pos, to_pos, floor_index)
|
||||
|
||||
# For diagonal or longer distances, build a path and check each step
|
||||
var path = []
|
||||
|
||||
# Simple path planning for orthogonal movement
|
||||
if from_pos.x == to_pos.x or from_pos.y == to_pos.y:
|
||||
var dx = sign(to_pos.x - from_pos.x)
|
||||
var dy = sign(to_pos.y - from_pos.y)
|
||||
var current = from_pos
|
||||
|
||||
while current != to_pos:
|
||||
var next = Vector2i(current.x + dx, current.y + dy)
|
||||
path.append([current, next])
|
||||
current = next
|
||||
else:
|
||||
# For diagonal movement, check both possible paths
|
||||
# Path 1: Move horizontally first, then vertically
|
||||
var mid1 = Vector2i(to_pos.x, from_pos.y)
|
||||
var path1_blocked = is_blocked_by_obstacle(from_pos, mid1, floor_index) or is_blocked_by_obstacle(mid1, to_pos, floor_index)
|
||||
|
||||
# Path 2: Move vertically first, then horizontally
|
||||
var mid2 = Vector2i(from_pos.x, to_pos.y)
|
||||
var path2_blocked = is_blocked_by_obstacle(from_pos, mid2, floor_index) or is_blocked_by_obstacle(mid2, to_pos, floor_index)
|
||||
|
||||
# Movement is blocked if both paths are blocked
|
||||
return path1_blocked and path2_blocked
|
||||
|
||||
# Check each step in the path
|
||||
for step in path:
|
||||
if is_movement_blocked(step[0], step[1], floor_index):
|
||||
return true
|
||||
|
||||
return false
|
||||
|
||||
# Place an obstacle at the specified position with a specific orientation
|
||||
func place_obstacle(pos: Vector3i, obstacle_item: int, orientation: int) -> bool:
|
||||
# Always place on floor 3
|
||||
pos.y = 3
|
||||
|
||||
if get_cell_item(pos) != -1:
|
||||
return false # Cell is already occupied
|
||||
|
||||
# Set the obstacle item with the specified orientation
|
||||
set_cell_item(pos, obstacle_item, orientation)
|
||||
|
||||
# Store the obstacle information
|
||||
obstacles[pos] = orientation
|
||||
|
||||
# Re-initialize A* pathfinding to account for the new obstacle
|
||||
initialize_astar()
|
||||
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user